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.model;
037:
038: import java.util.Collection;
039:
040: import javax.activation.MimeType;
041: import javax.xml.namespace.QName;
042:
043: import com.sun.codemodel.JClass;
044: import com.sun.codemodel.JExpression;
045: import com.sun.tools.xjc.model.nav.NClass;
046: import com.sun.tools.xjc.model.nav.NType;
047: import com.sun.tools.xjc.outline.Aspect;
048: import com.sun.tools.xjc.outline.Outline;
049: import com.sun.xml.bind.v2.model.annotation.Locatable;
050: import com.sun.xml.bind.v2.model.core.EnumLeafInfo;
051: import com.sun.xml.bind.v2.model.core.ID;
052: import com.sun.xml.bind.v2.model.core.NonElement;
053: import com.sun.xml.bind.v2.model.core.Element;
054: import com.sun.xml.bind.v2.runtime.Location;
055: import com.sun.xml.xsom.XSComponent;
056: import com.sun.xml.xsom.XmlString;
057:
058: import org.xml.sax.Locator;
059:
060: /**
061: * Transducer that converts a string into an "enumeration class."
062: *
063: * The structure of the generated class needs to precisely
064: * follow the JAXB spec.
065: *
066: * @author
067: * <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
068: */
069: public final class CEnumLeafInfo implements
070: EnumLeafInfo<NType, NClass>, NClass, CNonElement {
071: /**
072: * The {@link Model} object to which this bean belongs.
073: */
074: public final Model model;
075:
076: /**
077: * The parent into which the enum class should be generated.
078: */
079: public final CClassInfoParent parent;
080:
081: /**
082: * Short name of the generated type-safe enum.
083: */
084: public final String shortName;
085:
086: private final QName typeName;
087:
088: private final XSComponent source;
089:
090: /**
091: * Represents the underlying type of this enumeration
092: * and its conversion.
093: *
094: * <p>
095: * To parse XML into a constant, we use the base type
096: * to do lexical -> value, then use a map to pick up the right one.
097: *
098: * <p>
099: * Hence this also represents the type of the Java value.
100: * For example, if this is an enumeration of xs:int,
101: * then this field will be Java int.
102: */
103: public final CNonElement base;
104:
105: /**
106: * List of enum members.
107: */
108: public final Collection<CEnumConstant> members;
109:
110: private final CCustomizations customizations;
111:
112: /**
113: * @see #getLocator()
114: */
115: private final Locator sourceLocator;
116:
117: public String javadoc;
118:
119: public CEnumLeafInfo(Model model, QName typeName,
120: CClassInfoParent container, String shortName,
121: CNonElement base, Collection<CEnumConstant> _members,
122: XSComponent source, CCustomizations customizations,
123: Locator _sourceLocator) {
124: this .model = model;
125: this .parent = container;
126: this .shortName = model.allocator.assignClassName(parent,
127: shortName);
128: this .base = base;
129: this .members = _members;
130: this .source = source;
131: if (customizations == null)
132: customizations = CCustomizations.EMPTY;
133: this .customizations = customizations;
134: this .sourceLocator = _sourceLocator;
135: this .typeName = typeName;
136:
137: for (CEnumConstant mem : members)
138: mem.setParent(this );
139:
140: model.add(this );
141:
142: // TODO: can we take advantage of the fact that enum can be XmlRootElement?
143: }
144:
145: /**
146: * Source line information that points to the place
147: * where this type-safe enum is defined.
148: * Used to report error messages.
149: */
150: public Locator getLocator() {
151: return sourceLocator;
152: }
153:
154: public QName getTypeName() {
155: return typeName;
156: }
157:
158: public NType getType() {
159: return this ;
160: }
161:
162: /**
163: * @deprecated
164: * why are you calling the method whose return value is known?
165: */
166: public boolean canBeReferencedByIDREF() {
167: return false;
168: }
169:
170: public boolean isElement() {
171: return false;
172: }
173:
174: public QName getElementName() {
175: return null;
176: }
177:
178: public Element<NType, NClass> asElement() {
179: return null;
180: }
181:
182: public NClass getClazz() {
183: return this ;
184: }
185:
186: public XSComponent getSchemaComponent() {
187: return source;
188: }
189:
190: public JClass toType(Outline o, Aspect aspect) {
191: return o.getEnum(this ).clazz;
192: }
193:
194: public boolean isAbstract() {
195: return false;
196: }
197:
198: public boolean isBoxedType() {
199: return false;
200: }
201:
202: public String fullName() {
203: return parent.fullName() + '.' + shortName;
204: }
205:
206: public boolean isPrimitive() {
207: return false;
208: }
209:
210: public boolean isSimpleType() {
211: return true;
212: }
213:
214: /**
215: * The spec says the value field in the enum class will be generated
216: * only under certain circumstances.
217: *
218: * @return
219: * true if the generated enum class should have the value field.
220: */
221: public boolean needsValueField() {
222: for (CEnumConstant cec : members) {
223: if (!cec.getName().equals(cec.getLexicalValue()))
224: return true;
225: }
226: return false;
227: }
228:
229: public JExpression createConstant(Outline outline, XmlString literal) {
230: // correctly identifying which constant it maps to is hard, so
231: // here I'm cheating
232: JClass type = toType(outline, Aspect.EXPOSED);
233: for (CEnumConstant mem : members) {
234: if (mem.getLexicalValue().equals(literal.value))
235: return type.staticRef(mem.getName());
236: }
237: return null;
238: }
239:
240: @Deprecated
241: public boolean isCollection() {
242: return false;
243: }
244:
245: @Deprecated
246: public CAdapter getAdapterUse() {
247: return null;
248: }
249:
250: @Deprecated
251: public CNonElement getInfo() {
252: return this ;
253: }
254:
255: public ID idUse() {
256: return ID.NONE;
257: }
258:
259: public MimeType getExpectedMimeType() {
260: return null;
261: }
262:
263: public Collection<CEnumConstant> getConstants() {
264: return members;
265: }
266:
267: public NonElement<NType, NClass> getBaseType() {
268: return base;
269: }
270:
271: public CCustomizations getCustomizations() {
272: return customizations;
273: }
274:
275: public Locatable getUpstream() {
276: throw new UnsupportedOperationException();
277: }
278:
279: public Location getLocation() {
280: throw new UnsupportedOperationException();
281: }
282: }
|