src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/BinderImpl.java

Mon, 28 May 2018 10:36:45 +0800

author
aoqi
date
Mon, 28 May 2018 10:36:45 +0800
changeset 1546
dc8316632248
parent 0
373ffda63c9a
permissions
-rw-r--r--

Merge

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

mercurial