325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.xjc.model;
325N/A
325N/Aimport java.awt.*;
325N/Aimport java.math.BigDecimal;
325N/Aimport java.math.BigInteger;
325N/Aimport java.util.HashMap;
325N/Aimport java.util.Map;
325N/A
325N/Aimport javax.activation.DataHandler;
325N/Aimport javax.activation.MimeType;
325N/Aimport javax.xml.bind.DatatypeConverter;
325N/Aimport javax.xml.bind.annotation.XmlIDREF;
325N/Aimport javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
325N/Aimport javax.xml.bind.annotation.adapters.HexBinaryAdapter;
325N/Aimport javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
325N/Aimport javax.xml.bind.annotation.adapters.XmlAdapter;
325N/Aimport javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
325N/Aimport javax.xml.datatype.Duration;
325N/Aimport javax.xml.datatype.XMLGregorianCalendar;
325N/Aimport javax.xml.namespace.QName;
325N/Aimport javax.xml.transform.Source;
325N/A
325N/Aimport com.sun.codemodel.internal.JExpr;
325N/Aimport com.sun.codemodel.internal.JExpression;
325N/Aimport com.sun.codemodel.internal.JType;
325N/Aimport com.sun.tools.internal.xjc.model.nav.NClass;
325N/Aimport com.sun.tools.internal.xjc.model.nav.NType;
325N/Aimport com.sun.tools.internal.xjc.model.nav.NavigatorImpl;
325N/Aimport com.sun.tools.internal.xjc.outline.Aspect;
325N/Aimport com.sun.tools.internal.xjc.outline.Outline;
325N/Aimport com.sun.tools.internal.xjc.runtime.ZeroOneBooleanAdapter;
325N/Aimport com.sun.tools.internal.xjc.util.NamespaceContextAdapter;
325N/Aimport com.sun.xml.internal.bind.DatatypeConverterImpl;
325N/Aimport com.sun.xml.internal.bind.v2.WellKnownNamespace;
325N/Aimport com.sun.xml.internal.bind.v2.model.core.ID;
325N/Aimport com.sun.xml.internal.bind.v2.model.impl.BuiltinLeafInfoImpl;
325N/Aimport com.sun.xml.internal.xsom.XSComponent;
325N/Aimport com.sun.xml.internal.xsom.XmlString;
325N/A
325N/Aimport org.xml.sax.Locator;
325N/A
325N/A/**
325N/A * Encapsulates the default handling for leaf classes (which are bound
325N/A * to text in XML.) In particular this class knows how to convert
325N/A * the lexical value into the Java class according to this default rule.
325N/A *
325N/A * <p>
325N/A * This represents the spec-defined default handling for the Java
325N/A * type ({@link #getType()}.
325N/A *
325N/A * <p>
325N/A * For those Java classes (such as {@link String} or {@link Boolean})
325N/A * where the spec designates a specific default handling, there are
325N/A * constants in this class (such as {@link #STRING} or {@link #BOOLEAN}.)
325N/A *
325N/A * <p>
325N/A * The generated type-safe enum classes are also a leaf class,
325N/A * and as such there are {@link CEnumLeafInfo} that represents it
325N/A * as {@link CBuiltinLeafInfo}.
325N/A *
325N/A * <p>
325N/A * This class represents the <b>default handling</b>, and therefore
325N/A * we can only have one instance per one {@link NType}. Handling of
325N/A * other XML Schema types (such as xs:token) are represented as
325N/A * a general {@link TypeUse} objects.
325N/A *
325N/A *
325N/A * @author Kohsuke Kawaguchi
325N/A */
325N/Apublic abstract class CBuiltinLeafInfo extends BuiltinLeafInfoImpl<NType,NClass> implements CNonElement {
325N/A
325N/A private final ID id;
325N/A
325N/A // no derived class other than the spec-defined ones. definitely not for enum.
325N/A private CBuiltinLeafInfo(NType typeToken, QName typeName, ID id) {
325N/A super(typeToken,typeName);
325N/A this.id = id;
325N/A }
325N/A
325N/A /**
325N/A * Gets the code model representation of this type.
325N/A */
325N/A public JType toType(Outline o, Aspect aspect) {
325N/A return getType().toType(o,aspect);
325N/A }
325N/A
325N/A /**
325N/A * Since {@link CBuiltinLeafInfo} represents a default binding,
325N/A * it is never a collection.
325N/A */
325N/A @Deprecated
325N/A public final boolean isCollection() {
325N/A return false;
325N/A }
325N/A
325N/A /**
325N/A * Guaranteed to return this.
325N/A */
325N/A @Deprecated
325N/A public CNonElement getInfo() {
325N/A return this;
325N/A }
325N/A
325N/A public ID idUse() {
325N/A return id;
325N/A }
325N/A
325N/A /**
325N/A * {@link CBuiltinLeafInfo} never has a default associated MIME type.
325N/A */
325N/A public MimeType getExpectedMimeType() {
325N/A return null;
325N/A }
325N/A
325N/A @Deprecated
325N/A public final CAdapter getAdapterUse() {
325N/A return null;
325N/A }
325N/A
325N/A public Locator getLocator() {
325N/A return Model.EMPTY_LOCATOR;
325N/A }
325N/A
325N/A public final XSComponent getSchemaComponent() {
325N/A throw new UnsupportedOperationException("TODO. If you hit this, let us know.");
325N/A }
325N/A
325N/A /**
325N/A * Creates a {@link TypeUse} that represents a collection of this {@link CBuiltinLeafInfo}.
325N/A */
325N/A public final TypeUse makeCollection() {
325N/A return TypeUseFactory.makeCollection(this);
325N/A }
325N/A
325N/A /**
325N/A * Creates a {@link TypeUse} that represents an adapted use of this {@link CBuiltinLeafInfo}.
325N/A */
325N/A public final TypeUse makeAdapted( Class<? extends XmlAdapter> adapter, boolean copy ) {
325N/A return TypeUseFactory.adapt(this,adapter,copy);
325N/A }
325N/A
325N/A /**
325N/A * Creates a {@link TypeUse} that represents a MIME-type assocaited version of this {@link CBuiltinLeafInfo}.
325N/A */
325N/A public final TypeUse makeMimeTyped( MimeType mt ) {
325N/A return TypeUseFactory.makeMimeTyped(this,mt);
325N/A }
325N/A
325N/A /**
325N/A * {@link CBuiltinLeafInfo} for Java classes that have
325N/A * the spec defined built-in binding semantics.
325N/A */
325N/A private static abstract class Builtin extends CBuiltinLeafInfo {
325N/A protected Builtin(Class c, String typeName) {
325N/A this(c,typeName,com.sun.xml.internal.bind.v2.model.core.ID.NONE);
325N/A }
325N/A protected Builtin(Class c, String typeName, ID id) {
325N/A super(NavigatorImpl.theInstance.ref(c), new QName(WellKnownNamespace.XML_SCHEMA,typeName),id);
325N/A LEAVES.put(getType(),this);
325N/A }
325N/A
325N/A /**
325N/A * No vendor customization in the built-in classes.
325N/A */
325N/A public CCustomizations getCustomizations() {
325N/A return CCustomizations.EMPTY;
325N/A }
325N/A }
325N/A
325N/A private static final class NoConstantBuiltin extends Builtin {
325N/A public NoConstantBuiltin(Class c, String typeName) {
325N/A super(c, typeName);
325N/A }
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return null;
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * All built-in leaves.
325N/A */
325N/A public static final Map<NType,CBuiltinLeafInfo> LEAVES = new HashMap<NType,CBuiltinLeafInfo>();
325N/A
325N/A
325N/A public static final CBuiltinLeafInfo ANYTYPE = new NoConstantBuiltin(Object.class,"anyType");
325N/A public static final CBuiltinLeafInfo STRING = new Builtin(String.class,"string") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.lit(lexical.value);
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo BOOLEAN = new Builtin(Boolean.class,"boolean") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.lit(DatatypeConverterImpl._parseBoolean(lexical.value));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo INT = new Builtin(Integer.class,"int") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.lit(DatatypeConverterImpl._parseInt(lexical.value));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo LONG = new Builtin(Long.class,"long") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.lit(DatatypeConverterImpl._parseLong(lexical.value));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo BYTE = new Builtin(Byte.class,"byte") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.cast(
325N/A outline.getCodeModel().BYTE,
325N/A JExpr.lit(DatatypeConverterImpl._parseByte(lexical.value)));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo SHORT = new Builtin(Short.class,"short") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.cast(
325N/A outline.getCodeModel().SHORT,
325N/A JExpr.lit(DatatypeConverterImpl._parseShort(lexical.value)));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo FLOAT = new Builtin(Float.class,"float") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.lit(DatatypeConverterImpl._parseFloat(lexical.value));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo DOUBLE = new Builtin(Double.class,"double") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr.lit(DatatypeConverterImpl._parseDouble(lexical.value));
325N/A }
325N/A };
325N/A public static final CBuiltinLeafInfo QNAME = new Builtin(QName.class,"QName") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A QName qn = DatatypeConverterImpl._parseQName(lexical.value,new NamespaceContextAdapter(lexical));
325N/A return JExpr._new(outline.getCodeModel().ref(QName.class))
325N/A .arg(qn.getNamespaceURI())
325N/A .arg(qn.getLocalPart())
325N/A .arg(qn.getPrefix());
325N/A }
325N/A };
325N/A // XMLGregorianCalendar is mutable, so we can't support default values anyhow.
325N/A // For CALENAR we are uses a most unlikely name so as to avoid potential name
325N/A // conflicts in the furture.
325N/A public static final CBuiltinLeafInfo CALENDAR = new NoConstantBuiltin(XMLGregorianCalendar.class,"\u0000");
325N/A public static final CBuiltinLeafInfo DURATION = new NoConstantBuiltin(Duration.class,"duration");
325N/A
325N/A public static final CBuiltinLeafInfo BIG_INTEGER = new Builtin(BigInteger.class,"integer") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr._new(outline.getCodeModel().ref(BigInteger.class)).arg(lexical.value.trim());
325N/A }
325N/A };
325N/A
325N/A public static final CBuiltinLeafInfo BIG_DECIMAL = new Builtin(BigDecimal.class,"decimal") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return JExpr._new(outline.getCodeModel().ref(BigDecimal.class)).arg(lexical.value.trim());
325N/A }
325N/A };
325N/A
325N/A public static final CBuiltinLeafInfo BASE64_BYTE_ARRAY = new Builtin(byte[].class,"base64Binary") {
325N/A public JExpression createConstant(Outline outline, XmlString lexical) {
325N/A return outline.getCodeModel().ref(DatatypeConverter.class).staticInvoke("parseBase64Binary").arg(lexical.value);
325N/A }
325N/A };
325N/A
325N/A public static final CBuiltinLeafInfo DATA_HANDLER = new NoConstantBuiltin(DataHandler.class,"base64Binary");
325N/A public static final CBuiltinLeafInfo IMAGE = new NoConstantBuiltin(Image.class,"base64Binary");
325N/A public static final CBuiltinLeafInfo XML_SOURCE = new NoConstantBuiltin(Source.class,"base64Binary");
325N/A
325N/A public static final TypeUse HEXBIN_BYTE_ARRAY =
325N/A STRING.makeAdapted(HexBinaryAdapter.class,false);
325N/A
325N/A
325N/A // TODO: not sure if they should belong here,
325N/A // but I couldn't find other places that fit.
325N/A public static final TypeUse TOKEN =
325N/A STRING.makeAdapted(CollapsedStringAdapter.class,false);
325N/A
325N/A public static final TypeUse NORMALIZED_STRING =
325N/A STRING.makeAdapted(NormalizedStringAdapter.class,false);
325N/A
325N/A public static final TypeUse ID = TypeUseFactory.makeID(TOKEN,com.sun.xml.internal.bind.v2.model.core.ID.ID);
325N/A
325N/A /**
325N/A * boolean restricted to 0 or 1.
325N/A */
325N/A public static final TypeUse BOOLEAN_ZERO_OR_ONE =
325N/A STRING.makeAdapted(ZeroOneBooleanAdapter.class,true);
325N/A
325N/A /**
325N/A * IDREF.
325N/A *
325N/A * IDREF is has a whitespace normalization semantics of token, but
325N/A * we don't want {@link XmlJavaTypeAdapter} and {@link XmlIDREF} to interact.
325N/A */
325N/A public static final TypeUse IDREF = TypeUseFactory.makeID(ANYTYPE,com.sun.xml.internal.bind.v2.model.core.ID.IDREF);
325N/A
325N/A /**
325N/A * For all list of strings, such as NMTOKENS, ENTITIES.
325N/A */
325N/A public static final TypeUse STRING_LIST =
325N/A STRING.makeCollection();
325N/A}