src/share/jaxws_classes/com/sun/xml/internal/bind/v2/runtime/property/ArrayReferenceNodeProperty.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/ArrayReferenceNodeProperty.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,167 @@
     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 +
    1.33 +import javax.xml.bind.JAXBException;
    1.34 +import javax.xml.bind.annotation.DomHandler;
    1.35 +import javax.xml.stream.XMLStreamException;
    1.36 +
    1.37 +import com.sun.xml.internal.bind.v2.ClassFactory;
    1.38 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
    1.39 +import com.sun.xml.internal.bind.v2.model.core.WildcardMode;
    1.40 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElement;
    1.41 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeReferencePropertyInfo;
    1.42 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
    1.43 +import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
    1.44 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
    1.45 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
    1.46 +import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator;
    1.47 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
    1.48 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
    1.49 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver;
    1.50 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
    1.51 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.WildcardLoader;
    1.52 +import com.sun.xml.internal.bind.v2.util.QNameMap;
    1.53 +
    1.54 +import org.xml.sax.SAXException;
    1.55 +
    1.56 +/**
    1.57 + * @author Kohsuke Kawaguchi
    1.58 + */
    1.59 +class ArrayReferenceNodeProperty<BeanT,ListT,ItemT> extends ArrayERProperty<BeanT,ListT,ItemT> {
    1.60 +
    1.61 +    /**
    1.62 +     * Expected element names and what class to unmarshal.
    1.63 +     */
    1.64 +    private final QNameMap<JaxBeanInfo> expectedElements = new QNameMap<JaxBeanInfo>();
    1.65 +
    1.66 +    private final boolean isMixed;
    1.67 +
    1.68 +    private final DomHandler domHandler;
    1.69 +    private final WildcardMode wcMode;
    1.70 +
    1.71 +    public ArrayReferenceNodeProperty(JAXBContextImpl p, RuntimeReferencePropertyInfo prop) {
    1.72 +        super(p, prop, prop.getXmlName(), prop.isCollectionNillable());
    1.73 +
    1.74 +        for (RuntimeElement e : prop.getElements()) {
    1.75 +            JaxBeanInfo bi = p.getOrCreate(e);
    1.76 +            expectedElements.put( e.getElementName().getNamespaceURI(),e.getElementName().getLocalPart(), bi );
    1.77 +        }
    1.78 +
    1.79 +        isMixed = prop.isMixed();
    1.80 +
    1.81 +        if(prop.getWildcard()!=null) {
    1.82 +            domHandler = (DomHandler) ClassFactory.create(prop.getDOMHandler());
    1.83 +            wcMode = prop.getWildcard();
    1.84 +        } else {
    1.85 +            domHandler = null;
    1.86 +            wcMode = null;
    1.87 +        }
    1.88 +    }
    1.89 +
    1.90 +    protected final void serializeListBody(BeanT o, XMLSerializer w, ListT list) throws IOException, XMLStreamException, SAXException {
    1.91 +        ListIterator<ItemT> itr = lister.iterator(list, w);
    1.92 +
    1.93 +        while(itr.hasNext()) {
    1.94 +            try {
    1.95 +                ItemT item = itr.next();
    1.96 +                if (item != null) {
    1.97 +                    if(isMixed && item.getClass()==String.class) {
    1.98 +                        w.text((String)item,null);
    1.99 +                    } else {
   1.100 +                        JaxBeanInfo bi = w.grammar.getBeanInfo(item,true);
   1.101 +                        if(bi.jaxbType==Object.class && domHandler!=null)
   1.102 +                            // even if 'v' is a DOM node, it always derive from Object,
   1.103 +                            // so the getBeanInfo returns BeanInfo for Object
   1.104 +                            w.writeDom(item,domHandler,o,fieldName);
   1.105 +                        else
   1.106 +                            bi.serializeRoot(item,w);
   1.107 +                    }
   1.108 +                }
   1.109 +            } catch (JAXBException e) {
   1.110 +                w.reportError(fieldName,e);
   1.111 +                // recover by ignoring this item
   1.112 +            }
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    public void createBodyUnmarshaller(UnmarshallerChain chain, QNameMap<ChildLoader> loaders) {
   1.117 +        final int offset = chain.allocateOffset();
   1.118 +
   1.119 +        Receiver recv = new ReceiverImpl(offset);
   1.120 +
   1.121 +        for( QNameMap.Entry<JaxBeanInfo> n : expectedElements.entrySet() ) {
   1.122 +            final JaxBeanInfo beanInfo = n.getValue();
   1.123 +            loaders.put(n.nsUri,n.localName,new ChildLoader(beanInfo.getLoader(chain.context,true),recv));
   1.124 +        }
   1.125 +
   1.126 +        if(isMixed) {
   1.127 +            // handler for processing mixed contents.
   1.128 +            loaders.put(TEXT_HANDLER,
   1.129 +                new ChildLoader(new MixedTextLoader(recv),null));
   1.130 +        }
   1.131 +
   1.132 +        if(domHandler!=null) {
   1.133 +            loaders.put(CATCH_ALL,
   1.134 +                new ChildLoader(new WildcardLoader(domHandler,wcMode),recv));
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    private static final class MixedTextLoader extends Loader {
   1.139 +
   1.140 +        private final Receiver recv;
   1.141 +
   1.142 +        public MixedTextLoader(Receiver recv) {
   1.143 +            super(true);
   1.144 +            this.recv = recv;
   1.145 +        }
   1.146 +
   1.147 +        public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException {
   1.148 +            if(text.length()!=0) // length 0 text is pointless
   1.149 +                recv.receive(state,text.toString());
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +
   1.154 +    public PropertyKind getKind() {
   1.155 +        return PropertyKind.REFERENCE;
   1.156 +    }
   1.157 +
   1.158 +    @Override
   1.159 +    public Accessor getElementPropertyAccessor(String nsUri, String localName) {
   1.160 +        // TODO: unwrap JAXBElement
   1.161 +        if(wrapperTagName!=null) {
   1.162 +            if(wrapperTagName.equals(nsUri,localName))
   1.163 +                return acc;
   1.164 +        } else {
   1.165 +            if(expectedElements.containsKey(nsUri,localName))
   1.166 +                return acc;
   1.167 +        }
   1.168 +        return null;
   1.169 +    }
   1.170 +}

mercurial