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/ArrayERProperty.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,200 @@ 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.util.Collection; 1.33 + 1.34 +import javax.xml.namespace.QName; 1.35 +import javax.xml.stream.XMLStreamException; 1.36 + 1.37 +import com.sun.xml.internal.bind.api.AccessorException; 1.38 +import com.sun.xml.internal.bind.v2.util.QNameMap; 1.39 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; 1.40 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 1.41 +import com.sun.xml.internal.bind.v2.runtime.Name; 1.42 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 1.43 +import com.sun.xml.internal.bind.v2.runtime.reflect.Lister; 1.44 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 1.45 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; 1.46 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; 1.47 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 1.48 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; 1.49 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope; 1.50 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; 1.51 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; 1.52 + 1.53 +import org.xml.sax.SAXException; 1.54 + 1.55 +/** 1.56 + * Commonality between {@link ArrayElementProperty} and {@link ArrayReferenceNodeProperty}. 1.57 + * 1.58 + * Mostly handles the unmarshalling of the wrapper element. 1.59 + * 1.60 + * @author Kohsuke Kawaguchi 1.61 + */ 1.62 +abstract class ArrayERProperty<BeanT,ListT,ItemT> extends ArrayProperty<BeanT,ListT,ItemT> { 1.63 + 1.64 + /** 1.65 + * Wrapper tag name if any, or null. 1.66 + */ 1.67 + protected final Name wrapperTagName; 1.68 + 1.69 + /** 1.70 + * True if the wrapper tag name is nillable. 1.71 + * Always false if {@link #wrapperTagName}==null. 1.72 + */ 1.73 + protected final boolean isWrapperNillable; 1.74 + 1.75 + protected ArrayERProperty(JAXBContextImpl grammar, RuntimePropertyInfo prop, QName tagName, boolean isWrapperNillable) { 1.76 + super(grammar,prop); 1.77 + if(tagName==null) 1.78 + this.wrapperTagName = null; 1.79 + else 1.80 + this.wrapperTagName = grammar.nameBuilder.createElementName(tagName); 1.81 + this.isWrapperNillable = isWrapperNillable; 1.82 + } 1.83 + 1.84 + /** 1.85 + * Used to handle the collection wrapper element. 1.86 + */ 1.87 + private static final class ItemsLoader extends Loader { 1.88 + 1.89 + private final Accessor acc; 1.90 + private final Lister lister; 1.91 + 1.92 + public ItemsLoader(Accessor acc, Lister lister, QNameMap<ChildLoader> children) { 1.93 + super(false); 1.94 + this.acc = acc; 1.95 + this.lister = lister; 1.96 + this.children = children; 1.97 + } 1.98 + 1.99 + @Override 1.100 + public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.101 + UnmarshallingContext context = state.getContext(); 1.102 + context.startScope(1); 1.103 + // inherit the target so that our children can access its target 1.104 + state.target = state.prev.target; 1.105 + 1.106 + // start it now, so that even if there's no children we can still return empty collection 1.107 + context.getScope(0).start(acc,lister); 1.108 + } 1.109 + 1.110 + private final QNameMap<ChildLoader> children; 1.111 + 1.112 + @Override 1.113 + public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.114 + ChildLoader child = children.get(ea.uri,ea.local); 1.115 + if (child == null) { 1.116 + child = children.get(CATCH_ALL); 1.117 + } 1.118 + if (child == null) { 1.119 + super.childElement(state,ea); 1.120 + return; 1.121 + } 1.122 + state.loader = child.loader; 1.123 + state.receiver = child.receiver; 1.124 + } 1.125 + 1.126 + @Override 1.127 + public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.128 + state.getContext().endScope(1); 1.129 + } 1.130 + 1.131 + @Override 1.132 + public Collection<QName> getExpectedChildElements() { 1.133 + return children.keySet(); 1.134 + } 1.135 + } 1.136 + 1.137 + public final void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { 1.138 + ListT list = acc.get(o); 1.139 + 1.140 + if(list!=null) { 1.141 + if(wrapperTagName!=null) { 1.142 + w.startElement(wrapperTagName,null); 1.143 + w.endNamespaceDecls(list); 1.144 + w.endAttributes(); 1.145 + } 1.146 + 1.147 + serializeListBody(o,w,list); 1.148 + 1.149 + if(wrapperTagName!=null) 1.150 + w.endElement(); 1.151 + } else { 1.152 + // list is null 1.153 + if(isWrapperNillable) { 1.154 + w.startElement(wrapperTagName,null); 1.155 + w.writeXsiNilTrue(); 1.156 + w.endElement(); 1.157 + } // otherwise don't print the wrapper tag name 1.158 + } 1.159 + } 1.160 + 1.161 + /** 1.162 + * Serializes the items of the list. 1.163 + * This method is invoked after the necessary wrapper tag is produced (if necessary.) 1.164 + * 1.165 + * @param list 1.166 + * always non-null. 1.167 + */ 1.168 + protected abstract void serializeListBody(BeanT o, XMLSerializer w, ListT list) throws IOException, XMLStreamException, SAXException, AccessorException; 1.169 + 1.170 + /** 1.171 + * Creates the unmarshaler to unmarshal the body. 1.172 + */ 1.173 + protected abstract void createBodyUnmarshaller(UnmarshallerChain chain, QNameMap<ChildLoader> loaders); 1.174 + 1.175 + 1.176 + public final void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> loaders) { 1.177 + if(wrapperTagName!=null) { 1.178 + UnmarshallerChain c = new UnmarshallerChain(chain.context); 1.179 + QNameMap<ChildLoader> m = new QNameMap<ChildLoader>(); 1.180 + createBodyUnmarshaller(c,m); 1.181 + Loader loader = new ItemsLoader(acc, lister, m); 1.182 + if(isWrapperNillable || chain.context.allNillable) 1.183 + loader = new XsiNilLoader(loader); 1.184 + loaders.put(wrapperTagName,new ChildLoader(loader,null)); 1.185 + } else { 1.186 + createBodyUnmarshaller(chain,loaders); 1.187 + } 1.188 + } 1.189 + 1.190 + /** 1.191 + * {@link Receiver} that puts the child object into the {@link Scope} object. 1.192 + */ 1.193 + protected final class ReceiverImpl implements Receiver { 1.194 + private final int offset; 1.195 + 1.196 + protected ReceiverImpl(int offset) { 1.197 + this.offset = offset; 1.198 + } 1.199 + 1.200 + public void receive(UnmarshallingContext.State state, Object o) throws SAXException { 1.201 + state.getContext().getScope(offset).add(acc,lister,o); 1.202 + } 1.203 + }}