1 /*
   2  * Copyright (c) 1997, 2013, 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 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 }