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