1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/BinderImpl.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,277 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.xml.internal.bind.v2.runtime; 1.30 + 1.31 +import javax.xml.bind.Binder; 1.32 +import javax.xml.bind.JAXBElement; 1.33 +import javax.xml.bind.JAXBException; 1.34 +import javax.xml.bind.PropertyException; 1.35 +import javax.xml.bind.ValidationEventHandler; 1.36 +import javax.xml.validation.Schema; 1.37 +import javax.xml.namespace.QName; 1.38 + 1.39 +import com.sun.xml.internal.bind.unmarshaller.InfosetScanner; 1.40 +import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput; 1.41 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor; 1.42 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector; 1.43 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; 1.44 + 1.45 +import org.w3c.dom.Element; 1.46 +import org.w3c.dom.Node; 1.47 +import org.xml.sax.SAXException; 1.48 + 1.49 +/** 1.50 + * Implementation of {@link Binder}. 1.51 + * 1.52 + * TODO: investigate how much in-place unmarshalling is implemented 1.53 + * - some preliminary work is there. Probably buggy. 1.54 + * TODO: work on the marshaller side. 1.55 + * 1.56 + * @author 1.57 + * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.58 + */ 1.59 +public class BinderImpl<XmlNode> extends Binder<XmlNode> { 1.60 + 1.61 + /** 1.62 + * The parent context object. 1.63 + */ 1.64 + private final JAXBContextImpl context; 1.65 + 1.66 + /** 1.67 + * Lazily created unmarshaller to do XML->Java binding. 1.68 + * @see #getUnmarshaller() 1.69 + */ 1.70 + private UnmarshallerImpl unmarshaller; 1.71 + 1.72 + /** 1.73 + * Lazily create marshaller to do Java->XML binding. 1.74 + * @see #getMarshaller() 1.75 + */ 1.76 + private MarshallerImpl marshaller; 1.77 + 1.78 + private final InfosetScanner<XmlNode> scanner; 1.79 + 1.80 + /** 1.81 + * A {@link Binder} always works with the same 1.82 + * association map. 1.83 + */ 1.84 + private final AssociationMap<XmlNode> assoc = new AssociationMap<XmlNode>(); 1.85 + 1.86 + BinderImpl(JAXBContextImpl _context,InfosetScanner<XmlNode> scanner) { 1.87 + this.context = _context; 1.88 + this.scanner = scanner; 1.89 + } 1.90 + 1.91 + private UnmarshallerImpl getUnmarshaller() { 1.92 + if(unmarshaller==null) 1.93 + unmarshaller = new UnmarshallerImpl(context,assoc); 1.94 + return unmarshaller; 1.95 + } 1.96 + 1.97 + private MarshallerImpl getMarshaller() { 1.98 + if(marshaller==null) 1.99 + marshaller = new MarshallerImpl(context,assoc); 1.100 + return marshaller; 1.101 + } 1.102 + 1.103 + public void marshal(Object jaxbObject, XmlNode xmlNode) throws JAXBException { 1.104 + if ((xmlNode == null) || (jaxbObject == null)) 1.105 + throw new IllegalArgumentException(); 1.106 + getMarshaller().marshal(jaxbObject,createOutput(xmlNode)); 1.107 + } 1.108 + 1.109 + // TODO move this to a sub class once we support something other than W3C DOM 1.110 + private DOMOutput createOutput(XmlNode xmlNode) { 1.111 + return new DOMOutput((Node)xmlNode,assoc); 1.112 + } 1.113 + 1.114 + 1.115 + public Object updateJAXB(XmlNode xmlNode) throws JAXBException { 1.116 + return associativeUnmarshal(xmlNode,true,null); 1.117 + } 1.118 + 1.119 + public Object unmarshal( XmlNode xmlNode ) throws JAXBException { 1.120 + return associativeUnmarshal(xmlNode,false,null); 1.121 + } 1.122 + 1.123 + public <T> JAXBElement<T> unmarshal(XmlNode xmlNode, Class<T> expectedType) throws JAXBException { 1.124 + if(expectedType==null) throw new IllegalArgumentException(); 1.125 + return (JAXBElement)associativeUnmarshal(xmlNode,true,expectedType); 1.126 + } 1.127 + 1.128 + public void setSchema(Schema schema) { 1.129 + getMarshaller().setSchema(schema); 1.130 + getUnmarshaller().setSchema(schema); 1.131 + } 1.132 + 1.133 + public Schema getSchema() { 1.134 + return getUnmarshaller().getSchema(); 1.135 + } 1.136 + 1.137 + private Object associativeUnmarshal(XmlNode xmlNode, boolean inplace, Class expectedType) throws JAXBException { 1.138 + if (xmlNode == null) 1.139 + throw new IllegalArgumentException(); 1.140 + 1.141 + JaxBeanInfo bi = null; 1.142 + if(expectedType!=null) 1.143 + bi = context.getBeanInfo(expectedType, true); 1.144 + 1.145 + InterningXmlVisitor handler = new InterningXmlVisitor( 1.146 + getUnmarshaller().createUnmarshallerHandler(scanner,inplace,bi)); 1.147 + scanner.setContentHandler(new SAXConnector(handler,scanner.getLocator())); 1.148 + try { 1.149 + scanner.scan(xmlNode); 1.150 + } catch( SAXException e ) { 1.151 + throw unmarshaller.createUnmarshalException(e); 1.152 + } 1.153 + 1.154 + return handler.getContext().getResult(); 1.155 + } 1.156 + 1.157 + public XmlNode getXMLNode(Object jaxbObject) { 1.158 + if(jaxbObject==null) 1.159 + throw new IllegalArgumentException(); 1.160 + AssociationMap.Entry<XmlNode> e = assoc.byPeer(jaxbObject); 1.161 + if(e==null) return null; 1.162 + return e.element(); 1.163 + } 1.164 + 1.165 + public Object getJAXBNode(XmlNode xmlNode) { 1.166 + if(xmlNode==null) 1.167 + throw new IllegalArgumentException(); 1.168 + AssociationMap.Entry e = assoc.byElement(xmlNode); 1.169 + if(e==null) return null; 1.170 + if(e.outer()!=null) return e.outer(); 1.171 + return e.inner(); 1.172 + } 1.173 + 1.174 + public XmlNode updateXML(Object jaxbObject) throws JAXBException { 1.175 + return updateXML(jaxbObject,getXMLNode(jaxbObject)); 1.176 + } 1.177 + 1.178 + public XmlNode updateXML(Object jaxbObject, XmlNode xmlNode) throws JAXBException { 1.179 + if(jaxbObject==null || xmlNode==null) throw new IllegalArgumentException(); 1.180 + 1.181 + // TODO 1.182 + // for now just marshal 1.183 + // TODO: object model independenc 1.184 + Element e = (Element)xmlNode; 1.185 + Node ns = e.getNextSibling(); 1.186 + Node p = e.getParentNode(); 1.187 + p.removeChild(e); 1.188 + 1.189 + // if the type object is passed, the following step is necessary to make 1.190 + // the marshalling successful. 1.191 + JaxBeanInfo bi = context.getBeanInfo(jaxbObject, true); 1.192 + if(!bi.isElement()) 1.193 + jaxbObject = new JAXBElement(new QName(e.getNamespaceURI(),e.getLocalName()),bi.jaxbType,jaxbObject); 1.194 + 1.195 + 1.196 + getMarshaller().marshal(jaxbObject,p); 1.197 + Node newNode = p.getLastChild(); 1.198 + p.removeChild(newNode); 1.199 + p.insertBefore(newNode,ns); 1.200 + 1.201 + return (XmlNode)newNode; 1.202 + } 1.203 + 1.204 + public void setEventHandler(ValidationEventHandler handler) throws JAXBException { 1.205 + getUnmarshaller().setEventHandler(handler); 1.206 + getMarshaller().setEventHandler(handler); 1.207 + } 1.208 + 1.209 + public ValidationEventHandler getEventHandler() { 1.210 + return getUnmarshaller().getEventHandler(); 1.211 + } 1.212 + 1.213 + public Object getProperty(String name) throws PropertyException { 1.214 + if (name == null) 1.215 + throw new IllegalArgumentException(Messages.NULL_PROPERTY_NAME.format()); 1.216 + 1.217 + // exclude RI properties that don't make sense for Binder 1.218 + if (excludeProperty(name)) { 1.219 + throw new PropertyException(name); 1.220 + } 1.221 + 1.222 + Object prop = null; 1.223 + PropertyException pe = null; 1.224 + 1.225 + try { 1.226 + prop = getMarshaller().getProperty(name); 1.227 + return prop; 1.228 + } catch (PropertyException p) { 1.229 + pe = p; 1.230 + } 1.231 + 1.232 + try { 1.233 + prop = getUnmarshaller().getProperty(name); 1.234 + return prop; 1.235 + } catch (PropertyException p) { 1.236 + pe = p; 1.237 + } 1.238 + 1.239 + pe.setStackTrace(Thread.currentThread().getStackTrace()); 1.240 + throw pe; 1.241 + } 1.242 + 1.243 + public void setProperty(String name, Object value) throws PropertyException { 1.244 + if (name == null) 1.245 + throw new IllegalArgumentException(Messages.NULL_PROPERTY_NAME.format()); 1.246 + 1.247 + // exclude RI properties that don't make sense for Binder 1.248 + if (excludeProperty(name)) { 1.249 + throw new PropertyException(name, value); 1.250 + } 1.251 + 1.252 + PropertyException pe = null; 1.253 + 1.254 + try { 1.255 + getMarshaller().setProperty(name, value); 1.256 + return; 1.257 + } catch (PropertyException p) { 1.258 + pe = p; 1.259 + } 1.260 + 1.261 + try { 1.262 + getUnmarshaller().setProperty(name, value); 1.263 + return; 1.264 + } catch (PropertyException p) { 1.265 + pe = p; 1.266 + } 1.267 + 1.268 + // replace the stacktrace - we don't want to see a trace 1.269 + // originating from Un|Marshaller.setProperty 1.270 + pe.setStackTrace(Thread.currentThread().getStackTrace()); 1.271 + throw pe; 1.272 + } 1.273 + 1.274 + private boolean excludeProperty(String name) { 1.275 + return name.equals( 1.276 + MarshallerImpl.ENCODING_HANDLER) || 1.277 + name.equals(MarshallerImpl.XMLDECLARATION) || 1.278 + name.equals(MarshallerImpl.XML_HEADERS); 1.279 + } 1.280 +}