1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.bind.v2.runtime.property; 27 28 29 import java.io.IOException; 30 31 import javax.xml.bind.annotation.XmlValue; 32 import javax.xml.stream.XMLStreamException; 33 34 import com.sun.xml.internal.bind.api.AccessorException; 35 import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 36 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeValuePropertyInfo; 37 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 38 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 39 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 40 import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor; 41 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; 42 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ValuePropertyLoader; 43 import com.sun.xml.internal.bind.v2.util.QNameMap; 44 45 import org.xml.sax.SAXException; 46 47 /** 48 * {@link Property} implementation for {@link XmlValue} properties. 49 * 50 * <p> 51 * This one works for both leaves and nodes, scalars and arrays. 52 * 53 * @author Bhakti Mehta (bhakti.mehta@sun.com) 54 */ 55 public final class ValueProperty<BeanT> extends PropertyImpl<BeanT> { 56 57 /** 58 * Heart of the conversion logic. 59 */ 60 private final TransducedAccessor<BeanT> xacc; 61 private final Accessor<BeanT,?> acc; 62 63 64 public ValueProperty(JAXBContextImpl context, RuntimeValuePropertyInfo prop) { 65 super(context,prop); 66 xacc = TransducedAccessor.get(context,prop); 67 acc = prop.getAccessor(); // we only use this for binder, so don't waste memory by optimizing 68 } 69 70 public final void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { 71 if(xacc.hasValue(o)) 72 xacc.writeText(w,o,fieldName); 73 } 74 75 public void serializeURIs(BeanT o, XMLSerializer w) throws SAXException, AccessorException { 76 xacc.declareNamespace(o,w); 77 } 78 79 public boolean hasSerializeURIAction() { 80 return xacc.useNamespace(); 81 } 82 83 public void buildChildElementUnmarshallers(UnmarshallerChain chainElem, QNameMap<ChildLoader> handlers) { 84 handlers.put(StructureLoaderBuilder.TEXT_HANDLER, 85 new ChildLoader(new ValuePropertyLoader(xacc),null)); 86 } 87 88 public PropertyKind getKind() { 89 return PropertyKind.VALUE; 90 } 91 92 public void reset(BeanT o) throws AccessorException { 93 acc.set(o,null); 94 } 95 96 public String getIdValue(BeanT bean) throws AccessorException, SAXException { 97 return xacc.print(bean).toString(); 98 } 99 100 }