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/SingleMapNodeProperty.java Tue Mar 06 16:09:35 2012 -0800 1.3 @@ -0,0 +1,287 @@ 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.HashMap; 1.33 +import java.util.LinkedHashMap; 1.34 +import java.util.Map; 1.35 +import java.util.TreeMap; 1.36 +import java.util.Collection; 1.37 +import java.util.Collections; 1.38 +import java.util.Arrays; 1.39 +import java.util.Set; 1.40 + 1.41 +import javax.xml.stream.XMLStreamException; 1.42 +import javax.xml.namespace.QName; 1.43 + 1.44 +import com.sun.xml.internal.bind.api.AccessorException; 1.45 +import com.sun.xml.internal.bind.v2.ClassFactory; 1.46 +import com.sun.xml.internal.bind.v2.util.QNameMap; 1.47 +import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 1.48 +import com.sun.xml.internal.bind.v2.model.nav.ReflectionNavigator; 1.49 +import com.sun.xml.internal.bind.v2.model.runtime.RuntimeMapPropertyInfo; 1.50 +import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 1.51 +import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; 1.52 +import com.sun.xml.internal.bind.v2.runtime.Name; 1.53 +import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 1.54 +import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 1.55 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; 1.56 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; 1.57 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 1.58 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; 1.59 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; 1.60 +import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.State; 1.61 + 1.62 +import org.xml.sax.SAXException; 1.63 + 1.64 +/** 1.65 + * @author Kohsuke Kawaguchi 1.66 + */ 1.67 +final class SingleMapNodeProperty<BeanT,ValueT extends Map> extends PropertyImpl<BeanT> { 1.68 + 1.69 + private final Accessor<BeanT,ValueT> acc; 1.70 + /** 1.71 + * The tag name that surrounds the whole property. 1.72 + */ 1.73 + private final Name tagName; 1.74 + /** 1.75 + * The tag name that corresponds to the 'entry' element. 1.76 + */ 1.77 + private final Name entryTag; 1.78 + private final Name keyTag; 1.79 + private final Name valueTag; 1.80 + 1.81 + private final boolean nillable; 1.82 + 1.83 + private JaxBeanInfo keyBeanInfo; 1.84 + private JaxBeanInfo valueBeanInfo; 1.85 + 1.86 + /** 1.87 + * The implementation class for this property. 1.88 + * If the property is null, we create an instance of this class. 1.89 + */ 1.90 + private final Class<? extends ValueT> mapImplClass; 1.91 + 1.92 + public SingleMapNodeProperty(JAXBContextImpl context, RuntimeMapPropertyInfo prop) { 1.93 + super(context, prop); 1.94 + acc = prop.getAccessor().optimize(context); 1.95 + this.tagName = context.nameBuilder.createElementName(prop.getXmlName()); 1.96 + this.entryTag = context.nameBuilder.createElementName("","entry"); 1.97 + this.keyTag = context.nameBuilder.createElementName("","key"); 1.98 + this.valueTag = context.nameBuilder.createElementName("","value"); 1.99 + this.nillable = prop.isCollectionNillable(); 1.100 + this.keyBeanInfo = context.getOrCreate(prop.getKeyType()); 1.101 + this.valueBeanInfo = context.getOrCreate(prop.getValueType()); 1.102 + 1.103 + // infer the implementation class 1.104 + Class<ValueT> sig = ReflectionNavigator.REFLECTION.erasure(prop.getRawType()); 1.105 + mapImplClass = ClassFactory.inferImplClass(sig,knownImplClasses); 1.106 + // TODO: error check for mapImplClass==null 1.107 + // what is the error reporting path for this part of the code? 1.108 + } 1.109 + 1.110 + private static final Class[] knownImplClasses = { 1.111 + HashMap.class, TreeMap.class, LinkedHashMap.class 1.112 + }; 1.113 + 1.114 + public void reset(BeanT bean) throws AccessorException { 1.115 + acc.set(bean,null); 1.116 + } 1.117 + 1.118 + 1.119 + /** 1.120 + * A Map property can never be ID. 1.121 + */ 1.122 + public String getIdValue(BeanT bean) { 1.123 + return null; 1.124 + } 1.125 + 1.126 + public PropertyKind getKind() { 1.127 + return PropertyKind.MAP; 1.128 + } 1.129 + 1.130 + public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) { 1.131 + keyLoader = keyBeanInfo.getLoader(chain.context,true); 1.132 + valueLoader = valueBeanInfo.getLoader(chain.context,true); 1.133 + handlers.put(tagName,new ChildLoader(itemsLoader,null)); 1.134 + } 1.135 + 1.136 + private Loader keyLoader; 1.137 + private Loader valueLoader; 1.138 + 1.139 + /** 1.140 + * Handles <items> and </items>. 1.141 + * 1.142 + * The target will be set to a {@link Map}. 1.143 + */ 1.144 + private final Loader itemsLoader = new Loader(false) { 1.145 + 1.146 + private ThreadLocal<BeanT> target = new ThreadLocal<BeanT>(); 1.147 + private ThreadLocal<ValueT> map = new ThreadLocal<ValueT>(); 1.148 + 1.149 + @Override 1.150 + public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.151 + // create or obtain the Map object 1.152 + try { 1.153 + target.set((BeanT)state.prev.target); 1.154 + map.set(acc.get(target.get())); 1.155 + if(map.get() == null) { 1.156 + map.set(ClassFactory.create(mapImplClass)); 1.157 + } 1.158 + map.get().clear(); 1.159 + state.target = map.get(); 1.160 + } catch (AccessorException e) { 1.161 + // recover from error by setting a dummy Map that receives and discards the values 1.162 + handleGenericException(e,true); 1.163 + state.target = new HashMap(); 1.164 + } 1.165 + } 1.166 + 1.167 + @Override 1.168 + public void leaveElement(State state, TagName ea) throws SAXException { 1.169 + super.leaveElement(state, ea); 1.170 + try { 1.171 + acc.set(target.get(), map.get()); 1.172 + target.remove(); 1.173 + } catch (AccessorException ex) { 1.174 + handleGenericException(ex,true); 1.175 + } 1.176 + } 1.177 + 1.178 + @Override 1.179 + public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.180 + if(ea.matches(entryTag)) { 1.181 + state.loader = entryLoader; 1.182 + } else { 1.183 + super.childElement(state,ea); 1.184 + } 1.185 + } 1.186 + 1.187 + @Override 1.188 + public Collection<QName> getExpectedChildElements() { 1.189 + return Collections.singleton(entryTag.toQName()); 1.190 + } 1.191 + }; 1.192 + 1.193 + /** 1.194 + * Handles <entry> and </entry>. 1.195 + * 1.196 + * The target will be set to a {@link Map}. 1.197 + */ 1.198 + private final Loader entryLoader = new Loader(false) { 1.199 + @Override 1.200 + public void startElement(UnmarshallingContext.State state, TagName ea) { 1.201 + state.target = new Object[2]; // this is inefficient 1.202 + } 1.203 + 1.204 + @Override 1.205 + public void leaveElement(UnmarshallingContext.State state, TagName ea) { 1.206 + Object[] keyValue = (Object[])state.target; 1.207 + Map map = (Map) state.prev.target; 1.208 + map.put(keyValue[0],keyValue[1]); 1.209 + } 1.210 + 1.211 + @Override 1.212 + public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { 1.213 + if(ea.matches(keyTag)) { 1.214 + state.loader = keyLoader; 1.215 + state.receiver = keyReceiver; 1.216 + return; 1.217 + } 1.218 + if(ea.matches(valueTag)) { 1.219 + state.loader = valueLoader; 1.220 + state.receiver = valueReceiver; 1.221 + return; 1.222 + } 1.223 + super.childElement(state,ea); 1.224 + } 1.225 + 1.226 + @Override 1.227 + public Collection<QName> getExpectedChildElements() { 1.228 + return Arrays.asList(keyTag.toQName(),valueTag.toQName()); 1.229 + } 1.230 + }; 1.231 + 1.232 + private static final class ReceiverImpl implements Receiver { 1.233 + private final int index; 1.234 + public ReceiverImpl(int index) { 1.235 + this.index = index; 1.236 + } 1.237 + public void receive(UnmarshallingContext.State state, Object o) { 1.238 + ((Object[])state.target)[index] = o; 1.239 + } 1.240 + } 1.241 + 1.242 + private static final Receiver keyReceiver = new ReceiverImpl(0); 1.243 + private static final Receiver valueReceiver = new ReceiverImpl(1); 1.244 + 1.245 + @Override 1.246 + public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { 1.247 + ValueT v = acc.get(o); 1.248 + if(v!=null) { 1.249 + bareStartTag(w,tagName,v); 1.250 + for( Map.Entry e : (Set<Map.Entry>)v.entrySet() ) { 1.251 + bareStartTag(w,entryTag,null); 1.252 + 1.253 + Object key = e.getKey(); 1.254 + if(key!=null) { 1.255 + w.startElement(keyTag,key); 1.256 + w.childAsXsiType(key,fieldName,keyBeanInfo, false); 1.257 + w.endElement(); 1.258 + } 1.259 + 1.260 + Object value = e.getValue(); 1.261 + if(value!=null) { 1.262 + w.startElement(valueTag,value); 1.263 + w.childAsXsiType(value,fieldName,valueBeanInfo, false); 1.264 + w.endElement(); 1.265 + } 1.266 + 1.267 + w.endElement(); 1.268 + } 1.269 + w.endElement(); 1.270 + } else 1.271 + if(nillable) { 1.272 + w.startElement(tagName,null); 1.273 + w.writeXsiNilTrue(); 1.274 + w.endElement(); 1.275 + } 1.276 + } 1.277 + 1.278 + private void bareStartTag(XMLSerializer w, Name tagName, Object peer) throws IOException, XMLStreamException, SAXException { 1.279 + w.startElement(tagName,peer); 1.280 + w.endNamespaceDecls(peer); 1.281 + w.endAttributes(); 1.282 + } 1.283 + 1.284 + @Override 1.285 + public Accessor getElementPropertyAccessor(String nsUri, String localName) { 1.286 + if(tagName.equals(nsUri,localName)) 1.287 + return acc; 1.288 + return null; 1.289 + } 1.290 +}