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.tools.internal.xjc.model;
  27 
  28 import java.util.Collection;
  29 
  30 import javax.activation.MimeType;
  31 import javax.xml.namespace.QName;
  32 
  33 import com.sun.codemodel.internal.JClass;
  34 import com.sun.codemodel.internal.JExpression;
  35 import com.sun.tools.internal.xjc.model.nav.NClass;
  36 import com.sun.tools.internal.xjc.model.nav.NType;
  37 import com.sun.tools.internal.xjc.outline.Aspect;
  38 import com.sun.tools.internal.xjc.outline.Outline;
  39 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
  40 import com.sun.xml.internal.bind.v2.model.core.EnumLeafInfo;
  41 import com.sun.xml.internal.bind.v2.model.core.ID;
  42 import com.sun.xml.internal.bind.v2.model.core.NonElement;
  43 import com.sun.xml.internal.bind.v2.model.core.Element;
  44 import com.sun.xml.internal.bind.v2.runtime.Location;
  45 import com.sun.xml.internal.xsom.XSComponent;
  46 import com.sun.xml.internal.xsom.XmlString;
  47 
  48 import org.xml.sax.Locator;
  49 
  50 /**
  51  * Transducer that converts a string into an "enumeration class."
  52  *
  53  * The structure of the generated class needs to precisely
  54  * follow the JAXB spec.
  55  *
  56  * @author
  57  *    <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
  58  */
  59 public final class CEnumLeafInfo implements EnumLeafInfo<NType,NClass>, NClass, CNonElement
  60 {
  61     /**
  62      * The {@link Model} object to which this bean belongs.
  63      */
  64     public final Model model;
  65 
  66     /**
  67      * The parent into which the enum class should be generated.
  68      */
  69     public final CClassInfoParent parent;
  70 
  71     /**
  72      * Short name of the generated type-safe enum.
  73      */
  74     public final String shortName;
  75 
  76     private final QName typeName;
  77 
  78     private final XSComponent source;
  79 
  80     /**
  81      * Represents the underlying type of this enumeration
  82      * and its conversion.
  83      *
  84      * <p>
  85      * To parse XML into a constant, we use the base type
  86      * to do lexical -> value, then use a map to pick up the right one.
  87      *
  88      * <p>
  89      * Hence this also represents the type of the Java value.
  90      * For example, if this is an enumeration of xs:int,
  91      * then this field will be Java int.
  92      */
  93     public final CNonElement base;
  94 
  95 
  96     /**
  97      * List of enum members.
  98      */
  99     public final Collection<CEnumConstant> members;
 100 
 101     private final CCustomizations customizations;
 102 
 103     /**
 104      * @see #getLocator()
 105      */
 106     private final Locator sourceLocator;
 107 
 108     public String javadoc;
 109 
 110     public CEnumLeafInfo(Model model,
 111                          QName typeName,
 112                          CClassInfoParent container,
 113                          String shortName,
 114                          CNonElement base,
 115                          Collection<CEnumConstant> _members,
 116                          XSComponent source,
 117                          CCustomizations customizations,
 118                          Locator _sourceLocator) {
 119         this.model = model;
 120         this.parent = container;
 121         this.shortName = model.allocator.assignClassName(parent,shortName);
 122         this.base = base;
 123         this.members = _members;
 124         this.source = source;
 125         if(customizations==null)
 126             customizations = CCustomizations.EMPTY;
 127         this.customizations = customizations;
 128         this.sourceLocator = _sourceLocator;
 129         this.typeName = typeName;
 130 
 131         for( CEnumConstant mem : members )
 132             mem.setParent(this);
 133 
 134         model.add(this);
 135 
 136         // TODO: can we take advantage of the fact that enum can be XmlRootElement?
 137     }
 138 
 139     /**
 140      * Source line information that points to the place
 141      * where this type-safe enum is defined.
 142      * Used to report error messages.
 143      */
 144     public Locator getLocator() {
 145         return sourceLocator;
 146     }
 147 
 148     public QName getTypeName() {
 149         return typeName;
 150     }
 151 
 152     public NType getType() {
 153         return this;
 154     }
 155 
 156     /**
 157      * @deprecated
 158      *      why are you calling the method whose return value is known?
 159      */
 160     public boolean canBeReferencedByIDREF() {
 161         return false;
 162     }
 163 
 164     public boolean isElement() {
 165         return false;
 166     }
 167 
 168     public QName getElementName() {
 169         return null;
 170     }
 171 
 172     public Element<NType,NClass> asElement() {
 173         return null;
 174     }
 175 
 176     public NClass getClazz() {
 177         return this;
 178     }
 179 
 180     public XSComponent getSchemaComponent() {
 181         return source;
 182     }
 183 
 184     public JClass toType(Outline o, Aspect aspect) {
 185         return o.getEnum(this).clazz;
 186     }
 187 
 188     public boolean isAbstract() {
 189         return false;
 190     }
 191 
 192     public boolean isBoxedType() {
 193         return false;
 194     }
 195 
 196     public String fullName() {
 197         return parent.fullName()+'.'+shortName;
 198     }
 199 
 200     public boolean isPrimitive() {
 201         return false;
 202     }
 203 
 204     public boolean isSimpleType() {
 205         return true;
 206     }
 207 
 208 
 209     /**
 210      * The spec says the value field in the enum class will be generated
 211      * only under certain circumstances.
 212      *
 213      * @return
 214      *      true if the generated enum class should have the value field.
 215      */
 216     public boolean needsValueField() {
 217         for (CEnumConstant cec : members) {
 218             if(!cec.getName().equals(cec.getLexicalValue()))
 219                 return true;
 220         }
 221         return false;
 222     }
 223 
 224     public JExpression createConstant(Outline outline, XmlString literal) {
 225         // correctly identifying which constant it maps to is hard, so
 226         // here I'm cheating
 227         JClass type = toType(outline,Aspect.EXPOSED);
 228         for (CEnumConstant mem : members) {
 229             if(mem.getLexicalValue().equals(literal.value))
 230                 return type.staticRef(mem.getName());
 231         }
 232         return null;
 233     }
 234 
 235     @Deprecated
 236     public boolean isCollection() {
 237         return false;
 238     }
 239 
 240     @Deprecated
 241     public CAdapter getAdapterUse() {
 242         return null;
 243     }
 244 
 245     @Deprecated
 246     public CNonElement getInfo() {
 247         return this;
 248     }
 249 
 250     public ID idUse() {
 251         return ID.NONE;
 252     }
 253 
 254     public MimeType getExpectedMimeType() {
 255         return null;
 256     }
 257 
 258     public Collection<CEnumConstant> getConstants() {
 259         return members;
 260     }
 261 
 262     public NonElement<NType,NClass> getBaseType() {
 263         return base;
 264     }
 265 
 266     public CCustomizations getCustomizations() {
 267         return customizations;
 268     }
 269 
 270     public Locatable getUpstream() {
 271         throw new UnsupportedOperationException();
 272     }
 273 
 274     public Location getLocation() {
 275         throw new UnsupportedOperationException();
 276     }
 277 }