Mon, 28 May 2018 10:36:45 +0800
Merge
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.bind.v2.runtime;
28 import javax.xml.bind.Binder;
29 import javax.xml.bind.JAXBElement;
30 import javax.xml.bind.JAXBException;
31 import javax.xml.bind.PropertyException;
32 import javax.xml.bind.ValidationEventHandler;
33 import javax.xml.validation.Schema;
34 import javax.xml.namespace.QName;
36 import com.sun.xml.internal.bind.unmarshaller.InfosetScanner;
37 import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput;
38 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor;
39 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector;
40 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl;
42 import org.w3c.dom.Element;
43 import org.w3c.dom.Node;
44 import org.xml.sax.SAXException;
46 /**
47 * Implementation of {@link Binder}.
48 *
49 * TODO: investigate how much in-place unmarshalling is implemented
50 * - some preliminary work is there. Probably buggy.
51 * TODO: work on the marshaller side.
52 *
53 * @author
54 * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
55 */
56 public class BinderImpl<XmlNode> extends Binder<XmlNode> {
58 /**
59 * The parent context object.
60 */
61 private final JAXBContextImpl context;
63 /**
64 * Lazily created unmarshaller to do XML->Java binding.
65 * @see #getUnmarshaller()
66 */
67 private UnmarshallerImpl unmarshaller;
69 /**
70 * Lazily create marshaller to do Java->XML binding.
71 * @see #getMarshaller()
72 */
73 private MarshallerImpl marshaller;
75 private final InfosetScanner<XmlNode> scanner;
77 /**
78 * A {@link Binder} always works with the same
79 * association map.
80 */
81 private final AssociationMap<XmlNode> assoc = new AssociationMap<XmlNode>();
83 BinderImpl(JAXBContextImpl _context,InfosetScanner<XmlNode> scanner) {
84 this.context = _context;
85 this.scanner = scanner;
86 }
88 private UnmarshallerImpl getUnmarshaller() {
89 if(unmarshaller==null)
90 unmarshaller = new UnmarshallerImpl(context,assoc);
91 return unmarshaller;
92 }
94 private MarshallerImpl getMarshaller() {
95 if(marshaller==null)
96 marshaller = new MarshallerImpl(context,assoc);
97 return marshaller;
98 }
100 public void marshal(Object jaxbObject, XmlNode xmlNode) throws JAXBException {
101 if ((xmlNode == null) || (jaxbObject == null))
102 throw new IllegalArgumentException();
103 getMarshaller().marshal(jaxbObject,createOutput(xmlNode));
104 }
106 // TODO move this to a sub class once we support something other than W3C DOM
107 private DOMOutput createOutput(XmlNode xmlNode) {
108 return new DOMOutput((Node)xmlNode,assoc);
109 }
112 public Object updateJAXB(XmlNode xmlNode) throws JAXBException {
113 return associativeUnmarshal(xmlNode,true,null);
114 }
116 public Object unmarshal( XmlNode xmlNode ) throws JAXBException {
117 return associativeUnmarshal(xmlNode,false,null);
118 }
120 public <T> JAXBElement<T> unmarshal(XmlNode xmlNode, Class<T> expectedType) throws JAXBException {
121 if(expectedType==null) throw new IllegalArgumentException();
122 return (JAXBElement)associativeUnmarshal(xmlNode,true,expectedType);
123 }
125 public void setSchema(Schema schema) {
126 getMarshaller().setSchema(schema);
127 getUnmarshaller().setSchema(schema);
128 }
130 public Schema getSchema() {
131 return getUnmarshaller().getSchema();
132 }
134 private Object associativeUnmarshal(XmlNode xmlNode, boolean inplace, Class expectedType) throws JAXBException {
135 if (xmlNode == null)
136 throw new IllegalArgumentException();
138 JaxBeanInfo bi = null;
139 if(expectedType!=null)
140 bi = context.getBeanInfo(expectedType, true);
142 InterningXmlVisitor handler = new InterningXmlVisitor(
143 getUnmarshaller().createUnmarshallerHandler(scanner,inplace,bi));
144 scanner.setContentHandler(new SAXConnector(handler,scanner.getLocator()));
145 try {
146 scanner.scan(xmlNode);
147 } catch( SAXException e ) {
148 throw unmarshaller.createUnmarshalException(e);
149 }
151 return handler.getContext().getResult();
152 }
154 public XmlNode getXMLNode(Object jaxbObject) {
155 if(jaxbObject==null)
156 throw new IllegalArgumentException();
157 AssociationMap.Entry<XmlNode> e = assoc.byPeer(jaxbObject);
158 if(e==null) return null;
159 return e.element();
160 }
162 public Object getJAXBNode(XmlNode xmlNode) {
163 if(xmlNode==null)
164 throw new IllegalArgumentException();
165 AssociationMap.Entry e = assoc.byElement(xmlNode);
166 if(e==null) return null;
167 if(e.outer()!=null) return e.outer();
168 return e.inner();
169 }
171 public XmlNode updateXML(Object jaxbObject) throws JAXBException {
172 return updateXML(jaxbObject,getXMLNode(jaxbObject));
173 }
175 public XmlNode updateXML(Object jaxbObject, XmlNode xmlNode) throws JAXBException {
176 if(jaxbObject==null || xmlNode==null) throw new IllegalArgumentException();
178 // TODO
179 // for now just marshal
180 // TODO: object model independenc
181 Element e = (Element)xmlNode;
182 Node ns = e.getNextSibling();
183 Node p = e.getParentNode();
184 p.removeChild(e);
186 // if the type object is passed, the following step is necessary to make
187 // the marshalling successful.
188 JaxBeanInfo bi = context.getBeanInfo(jaxbObject, true);
189 if(!bi.isElement())
190 jaxbObject = new JAXBElement(new QName(e.getNamespaceURI(),e.getLocalName()),bi.jaxbType,jaxbObject);
193 getMarshaller().marshal(jaxbObject,p);
194 Node newNode = p.getLastChild();
195 p.removeChild(newNode);
196 p.insertBefore(newNode,ns);
198 return (XmlNode)newNode;
199 }
201 public void setEventHandler(ValidationEventHandler handler) throws JAXBException {
202 getUnmarshaller().setEventHandler(handler);
203 getMarshaller().setEventHandler(handler);
204 }
206 public ValidationEventHandler getEventHandler() {
207 return getUnmarshaller().getEventHandler();
208 }
210 public Object getProperty(String name) throws PropertyException {
211 if (name == null)
212 throw new IllegalArgumentException(Messages.NULL_PROPERTY_NAME.format());
214 // exclude RI properties that don't make sense for Binder
215 if (excludeProperty(name)) {
216 throw new PropertyException(name);
217 }
219 Object prop = null;
220 PropertyException pe = null;
222 try {
223 prop = getMarshaller().getProperty(name);
224 return prop;
225 } catch (PropertyException p) {
226 pe = p;
227 }
229 try {
230 prop = getUnmarshaller().getProperty(name);
231 return prop;
232 } catch (PropertyException p) {
233 pe = p;
234 }
236 pe.setStackTrace(Thread.currentThread().getStackTrace());
237 throw pe;
238 }
240 public void setProperty(String name, Object value) throws PropertyException {
241 if (name == null)
242 throw new IllegalArgumentException(Messages.NULL_PROPERTY_NAME.format());
244 // exclude RI properties that don't make sense for Binder
245 if (excludeProperty(name)) {
246 throw new PropertyException(name, value);
247 }
249 PropertyException pe = null;
251 try {
252 getMarshaller().setProperty(name, value);
253 return;
254 } catch (PropertyException p) {
255 pe = p;
256 }
258 try {
259 getUnmarshaller().setProperty(name, value);
260 return;
261 } catch (PropertyException p) {
262 pe = p;
263 }
265 // replace the stacktrace - we don't want to see a trace
266 // originating from Un|Marshaller.setProperty
267 pe.setStackTrace(Thread.currentThread().getStackTrace());
268 throw pe;
269 }
271 private boolean excludeProperty(String name) {
272 return name.equals(
273 MarshallerImpl.ENCODING_HANDLER) ||
274 name.equals(MarshallerImpl.XMLDECLARATION) ||
275 name.equals(MarshallerImpl.XML_HEADERS);
276 }
277 }