BinderImpl.java revision 524:dcaa586ab756
1/* 2 * Copyright (c) 1997, 2012, 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 */ 25 26package com.sun.xml.internal.bind.v2.runtime; 27 28import javax.xml.bind.Binder; 29import javax.xml.bind.JAXBElement; 30import javax.xml.bind.JAXBException; 31import javax.xml.bind.PropertyException; 32import javax.xml.bind.ValidationEventHandler; 33import javax.xml.validation.Schema; 34import javax.xml.namespace.QName; 35 36import com.sun.xml.internal.bind.unmarshaller.InfosetScanner; 37import com.sun.xml.internal.bind.v2.runtime.output.DOMOutput; 38import com.sun.xml.internal.bind.v2.runtime.unmarshaller.InterningXmlVisitor; 39import com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector; 40import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl; 41 42import org.w3c.dom.Element; 43import org.w3c.dom.Node; 44import org.xml.sax.SAXException; 45 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 */ 56public class BinderImpl<XmlNode> extends Binder<XmlNode> { 57 58 /** 59 * The parent context object. 60 */ 61 private final JAXBContextImpl context; 62 63 /** 64 * Lazily created unmarshaller to do XML->Java binding. 65 * @see #getUnmarshaller() 66 */ 67 private UnmarshallerImpl unmarshaller; 68 69 /** 70 * Lazily create marshaller to do Java->XML binding. 71 * @see #getMarshaller() 72 */ 73 private MarshallerImpl marshaller; 74 75 private final InfosetScanner<XmlNode> scanner; 76 77 /** 78 * A {@link Binder} always works with the same 79 * association map. 80 */ 81 private final AssociationMap<XmlNode> assoc = new AssociationMap<XmlNode>(); 82 83 BinderImpl(JAXBContextImpl _context,InfosetScanner<XmlNode> scanner) { 84 this.context = _context; 85 this.scanner = scanner; 86 } 87 88 private UnmarshallerImpl getUnmarshaller() { 89 if(unmarshaller==null) 90 unmarshaller = new UnmarshallerImpl(context,assoc); 91 return unmarshaller; 92 } 93 94 private MarshallerImpl getMarshaller() { 95 if(marshaller==null) 96 marshaller = new MarshallerImpl(context,assoc); 97 return marshaller; 98 } 99 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 } 105 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 } 110 111 112 public Object updateJAXB(XmlNode xmlNode) throws JAXBException { 113 return associativeUnmarshal(xmlNode,true,null); 114 } 115 116 public Object unmarshal( XmlNode xmlNode ) throws JAXBException { 117 return associativeUnmarshal(xmlNode,false,null); 118 } 119 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 } 124 125 public void setSchema(Schema schema) { 126 getMarshaller().setSchema(schema); 127 getUnmarshaller().setSchema(schema); 128 } 129 130 public Schema getSchema() { 131 return getUnmarshaller().getSchema(); 132 } 133 134 private Object associativeUnmarshal(XmlNode xmlNode, boolean inplace, Class expectedType) throws JAXBException { 135 if (xmlNode == null) 136 throw new IllegalArgumentException(); 137 138 JaxBeanInfo bi = null; 139 if(expectedType!=null) 140 bi = context.getBeanInfo(expectedType, true); 141 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 } 150 151 return handler.getContext().getResult(); 152 } 153 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 } 161 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 } 170 171 public XmlNode updateXML(Object jaxbObject) throws JAXBException { 172 return updateXML(jaxbObject,getXMLNode(jaxbObject)); 173 } 174 175 public XmlNode updateXML(Object jaxbObject, XmlNode xmlNode) throws JAXBException { 176 if(jaxbObject==null || xmlNode==null) throw new IllegalArgumentException(); 177 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); 185 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); 191 192 193 getMarshaller().marshal(jaxbObject,p); 194 Node newNode = p.getLastChild(); 195 p.removeChild(newNode); 196 p.insertBefore(newNode,ns); 197 198 return (XmlNode)newNode; 199 } 200 201 public void setEventHandler(ValidationEventHandler handler) throws JAXBException { 202 getUnmarshaller().setEventHandler(handler); 203 getMarshaller().setEventHandler(handler); 204 } 205 206 public ValidationEventHandler getEventHandler() { 207 return getUnmarshaller().getEventHandler(); 208 } 209 210 public Object getProperty(String name) throws PropertyException { 211 if (name == null) 212 throw new IllegalArgumentException(Messages.NULL_PROPERTY_NAME.format()); 213 214 // exclude RI properties that don't make sense for Binder 215 if (excludeProperty(name)) { 216 throw new PropertyException(name); 217 } 218 219 Object prop = null; 220 PropertyException pe = null; 221 222 try { 223 prop = getMarshaller().getProperty(name); 224 return prop; 225 } catch (PropertyException p) { 226 pe = p; 227 } 228 229 try { 230 prop = getUnmarshaller().getProperty(name); 231 return prop; 232 } catch (PropertyException p) { 233 pe = p; 234 } 235 236 pe.setStackTrace(Thread.currentThread().getStackTrace()); 237 throw pe; 238 } 239 240 public void setProperty(String name, Object value) throws PropertyException { 241 if (name == null) 242 throw new IllegalArgumentException(Messages.NULL_PROPERTY_NAME.format()); 243 244 // exclude RI properties that don't make sense for Binder 245 if (excludeProperty(name)) { 246 throw new PropertyException(name, value); 247 } 248 249 PropertyException pe = null; 250 251 try { 252 getMarshaller().setProperty(name, value); 253 return; 254 } catch (PropertyException p) { 255 pe = p; 256 } 257 258 try { 259 getUnmarshaller().setProperty(name, value); 260 return; 261 } catch (PropertyException p) { 262 pe = p; 263 } 264 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 } 270 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} 278