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

changeset 286
f50545b5e2f1
parent 0
373ffda63c9a
     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/property/SingleElementNodeProperty.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,182 @@
     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.property;
    1.30 +
    1.31 +import java.io.IOException;
    1.32 +import java.lang.reflect.Modifier;
    1.33 +import java.lang.reflect.Type;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.Map;
    1.36 +
    1.37 +import javax.xml.namespace.QName;
    1.38 +import javax.xml.stream.XMLStreamException;
    1.39 +
    1.40 +import com.sun.xml.internal.bind.api.AccessorException;
    1.41 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
    1.42 +import com.sun.xml.internal.bind.v2.model.core.TypeRef;
    1.43 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
    1.44 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeInfo;
    1.45 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef;
    1.46 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.47 +import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
    1.48 +import com.sun.xml.internal.bind.v2.runtime.Name;
    1.49 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.50 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    1.51 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
    1.52 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DefaultValueLoaderDecorator;
    1.53 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
    1.54 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
    1.55 +import com.sun.xml.internal.bind.v2.util.QNameMap;
    1.56 +
    1.57 +import javax.xml.bind.JAXBElement;
    1.58 +import org.xml.sax.SAXException;
    1.59 +
    1.60 +/**
    1.61 + * @author Kohsuke Kawaguchi (kk@kohsuke.org)
    1.62 + */
    1.63 +final class SingleElementNodeProperty<BeanT,ValueT> extends PropertyImpl<BeanT> {
    1.64 +
    1.65 +    private final Accessor<BeanT,ValueT> acc;
    1.66 +
    1.67 +    private final boolean nillable;
    1.68 +
    1.69 +    private final QName[] acceptedElements;
    1.70 +
    1.71 +    private final Map<Class,TagAndType> typeNames = new HashMap<Class,TagAndType>();
    1.72 +
    1.73 +    private RuntimeElementPropertyInfo prop;
    1.74 +
    1.75 +    /**
    1.76 +     * The tag name used to produce xsi:nil. The first one in the list.
    1.77 +     */
    1.78 +    private final Name nullTagName;
    1.79 +
    1.80 +    public SingleElementNodeProperty(JAXBContextImpl context, RuntimeElementPropertyInfo prop) {
    1.81 +        super(context,prop);
    1.82 +        acc = prop.getAccessor().optimize(context);
    1.83 +        this.prop = prop;
    1.84 +
    1.85 +        QName nt = null;
    1.86 +        boolean nil = false;
    1.87 +
    1.88 +        acceptedElements = new QName[prop.getTypes().size()];
    1.89 +        for( int i=0; i<acceptedElements.length; i++ )
    1.90 +            acceptedElements[i] = prop.getTypes().get(i).getTagName();
    1.91 +
    1.92 +        for (RuntimeTypeRef e : prop.getTypes()) {
    1.93 +            JaxBeanInfo beanInfo = context.getOrCreate(e.getTarget());
    1.94 +            if(nt==null)    nt = e.getTagName();
    1.95 +            typeNames.put( beanInfo.jaxbType, new TagAndType(
    1.96 +                context.nameBuilder.createElementName(e.getTagName()),beanInfo) );
    1.97 +            nil |= e.isNillable();
    1.98 +        }
    1.99 +
   1.100 +        nullTagName = context.nameBuilder.createElementName(nt);
   1.101 +
   1.102 +        nillable = nil;
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public void wrapUp() {
   1.107 +        super.wrapUp();
   1.108 +        prop = null;
   1.109 +    }
   1.110 +
   1.111 +    public void reset(BeanT bean) throws AccessorException {
   1.112 +        acc.set(bean,null);
   1.113 +    }
   1.114 +
   1.115 +    public String getIdValue(BeanT beanT) {
   1.116 +        return null;
   1.117 +    }
   1.118 +
   1.119 +    @Override
   1.120 +    public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException {
   1.121 +        ValueT v = acc.get(o);
   1.122 +        if (v!=null) {
   1.123 +            Class vtype = v.getClass();
   1.124 +            TagAndType tt=typeNames.get(vtype); // quick way that usually works
   1.125 +
   1.126 +            if(tt==null) {// slow way that always works
   1.127 +                for (Map.Entry<Class,TagAndType> e : typeNames.entrySet()) {
   1.128 +                    if(e.getKey().isAssignableFrom(vtype)) {
   1.129 +                        tt = e.getValue();
   1.130 +                        break;
   1.131 +                    }
   1.132 +                }
   1.133 +            }
   1.134 +
   1.135 +            boolean addNilDecl = (o instanceof JAXBElement) && ((JAXBElement)o).isNil();
   1.136 +            if(tt==null) {
   1.137 +                // actually this is an error, because the actual type was not a sub-type
   1.138 +                // of any of the types specified in the annotations,
   1.139 +                // but for the purpose of experimenting with simple type substitution,
   1.140 +                // it's convenient to marshal this anyway (for example so that classes
   1.141 +                // generated from simple types like String can be marshalled as expected.)
   1.142 +                w.startElement(typeNames.values().iterator().next().tagName,null);
   1.143 +                w.childAsXsiType(v,fieldName,w.grammar.getBeanInfo(Object.class), addNilDecl && nillable);
   1.144 +            } else {
   1.145 +                w.startElement(tt.tagName,null);
   1.146 +                w.childAsXsiType(v,fieldName,tt.beanInfo, addNilDecl && nillable);
   1.147 +            }
   1.148 +            w.endElement();
   1.149 +        } else if (nillable) {
   1.150 +            w.startElement(nullTagName,null);
   1.151 +            w.writeXsiNilTrue();
   1.152 +            w.endElement();
   1.153 +        }
   1.154 +    }
   1.155 +
   1.156 +    public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) {
   1.157 +        JAXBContextImpl context = chain.context;
   1.158 +
   1.159 +        for (TypeRef<Type,Class> e : prop.getTypes()) {
   1.160 +            JaxBeanInfo bi = context.getOrCreate((RuntimeTypeInfo) e.getTarget());
   1.161 +            // if the expected Java type is already final, type substitution won't really work anyway.
   1.162 +            // this also traps cases like trying to substitute xsd:long element with xsi:type='xsd:int'
   1.163 +            Loader l = bi.getLoader(context,!Modifier.isFinal(bi.jaxbType.getModifiers()));
   1.164 +            if(e.getDefaultValue()!=null)
   1.165 +                l = new DefaultValueLoaderDecorator(l,e.getDefaultValue());
   1.166 +            if(nillable || chain.context.allNillable)
   1.167 +                l = new XsiNilLoader.Single(l,acc);
   1.168 +            handlers.put( e.getTagName(), new ChildLoader(l,acc));
   1.169 +        }
   1.170 +    }
   1.171 +
   1.172 +    public PropertyKind getKind() {
   1.173 +        return PropertyKind.ELEMENT;
   1.174 +    }
   1.175 +
   1.176 +    @Override
   1.177 +    public Accessor getElementPropertyAccessor(String nsUri, String localName) {
   1.178 +        for( QName n : acceptedElements) {
   1.179 +            if(n.getNamespaceURI().equals(nsUri) && n.getLocalPart().equals(localName))
   1.180 +                return acc;
   1.181 +        }
   1.182 +        return null;
   1.183 +    }
   1.184 +
   1.185 +}

mercurial