001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.tools.xjc.reader.xmlschema.bindinfo;
037:
038: import java.util.HashMap;
039: import java.util.Map;
040:
041: import javax.xml.bind.annotation.XmlAttribute;
042: import javax.xml.bind.annotation.XmlElement;
043: import javax.xml.bind.annotation.XmlRootElement;
044: import javax.xml.bind.annotation.XmlTransient;
045: import javax.xml.namespace.QName;
046:
047: import com.sun.tools.xjc.reader.Const;
048: import com.sun.tools.xjc.reader.xmlschema.SimpleTypeBuilder;
049:
050: /**
051: * Enumeration customization.
052: * <p>
053: * This customization binds a simple type to a type-safe enum class.
054: * The actual binding process takes place in {@link SimpleTypeBuilder}.
055: *
056: * <p>
057: * This customization is acknowledged by {@link SimpleTypeBuilder}.
058: *
059: * @author
060: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
061: */
062: @XmlRootElement(name="typesafeEnumClass")
063: public final class BIEnum extends AbstractDeclarationImpl {
064:
065: /**
066: * If false, it means not to bind to a type-safe enum.
067: *
068: * this takes precedence over all the other properties of this class.
069: */
070: @XmlAttribute(name="map")
071: private boolean map = true;
072:
073: /** Gets the specified class name, or null if not specified. */
074: @XmlAttribute(name="name")
075: public String className = null;
076:
077: /**
078: * @see BIClass#getExistingClassRef()
079: */
080: @XmlAttribute(name="ref")
081: public String ref;
082:
083: /**
084: * Gets the javadoc comment specified in the customization.
085: * Can be null if none is specified.
086: */
087: @XmlElement
088: public final String javadoc = null;
089:
090: public boolean isMapped() {
091: return map;
092: }
093:
094: /**
095: * Gets the map that contains XML value->BIEnumMember pairs.
096: * This table is built from <enumMember> customizations.
097: *
098: * Always return non-null.
099: */
100: @XmlTransient
101: public final Map<String, BIEnumMember> members = new HashMap<String, BIEnumMember>();
102:
103: public QName getName() {
104: return NAME;
105: }
106:
107: public void setParent(BindInfo p) {
108: super .setParent(p);
109: for (BIEnumMember mem : members.values())
110: mem.setParent(p);
111:
112: // if this specifies a reference to external class,
113: // then it's OK even if noone actually refers this class.
114: if (ref != null)
115: markAsAcknowledged();
116: }
117:
118: /** Name of this declaration. */
119: public static final QName NAME = new QName(Const.JAXB_NSURI, "enum");
120:
121: // setter method for JAXB runtime
122: @XmlElement(name="typesafeEnumMember")
123: private void setMembers(BIEnumMember2[] mems) {
124: for (BIEnumMember2 e : mems)
125: members.put(e.value, e);
126: }
127:
128: /**
129: * {@link BIEnumMember} used inside {@link BIEnum} has additional 'value' attribute.
130: */
131: static class BIEnumMember2 extends BIEnumMember {
132: /**
133: * The lexical representaion of the constant to which we are attaching.
134: */
135: @XmlAttribute(required=true)
136: String value;
137: }
138: }
|