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

Sun, 31 Aug 2014 16:14:36 +0400

author
aefimov
date
Sun, 31 Aug 2014 16:14:36 +0400
changeset 707
31893650acaf
parent 650
121e938cb9c3
child 760
e530533619ec
permissions
-rw-r--r--

8036981: JAXB not preserving formatting for xsd:any Mixed content
Reviewed-by: lancea, mkos

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

mercurial