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 import java.io.IOException; 29 30 import javax.xml.stream.XMLStreamException; 31 32 import com.sun.xml.internal.bind.api.AccessorException; 33 import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 34 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo; 35 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef; 36 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 37 import com.sun.xml.internal.bind.v2.runtime.Name; 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.DefaultValueLoaderDecorator; 43 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyLoader; 44 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyXsiLoader; 45 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; 47 import com.sun.xml.internal.bind.v2.util.QNameMap; 48 49 import org.xml.sax.SAXException; 50 51 /** 52 * {@link Property} that contains a leaf value. 53 * 54 * @author Kohsuke Kawaguchi (kk@kohsuke.org) 55 */ 56 final class SingleElementLeafProperty<BeanT> extends PropertyImpl<BeanT> { 57 58 private final Name tagName; 59 private final boolean nillable; 60 private final Accessor acc; 61 private final String defaultValue; 62 private final TransducedAccessor<BeanT> xacc; 63 private final boolean improvedXsiTypeHandling; 64 65 public SingleElementLeafProperty(JAXBContextImpl context, RuntimeElementPropertyInfo prop) { 66 super(context, prop); 67 RuntimeTypeRef ref = prop.getTypes().get(0); 68 tagName = context.nameBuilder.createElementName(ref.getTagName()); 69 assert tagName != null; 70 nillable = ref.isNillable(); 71 defaultValue = ref.getDefaultValue(); 72 this.acc = prop.getAccessor().optimize(context); 73 74 xacc = TransducedAccessor.get(context, ref); 75 assert xacc != null; 76 77 improvedXsiTypeHandling = context.improvedXsiTypeHandling; 78 } 79 80 public void reset(BeanT o) throws AccessorException { 81 acc.set(o, null); 82 } 83 84 public String getIdValue(BeanT bean) throws AccessorException, SAXException { 85 return xacc.print(bean).toString(); 86 } 87 88 @Override 89 public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { 90 boolean hasValue = xacc.hasValue(o); 91 92 Object obj = null; 93 94 try { 95 obj = acc.getUnadapted(o); 96 } catch (AccessorException ae) { 97 ; // noop 98 } 99 100 Class valueType = acc.getValueType(); 101 102 // check for different type than expected. If found, add xsi:type declaration 103 if (improvedXsiTypeHandling && !acc.isAdapted() && 104 (obj!=null) && ( !obj.getClass().equals(valueType))&& 105 (!valueType.isPrimitive() && acc.isValueTypeAbstractable() )) { 106 107 w.startElement(tagName, outerPeer); 108 w.childAsXsiType(obj, fieldName, w.grammar.getBeanInfo(valueType), nillable); 109 w.endElement(); 110 111 } else { // current type is expected 112 113 if (hasValue) { 114 xacc.writeLeafElement(w, tagName, o, fieldName); 115 } else if (nillable) { 116 w.startElement(tagName, null); 117 w.writeXsiNilTrue(); 118 w.endElement(); 119 } 120 } 121 } 122 123 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) { 124 Loader l = new LeafPropertyLoader(xacc); 125 if (defaultValue != null) 126 l = new DefaultValueLoaderDecorator(l, defaultValue); 127 if (nillable || chain.context.allNillable) 128 l = new XsiNilLoader.Single(l, acc); 129 130 // LeafPropertyXsiLoader doesn't work well with nillable elements 131 if (improvedXsiTypeHandling && !nillable) 132 l = new LeafPropertyXsiLoader(l, xacc, acc); 133 134 handlers.put(tagName, new ChildLoader(l, null)); 135 } 136 137 138 public PropertyKind getKind() { 139 return PropertyKind.ELEMENT; 140 } 141 142 @Override 143 public Accessor getElementPropertyAccessor(String nsUri, String localName) { 144 if (tagName.equals(nsUri, localName)) 145 return acc; 146 else 147 return null; 148 } 149 } | 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 import java.io.IOException; 29 import java.lang.reflect.Modifier; 30 31 import javax.xml.bind.JAXBElement; 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.ID; 36 import com.sun.xml.internal.bind.v2.model.core.PropertyKind; 37 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeElementPropertyInfo; 38 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeTypeRef; 39 import com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl; 40 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl; 41 import com.sun.xml.internal.bind.v2.runtime.Name; 42 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer; 43 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor; 44 import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor; 45 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader; 46 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.DefaultValueLoaderDecorator; 47 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyLoader; 48 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.LeafPropertyXsiLoader; 49 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader; 50 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader; 51 import com.sun.xml.internal.bind.v2.util.QNameMap; 52 53 import org.xml.sax.SAXException; 54 55 /** 56 * {@link Property} that contains a leaf value. 57 * 58 * @author Kohsuke Kawaguchi (kk@kohsuke.org) 59 */ 60 final class SingleElementLeafProperty<BeanT> extends PropertyImpl<BeanT> { 61 62 private final Name tagName; 63 private final boolean nillable; 64 private final Accessor acc; 65 private final String defaultValue; 66 private final TransducedAccessor<BeanT> xacc; 67 private final boolean improvedXsiTypeHandling; 68 private final boolean idRef; 69 70 public SingleElementLeafProperty(JAXBContextImpl context, RuntimeElementPropertyInfo prop) { 71 super(context, prop); 72 RuntimeTypeRef ref = prop.getTypes().get(0); 73 tagName = context.nameBuilder.createElementName(ref.getTagName()); 74 assert tagName != null; 75 nillable = ref.isNillable(); 76 defaultValue = ref.getDefaultValue(); 77 this.acc = prop.getAccessor().optimize(context); 78 79 xacc = TransducedAccessor.get(context, ref); 80 assert xacc != null; 81 82 improvedXsiTypeHandling = context.improvedXsiTypeHandling; 83 idRef = ref.getSource().id() == ID.IDREF; 84 } 85 86 public void reset(BeanT o) throws AccessorException { 87 acc.set(o, null); 88 } 89 90 public String getIdValue(BeanT bean) throws AccessorException, SAXException { 91 return xacc.print(bean).toString(); 92 } 93 94 @Override 95 public void serializeBody(BeanT o, XMLSerializer w, Object outerPeer) throws SAXException, AccessorException, IOException, XMLStreamException { 96 boolean hasValue = xacc.hasValue(o); 97 98 Object obj = null; 99 100 try { 101 obj = acc.getUnadapted(o); 102 } catch (AccessorException ae) { 103 ; // noop 104 } 105 106 Class valueType = acc.getValueType(); 107 108 // check for different type than expected. If found, add xsi:type declaration 109 if (xsiTypeNeeded(o, w, obj, valueType)) { 110 w.startElement(tagName, outerPeer); 111 w.childAsXsiType(obj, fieldName, w.grammar.getBeanInfo(valueType), false); 112 w.endElement(); 113 } else { // current type is expected 114 if (hasValue) { 115 xacc.writeLeafElement(w, tagName, o, fieldName); 116 } else if (nillable) { 117 w.startElement(tagName, null); 118 w.writeXsiNilTrue(); 119 w.endElement(); 120 } 121 } 122 } 123 124 /** 125 * Checks if xsi type needed to be specified 126 */ 127 private boolean xsiTypeNeeded(BeanT bean, XMLSerializer w, Object value, Class valueTypeClass) { 128 if (!improvedXsiTypeHandling) // improved xsi type set 129 return false; 130 if (acc.isAdapted()) // accessor is not adapted 131 return false; 132 if (value == null) // value is not null 133 return false; 134 if (value.getClass().equals(valueTypeClass)) // value represented by different class 135 return false; 136 if (idRef) // IDREF 137 return false; 138 if (valueTypeClass.isPrimitive()) // is not primitive 139 return false; 140 return acc.isValueTypeAbstractable() || isNillableAbstract(bean, w.grammar, value, valueTypeClass); 141 } 142 143 /** 144 * Checks if element is nillable and represented by abstract class. 145 */ 146 private boolean isNillableAbstract(BeanT bean, JAXBContextImpl context, Object value, Class valueTypeClass) { 147 if (!nillable) // check if element is nillable 148 return false; 149 if (valueTypeClass != Object.class) // required type wasn't recognized 150 return false; 151 if (bean.getClass() != JAXBElement.class) // is JAXBElement 152 return false; 153 JAXBElement jaxbElement = (JAXBElement) bean; 154 Class valueClass = value.getClass(); 155 Class declaredTypeClass = jaxbElement.getDeclaredType(); 156 if (declaredTypeClass.equals(valueClass)) // JAXBElement<class> is different from unadapted class) 157 return false; 158 if (!declaredTypeClass.isAssignableFrom(valueClass)) // and is subclass from it 159 return false; 160 if (!Modifier.isAbstract(declaredTypeClass.getModifiers())) // declared class is abstract 161 return false; 162 return acc.isAbstractable(declaredTypeClass); // and is not builtin type 163 } 164 165 public void buildChildElementUnmarshallers(UnmarshallerChain chain, QNameMap<ChildLoader> handlers) { 166 Loader l = new LeafPropertyLoader(xacc); 167 if (defaultValue != null) 168 l = new DefaultValueLoaderDecorator(l, defaultValue); 169 if (nillable || chain.context.allNillable) 170 l = new XsiNilLoader.Single(l, acc); 171 172 // LeafPropertyXsiLoader doesn't work well with nillable elements 173 if (improvedXsiTypeHandling) 174 l = new LeafPropertyXsiLoader(l, xacc, acc); 175 176 handlers.put(tagName, new ChildLoader(l, null)); 177 } 178 179 180 public PropertyKind getKind() { 181 return PropertyKind.ELEMENT; 182 } 183 184 @Override 185 public Accessor getElementPropertyAccessor(String nsUri, String localName) { 186 if (tagName.equals(nsUri, localName)) 187 return acc; 188 else 189 return null; 190 } 191 } |