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

Fri, 22 Nov 2013 21:11:19 +0100

author
mkos
date
Fri, 22 Nov 2013 21:11:19 +0100
changeset 450
b0c2840e2513
parent 0
373ffda63c9a
permissions
-rw-r--r--

8010935: Better XML handling
8027378: Two closed/javax/xml/8005432 fails with jdk7u51b04
8028382: Two javax/xml/8005433 tests still fail after the fix JDK-8028147
Summary: base fix + fixes for test regressions; fix also reviewed by Maxim Soloviev, Alexander Fomin
Reviewed-by: mchung, mgrebac, mullan

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.property;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.lang.reflect.InvocationTargetException;
aoqi@0 30
aoqi@0 31 import javax.xml.bind.JAXBElement;
aoqi@0 32 import javax.xml.bind.JAXBException;
aoqi@0 33 import javax.xml.bind.annotation.DomHandler;
aoqi@0 34 import javax.xml.stream.XMLStreamException;
aoqi@0 35
aoqi@0 36 import com.sun.xml.internal.bind.api.AccessorException;
aoqi@0 37 import com.sun.xml.internal.bind.v2.ClassFactory;
aoqi@0 38 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
aoqi@0 39 import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
aoqi@0 40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElement;
aoqi@0 41 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeReferencePropertyInfo;
aoqi@0 42 import com.sun.xml.internal.bind.v2.runtime.ElementBeanInfoImpl;
aoqi@0 43 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
aoqi@0 44 import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
aoqi@0 45 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
aoqi@0 46 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
aoqi@0 47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
aoqi@0 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.WildcardLoader;
aoqi@0 49 import com.sun.xml.internal.bind.v2.util.QNameMap;
aoqi@0 50
aoqi@0 51 import org.xml.sax.SAXException;
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * @author Kohsuke Kawaguchi
aoqi@0 55 */
aoqi@0 56 final class SingleReferenceNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {
aoqi@0 57
aoqi@0 58 private final Accessor<BeanT,ValueT> acc;
aoqi@0 59
aoqi@0 60 private final QNameMap<JaxBeanInfo> expectedElements = new QNameMap<JaxBeanInfo>();
aoqi@0 61
aoqi@0 62 private final DomHandler domHandler;
aoqi@0 63 private final WildcardMode wcMode;
aoqi@0 64
aoqi@0 65 public SingleReferenceNodeProperty(JAXBContextImpl context, RuntimeReferencePropertyInfo prop) {
aoqi@0 66 super(context,prop);
aoqi@0 67 acc = prop.getAccessor().optimize(context);
aoqi@0 68
aoqi@0 69 for (RuntimeElement e : prop.getElements()) {
aoqi@0 70 expectedElements.put( e.getElementName(), context.getOrCreate(e) );
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 if(prop.getWildcard()!=null) {
aoqi@0 74 domHandler = (DomHandler) ClassFactory.create(prop.getDOMHandler());
aoqi@0 75 wcMode = prop.getWildcard();
aoqi@0 76 } else {
aoqi@0 77 domHandler = null;
aoqi@0 78 wcMode = null;
aoqi@0 79 }
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 public void reset(BeanT bean) throws AccessorException {
aoqi@0 83 acc.set(bean,null);
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 public String getIdValue(BeanT beanT) {
aoqi@0 87 return null;
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException {
aoqi@0 91 ValueT v = acc.get(o);
aoqi@0 92 if(v!=null) {
aoqi@0 93 try {
aoqi@0 94 JaxBeanInfo bi = w.grammar.getBeanInfo(v,true);
aoqi@0 95 if(bi.jaxbType==Object.class && domHandler!=null)
aoqi@0 96 // even if 'v' is a DOM node, it always derive from Object,
aoqi@0 97 // so the getBeanInfo returns BeanInfo for Object
aoqi@0 98 w.writeDom(v,domHandler,o,fieldName);
aoqi@0 99 else
aoqi@0 100 bi.serializeRoot(v,w);
aoqi@0 101 } catch (JAXBException e) {
aoqi@0 102 w.reportError(fieldName,e);
aoqi@0 103 // recover by ignoring this property
aoqi@0 104 }
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
aoqi@0 109 for (QNameMap.Entry<JaxBeanInfo> n : expectedElements.entrySet())
aoqi@0 110 handlers.put(n.nsUri,n.localName, new ChildLoader(n.getValue().getLoader(chain.context,true),acc));
aoqi@0 111
aoqi@0 112 if(domHandler!=null)
aoqi@0 113 handlers.put(CATCH_ALL,new ChildLoader(new WildcardLoader(domHandler,wcMode),acc));
aoqi@0 114
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 public PropertyKind getKind() {
aoqi@0 118 return PropertyKind.REFERENCE;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 @Override
aoqi@0 122 public Accessor getElementPropertyAccessor(String nsUri, String localName) {
aoqi@0 123 JaxBeanInfo bi = expectedElements.get(nsUri, localName);
aoqi@0 124 if(bi!=null) {
aoqi@0 125 if(bi instanceof ElementBeanInfoImpl) {
aoqi@0 126 final ElementBeanInfoImpl ebi = (ElementBeanInfoImpl) bi;
aoqi@0 127 // a JAXBElement. We need to handle JAXBElement for JAX-WS
aoqi@0 128 return new Accessor<BeanT,Object>(ebi.expectedType) {
aoqi@0 129 public Object get(BeanT bean) throws AccessorException {
aoqi@0 130 ValueT r = acc.get(bean);
aoqi@0 131 if(r instanceof JAXBElement) {
aoqi@0 132 return ((JAXBElement)r).getValue();
aoqi@0 133 } else
aoqi@0 134 // this is sloppy programming, but hey...
aoqi@0 135 return r;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public void set(BeanT bean, Object value) throws AccessorException {
aoqi@0 139 if(value!=null) {
aoqi@0 140 try {
aoqi@0 141 value = ebi.createInstanceFromValue(value);
aoqi@0 142 } catch (IllegalAccessException e) {
aoqi@0 143 throw new AccessorException(e);
aoqi@0 144 } catch (InvocationTargetException e) {
aoqi@0 145 throw new AccessorException(e);
aoqi@0 146 } catch (InstantiationException e) {
aoqi@0 147 throw new AccessorException(e);
aoqi@0 148 }
aoqi@0 149 }
aoqi@0 150 acc.set(bean,(ValueT)value);
aoqi@0 151 }
aoqi@0 152 };
aoqi@0 153 } else {
aoqi@0 154 // a custom element type, like @XmlRootElement class Foo { ... }
aoqi@0 155 return acc;
aoqi@0 156 }
aoqi@0 157 } else
aoqi@0 158 return null;
aoqi@0 159 }
aoqi@0 160 }

mercurial