001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.bind.v2.runtime.property;
038:
039: import java.io.IOException;
040: import java.util.Collection;
041:
042: import javax.xml.namespace.QName;
043: import javax.xml.stream.XMLStreamException;
044:
045: import com.sun.xml.bind.api.AccessorException;
046: import com.sun.xml.bind.v2.util.QNameMap;
047: import com.sun.xml.bind.v2.model.runtime.RuntimePropertyInfo;
048: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
049: import com.sun.xml.bind.v2.runtime.Name;
050: import com.sun.xml.bind.v2.runtime.XMLSerializer;
051: import com.sun.xml.bind.v2.runtime.reflect.Lister;
052: import com.sun.xml.bind.v2.runtime.reflect.Accessor;
053: import com.sun.xml.bind.v2.runtime.unmarshaller.ChildLoader;
054: import com.sun.xml.bind.v2.runtime.unmarshaller.TagName;
055: import com.sun.xml.bind.v2.runtime.unmarshaller.Loader;
056: import com.sun.xml.bind.v2.runtime.unmarshaller.Receiver;
057: import com.sun.xml.bind.v2.runtime.unmarshaller.Scope;
058: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext;
059: import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader;
060:
061: import org.xml.sax.SAXException;
062:
063: /**
064: * Commonality between {@link ArrayElementProperty} and {@link ArrayReferenceNodeProperty}.
065: *
066: * Mostly handles the unmarshalling of the wrapper element.
067: *
068: * @author Kohsuke Kawaguchi
069: */
070: abstract class ArrayERProperty<BeanT, ListT, ItemT> extends
071: ArrayProperty<BeanT, ListT, ItemT> {
072:
073: /**
074: * Wrapper tag name if any, or null.
075: */
076: protected final Name wrapperTagName;
077:
078: /**
079: * True if the wrapper tag name is nillable.
080: * Always false if {@link #wrapperTagName}==null.
081: */
082: protected final boolean isWrapperNillable;
083:
084: protected ArrayERProperty(JAXBContextImpl grammar,
085: RuntimePropertyInfo prop, QName tagName,
086: boolean isWrapperNillable) {
087: super (grammar, prop);
088: if (tagName == null)
089: this .wrapperTagName = null;
090: else
091: this .wrapperTagName = grammar.nameBuilder
092: .createElementName(tagName);
093: this .isWrapperNillable = isWrapperNillable;
094: }
095:
096: /**
097: * Used to handle the collection wrapper element.
098: */
099: private static final class ItemsLoader extends Loader {
100:
101: private final Accessor acc;
102: private final Lister lister;
103:
104: public ItemsLoader(Accessor acc, Lister lister,
105: QNameMap<ChildLoader> children) {
106: super (false);
107: this .acc = acc;
108: this .lister = lister;
109: this .children = children;
110: }
111:
112: @Override
113: public void startElement(UnmarshallingContext.State state,
114: TagName ea) throws SAXException {
115: UnmarshallingContext context = state.getContext();
116: context.startScope(1);
117: // inherit the target so that our children can access its target
118: state.target = state.prev.target;
119:
120: // start it now, so that even if there's no children we can still return empty collection
121: context.getScope(0).start(acc, lister);
122: }
123:
124: private final QNameMap<ChildLoader> children;
125:
126: @Override
127: public void childElement(UnmarshallingContext.State state,
128: TagName ea) throws SAXException {
129: ChildLoader child = children.get(ea.uri, ea.local);
130: if (child != null) {
131: state.loader = child.loader;
132: state.receiver = child.receiver;
133: } else {
134: super .childElement(state, ea);
135: }
136: }
137:
138: @Override
139: public void leaveElement(UnmarshallingContext.State state,
140: TagName ea) throws SAXException {
141: state.getContext().endScope(1);
142: }
143:
144: @Override
145: public Collection<QName> getExpectedChildElements() {
146: return children.keySet();
147: }
148: }
149:
150: public final void serializeBody(BeanT o, XMLSerializer w,
151: Object outerPeer) throws SAXException, AccessorException,
152: IOException, XMLStreamException {
153: ListT list = acc.get(o);
154:
155: if (list != null) {
156: if (wrapperTagName != null) {
157: w.startElement(wrapperTagName, null);
158: w.endNamespaceDecls(list);
159: w.endAttributes();
160: }
161:
162: serializeListBody(o, w, list);
163:
164: if (wrapperTagName != null)
165: w.endElement();
166: } else {
167: // list is null
168: if (isWrapperNillable) {
169: w.startElement(wrapperTagName, null);
170: w.writeXsiNilTrue();
171: w.endElement();
172: } // otherwise don't print the wrapper tag name
173: }
174: }
175:
176: /**
177: * Serializses the items of the list.
178: * This method is invoked after the necessary wrapper tag is produced (if necessary.)
179: *
180: * @param list
181: * always non-null.
182: */
183: protected abstract void serializeListBody(BeanT o, XMLSerializer w,
184: ListT list) throws IOException, XMLStreamException,
185: SAXException, AccessorException;
186:
187: /**
188: * Creates the unmarshaller to unmarshal the body.
189: */
190: protected abstract void createBodyUnmarshaller(
191: UnmarshallerChain chain, QNameMap<ChildLoader> loaders);
192:
193: public final void buildChildElementUnmarshallers(
194: UnmarshallerChain chain, QNameMap<ChildLoader> loaders) {
195: if (wrapperTagName != null) {
196: UnmarshallerChain c = new UnmarshallerChain(chain.context);
197: QNameMap<ChildLoader> m = new QNameMap<ChildLoader>();
198: createBodyUnmarshaller(c, m);
199: Loader loader = new ItemsLoader(acc, lister, m);
200: if (isWrapperNillable || chain.context.allNillable)
201: loader = new XsiNilLoader(loader);
202: loaders.put(wrapperTagName, new ChildLoader(loader, null));
203: } else {
204: createBodyUnmarshaller(chain, loaders);
205: }
206: }
207:
208: /**
209: * {@link Receiver} that puts the child object into the {@link Scope} object.
210: */
211: protected final class ReceiverImpl implements Receiver {
212: private final int offset;
213:
214: protected ReceiverImpl(int offset) {
215: this .offset = offset;
216: }
217:
218: public void receive(UnmarshallingContext.State state, Object o)
219: throws SAXException {
220: state.getContext().getScope(offset).add(acc, lister, o);
221: }
222: }
223: }
|