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.util.Collection; aoqi@0: aoqi@0: import javax.xml.namespace.QName; 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.util.QNameMap; aoqi@0: import com.sun.xml.internal.bind.v2.model.runtime.RuntimePropertyInfo; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.Name; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Lister; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName; 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.Scope; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext; aoqi@0: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; aoqi@0: aoqi@0: import org.xml.sax.SAXException; aoqi@0: aoqi@0: /** aoqi@0: * Commonality between {@link ArrayElementProperty} and {@link ArrayReferenceNodeProperty}. aoqi@0: * aoqi@0: * Mostly handles the unmarshalling of the wrapper element. aoqi@0: * aoqi@0: * @author Kohsuke Kawaguchi aoqi@0: */ aoqi@0: abstract class ArrayERProperty extends ArrayProperty { aoqi@0: aoqi@0: /** aoqi@0: * Wrapper tag name if any, or null. aoqi@0: */ aoqi@0: protected final Name wrapperTagName; aoqi@0: aoqi@0: /** aoqi@0: * True if the wrapper tag name is nillable. aoqi@0: * Always false if {@link #wrapperTagName}==null. aoqi@0: */ aoqi@0: protected final boolean isWrapperNillable; aoqi@0: aoqi@0: protected ArrayERProperty(JAXBContextImpl grammar, RuntimePropertyInfo prop, QName tagName, boolean isWrapperNillable) { aoqi@0: super(grammar,prop); aoqi@0: if(tagName==null) aoqi@0: this.wrapperTagName = null; aoqi@0: else aoqi@0: this.wrapperTagName = grammar.nameBuilder.createElementName(tagName); aoqi@0: this.isWrapperNillable = isWrapperNillable; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Used to handle the collection wrapper element. aoqi@0: */ aoqi@0: private static final class ItemsLoader extends Loader { aoqi@0: aoqi@0: private final Accessor acc; aoqi@0: private final Lister lister; aoqi@0: aoqi@0: public ItemsLoader(Accessor acc, Lister lister, QNameMap children) { aoqi@0: super(false); aoqi@0: this.acc = acc; aoqi@0: this.lister = lister; aoqi@0: this.children = children; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void startElement(UnmarshallingContext.State state, TagName ea) throws SAXException { aoqi@0: UnmarshallingContext context = state.getContext(); aoqi@0: context.startScope(1); aoqi@0: // inherit the target so that our children can access its target aoqi@0: state.target = state.prev.target; aoqi@0: aoqi@0: // start it now, so that even if there's no children we can still return empty collection aoqi@0: context.getScope(0).start(acc,lister); aoqi@0: } aoqi@0: aoqi@0: private final QNameMap children; aoqi@0: aoqi@0: @Override aoqi@0: public void childElement(UnmarshallingContext.State state, TagName ea) throws SAXException { aoqi@0: ChildLoader child = children.get(ea.uri,ea.local); aoqi@0: if (child == null) { aoqi@0: child = children.get(CATCH_ALL); aoqi@0: } aoqi@0: if (child == null) { aoqi@0: super.childElement(state,ea); aoqi@0: return; aoqi@0: } aoqi@0: state.loader = child.loader; aoqi@0: state.receiver = child.receiver; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public void leaveElement(UnmarshallingContext.State state, TagName ea) throws SAXException { aoqi@0: state.getContext().endScope(1); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public Collection getExpectedChildElements() { aoqi@0: return children.keySet(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: public final void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { aoqi@0: ListT list = acc.get(o); aoqi@0: aoqi@0: if(list!=null) { aoqi@0: if(wrapperTagName!=null) { aoqi@0: w.startElement(wrapperTagName,null); aoqi@0: w.endNamespaceDecls(list); aoqi@0: w.endAttributes(); aoqi@0: } aoqi@0: aoqi@0: serializeListBody(o,w,list); aoqi@0: aoqi@0: if(wrapperTagName!=null) aoqi@0: w.endElement(); aoqi@0: } else { aoqi@0: // list is null aoqi@0: if(isWrapperNillable) { aoqi@0: w.startElement(wrapperTagName,null); aoqi@0: w.writeXsiNilTrue(); aoqi@0: w.endElement(); aoqi@0: } // otherwise don't print the wrapper tag name aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Serializes the items of the list. aoqi@0: * This method is invoked after the necessary wrapper tag is produced (if necessary.) aoqi@0: * aoqi@0: * @param list aoqi@0: * always non-null. aoqi@0: */ aoqi@0: protected abstract void serializeListBody(BeanT o, XMLSerializer w, ListT list) throws IOException, XMLStreamException, SAXException, AccessorException; aoqi@0: aoqi@0: /** aoqi@0: * Creates the unmarshaler to unmarshal the body. aoqi@0: */ aoqi@0: protected abstract void createBodyUnmarshaller(UnmarshallerChain chain, QNameMap loaders); aoqi@0: aoqi@0: aoqi@0: public final void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap loaders) { aoqi@0: if(wrapperTagName!=null) { aoqi@0: UnmarshallerChain c = new UnmarshallerChain(chain.context); aoqi@0: QNameMap m = new QNameMap(); aoqi@0: createBodyUnmarshaller(c,m); aoqi@0: Loader loader = new ItemsLoader(acc, lister, m); aoqi@0: if(isWrapperNillable || chain.context.allNillable) aoqi@0: loader = new XsiNilLoader(loader); aoqi@0: loaders.put(wrapperTagName,new ChildLoader(loader,null)); aoqi@0: } else { aoqi@0: createBodyUnmarshaller(chain,loaders); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * {@link Receiver} that puts the child object into the {@link Scope} object. aoqi@0: */ aoqi@0: protected final class ReceiverImpl implements Receiver { aoqi@0: private final int offset; aoqi@0: aoqi@0: protected ReceiverImpl(int offset) { aoqi@0: this.offset = offset; aoqi@0: } aoqi@0: aoqi@0: public void receive(UnmarshallingContext.State state, Object o) throws SAXException { aoqi@0: state.getContext().getScope(offset).add(acc,lister,o); aoqi@0: } aoqi@0: }}