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 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.util.QNameMap;
  34 import com.sun.xml.internal.bind.v2.model.core.AttributePropertyInfo;
  35 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
  36 import com.sun.xml.internal.bind.v2.model.runtime.RuntimeAttributePropertyInfo;
  37 import com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl;
  38 import com.sun.xml.internal.bind.v2.runtime.Name;
  39 import com.sun.xml.internal.bind.v2.runtime.XMLSerializer;
  40 import com.sun.xml.internal.bind.v2.runtime.JaxBeanInfo;
  41 import com.sun.xml.internal.bind.v2.runtime.unmarshaller.ChildLoader;
  42 import com.sun.xml.internal.bind.v2.runtime.reflect.Accessor;
  43 import com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor;
  44 
  45 import org.xml.sax.SAXException;
  46 
  47 /**
  48  * {@link Property} implementation for {@link AttributePropertyInfo}.
  49  *
  50  * <p>
  51  * This one works for both leaves and nodes, scalars and arrays.
  52  *
  53  * <p>
  54  * Implements {@link Comparable} so that it can be sorted lexicographically.
  55  *
  56  * @author Kohsuke Kawaguchi (kk@kohsuke.org)
  57  */
  58 public final class AttributeProperty<BeanT> extends PropertyImpl<BeanT>
  59     implements Comparable<AttributeProperty> {
  60 
  61     /**
  62      * Attribute name.
  63      */
  64     public final Name attName;
  65 
  66     /**
  67      * Heart of the conversion logic.
  68      */
  69     public final TransducedAccessor<BeanT> xacc;
  70 
  71     private final Accessor acc;
  72 
  73     public AttributeProperty(JAXBContextImpl context, RuntimeAttributePropertyInfo prop) {
  74         super(context,prop);
  75         this.attName = context.nameBuilder.createAttributeName(prop.getXmlName());
  76         this.xacc = TransducedAccessor.get(context,prop);
  77         this.acc = prop.getAccessor();   // we only use this for binder, so don't waste memory by optimizing
  78     }
  79 
  80     /**
  81      * Marshals one attribute.
  82      *
  83      * @see JaxBeanInfo#serializeAttributes(Object, XMLSerializer)
  84      */
  85     public void serializeAttributes(BeanT o, XMLSerializer w) throws SAXException, AccessorException, IOException, XMLStreamException {
  86         CharSequence value = xacc.print(o);
  87         if(value!=null)
  88             w.attribute(attName,value.toString());
  89     }
  90 
  91     public void serializeURIs(BeanT o, XMLSerializer w) throws AccessorException, SAXException {
  92         xacc.declareNamespace(o,w);
  93     }
  94 
  95     public boolean hasSerializeURIAction() {
  96         return xacc.useNamespace();
  97     }
  98 
  99     public void buildChildElementUnmarshallers(UnmarshallerChain chainElem, QNameMap<ChildLoader> handlers) {
 100         throw new IllegalStateException();
 101     }
 102 
 103 
 104     public PropertyKind getKind() {
 105         return PropertyKind.ATTRIBUTE;
 106     }
 107 
 108     public void reset(BeanT o) throws AccessorException {
 109         acc.set(o,null);
 110     }
 111 
 112     public String getIdValue(BeanT bean) throws AccessorException, SAXException {
 113         return xacc.print(bean).toString();
 114     }
 115 
 116     public int compareTo(AttributeProperty that) {
 117         return this.attName.compareTo(that.attName);
 118     }
 119 }