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

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

mercurial