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

Fri, 04 Oct 2013 16:21:34 +0100

author
mkos
date
Fri, 04 Oct 2013 16:21:34 +0100
changeset 408
b0610cd08440
parent 286
f50545b5e2f1
child 450
b0c2840e2513
permissions
-rw-r--r--

8025054: Update JAX-WS RI integration to 2.2.9-b130926.1035
Reviewed-by: chegar

     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;
    28 import java.io.IOException;
    29 import java.lang.reflect.Constructor;
    30 import java.lang.reflect.InvocationTargetException;
    32 import javax.xml.bind.JAXBElement;
    33 import javax.xml.bind.JAXBException;
    34 import javax.xml.namespace.QName;
    35 import javax.xml.stream.XMLStreamException;
    37 import com.sun.xml.internal.bind.api.AccessorException;
    38 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
    39 import com.sun.xml.internal.bind.v2.model.nav.Navigator;
    40 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo;
    41 import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo;
    42 import com.sun.xml.internal.bind.v2.runtime.property.Property;
    43 import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory;
    44 import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain;
    45 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
    47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder;
    48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter;
    49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
    50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
    51 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    52 import com.sun.xml.internal.bind.v2.util.QNameMap;
    54 import org.xml.sax.SAXException;
    56 /**
    57  * {@link JaxBeanInfo} implementation for {@link RuntimeElementInfo}.
    58  *
    59  * @author Kohsuke Kawaguchi
    60  */
    61 public final class ElementBeanInfoImpl extends JaxBeanInfo<JAXBElement> {
    63     private Loader loader;
    65     private final Property property;
    67     // used to create new instances of JAXBElement.
    68     private final QName tagName;
    69     public final Class expectedType;
    70     private final Class scope;
    72     /**
    73      * If non-null, use this to create an instance.
    74      * It takes one value.
    75      */
    76     private final Constructor<? extends JAXBElement> constructor;
    78     ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) {
    79         super(grammar,rei,(Class<JAXBElement>)rei.getType(),true,false,true);
    81         this.property = PropertyFactory.create(grammar,rei.getProperty());
    83         tagName = rei.getElementName();
    84         expectedType = Navigator.REFLECTION.erasure(rei.getContentInMemoryType());
    85         scope = rei.getScope()==null ? JAXBElement.GlobalScope.class : rei.getScope().getClazz();
    87         Class type = Navigator.REFLECTION.erasure(rei.getType());
    88         if(type==JAXBElement.class)
    89             constructor = null;
    90         else {
    91             try {
    92                 constructor = type.getConstructor(expectedType);
    93             } catch (NoSuchMethodException e) {
    94                 NoSuchMethodError x = new NoSuchMethodError("Failed to find the constructor for " + type + " with " + expectedType);
    95                 x.initCause(e);
    96                 throw x;
    97             }
    98         }
    99     }
   101     /**
   102      * The constructor for the sole instanceof {@link JaxBeanInfo} for
   103      * handling user-created {@link JAXBElement}.
   104      *
   105      * Such {@link JaxBeanInfo} is used only for marshalling.
   106      *
   107      * This is a hack.
   108      */
   109     protected ElementBeanInfoImpl(final JAXBContextImpl grammar) {
   110         super(grammar,null,JAXBElement.class,true,false,true);
   111         tagName = null;
   112         expectedType = null;
   113         scope = null;
   114         constructor = null;
   116         this.property = new Property<JAXBElement>() {
   117             public void reset(JAXBElement o) {
   118                 throw new UnsupportedOperationException();
   119             }
   121             public void serializeBody(JAXBElement e, XMLSerializer target, Object outerPeer) throws SAXException, IOException, XMLStreamException {
   122                 Class scope = e.getScope();
   123                 if(e.isGlobalScope())   scope = null;
   124                 QName n = e.getName();
   125                 ElementBeanInfoImpl bi = grammar.getElement(scope,n);
   126                 if(bi==null) {
   127                     // infer what to do from the type
   128                     JaxBeanInfo tbi;
   129                     try {
   130                         tbi = grammar.getBeanInfo(e.getDeclaredType(),true);
   131                     } catch (JAXBException x) {
   132                         // if e.getDeclaredType() isn't known to this JAXBContext
   133                         target.reportError(null,x);
   134                         return;
   135                     }
   136                     Object value = e.getValue();
   137                     target.startElement(n.getNamespaceURI(),n.getLocalPart(),n.getPrefix(),null);
   138                     if(value==null) {
   139                         target.writeXsiNilTrue();
   140                     } else {
   141                         target.childAsXsiType(value,"value",tbi, false);
   142                     }
   143                     target.endElement();
   144                 } else {
   145                     try {
   146                         bi.property.serializeBody(e,target,e);
   147                     } catch (AccessorException x) {
   148                         target.reportError(null,x);
   149                     }
   150                 }
   151             }
   153             public void serializeURIs(JAXBElement o, XMLSerializer target) {
   154             }
   156             public boolean hasSerializeURIAction() {
   157                 return false;
   158             }
   160             public String getIdValue(JAXBElement o) {
   161                 return null;
   162             }
   164             public PropertyKind getKind() {
   165                 return PropertyKind.ELEMENT;
   166             }
   168             public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
   169             }
   171             public Accessor getElementPropertyAccessor(String nsUri, String localName) {
   172                 throw new UnsupportedOperationException();
   173             }
   175             public void wrapUp() {
   176             }
   178             public RuntimePropertyInfo getInfo() {
   179                 return property.getInfo();
   180             }
   182             public boolean isHiddenByOverride() {
   183                 return false;
   184             }
   186             public void setHiddenByOverride(boolean hidden) {
   187                 throw new UnsupportedOperationException("Not supported on jaxbelements.");
   188             }
   190             public String getFieldName() {
   191                 return null;
   192             }
   194         };
   195     }
   197     /**
   198      * Use the previous {@link UnmarshallingContext.State}'s target to store
   199      * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader}
   200      * to correctly find the parent object.
   201      * This is a hack.
   202      */
   203     private final class IntercepterLoader extends Loader implements Intercepter {
   204         private final Loader core;
   206         public IntercepterLoader(Loader core) {
   207             this.core = core;
   208         }
   210         @Override
   211         public final void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException {
   212             state.loader = core;
   213             state.intercepter = this;
   215             // TODO: make sure there aren't too many duplicate of this code
   216             // create the object to unmarshal
   217             Object child;
   218             UnmarshallingContext context = state.getContext();
   220             // let's see if we can reuse the existing peer object
   221             child = context.getOuterPeer();
   223             if(child!=null && jaxbType!=child.getClass())
   224                 child = null;   // unexpected type.
   226             if(child!=null)
   227                 reset((JAXBElement)child,context);
   229             if(child==null)
   230                 child = context.createInstance(ElementBeanInfoImpl.this);
   232             fireBeforeUnmarshal(ElementBeanInfoImpl.this, child, state);
   234             context.recordOuterPeer(child);
   235             UnmarshallingContext.State p = state.prev;
   236             p.backup = p.target;
   237             p.target = child;
   239             core.startElement(state,ea);
   240         }
   242         public Object intercept(UnmarshallingContext.State state, Object o) throws SAXException {
   243             JAXBElement e = (JAXBElement)state.target;
   244             state.target = state.backup;
   245             state.backup = null;
   247             if (state.nil) {
   248                 e.setNil(true);
   249                 state.nil = false;
   250             }
   252             if(o!=null)
   253                 // if the value is a leaf type, it's often already set to the element
   254                 // through Accessor.
   255                 e.setValue(o);
   257             fireAfterUnmarshal(ElementBeanInfoImpl.this, e, state);
   259             return e;
   260         }
   261     }
   263     public String getElementNamespaceURI(JAXBElement e) {
   264         return e.getName().getNamespaceURI();
   265     }
   267     public String getElementLocalName(JAXBElement e) {
   268         return e.getName().getLocalPart();
   269     }
   271     public Loader getLoader(JAXBContextImpl context, boolean typeSubstitutionCapable) {
   272         if(loader==null) {
   273             // this has to be done lazily to avoid cyclic reference issue
   274             UnmarshallerChain c = new UnmarshallerChain(context);
   275             QNameMap<ChildLoader> result = new QNameMap<ChildLoader>();
   276             property.buildChildElementUnmarshallers(c,result);
   277             if(result.size()==1)
   278                 // for ElementBeanInfoImpl created from RuntimeElementInfo
   279                 this.loader = new IntercepterLoader(result.getOne().getValue().loader);
   280             else
   281                 // for special ElementBeanInfoImpl only used for marshalling
   282                 this.loader = Discarder.INSTANCE;
   283         }
   284         return loader;
   285     }
   287     public final JAXBElement createInstance(UnmarshallingContext context) throws IllegalAccessException, InvocationTargetException, InstantiationException {
   288         return createInstanceFromValue(null);
   289     }
   291     public final JAXBElement createInstanceFromValue(Object o) throws IllegalAccessException, InvocationTargetException, InstantiationException {
   292         if(constructor==null)
   293             return new JAXBElement(tagName,expectedType,scope,o);
   294         else
   295             return constructor.newInstance(o);
   296     }
   298     public boolean reset(JAXBElement e, UnmarshallingContext context) {
   299         e.setValue(null);
   300         return true;
   301     }
   303     public String getId(JAXBElement e, XMLSerializer target) {
   304         // TODO: is this OK? Should we be returning the ID value of the type property?
   305         /*
   306             There's one case where we JAXBElement needs to be designated as ID,
   307             and that is when there's a global element whose type is ID.
   308         */
   309         Object o = e.getValue();
   310         if(o instanceof String)
   311             return (String)o;
   312         else
   313             return null;
   314     }
   316     public void serializeBody(JAXBElement element, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
   317         try {
   318             property.serializeBody(element,target,null);
   319         } catch (AccessorException x) {
   320             target.reportError(null,x);
   321         }
   322     }
   324     public void serializeRoot(JAXBElement e, XMLSerializer target) throws SAXException, IOException, XMLStreamException {
   325         serializeBody(e,target);
   326     }
   328     public void serializeAttributes(JAXBElement e, XMLSerializer target) {
   329         // noop
   330     }
   332     public void serializeURIs(JAXBElement e, XMLSerializer target) {
   333         // noop
   334     }
   336     public final Transducer<JAXBElement> getTransducer() {
   337         return null;
   338     }
   340     @Override
   341     public void wrapUp() {
   342         super.wrapUp();
   343         property.wrapUp();
   344     }
   346     @Override
   347     public void link(JAXBContextImpl grammar) {
   348         super.link(grammar);
   349         getLoader(grammar,true);    // make sure to build them, if we hadn't done so
   350     }
   351 }

mercurial