Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aefimov@650 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.bind.v2.runtime.property; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.IOException; |
aoqi@0 | 29 | import java.util.HashMap; |
aoqi@0 | 30 | import java.util.LinkedHashMap; |
aoqi@0 | 31 | import java.util.Map; |
aoqi@0 | 32 | import java.util.TreeMap; |
aoqi@0 | 33 | import java.util.Collection; |
aoqi@0 | 34 | import java.util.Collections; |
aoqi@0 | 35 | import java.util.Arrays; |
aoqi@0 | 36 | import java.util.Set; |
aoqi@0 | 37 | |
aoqi@0 | 38 | import javax.xml.stream.XMLStreamException; |
aoqi@0 | 39 | import javax.xml.namespace.QName; |
aoqi@0 | 40 | |
aoqi@0 | 41 | import com.sun.xml.internal.bind.api.AccessorException; |
aoqi@0 | 42 | import com.sun.xml.internal.bind.v2.ClassFactory; |
aoqi@0 | 43 | import com.sun.xml.internal.bind.v2.util.QNameMap; |
aoqi@0 | 44 | import com.sun.xml.internal.bind.v2.model.core.PropertyKind; |
aoqi@0 | 45 | import com.sun.xml.internal.bind.v2.model.runtime.RuntimeMapPropertyInfo; |
aoqi@0 | 46 | import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; |
aoqi@0 | 47 | import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; |
aoqi@0 | 48 | import com.sun.xml.internal.bind.v2.runtime.Name; |
aoqi@0 | 49 | import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; |
aoqi@0 | 50 | import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; |
aoqi@0 | 51 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; |
aoqi@0 | 52 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; |
aoqi@0 | 53 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; |
aoqi@0 | 54 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; |
aoqi@0 | 55 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; |
aoqi@0 | 56 | import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.State; |
aoqi@0 | 57 | |
aoqi@0 | 58 | import org.xml.sax.SAXException; |
aoqi@0 | 59 | |
aoqi@0 | 60 | /** |
aoqi@0 | 61 | * @author Kohsuke Kawaguchi |
aoqi@0 | 62 | */ |
aoqi@0 | 63 | final class SingleMapNodeProperty<BeanT,ValueT extends Map> extends PropertyImpl<BeanT> { |
aoqi@0 | 64 | |
aoqi@0 | 65 | private final Accessor<BeanT,ValueT> acc; |
aoqi@0 | 66 | /** |
aoqi@0 | 67 | * The tag name that surrounds the whole property. |
aoqi@0 | 68 | */ |
aoqi@0 | 69 | private final Name tagName; |
aoqi@0 | 70 | /** |
aoqi@0 | 71 | * The tag name that corresponds to the 'entry' element. |
aoqi@0 | 72 | */ |
aoqi@0 | 73 | private final Name entryTag; |
aoqi@0 | 74 | private final Name keyTag; |
aoqi@0 | 75 | private final Name valueTag; |
aoqi@0 | 76 | |
aoqi@0 | 77 | private final boolean nillable; |
aoqi@0 | 78 | |
aoqi@0 | 79 | private JaxBeanInfo keyBeanInfo; |
aoqi@0 | 80 | private JaxBeanInfo valueBeanInfo; |
aoqi@0 | 81 | |
aoqi@0 | 82 | /** |
aoqi@0 | 83 | * The implementation class for this property. |
aoqi@0 | 84 | * If the property is null, we create an instance of this class. |
aoqi@0 | 85 | */ |
aoqi@0 | 86 | private final Class<? extends ValueT> mapImplClass; |
aoqi@0 | 87 | |
aoqi@0 | 88 | public SingleMapNodeProperty(JAXBContextImpl context, RuntimeMapPropertyInfo prop) { |
aoqi@0 | 89 | super(context, prop); |
aoqi@0 | 90 | acc = prop.getAccessor().optimize(context); |
aoqi@0 | 91 | this.tagName = context.nameBuilder.createElementName(prop.getXmlName()); |
aoqi@0 | 92 | this.entryTag = context.nameBuilder.createElementName("","entry"); |
aoqi@0 | 93 | this.keyTag = context.nameBuilder.createElementName("","key"); |
aoqi@0 | 94 | this.valueTag = context.nameBuilder.createElementName("","value"); |
aoqi@0 | 95 | this.nillable = prop.isCollectionNillable(); |
aoqi@0 | 96 | this.keyBeanInfo = context.getOrCreate(prop.getKeyType()); |
aoqi@0 | 97 | this.valueBeanInfo = context.getOrCreate(prop.getValueType()); |
aoqi@0 | 98 | |
aoqi@0 | 99 | // infer the implementation class |
aoqi@0 | 100 | //noinspection unchecked |
aoqi@0 | 101 | Class<ValueT> sig = (Class<ValueT>) Utils.REFLECTION_NAVIGATOR.erasure(prop.getRawType()); |
aoqi@0 | 102 | mapImplClass = ClassFactory.inferImplClass(sig,knownImplClasses); |
aoqi@0 | 103 | // TODO: error check for mapImplClass==null |
aoqi@0 | 104 | // what is the error reporting path for this part of the code? |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | private static final Class[] knownImplClasses = { |
aoqi@0 | 108 | HashMap.class, TreeMap.class, LinkedHashMap.class |
aoqi@0 | 109 | }; |
aoqi@0 | 110 | |
aoqi@0 | 111 | public void reset(BeanT bean) throws AccessorException { |
aoqi@0 | 112 | acc.set(bean,null); |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | |
aoqi@0 | 116 | /** |
aoqi@0 | 117 | * A Map property can never be ID. |
aoqi@0 | 118 | */ |
aoqi@0 | 119 | public String getIdValue(BeanT bean) { |
aoqi@0 | 120 | return null; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | public PropertyKind getKind() { |
aoqi@0 | 124 | return PropertyKind.MAP; |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) { |
aoqi@0 | 128 | keyLoader = keyBeanInfo.getLoader(chain.context,true); |
aoqi@0 | 129 | valueLoader = valueBeanInfo.getLoader(chain.context,true); |
aoqi@0 | 130 | handlers.put(tagName,new ChildLoader(itemsLoader,null)); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | private Loader keyLoader; |
aoqi@0 | 134 | private Loader valueLoader; |
aoqi@0 | 135 | |
aoqi@0 | 136 | /** |
aoqi@0 | 137 | * Handles <items> and </items>. |
aoqi@0 | 138 | * |
aoqi@0 | 139 | * The target will be set to a {@link Map}. |
aoqi@0 | 140 | */ |
aoqi@0 | 141 | private final Loader itemsLoader = new Loader(false) { |
aoqi@0 | 142 | |
aoqi@0 | 143 | private ThreadLocal<BeanT> target = new ThreadLocal<BeanT>(); |
aoqi@0 | 144 | private ThreadLocal<ValueT> map = new ThreadLocal<ValueT>(); |
aoqi@0 | 145 | private int depthCounter = 0; // needed to clean ThreadLocals |
aoqi@0 | 146 | |
aoqi@0 | 147 | @Override |
aoqi@0 | 148 | public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
aoqi@0 | 149 | // create or obtain the Map object |
aoqi@0 | 150 | try { |
aefimov@650 | 151 | target.set((BeanT)state.getPrev().getTarget()); |
aoqi@0 | 152 | map.set(acc.get(target.get())); |
aoqi@0 | 153 | depthCounter++; |
aoqi@0 | 154 | if(map.get() == null) { |
aoqi@0 | 155 | map.set(ClassFactory.create(mapImplClass)); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | map.get().clear(); |
aefimov@650 | 158 | state.setTarget(map.get()); |
aoqi@0 | 159 | } catch (AccessorException e) { |
aoqi@0 | 160 | // recover from error by setting a dummy Map that receives and discards the values |
aoqi@0 | 161 | handleGenericException(e,true); |
aefimov@650 | 162 | state.setTarget(new HashMap()); |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | @Override |
aoqi@0 | 167 | public void leaveElement(State state, TagName ea) throws SAXException { |
aoqi@0 | 168 | super.leaveElement(state, ea); |
aoqi@0 | 169 | try { |
aoqi@0 | 170 | acc.set(target.get(), map.get()); |
aoqi@0 | 171 | if (--depthCounter == 0) { |
aoqi@0 | 172 | target.remove(); |
aoqi@0 | 173 | map.remove(); |
aoqi@0 | 174 | } |
aoqi@0 | 175 | } catch (AccessorException ex) { |
aoqi@0 | 176 | handleGenericException(ex,true); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | @Override |
aoqi@0 | 181 | public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
aoqi@0 | 182 | if(ea.matches(entryTag)) { |
aefimov@650 | 183 | state.setLoader(entryLoader); |
aoqi@0 | 184 | } else { |
aoqi@0 | 185 | super.childElement(state,ea); |
aoqi@0 | 186 | } |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | @Override |
aoqi@0 | 190 | public Collection<QName> getExpectedChildElements() { |
aoqi@0 | 191 | return Collections.singleton(entryTag.toQName()); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | }; |
aoqi@0 | 194 | |
aoqi@0 | 195 | /** |
aoqi@0 | 196 | * Handles <entry> and </entry>. |
aoqi@0 | 197 | * |
aoqi@0 | 198 | * The target will be set to a {@link Map}. |
aoqi@0 | 199 | */ |
aoqi@0 | 200 | private final Loader entryLoader = new Loader(false) { |
aoqi@0 | 201 | @Override |
aoqi@0 | 202 | public void startElement(UnmarshallingContext.State state, TagName ea) { |
aefimov@650 | 203 | state.setTarget(new Object[2]); // this is inefficient |
aoqi@0 | 204 | } |
aoqi@0 | 205 | |
aoqi@0 | 206 | @Override |
aoqi@0 | 207 | public void leaveElement(UnmarshallingContext.State state, TagName ea) { |
aefimov@650 | 208 | Object[] keyValue = (Object[])state.getTarget(); |
aefimov@650 | 209 | Map map = (Map) state.getPrev().getTarget(); |
aoqi@0 | 210 | map.put(keyValue[0],keyValue[1]); |
aoqi@0 | 211 | } |
aoqi@0 | 212 | |
aoqi@0 | 213 | @Override |
aoqi@0 | 214 | public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { |
aoqi@0 | 215 | if(ea.matches(keyTag)) { |
aefimov@650 | 216 | state.setLoader(keyLoader); |
aefimov@650 | 217 | state.setReceiver(keyReceiver); |
aoqi@0 | 218 | return; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | if(ea.matches(valueTag)) { |
aefimov@650 | 221 | state.setLoader(valueLoader); |
aefimov@650 | 222 | state.setReceiver(valueReceiver); |
aoqi@0 | 223 | return; |
aoqi@0 | 224 | } |
aoqi@0 | 225 | super.childElement(state,ea); |
aoqi@0 | 226 | } |
aoqi@0 | 227 | |
aoqi@0 | 228 | @Override |
aoqi@0 | 229 | public Collection<QName> getExpectedChildElements() { |
aoqi@0 | 230 | return Arrays.asList(keyTag.toQName(),valueTag.toQName()); |
aoqi@0 | 231 | } |
aoqi@0 | 232 | }; |
aoqi@0 | 233 | |
aoqi@0 | 234 | private static final class ReceiverImpl implements Receiver { |
aoqi@0 | 235 | private final int index; |
aoqi@0 | 236 | public ReceiverImpl(int index) { |
aoqi@0 | 237 | this.index = index; |
aoqi@0 | 238 | } |
aoqi@0 | 239 | public void receive(UnmarshallingContext.State state, Object o) { |
aefimov@650 | 240 | ((Object[])state.getTarget())[index] = o; |
aoqi@0 | 241 | } |
aoqi@0 | 242 | } |
aoqi@0 | 243 | |
aoqi@0 | 244 | private static final Receiver keyReceiver = new ReceiverImpl(0); |
aoqi@0 | 245 | private static final Receiver valueReceiver = new ReceiverImpl(1); |
aoqi@0 | 246 | |
aoqi@0 | 247 | @Override |
aoqi@0 | 248 | public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { |
aoqi@0 | 249 | ValueT v = acc.get(o); |
aoqi@0 | 250 | if(v!=null) { |
aoqi@0 | 251 | bareStartTag(w,tagName,v); |
aoqi@0 | 252 | for( Map.Entry e : (Set<Map.Entry>)v.entrySet() ) { |
aoqi@0 | 253 | bareStartTag(w,entryTag,null); |
aoqi@0 | 254 | |
aoqi@0 | 255 | Object key = e.getKey(); |
aoqi@0 | 256 | if(key!=null) { |
aoqi@0 | 257 | w.startElement(keyTag,key); |
aoqi@0 | 258 | w.childAsXsiType(key,fieldName,keyBeanInfo, false); |
aoqi@0 | 259 | w.endElement(); |
aoqi@0 | 260 | } |
aoqi@0 | 261 | |
aoqi@0 | 262 | Object value = e.getValue(); |
aoqi@0 | 263 | if(value!=null) { |
aoqi@0 | 264 | w.startElement(valueTag,value); |
aoqi@0 | 265 | w.childAsXsiType(value,fieldName,valueBeanInfo, false); |
aoqi@0 | 266 | w.endElement(); |
aoqi@0 | 267 | } |
aoqi@0 | 268 | |
aoqi@0 | 269 | w.endElement(); |
aoqi@0 | 270 | } |
aoqi@0 | 271 | w.endElement(); |
aoqi@0 | 272 | } else |
aoqi@0 | 273 | if(nillable) { |
aoqi@0 | 274 | w.startElement(tagName,null); |
aoqi@0 | 275 | w.writeXsiNilTrue(); |
aoqi@0 | 276 | w.endElement(); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | } |
aoqi@0 | 279 | |
aoqi@0 | 280 | private void bareStartTag(XMLSerializer w, Name tagName, Object peer) throws IOException, XMLStreamException, SAXException { |
aoqi@0 | 281 | w.startElement(tagName,peer); |
aoqi@0 | 282 | w.endNamespaceDecls(peer); |
aoqi@0 | 283 | w.endAttributes(); |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | @Override |
aoqi@0 | 287 | public Accessor getElementPropertyAccessor(String nsUri, String localName) { |
aoqi@0 | 288 | if(tagName.equals(nsUri,localName)) |
aoqi@0 | 289 | return acc; |
aoqi@0 | 290 | return null; |
aoqi@0 | 291 | } |
aoqi@0 | 292 | } |