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.bind.annotation.XmlValue;
031: import javax.xml.stream.XMLStreamException;
032:
033: import com.sun.xml.internal.bind.api.AccessorException;
034: import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
035: import com.sun.xml.internal.bind.v2.model.runtime.RuntimeValuePropertyInfo;
036: import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
037: import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
038: import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
039: import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
040: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
041: import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValuePropertyLoader;
042: import com.sun.xml.internal.bind.v2.util.QNameMap;
043:
044: import org.xml.sax.SAXException;
045:
046: /**
047: * {@link Property} implementation for {@link XmlValue} properties.
048: *
049: * <p>
050: * This one works for both leaves and nodes, scalars and arrays.
051: *
052: * @author Bhakti Mehta (bhakti.mehta@sun.com)
053: */
054: public final class ValueProperty<BeanT> extends PropertyImpl<BeanT> {
055:
056: /**
057: * Heart of the conversion logic.
058: */
059: private final TransducedAccessor<BeanT> xacc;
060: private final Accessor<BeanT, ?> acc;
061:
062: public ValueProperty(JAXBContextImpl grammar,
063: RuntimeValuePropertyInfo prop) {
064: super (grammar, prop);
065: xacc = TransducedAccessor.get(prop);
066: acc = prop.getAccessor(); // we only use this for binder, so don't waste memory by optimizing
067: }
068:
069: public final void serializeBody(BeanT o, XMLSerializer w,
070: Object outerPeer) throws SAXException, AccessorException,
071: IOException, XMLStreamException {
072: if (xacc.hasValue(o))
073: xacc.writeText(w, o, fieldName);
074: }
075:
076: public void serializeURIs(BeanT o, XMLSerializer w)
077: throws SAXException, AccessorException {
078: xacc.declareNamespace(o, w);
079: }
080:
081: public boolean hasSerializeURIAction() {
082: return xacc.useNamespace();
083: }
084:
085: public void buildChildElementUnmarshallers(
086: UnmarshallerChain chainElem, QNameMap<ChildLoader> handlers) {
087: handlers.put(StructureLoaderBuilder.TEXT_HANDLER,
088: new ChildLoader(new ValuePropertyLoader(xacc), null));
089: }
090:
091: public PropertyKind getKind() {
092: return PropertyKind.VALUE;
093: }
094:
095: public void reset(BeanT o) throws AccessorException {
096: acc.set(o, null);
097: }
098:
099: public String getIdValue(BeanT bean) throws AccessorException,
100: SAXException {
101: return xacc.print(bean).toString();
102: }
103:
104: }
|