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.property;
027:
028: import java.io.IOException;
029:
030: import javax.xml.stream.XMLStreamException;
031:
032: import com.sun.xml.internal.bind.api.AccessorException;
033: import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
034: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo;
035: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef;
036: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
037: import com.sun.xml.internal.bind.v2.runtime.Name;
038: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
039: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
040: import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
041: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
042: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DefaultValueLoaderDecorator;
043: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyLoader;
044: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader;
045: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
046: import com.sun.xml.internal.bind.v2.util.QNameMap;
047:
048: import org.xml.sax.SAXException;
049:
050: /**
051: * {@link Property} that contains a leaf value.
052: *
053: * @author Kohsuke Kawaguchi (kk@kohsuke.org)
054: */
055: final class SingleElementLeafProperty<BeanT> extends
056: PropertyImpl<BeanT> {
057:
058: private final Name tagName;
059: private final boolean nillable;
060: private final Accessor acc;
061: private final String defaultValue;
062: private final TransducedAccessor<BeanT> xacc;
063:
064: public SingleElementLeafProperty(JAXBContextImpl context,
065: RuntimeElementPropertyInfo prop) {
066: super (context, prop);
067: RuntimeTypeRef ref = prop.getTypes().get(0);
068: tagName = context.nameBuilder.createElementName(ref
069: .getTagName());
070: assert tagName != null;
071: nillable = ref.isNillable();
072: defaultValue = ref.getDefaultValue();
073: this .acc = prop.getAccessor().optimize();
074:
075: xacc = TransducedAccessor.get(ref);
076: assert xacc != null;
077: }
078:
079: public void reset(BeanT o) throws AccessorException {
080: acc.set(o, null);
081: }
082:
083: public String getIdValue(BeanT bean) throws AccessorException,
084: SAXException {
085: return xacc.print(bean).toString();
086: }
087:
088: public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer)
089: throws SAXException, AccessorException, IOException,
090: XMLStreamException {
091: boolean hasValue = xacc.hasValue(o);
092: if (hasValue) {
093: xacc.writeLeafElement(w, tagName, o, fieldName);
094: } else if (nillable) {
095: w.startElement(tagName, null);
096: w.writeXsiNilTrue();
097: w.endElement();
098: }
099: }
100:
101: public void buildChildElementUnmarshallers(UnmarshallerChain chain,
102: QNameMap<ChildLoader> handlers) {
103: Loader l = new LeafPropertyLoader(xacc);
104: if (defaultValue != null)
105: l = new DefaultValueLoaderDecorator(l, defaultValue);
106: if (nillable)
107: l = new XsiNilLoader.Single(l, acc);
108: handlers.put(tagName, new ChildLoader(l, null));
109: }
110:
111: public PropertyKind getKind() {
112: return PropertyKind.ELEMENT;
113: }
114:
115: @Override
116: public Accessor getElementPropertyAccessor(String nsUri,
117: String localName) {
118: if (tagName.equals(nsUri, localName))
119: return acc;
120: else
121: return null;
122: }
123: }
|