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;
038:
039: import java.io.IOException;
040:
041: import javax.xml.bind.ValidationEvent;
042: import javax.xml.bind.helpers.ValidationEventImpl;
043: import javax.xml.namespace.QName;
044: import javax.xml.stream.XMLStreamException;
045:
046: import com.sun.xml.bind.api.AccessorException;
047: import com.sun.xml.bind.v2.model.runtime.RuntimeLeafInfo;
048: import com.sun.xml.bind.v2.runtime.unmarshaller.Loader;
049: import com.sun.xml.bind.v2.runtime.unmarshaller.TextLoader;
050: import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext;
051: import com.sun.xml.bind.v2.runtime.unmarshaller.XsiTypeLoader;
052:
053: import org.xml.sax.SAXException;
054:
055: /**
056: * {@link JaxBeanInfo} implementation for immutable leaf classes.
057: *
058: * <p>
059: * Leaf classes are always bound to a text and they are often immutable.
060: * The JAXB spec allows this binding for a few special Java classes plus
061: * type-safe enums.
062: *
063: * <p>
064: * This implementation obtains necessary information from {@link RuntimeLeafInfo}.
065: *
066: * @author Kohsuke Kawaguchi
067: */
068: final class LeafBeanInfoImpl<BeanT> extends JaxBeanInfo<BeanT> {
069:
070: private final Loader loader;
071: private final Loader loaderWithSubst;
072:
073: private final Transducer<BeanT> xducer;
074:
075: /**
076: * Non-null only if the leaf is also an element.
077: */
078: private final Name tagName;
079:
080: public LeafBeanInfoImpl(JAXBContextImpl grammar, RuntimeLeafInfo li) {
081: super (grammar, li, li.getClazz(), li.getTypeNames(), li
082: .isElement(), true, false);
083:
084: xducer = li.getTransducer();
085: loader = new TextLoader(xducer);
086: loaderWithSubst = new XsiTypeLoader(this );
087:
088: if (isElement())
089: tagName = grammar.nameBuilder.createElementName(li
090: .getElementName());
091: else
092: tagName = null;
093: }
094:
095: public QName getTypeName(BeanT instance) {
096: QName tn = xducer.getTypeName(instance);
097: if (tn != null)
098: return tn;
099: // rely on default
100: return super .getTypeName(instance);
101: }
102:
103: public final String getElementNamespaceURI(BeanT _) {
104: return tagName.nsUri;
105: }
106:
107: public final String getElementLocalName(BeanT _) {
108: return tagName.localName;
109: }
110:
111: public BeanT createInstance(UnmarshallingContext context) {
112: throw new UnsupportedOperationException();
113: }
114:
115: public final boolean reset(BeanT bean, UnmarshallingContext context) {
116: return false;
117: }
118:
119: public final String getId(BeanT bean, XMLSerializer target) {
120: return null;
121: }
122:
123: public final void serializeBody(BeanT bean, XMLSerializer w)
124: throws SAXException, IOException, XMLStreamException {
125: // most of the times leaves are printed as leaf element/attribute property,
126: // so this code is only used for example when you have multiple XmlElement on a property
127: // and some of them are leaves. Hence this doesn't need to be super-fast.
128: try {
129: xducer.writeText(w, bean, null);
130: } catch (AccessorException e) {
131: w.reportError(null, e);
132: }
133: }
134:
135: public final void serializeAttributes(BeanT bean,
136: XMLSerializer target) {
137: // noop
138: }
139:
140: public final void serializeRoot(BeanT bean, XMLSerializer target)
141: throws SAXException, IOException, XMLStreamException {
142: if (tagName == null) {
143: target.reportError(new ValidationEventImpl(
144: ValidationEvent.ERROR,
145: Messages.UNABLE_TO_MARSHAL_NON_ELEMENT.format(bean
146: .getClass().getName()), null, null));
147: } else {
148: target.startElement(tagName, bean);
149: target.childAsSoleContent(bean, null);
150: target.endElement();
151: }
152: }
153:
154: public final void serializeURIs(BeanT bean, XMLSerializer target)
155: throws SAXException {
156: // TODO: maybe we should create another LeafBeanInfoImpl class for
157: // context-dependent xducers?
158: if (xducer.useNamespace()) {
159: try {
160: xducer.declareNamespace(bean, target);
161: } catch (AccessorException e) {
162: target.reportError(null, e);
163: }
164: }
165: }
166:
167: public final Loader getLoader(JAXBContextImpl context,
168: boolean typeSubstitutionCapable) {
169: if (typeSubstitutionCapable)
170: return loaderWithSubst;
171: else
172: return loader;
173: }
174:
175: public Transducer<BeanT> getTransducer() {
176: return xducer;
177: }
178: }
|