1 /*
   2  * Copyright (c) 1997, 2011, 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.tools.internal.xjc.model;
  27 
  28 import javax.xml.namespace.QName;
  29 
  30 import com.sun.tools.internal.xjc.model.nav.NClass;
  31 import com.sun.tools.internal.xjc.model.nav.NType;
  32 import com.sun.xml.internal.bind.v2.model.core.AttributePropertyInfo;
  33 import com.sun.xml.internal.bind.v2.model.core.PropertyKind;
  34 import com.sun.xml.internal.xsom.XSComponent;
  35 import com.sun.istack.internal.Nullable;
  36 
  37 import org.xml.sax.Locator;
  38 
  39 /**
  40  * {@link AttributePropertyInfo} for the compiler.
  41  *
  42  * @author Kohsuke Kawaguchi
  43  */
  44 public final class CAttributePropertyInfo extends CSingleTypePropertyInfo implements AttributePropertyInfo<NType,NClass> {
  45 
  46     private final QName attName;
  47     private final boolean isRequired;
  48 
  49     /**
  50      * @param type
  51      *      Represents the bound type of this attribute.
  52      * @param typeName
  53      *      XML Schema type name of this attribute. Optional for other schema languages.
  54      */
  55     public CAttributePropertyInfo(String name, XSComponent source, CCustomizations customizations,
  56                                   Locator locator, QName attName, TypeUse type, @Nullable QName typeName,
  57                                   boolean required ) {
  58         super(name, type, typeName, source, customizations, locator);
  59         isRequired = required;
  60         this.attName = attName;
  61     }
  62 
  63     public boolean isRequired() {
  64         return isRequired;
  65     }
  66 
  67     public QName getXmlName() {
  68         return attName;
  69     }
  70 
  71     /**
  72      * An optional attribute can never be unboxable,
  73      * for we need null to represent the absence.
  74      */
  75     public boolean isUnboxable() {
  76         if(!isRequired) return false;
  77         return super.isUnboxable();
  78     }
  79 
  80     @Override
  81     public boolean isOptionalPrimitive() {
  82         return !isRequired && super.isUnboxable();
  83     }
  84 
  85     public <V> V accept(CPropertyVisitor<V> visitor) {
  86         return visitor.onAttribute(this);
  87     }
  88 
  89     public final PropertyKind kind() {
  90         return  PropertyKind.ATTRIBUTE;
  91     }
  92 }