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.model.impl;
  27 
  28 import javax.xml.bind.annotation.XmlAttribute;
  29 import javax.xml.bind.annotation.XmlSchema;
  30 import javax.xml.namespace.QName;
  31 
  32 import com.sun.xml.internal.bind.api.impl.NameConverter;
  33 import com.sun.xml.internal.bind.v2.model.core.AttributePropertyInfo;
  34 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
  35 
  36 /**
  37  * @author Kohsuke Kawaguchi
  38  */
  39 class AttributePropertyInfoImpl<TypeT,ClassDeclT,FieldT,MethodT>
  40     extends SingleTypePropertyInfoImpl<TypeT,ClassDeclT,FieldT,MethodT>
  41     implements AttributePropertyInfo<TypeT,ClassDeclT> {
  42 
  43     private final QName xmlName;
  44 
  45     private final boolean isRequired;
  46 
  47     AttributePropertyInfoImpl(ClassInfoImpl<TypeT,ClassDeclT,FieldT,MethodT> parent, PropertySeed<TypeT,ClassDeclT,FieldT,MethodT> seed ) {
  48         super(parent,seed);
  49         XmlAttribute att = seed.readAnnotation(XmlAttribute.class);
  50         assert att!=null;
  51 
  52         if(att.required())
  53             isRequired = true;
  54         else isRequired = nav().isPrimitive(getIndividualType());
  55 
  56         this.xmlName = calcXmlName(att);
  57     }
  58 
  59     private QName calcXmlName(XmlAttribute att) {
  60         String uri;
  61         String local;
  62 
  63         uri = att.namespace();
  64         local = att.name();
  65 
  66         // compute the default
  67         if(local.equals("##default"))
  68             local = NameConverter.standard.toVariableName(getName());
  69         if(uri.equals("##default")) {
  70             XmlSchema xs = reader().getPackageAnnotation( XmlSchema.class, parent.getClazz(), this );
  71             // JAX-RPC doesn't want the default namespace URI swapping to take effect to
  72             // local "unqualified" elements. UGLY.
  73             if(xs!=null) {
  74                 switch(xs.attributeFormDefault()) {
  75                 case QUALIFIED:
  76                     uri = parent.getTypeName().getNamespaceURI();
  77                     if(uri.length()==0)
  78                         uri = parent.builder.defaultNsUri;
  79                     break;
  80                 case UNQUALIFIED:
  81                 case UNSET:
  82                     uri = "";
  83                 }
  84             } else
  85                 uri = "";
  86         }
  87 
  88         return new QName(uri.intern(),local.intern());
  89     }
  90 
  91     public boolean isRequired() {
  92         return isRequired;
  93     }
  94 
  95     public final QName getXmlName() {
  96         return xmlName;
  97     }
  98 
  99     public final PropertyKind kind() {
 100         return PropertyKind.ATTRIBUTE;
 101     }
 102 }