ohair@286: /* aefimov@650: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.bind.v2.runtime.property; ohair@286: ohair@286: import java.io.IOException; ohair@286: import java.util.HashMap; ohair@286: import java.util.LinkedHashMap; ohair@286: import java.util.Map; ohair@286: import java.util.TreeMap; ohair@286: import java.util.Collection; ohair@286: import java.util.Collections; ohair@286: import java.util.Arrays; ohair@286: import java.util.Set; ohair@286: ohair@286: import javax.xml.stream.XMLStreamException; ohair@286: import javax.xml.namespace.QName; ohair@286: ohair@286: import com.sun.xml.internal.bind.api.AccessorException; ohair@286: import com.sun.xml.internal.bind.v2.ClassFactory; ohair@286: import com.sun.xml.internal.bind.v2.util.QNameMap; ohair@286: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; ohair@286: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeMapPropertyInfo; ohair@286: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; ohair@286: import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; ohair@286: import com.sun.xml.internal.bind.v2.runtime.Name; ohair@286: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; ohair@286: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; ohair@286: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.State; ohair@286: ohair@286: import org.xml.sax.SAXException; ohair@286: ohair@286: /** ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: final class SingleMapNodeProperty extends PropertyImpl { ohair@286: ohair@286: private final Accessor acc; ohair@286: /** ohair@286: * The tag name that surrounds the whole property. ohair@286: */ ohair@286: private final Name tagName; ohair@286: /** ohair@286: * The tag name that corresponds to the 'entry' element. ohair@286: */ ohair@286: private final Name entryTag; ohair@286: private final Name keyTag; ohair@286: private final Name valueTag; ohair@286: ohair@286: private final boolean nillable; ohair@286: ohair@286: private JaxBeanInfo keyBeanInfo; ohair@286: private JaxBeanInfo valueBeanInfo; ohair@286: ohair@286: /** ohair@286: * The implementation class for this property. ohair@286: * If the property is null, we create an instance of this class. ohair@286: */ ohair@286: private final Class mapImplClass; ohair@286: ohair@286: public SingleMapNodeProperty(JAXBContextImpl context, RuntimeMapPropertyInfo prop) { ohair@286: super(context, prop); ohair@286: acc = prop.getAccessor().optimize(context); ohair@286: this.tagName = context.nameBuilder.createElementName(prop.getXmlName()); ohair@286: this.entryTag = context.nameBuilder.createElementName("","entry"); ohair@286: this.keyTag = context.nameBuilder.createElementName("","key"); ohair@286: this.valueTag = context.nameBuilder.createElementName("","value"); ohair@286: this.nillable = prop.isCollectionNillable(); ohair@286: this.keyBeanInfo = context.getOrCreate(prop.getKeyType()); ohair@286: this.valueBeanInfo = context.getOrCreate(prop.getValueType()); ohair@286: ohair@286: // infer the implementation class mkos@450: //noinspection unchecked mkos@450: Class sig = (Class) Utils.REFLECTION_NAVIGATOR.erasure(prop.getRawType()); ohair@286: mapImplClass = ClassFactory.inferImplClass(sig,knownImplClasses); ohair@286: // TODO: error check for mapImplClass==null ohair@286: // what is the error reporting path for this part of the code? ohair@286: } ohair@286: ohair@286: private static final Class[] knownImplClasses = { ohair@286: HashMap.class, TreeMap.class, LinkedHashMap.class ohair@286: }; ohair@286: ohair@286: public void reset(BeanT bean) throws AccessorException { ohair@286: acc.set(bean,null); ohair@286: } ohair@286: ohair@286: ohair@286: /** ohair@286: * A Map property can never be ID. ohair@286: */ ohair@286: public String getIdValue(BeanT bean) { ohair@286: return null; ohair@286: } ohair@286: ohair@286: public PropertyKind getKind() { ohair@286: return PropertyKind.MAP; ohair@286: } ohair@286: ohair@286: public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap handlers) { ohair@286: keyLoader = keyBeanInfo.getLoader(chain.context,true); ohair@286: valueLoader = valueBeanInfo.getLoader(chain.context,true); ohair@286: handlers.put(tagName,new ChildLoader(itemsLoader,null)); ohair@286: } ohair@286: ohair@286: private Loader keyLoader; ohair@286: private Loader valueLoader; ohair@286: ohair@286: /** ohair@286: * Handles <items> and </items>. ohair@286: * ohair@286: * The target will be set to a {@link Map}. ohair@286: */ ohair@286: private final Loader itemsLoader = new Loader(false) { ohair@286: mkos@450: private ThreadLocal target = new ThreadLocal(); mkos@450: private ThreadLocal map = new ThreadLocal(); mkos@450: private int depthCounter = 0; // needed to clean ThreadLocals ohair@286: ohair@286: @Override ohair@286: public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { ohair@286: // create or obtain the Map object ohair@286: try { aefimov@650: target.set((BeanT)state.getPrev().getTarget()); mkos@450: map.set(acc.get(target.get())); mkos@450: depthCounter++; mkos@450: if(map.get() == null) { mkos@450: map.set(ClassFactory.create(mapImplClass)); mkos@450: } mkos@450: map.get().clear(); aefimov@650: state.setTarget(map.get()); ohair@286: } catch (AccessorException e) { ohair@286: // recover from error by setting a dummy Map that receives and discards the values ohair@286: handleGenericException(e,true); aefimov@650: state.setTarget(new HashMap()); ohair@286: } ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void leaveElement(State state, TagName ea) throws SAXException { ohair@286: super.leaveElement(state, ea); ohair@286: try { mkos@450: acc.set(target.get(), map.get()); mkos@450: if (--depthCounter == 0) { mkos@450: target.remove(); mkos@450: map.remove(); mkos@450: } ohair@286: } catch (AccessorException ex) { ohair@286: handleGenericException(ex,true); ohair@286: } ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { ohair@286: if(ea.matches(entryTag)) { aefimov@650: state.setLoader(entryLoader); ohair@286: } else { ohair@286: super.childElement(state,ea); ohair@286: } ohair@286: } ohair@286: ohair@286: @Override ohair@286: public Collection getExpectedChildElements() { ohair@286: return Collections.singleton(entryTag.toQName()); ohair@286: } ohair@286: }; ohair@286: ohair@286: /** ohair@286: * Handles <entry> and </entry>. ohair@286: * ohair@286: * The target will be set to a {@link Map}. ohair@286: */ ohair@286: private final Loader entryLoader = new Loader(false) { ohair@286: @Override ohair@286: public void startElement(UnmarshallingContext.State state, TagName ea) { aefimov@650: state.setTarget(new Object[2]); // this is inefficient ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void leaveElement(UnmarshallingContext.State state, TagName ea) { aefimov@650: Object[] keyValue = (Object[])state.getTarget(); aefimov@650: Map map = (Map) state.getPrev().getTarget(); ohair@286: map.put(keyValue[0],keyValue[1]); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { ohair@286: if(ea.matches(keyTag)) { aefimov@650: state.setLoader(keyLoader); aefimov@650: state.setReceiver(keyReceiver); ohair@286: return; ohair@286: } ohair@286: if(ea.matches(valueTag)) { aefimov@650: state.setLoader(valueLoader); aefimov@650: state.setReceiver(valueReceiver); ohair@286: return; ohair@286: } ohair@286: super.childElement(state,ea); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public Collection getExpectedChildElements() { ohair@286: return Arrays.asList(keyTag.toQName(),valueTag.toQName()); ohair@286: } ohair@286: }; ohair@286: ohair@286: private static final class ReceiverImpl implements Receiver { ohair@286: private final int index; ohair@286: public ReceiverImpl(int index) { ohair@286: this.index = index; ohair@286: } ohair@286: public void receive(UnmarshallingContext.State state, Object o) { aefimov@650: ((Object[])state.getTarget())[index] = o; ohair@286: } ohair@286: } ohair@286: ohair@286: private static final Receiver keyReceiver = new ReceiverImpl(0); ohair@286: private static final Receiver valueReceiver = new ReceiverImpl(1); ohair@286: ohair@286: @Override ohair@286: public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { ohair@286: ValueT v = acc.get(o); ohair@286: if(v!=null) { ohair@286: bareStartTag(w,tagName,v); ohair@286: for( Map.Entry e : (Set)v.entrySet() ) { ohair@286: bareStartTag(w,entryTag,null); ohair@286: ohair@286: Object key = e.getKey(); ohair@286: if(key!=null) { ohair@286: w.startElement(keyTag,key); ohair@286: w.childAsXsiType(key,fieldName,keyBeanInfo, false); ohair@286: w.endElement(); ohair@286: } ohair@286: ohair@286: Object value = e.getValue(); ohair@286: if(value!=null) { ohair@286: w.startElement(valueTag,value); ohair@286: w.childAsXsiType(value,fieldName,valueBeanInfo, false); ohair@286: w.endElement(); ohair@286: } ohair@286: ohair@286: w.endElement(); ohair@286: } ohair@286: w.endElement(); ohair@286: } else ohair@286: if(nillable) { ohair@286: w.startElement(tagName,null); ohair@286: w.writeXsiNilTrue(); ohair@286: w.endElement(); ohair@286: } ohair@286: } ohair@286: ohair@286: private void bareStartTag(XMLSerializer w, Name tagName, Object peer) throws IOException, XMLStreamException, SAXException { ohair@286: w.startElement(tagName,peer); ohair@286: w.endNamespaceDecls(peer); ohair@286: w.endAttributes(); ohair@286: } ohair@286: ohair@286: @Override ohair@286: public Accessor getElementPropertyAccessor(String nsUri, String localName) { ohair@286: if(tagName.equals(nsUri,localName)) ohair@286: return acc; ohair@286: return null; ohair@286: } ohair@286: }