001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.bind.v2.runtime;
027:
028: import java.io.IOException;
029:
030: import javax.xml.bind.JAXBElement;
031: import javax.xml.bind.JAXBException;
032: import javax.xml.namespace.QName;
033: import javax.xml.stream.XMLStreamException;
034:
035: import com.sun.xml.internal.bind.api.AccessorException;
036: import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
037: import com.sun.xml.internal.bind.v2.model.nav.Navigator;
038: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementInfo;
039: import com.sun.xml.internal.bind.v2.runtime.property.Property;
040: import com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory;
041: import com.sun.xml.internal.bind.v2.runtime.property.UnmarshallerChain;
042: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
043: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
044: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Discarder;
045: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Intercepter;
046: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
047: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.TagName;
048: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext;
049: import com.sun.xml.internal.bind.v2.util.QNameMap;
050:
051: import org.xml.sax.SAXException;
052:
053: /**
054: * {@link JaxBeanInfo} implementation for {@link RuntimeElementInfo}.
055: *
056: * @author Kohsuke Kawaguchi
057: */
058: public final class ElementBeanInfoImpl extends JaxBeanInfo<JAXBElement> {
059:
060: private Loader loader;
061:
062: private final Property property;
063:
064: // used to create new instances of JAXBElement.
065: private final QName tagName;
066: public final Class expectedType;
067: private final Class scope;
068:
069: ElementBeanInfoImpl(JAXBContextImpl grammar, RuntimeElementInfo rei) {
070: super (grammar, rei, (Class<JAXBElement>) rei.getType(), true,
071: false, true);
072:
073: this .property = PropertyFactory.create(grammar, rei
074: .getProperty());
075:
076: tagName = rei.getElementName();
077: expectedType = Navigator.REFLECTION.erasure(rei
078: .getContentInMemoryType());
079: scope = rei.getScope() == null ? JAXBElement.GlobalScope.class
080: : rei.getScope().getClazz();
081: }
082:
083: /**
084: * The constructor for the sole instanceof {@link JaxBeanInfo} for
085: * handling user-created {@link JAXBElement}.
086: *
087: * Such {@link JaxBeanInfo} is used only for marshalling.
088: *
089: * This is a hack.
090: */
091: protected ElementBeanInfoImpl(final JAXBContextImpl grammar) {
092: super (grammar, null, JAXBElement.class, true, false, true);
093: tagName = null;
094: expectedType = null;
095: scope = null;
096:
097: this .property = new Property<JAXBElement>() {
098: public void reset(JAXBElement o) {
099: throw new UnsupportedOperationException();
100: }
101:
102: public void serializeBody(JAXBElement e,
103: XMLSerializer target, Object outerPeer)
104: throws SAXException, IOException,
105: XMLStreamException {
106: Class scope = e.getScope();
107: if (e.isGlobalScope())
108: scope = null;
109: QName n = e.getName();
110: ElementBeanInfoImpl bi = grammar.getElement(scope, n);
111: if (bi == null) {
112: // infer what to do from the type
113: JaxBeanInfo tbi;
114: try {
115: tbi = grammar.getBeanInfo(e.getDeclaredType(),
116: true);
117: } catch (JAXBException x) {
118: // if e.getDeclaredType() isn't known to this JAXBContext
119: target.reportError(null, x);
120: return;
121: }
122: Object value = e.getValue();
123: target.startElement(n.getNamespaceURI(), n
124: .getLocalPart(), n.getPrefix(), null);
125: if (value == null) {
126: target.writeXsiNilTrue();
127: } else {
128: target.childAsXsiType(value, "value", tbi);
129: }
130: target.endElement();
131: } else {
132: try {
133: bi.property.serializeBody(e, target, e);
134: } catch (AccessorException x) {
135: target.reportError(null, x);
136: }
137: }
138: }
139:
140: public void serializeURIs(JAXBElement o,
141: XMLSerializer target) {
142: }
143:
144: public boolean hasSerializeURIAction() {
145: return false;
146: }
147:
148: public String getIdValue(JAXBElement o) {
149: return null;
150: }
151:
152: public PropertyKind getKind() {
153: return PropertyKind.ELEMENT;
154: }
155:
156: public void buildChildElementUnmarshallers(
157: UnmarshallerChain chain,
158: QNameMap<ChildLoader> handlers) {
159: }
160:
161: public Accessor getElementPropertyAccessor(String nsUri,
162: String localName) {
163: throw new UnsupportedOperationException();
164: }
165:
166: public void wrapUp() {
167: }
168: };
169: }
170:
171: /**
172: * Use the previous {@link UnmarshallingContext.State}'s target to store
173: * {@link JAXBElement} object to be unmarshalled. This allows the property {@link Loader}
174: * to correctly find the parent object.
175: * This is a hack.
176: */
177: private final class IntercepterLoader extends Loader implements
178: Intercepter {
179: private final Loader core;
180:
181: public IntercepterLoader(Loader core) {
182: this .core = core;
183: }
184:
185: public final void startElement(
186: UnmarshallingContext.State state, TagName ea)
187: throws SAXException {
188: state.loader = core;
189: state.intercepter = this ;
190:
191: // TODO: make sure there aren't too many duplicate of this code
192: // create the object to unmarshal
193: Object child;
194: UnmarshallingContext context = state.getContext();
195:
196: // let's see if we can reuse the existing peer object
197: child = context.getOuterPeer();
198:
199: if (child != null && jaxbType != child.getClass())
200: child = null; // unexpected type.
201:
202: if (child != null)
203: reset((JAXBElement) child, context);
204:
205: if (child == null)
206: child = context
207: .createInstance(ElementBeanInfoImpl.this );
208:
209: fireBeforeUnmarshal(ElementBeanInfoImpl.this , child, state);
210:
211: context.recordOuterPeer(child);
212: UnmarshallingContext.State p = state.prev;
213: p.backup = p.target;
214: p.target = child;
215:
216: core.startElement(state, ea);
217: }
218:
219: public Object intercept(UnmarshallingContext.State state,
220: Object o) throws SAXException {
221: JAXBElement e = (JAXBElement) state.target;
222: state.target = state.backup;
223: state.backup = null;
224:
225: if (o != null)
226: // if the value is a leaf type, it's often already set to the element
227: // through Accessor.
228: e.setValue(o);
229:
230: fireAfterUnmarshal(ElementBeanInfoImpl.this , e, state);
231:
232: return e;
233: }
234: }
235:
236: public String getElementNamespaceURI(JAXBElement e) {
237: return e.getName().getNamespaceURI();
238: }
239:
240: public String getElementLocalName(JAXBElement e) {
241: return e.getName().getLocalPart();
242: }
243:
244: public Loader getLoader(JAXBContextImpl context,
245: boolean typeSubstitutionCapable) {
246: if (loader == null) {
247: // this has to be done lazily to avoid cyclic reference issue
248: UnmarshallerChain c = new UnmarshallerChain(context);
249: QNameMap<ChildLoader> result = new QNameMap<ChildLoader>();
250: property.buildChildElementUnmarshallers(c, result);
251: if (result.size() == 1)
252: // for ElementBeanInfoImpl created from RuntimeElementInfo
253: this .loader = new IntercepterLoader(result.getOne()
254: .getValue().loader);
255: else
256: // for special ElementBeanInfoImpl only used for marshalling
257: this .loader = Discarder.INSTANCE;
258: }
259: return loader;
260: }
261:
262: public final JAXBElement createInstance(UnmarshallingContext context) {
263: return createInstanceFromValue(null);
264: }
265:
266: public final JAXBElement createInstanceFromValue(Object o) {
267: return new JAXBElement(tagName, expectedType, scope, o);
268: }
269:
270: public boolean reset(JAXBElement e, UnmarshallingContext context) {
271: e.setValue(null);
272: return true;
273: }
274:
275: public String getId(JAXBElement e, XMLSerializer target) {
276: // TODO: is this OK? Should we be returning the ID value of the type property?
277: /*
278: There's one case where we JAXBElement needs to be designated as ID,
279: and that is when there's a global element whose type is ID.
280: */
281: Object o = e.getValue();
282: if (o instanceof String)
283: return (String) o;
284: else
285: return null;
286: }
287:
288: public void serializeBody(JAXBElement element, XMLSerializer target)
289: throws SAXException, IOException, XMLStreamException {
290: try {
291: property.serializeBody(element, target, null);
292: } catch (AccessorException x) {
293: target.reportError(null, x);
294: }
295: }
296:
297: public void serializeRoot(JAXBElement e, XMLSerializer target)
298: throws SAXException, IOException, XMLStreamException {
299: serializeBody(e, target);
300: }
301:
302: public void serializeAttributes(JAXBElement e, XMLSerializer target) {
303: // noop
304: }
305:
306: public void serializeURIs(JAXBElement e, XMLSerializer target) {
307: // noop
308: }
309:
310: public final Transducer<JAXBElement> getTransducer() {
311: return null;
312: }
313:
314: public void wrapUp() {
315: super .wrapUp();
316: property.wrapUp();
317: }
318:
319: public void link(JAXBContextImpl grammar) {
320: super .link(grammar);
321: getLoader(grammar, true); // make sure to build them, if we hadn't done so
322: }
323: }
|