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.lang.reflect.InvocationTargetException;
041:
042: import javax.xml.bind.JAXBElement;
043: import javax.xml.bind.JAXBException;
044: import javax.xml.bind.annotation.DomHandler;
045: import javax.xml.stream.XMLStreamException;
046:
047: import com.sun.xml.bind.api.AccessorException;
048: import com.sun.xml.bind.v2.ClassFactory;
049: import com.sun.xml.bind.v2.model.core.PropertyKind;
050: import com.sun.xml.bind.v2.model.core.WildcardMode;
051: import com.sun.xml.bind.v2.model.runtime.RuntimeElement;
052: import com.sun.xml.bind.v2.model.runtime.RuntimeReferencePropertyInfo;
053: import com.sun.xml.bind.v2.runtime.ElementBeanInfoImpl;
054: import com.sun.xml.bind.v2.runtime.JAXBContextImpl;
055: import com.sun.xml.bind.v2.runtime.JaxBeanInfo;
056: import com.sun.xml.bind.v2.runtime.XMLSerializer;
057: import com.sun.xml.bind.v2.runtime.reflect.Accessor;
058: import com.sun.xml.bind.v2.runtime.unmarshaller.ChildLoader;
059: import com.sun.xml.bind.v2.runtime.unmarshaller.WildcardLoader;
060: import com.sun.xml.bind.v2.util.QNameMap;
061:
062: import org.xml.sax.SAXException;
063:
064: /**
065: * @author Kohsuke Kawaguchi
066: */
067: final class SingleReferenceNodeProperty<BeanT, ValueT> extends
068: PropertyImpl<BeanT> {
069:
070: private final Accessor<BeanT, ValueT> acc;
071:
072: private final QNameMap<JaxBeanInfo> expectedElements = new QNameMap<JaxBeanInfo>();
073:
074: private final DomHandler domHandler;
075: private final WildcardMode wcMode;
076:
077: public SingleReferenceNodeProperty(JAXBContextImpl context,
078: RuntimeReferencePropertyInfo prop) {
079: super (context, prop);
080: acc = prop.getAccessor().optimize(context);
081:
082: for (RuntimeElement e : prop.getElements()) {
083: expectedElements.put(e.getElementName(), context
084: .getOrCreate(e));
085: }
086:
087: if (prop.getWildcard() != null) {
088: domHandler = (DomHandler) ClassFactory.create(prop
089: .getDOMHandler());
090: wcMode = prop.getWildcard();
091: } else {
092: domHandler = null;
093: wcMode = null;
094: }
095: }
096:
097: public void reset(BeanT bean) throws AccessorException {
098: acc.set(bean, null);
099: }
100:
101: public String getIdValue(BeanT beanT) {
102: return null;
103: }
104:
105: public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer)
106: throws SAXException, AccessorException, IOException,
107: XMLStreamException {
108: ValueT v = acc.get(o);
109: if (v != null) {
110: try {
111: JaxBeanInfo bi = w.grammar.getBeanInfo(v, true);
112: if (bi.jaxbType == Object.class && domHandler != null)
113: // even if 'v' is a DOM node, it always derive from Object,
114: // so the getBeanInfo returns BeanInfo for Object
115: w.writeDom(v, domHandler, o, fieldName);
116: else
117: bi.serializeRoot(v, w);
118: } catch (JAXBException e) {
119: w.reportError(fieldName, e);
120: // recover by ignoring this property
121: }
122: }
123: }
124:
125: public void buildChildElementUnmarshallers(UnmarshallerChain chain,
126: QNameMap<ChildLoader> handlers) {
127: for (QNameMap.Entry<JaxBeanInfo> n : expectedElements
128: .entrySet())
129: handlers.put(n.nsUri, n.localName, new ChildLoader(n
130: .getValue().getLoader(chain.context, true), acc));
131:
132: if (domHandler != null)
133: handlers.put(CATCH_ALL, new ChildLoader(new WildcardLoader(
134: domHandler, wcMode), acc));
135:
136: }
137:
138: public PropertyKind getKind() {
139: return PropertyKind.REFERENCE;
140: }
141:
142: @Override
143: public Accessor getElementPropertyAccessor(String nsUri,
144: String localName) {
145: JaxBeanInfo bi = expectedElements.get(nsUri, localName);
146: if (bi != null) {
147: if (bi instanceof ElementBeanInfoImpl) {
148: final ElementBeanInfoImpl ebi = (ElementBeanInfoImpl) bi;
149: // a JAXBElement. We need to handle JAXBElement for JAX-WS
150: return new Accessor<BeanT, Object>(ebi.expectedType) {
151: public Object get(BeanT bean)
152: throws AccessorException {
153: ValueT r = acc.get(bean);
154: if (r instanceof JAXBElement) {
155: return ((JAXBElement) r).getValue();
156: } else
157: // this is sloppy programming, but hey...
158: return r;
159: }
160:
161: public void set(BeanT bean, Object value)
162: throws AccessorException {
163: if (value != null) {
164: try {
165: value = ebi
166: .createInstanceFromValue(value);
167: } catch (IllegalAccessException e) {
168: throw new AccessorException(e);
169: } catch (InvocationTargetException e) {
170: throw new AccessorException(e);
171: } catch (InstantiationException e) {
172: throw new AccessorException(e);
173: }
174: }
175: acc.set(bean, (ValueT) value);
176: }
177: };
178: } else {
179: // a custom element type, like @XmlRootElement class Foo { ... }
180: return acc;
181: }
182: } else
183: return null;
184: }
185: }
|