aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.bind.v2.runtime.property; aoqi@0: aoqi@0: import java.io.IOException; aoqi@0: import java.lang.reflect.Type; aoqi@0: import java.util.HashMap; aoqi@0: import java.util.List; aoqi@0: import java.util.Map; aoqi@0: aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.api.AccessorException; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.TypeRef; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Name; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.RuntimeUtil; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Transducer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.ListIterator; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Lister; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.NullSafeAccessor; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DefaultValueLoaderDecorator; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Receiver; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TextLoader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; aoqi@0: import com.sun.xml.internal.bind.v2.util.QNameMap; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * {@link Property} implementation for multi-value property that maps to an element. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: abstract class ArrayElementProperty extends ArrayERProperty { aoqi@0: aoqi@0: private final Map typeMap = new HashMap(); aoqi@0: /** aoqi@0: * Set by the constructor and reset in the {@link #wrapUp()} method. aoqi@0: */ aoqi@0: private Map,JaxBeanInfo> refs = new HashMap, JaxBeanInfo>(); aoqi@0: /** aoqi@0: * Set by the constructor and reset in the {@link #wrapUp()} method. aoqi@0: */ aoqi@0: protected RuntimeElementPropertyInfo prop; aoqi@0: aoqi@0: /** aoqi@0: * Tag name used when we see null in the collection. Can be null. aoqi@0: */ aoqi@0: private final Name nillableTagName; aoqi@0: aoqi@0: protected ArrayElementProperty(JAXBContextImpl grammar, RuntimeElementPropertyInfo prop) { aoqi@0: super(grammar, prop, prop.getXmlName(), prop.isCollectionNillable()); aoqi@0: this.prop = prop; aoqi@0: aoqi@0: List types = prop.getTypes(); aoqi@0: aoqi@0: Name n = null; aoqi@0: aoqi@0: for (RuntimeTypeRef typeRef : types) { aoqi@0: Class type = (Class)typeRef.getTarget().getType(); aoqi@0: if(type.isPrimitive()) aoqi@0: type = RuntimeUtil.primitiveToBox.get(type); aoqi@0: aoqi@0: JaxBeanInfo beanInfo = grammar.getOrCreate(typeRef.getTarget()); aoqi@0: TagAndType tt = new TagAndType( aoqi@0: grammar.nameBuilder.createElementName(typeRef.getTagName()), aoqi@0: beanInfo); aoqi@0: typeMap.put(type,tt); aoqi@0: refs.put(typeRef,beanInfo); aoqi@0: if(typeRef.isNillable() && n==null) aoqi@0: n = tt.tagName; aoqi@0: } aoqi@0: aoqi@0: nillableTagName = n; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void wrapUp() { aoqi@0: super.wrapUp(); aoqi@0: refs = null; aoqi@0: prop = null; // avoid keeping model objects live aoqi@0: } aoqi@0: aoqi@0: protected void serializeListBody(BeanT beanT, XMLSerializer w, ListT list) throws IOException, XMLStreamException, SAXException, AccessorException { aoqi@0: ListIterator itr = lister.iterator(list, w); aoqi@0: aoqi@0: boolean isIdref = itr instanceof Lister.IDREFSIterator; // UGLY aoqi@0: aoqi@0: while(itr.hasNext()) { aoqi@0: try { aoqi@0: ItemT item = itr.next(); aoqi@0: if (item != null) { aoqi@0: Class itemType = item.getClass(); aoqi@0: if(isIdref) aoqi@0: // This should be the only place where we need to be aware aoqi@0: // that the iterator is iterating IDREFS. aoqi@0: itemType = ((Lister.IDREFSIterator)itr).last().getClass(); aoqi@0: aoqi@0: // normally, this returns non-null aoqi@0: TagAndType tt = typeMap.get(itemType); aoqi@0: while(tt==null && itemType!=null) { aoqi@0: // otherwise we'll just have to try the slow way aoqi@0: itemType = itemType.getSuperclass(); aoqi@0: tt = typeMap.get(itemType); aoqi@0: } aoqi@0: aoqi@0: if(tt==null) { aoqi@0: // item is not of the expected type. aoqi@0: // w.reportError(new ValidationEventImpl(ValidationEvent.ERROR, aoqi@0: // Messages.UNEXPECTED_JAVA_TYPE.format( aoqi@0: // item.getClass().getName(), aoqi@0: // getExpectedClassNameList() aoqi@0: // ), aoqi@0: // w.getCurrentLocation(fieldName))); aoqi@0: // continue; aoqi@0: aoqi@0: // see the similar code in SingleElementNodeProperty. aoqi@0: // for the purpose of simple type substitution, make it a non-error aoqi@0: aoqi@0: w.startElement(typeMap.values().iterator().next().tagName,null); aoqi@0: w.childAsXsiType(item,fieldName,w.grammar.getBeanInfo(Object.class), false); aoqi@0: } else { aoqi@0: w.startElement(tt.tagName,null); aoqi@0: serializeItem(tt.beanInfo,item,w); aoqi@0: } aoqi@0: aoqi@0: w.endElement(); aoqi@0: } else { aoqi@0: if(nillableTagName!=null) { aoqi@0: w.startElement(nillableTagName,null); aoqi@0: w.writeXsiNilTrue(); aoqi@0: w.endElement(); aoqi@0: } aoqi@0: } aoqi@0: } catch (JAXBException e) { aoqi@0: w.reportError(fieldName,e); aoqi@0: // recover by ignoring this item aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Serializes one item of the property. aoqi@0: */ aoqi@0: protected abstract void serializeItem(JaxBeanInfo expected, ItemT item, XMLSerializer w) throws SAXException, AccessorException, IOException, XMLStreamException; aoqi@0: aoqi@0: aoqi@0: public void createBodyUnmarshaller(UnmarshallerChain chain, QNameMap loaders) { aoqi@0: aoqi@0: // all items go to the same lister, aoqi@0: // so they should share the same offset. aoqi@0: int offset = chain.allocateOffset(); aoqi@0: Receiver recv = new ReceiverImpl(offset); aoqi@0: aoqi@0: for (RuntimeTypeRef typeRef : prop.getTypes()) { aoqi@0: aoqi@0: Name tagName = chain.context.nameBuilder.createElementName(typeRef.getTagName()); aoqi@0: Loader item = createItemUnmarshaller(chain,typeRef); aoqi@0: aoqi@0: if(typeRef.isNillable() || chain.context.allNillable) aoqi@0: item = new XsiNilLoader.Array(item); aoqi@0: if(typeRef.getDefaultValue()!=null) aoqi@0: item = new DefaultValueLoaderDecorator(item,typeRef.getDefaultValue()); aoqi@0: aoqi@0: loaders.put(tagName,new ChildLoader(item,recv)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final PropertyKind getKind() { aoqi@0: return PropertyKind.ELEMENT; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Creates a loader handler that unmarshals the body of the item. aoqi@0: * aoqi@0: *

aoqi@0: * This will be sandwiched into ... . aoqi@0: * aoqi@0: *

aoqi@0: * When unmarshalling the body of item, the Pack of {@link Lister} is available aoqi@0: * as the handler state. aoqi@0: * aoqi@0: * @param chain aoqi@0: * @param typeRef aoqi@0: */ aoqi@0: private Loader createItemUnmarshaller(UnmarshallerChain chain, RuntimeTypeRef typeRef) { aoqi@0: if(PropertyFactory.isLeaf(typeRef.getSource())) { aoqi@0: final Transducer xducer = typeRef.getTransducer(); aoqi@0: return new TextLoader(xducer); aoqi@0: } else { aoqi@0: return refs.get(typeRef).getLoader(chain.context,true); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public Accessor getElementPropertyAccessor(String nsUri, String localName) { aoqi@0: if(wrapperTagName!=null) { aoqi@0: if(wrapperTagName.equals(nsUri,localName)) aoqi@0: return acc; aoqi@0: } else { aoqi@0: for (TagAndType tt : typeMap.values()) { aoqi@0: if(tt.tagName.equals(nsUri,localName)) aoqi@0: // when we can't distinguish null and empty list, JAX-WS doesn't want to see aoqi@0: // null (just like any user apps), but since we are providing a raw accessor, aoqi@0: // which just grabs the value from the field, we wrap it so that it won't return aoqi@0: // null. aoqi@0: return new NullSafeAccessor(acc,lister); aoqi@0: } aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }