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.reader.xmlschema.bindinfo;
  27 
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 
  31 import javax.xml.bind.annotation.XmlAttribute;
  32 import javax.xml.bind.annotation.XmlElement;
  33 import javax.xml.bind.annotation.XmlRootElement;
  34 import javax.xml.bind.annotation.XmlTransient;
  35 import javax.xml.namespace.QName;
  36 
  37 import com.sun.tools.internal.xjc.reader.Const;
  38 import com.sun.tools.internal.xjc.reader.xmlschema.SimpleTypeBuilder;
  39 
  40 /**
  41  * Enumeration customization.
  42  * <p>
  43  * This customization binds a simple type to a type-safe enum class.
  44  * The actual binding process takes place in {@link SimpleTypeBuilder}.
  45  *
  46  * <p>
  47  * This customization is acknowledged by {@link SimpleTypeBuilder}.
  48  *
  49  * @author
  50  *  Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  51  */
  52 @XmlRootElement(name="typesafeEnumClass")
  53 public final class BIEnum extends AbstractDeclarationImpl {
  54 
  55     /**
  56      * If false, it means not to bind to a type-safe enum.
  57      *
  58      * this takes precedence over all the other properties of this class.
  59      */
  60     @XmlAttribute(name="map")
  61     private boolean map = true;
  62 
  63     /** Gets the specified class name, or null if not specified. */
  64     @XmlAttribute(name="name")
  65     public String className = null;
  66 
  67     /**
  68      * @see BIClass#getExistingClassRef()
  69      */
  70     @XmlAttribute(name="ref")
  71     public String ref;
  72 
  73     /**
  74      * Gets the javadoc comment specified in the customization.
  75      * Can be null if none is specified.
  76      */
  77     @XmlElement
  78     public final String javadoc = null;
  79 
  80     public boolean isMapped() {
  81         return map;
  82     }
  83 
  84     /**
  85      * Gets the map that contains XML value->BIEnumMember pairs.
  86      * This table is built from &lt;enumMember> customizations.
  87      *
  88      * Always return non-null.
  89      */
  90     @XmlTransient
  91     public final Map<String,BIEnumMember> members = new HashMap<String,BIEnumMember>();
  92 
  93     public QName getName() { return NAME; }
  94 
  95     public void setParent(BindInfo p) {
  96         super.setParent(p);
  97         for( BIEnumMember mem : members.values() )
  98             mem.setParent(p);
  99 
 100         // if this specifies a reference to external class,
 101         // then it's OK even if noone actually refers this class.
 102         if(ref!=null)
 103             markAsAcknowledged();
 104     }
 105 
 106     /** Name of this declaration. */
 107     public static final QName NAME = new QName(
 108         Const.JAXB_NSURI, "enum" );
 109 
 110     // setter method for JAXB runtime
 111     @XmlElement(name="typesafeEnumMember")
 112     private void setMembers(BIEnumMember2[] mems) {
 113         for (BIEnumMember2 e : mems)
 114             members.put(e.value,e);
 115     }
 116 
 117 
 118 
 119     /**
 120      * {@link BIEnumMember} used inside {@link BIEnum} has additional 'value' attribute.
 121      */
 122     static class BIEnumMember2 extends BIEnumMember {
 123         /**
 124          * The lexical representaion of the constant to which we are attaching.
 125          */
 126         @XmlAttribute(required=true)
 127         String value;
 128     }
 129 }