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: aoqi@0: import javax.xml.bind.JAXBException; aoqi@0: import javax.xml.bind.annotation.DomHandler; aoqi@0: import javax.xml.stream.XMLStreamException; aoqi@0: aoqi@0: import com.sun.xml.internal.bind.v2.ClassFactory; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.PropertyKind; aoqi@0: import com.sun.xml.internal.bind.v2.model.core.WildcardMode; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElement; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeReferencePropertyInfo; 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.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.unmarshaller.ChildLoader; 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.UnmarshallingContext; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.WildcardLoader; 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: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: class ArrayReferenceNodeProperty extends ArrayERProperty { aoqi@0: aoqi@0: /** aoqi@0: * Expected element names and what class to unmarshal. aoqi@0: */ aoqi@0: private final QNameMap expectedElements = new QNameMap(); aoqi@0: aoqi@0: private final boolean isMixed; aoqi@0: aoqi@0: private final DomHandler domHandler; aoqi@0: private final WildcardMode wcMode; aoqi@0: aoqi@0: public ArrayReferenceNodeProperty(JAXBContextImpl p, RuntimeReferencePropertyInfo prop) { aoqi@0: super(p, prop, prop.getXmlName(), prop.isCollectionNillable()); aoqi@0: aoqi@0: for (RuntimeElement e : prop.getElements()) { aoqi@0: JaxBeanInfo bi = p.getOrCreate(e); aoqi@0: expectedElements.put( e.getElementName().getNamespaceURI(),e.getElementName().getLocalPart(), bi ); aoqi@0: } aoqi@0: aoqi@0: isMixed = prop.isMixed(); aoqi@0: aoqi@0: if(prop.getWildcard()!=null) { aoqi@0: domHandler = (DomHandler) ClassFactory.create(prop.getDOMHandler()); aoqi@0: wcMode = prop.getWildcard(); aoqi@0: } else { aoqi@0: domHandler = null; aoqi@0: wcMode = null; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: protected final void serializeListBody(BeanT o, XMLSerializer w, ListT list) throws IOException, XMLStreamException, SAXException { aoqi@0: ListIterator itr = lister.iterator(list, w); aoqi@0: aoqi@0: while(itr.hasNext()) { aoqi@0: try { aoqi@0: ItemT item = itr.next(); aoqi@0: if (item != null) { aoqi@0: if(isMixed && item.getClass()==String.class) { aoqi@0: w.text((String)item,null); aoqi@0: } else { aoqi@0: JaxBeanInfo bi = w.grammar.getBeanInfo(item,true); aoqi@0: if(bi.jaxbType==Object.class && domHandler!=null) aoqi@0: // even if 'v' is a DOM node, it always derive from Object, aoqi@0: // so the getBeanInfo returns BeanInfo for Object aoqi@0: w.writeDom(item,domHandler,o,fieldName); aoqi@0: else aoqi@0: bi.serializeRoot(item,w); 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: public void createBodyUnmarshaller(UnmarshallerChain chain, QNameMap loaders) { aoqi@0: final int offset = chain.allocateOffset(); aoqi@0: aoqi@0: Receiver recv = new ReceiverImpl(offset); aoqi@0: aoqi@0: for( QNameMap.Entry n : expectedElements.entrySet() ) { aoqi@0: final JaxBeanInfo beanInfo = n.getValue(); aoqi@0: loaders.put(n.nsUri,n.localName,new ChildLoader(beanInfo.getLoader(chain.context,true),recv)); aoqi@0: } aoqi@0: aoqi@0: if(isMixed) { aoqi@0: // handler for processing mixed contents. aoqi@0: loaders.put(TEXT_HANDLER, aoqi@0: new ChildLoader(new MixedTextLoader(recv),null)); aoqi@0: } aoqi@0: aoqi@0: if(domHandler!=null) { aoqi@0: loaders.put(CATCH_ALL, aoqi@0: new ChildLoader(new WildcardLoader(domHandler,wcMode),recv)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: private static final class MixedTextLoader extends Loader { aoqi@0: aoqi@0: private final Receiver recv; aoqi@0: aoqi@0: public MixedTextLoader(Receiver recv) { aoqi@0: super(true); aoqi@0: this.recv = recv; aoqi@0: } aoqi@0: aoqi@0: public void text(UnmarshallingContext.State state, CharSequence text) throws SAXException { aoqi@0: if(text.length()!=0) // length 0 text is pointless aoqi@0: recv.receive(state,text.toString()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: public PropertyKind getKind() { aoqi@0: return PropertyKind.REFERENCE; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Accessor getElementPropertyAccessor(String nsUri, String localName) { aoqi@0: // TODO: unwrap JAXBElement aoqi@0: if(wrapperTagName!=null) { aoqi@0: if(wrapperTagName.equals(nsUri,localName)) aoqi@0: return acc; aoqi@0: } else { aoqi@0: if(expectedElements.containsKey(nsUri,localName)) aoqi@0: return acc; aoqi@0: } aoqi@0: return null; aoqi@0: } aoqi@0: }