ohair@286: /* ohair@286: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.tools.internal.xjc.model; ohair@286: ohair@286: import java.awt.*; ohair@286: import java.math.BigDecimal; ohair@286: import java.math.BigInteger; ohair@286: import java.util.HashMap; ohair@286: import java.util.Map; ohair@286: ohair@286: import javax.activation.DataHandler; ohair@286: import javax.activation.MimeType; ohair@286: import javax.xml.bind.DatatypeConverter; ohair@286: import javax.xml.bind.annotation.XmlIDREF; ohair@286: import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; ohair@286: import javax.xml.bind.annotation.adapters.HexBinaryAdapter; ohair@286: import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; ohair@286: import javax.xml.bind.annotation.adapters.XmlAdapter; ohair@286: import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; ohair@286: import javax.xml.datatype.Duration; ohair@286: import javax.xml.datatype.XMLGregorianCalendar; ohair@286: import javax.xml.namespace.QName; ohair@286: import javax.xml.transform.Source; ohair@286: ohair@286: import com.sun.codemodel.internal.JExpr; ohair@286: import com.sun.codemodel.internal.JExpression; ohair@286: import com.sun.codemodel.internal.JType; ohair@286: import com.sun.tools.internal.xjc.model.nav.NClass; alanb@368: import com.sun.xml.internal.bind.v2.model.annotation.Locatable; alanb@368: import com.sun.xml.internal.bind.v2.model.core.BuiltinLeafInfo; alanb@368: import com.sun.xml.internal.bind.v2.model.core.Element; alanb@368: import com.sun.xml.internal.bind.v2.model.core.LeafInfo; alanb@368: import com.sun.xml.internal.bind.v2.runtime.Location; ohair@286: import com.sun.tools.internal.xjc.model.nav.NType; ohair@286: import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; ohair@286: import com.sun.tools.internal.xjc.outline.Aspect; ohair@286: import com.sun.tools.internal.xjc.outline.Outline; ohair@286: import com.sun.tools.internal.xjc.runtime.ZeroOneBooleanAdapter; ohair@286: import com.sun.tools.internal.xjc.util.NamespaceContextAdapter; ohair@286: import com.sun.xml.internal.bind.v2.WellKnownNamespace; ohair@286: import com.sun.xml.internal.bind.v2.model.core.ID; ohair@286: import com.sun.xml.internal.xsom.XSComponent; ohair@286: import com.sun.xml.internal.xsom.XmlString; ohair@286: ohair@286: import org.xml.sax.Locator; ohair@286: ohair@286: /** ohair@286: * Encapsulates the default handling for leaf classes (which are bound ohair@286: * to text in XML.) In particular this class knows how to convert ohair@286: * the lexical value into the Java class according to this default rule. ohair@286: * ohair@286: *

ohair@286: * This represents the spec-defined default handling for the Java ohair@286: * type ({@link #getType()}. ohair@286: * ohair@286: *

ohair@286: * For those Java classes (such as {@link String} or {@link Boolean}) ohair@286: * where the spec designates a specific default handling, there are ohair@286: * constants in this class (such as {@link #STRING} or {@link #BOOLEAN}.) ohair@286: * ohair@286: *

ohair@286: * The generated type-safe enum classes are also a leaf class, ohair@286: * and as such there are {@link CEnumLeafInfo} that represents it ohair@286: * as {@link CBuiltinLeafInfo}. ohair@286: * ohair@286: *

ohair@286: * This class represents the default handling, and therefore ohair@286: * we can only have one instance per one {@link NType}. Handling of ohair@286: * other XML Schema types (such as xs:token) are represented as ohair@286: * a general {@link TypeUse} objects. ohair@286: * ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ alanb@368: public abstract class CBuiltinLeafInfo implements CNonElement, BuiltinLeafInfo, LeafInfo, Location { alanb@368: alanb@368: private final NType type; alanb@368: /** alanb@368: * Can be null for anonymous types. alanb@368: */ alanb@368: private final QName typeName; alanb@368: alanb@368: private final QName[] typeNames; ohair@286: ohair@286: private final ID id; ohair@286: ohair@286: // no derived class other than the spec-defined ones. definitely not for enum. alanb@368: private CBuiltinLeafInfo(NType typeToken, ID id, QName... typeNames) { alanb@368: this.type = typeToken; alanb@368: this.typeName = typeNames.length>0?typeNames[0]:null; alanb@368: this.typeNames = typeNames; ohair@286: this.id = id; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Gets the code model representation of this type. ohair@286: */ ohair@286: public JType toType(Outline o, Aspect aspect) { ohair@286: return getType().toType(o,aspect); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Since {@link CBuiltinLeafInfo} represents a default binding, ohair@286: * it is never a collection. ohair@286: */ ohair@286: @Deprecated ohair@286: public final boolean isCollection() { ohair@286: return false; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Guaranteed to return this. ohair@286: */ ohair@286: @Deprecated ohair@286: public CNonElement getInfo() { ohair@286: return this; ohair@286: } ohair@286: ohair@286: public ID idUse() { ohair@286: return id; ohair@286: } ohair@286: ohair@286: /** ohair@286: * {@link CBuiltinLeafInfo} never has a default associated MIME type. ohair@286: */ ohair@286: public MimeType getExpectedMimeType() { ohair@286: return null; ohair@286: } ohair@286: ohair@286: @Deprecated ohair@286: public final CAdapter getAdapterUse() { ohair@286: return null; ohair@286: } ohair@286: ohair@286: public Locator getLocator() { ohair@286: return Model.EMPTY_LOCATOR; ohair@286: } ohair@286: ohair@286: public final XSComponent getSchemaComponent() { ohair@286: throw new UnsupportedOperationException("TODO. If you hit this, let us know."); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a {@link TypeUse} that represents a collection of this {@link CBuiltinLeafInfo}. ohair@286: */ ohair@286: public final TypeUse makeCollection() { ohair@286: return TypeUseFactory.makeCollection(this); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a {@link TypeUse} that represents an adapted use of this {@link CBuiltinLeafInfo}. ohair@286: */ ohair@286: public final TypeUse makeAdapted( Class adapter, boolean copy ) { ohair@286: return TypeUseFactory.adapt(this,adapter,copy); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a {@link TypeUse} that represents a MIME-type assocaited version of this {@link CBuiltinLeafInfo}. ohair@286: */ ohair@286: public final TypeUse makeMimeTyped( MimeType mt ) { ohair@286: return TypeUseFactory.makeMimeTyped(this,mt); ohair@286: } ohair@286: ohair@286: /** alanb@368: * @deprecated always return false at this level. alanb@368: */ alanb@368: public final boolean isElement() { alanb@368: return false; alanb@368: } alanb@368: alanb@368: /** alanb@368: * @deprecated always return null at this level. alanb@368: */ alanb@368: public final QName getElementName() { alanb@368: return null; alanb@368: } alanb@368: alanb@368: /** alanb@368: * @deprecated always return null at this level. alanb@368: */ alanb@368: public final Element asElement() { alanb@368: return null; alanb@368: } alanb@368: alanb@368: /** alanb@368: * A reference to the representation of the type. alanb@368: */ alanb@368: public NType getType() { alanb@368: return type; alanb@368: } alanb@368: alanb@368: /** alanb@368: * Returns all the type names recognized by this bean info. alanb@368: * alanb@368: * @return alanb@368: * do not modify the returned array. alanb@368: */ alanb@368: public final QName[] getTypeNames() { alanb@368: return typeNames; alanb@368: } alanb@368: alanb@368: /** alanb@368: * Leaf-type cannot be referenced from IDREF. alanb@368: * alanb@368: * @deprecated alanb@368: * why are you calling a method whose return value is always known? alanb@368: */ alanb@368: public final boolean canBeReferencedByIDREF() { alanb@368: return false; alanb@368: } alanb@368: alanb@368: public QName getTypeName() { alanb@368: return typeName; alanb@368: } alanb@368: alanb@368: public Locatable getUpstream() { alanb@368: return null; alanb@368: } alanb@368: alanb@368: public Location getLocation() { alanb@368: // this isn't very accurate, but it's not too bad alanb@368: // doing it correctly need leaves to hold navigator. alanb@368: // otherwise revisit the design so that we take navigator as a parameter alanb@368: return this; alanb@368: } alanb@368: alanb@368: public boolean isSimpleType() { alanb@368: return true; alanb@368: } alanb@368: alanb@368: /** ohair@286: * {@link CBuiltinLeafInfo} for Java classes that have ohair@286: * the spec defined built-in binding semantics. ohair@286: */ ohair@286: private static abstract class Builtin extends CBuiltinLeafInfo { ohair@286: protected Builtin(Class c, String typeName) { ohair@286: this(c,typeName,com.sun.xml.internal.bind.v2.model.core.ID.NONE); ohair@286: } ohair@286: protected Builtin(Class c, String typeName, ID id) { alanb@368: super(NavigatorImpl.theInstance.ref(c), id, new QName(WellKnownNamespace.XML_SCHEMA,typeName)); ohair@286: LEAVES.put(getType(),this); ohair@286: } ohair@286: ohair@286: /** ohair@286: * No vendor customization in the built-in classes. ohair@286: */ ohair@286: public CCustomizations getCustomizations() { ohair@286: return CCustomizations.EMPTY; ohair@286: } ohair@286: } ohair@286: ohair@286: private static final class NoConstantBuiltin extends Builtin { ohair@286: public NoConstantBuiltin(Class c, String typeName) { ohair@286: super(c, typeName); ohair@286: } ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return null; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * All built-in leaves. ohair@286: */ ohair@286: public static final Map LEAVES = new HashMap(); ohair@286: ohair@286: ohair@286: public static final CBuiltinLeafInfo ANYTYPE = new NoConstantBuiltin(Object.class,"anyType"); ohair@286: public static final CBuiltinLeafInfo STRING = new Builtin(String.class,"string") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return JExpr.lit(lexical.value); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo BOOLEAN = new Builtin(Boolean.class,"boolean") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { alanb@368: return JExpr.lit(DatatypeConverter.parseBoolean(lexical.value)); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo INT = new Builtin(Integer.class,"int") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { alanb@368: return JExpr.lit(DatatypeConverter.parseInt(lexical.value)); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo LONG = new Builtin(Long.class,"long") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { alanb@368: return JExpr.lit(DatatypeConverter.parseLong(lexical.value)); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo BYTE = new Builtin(Byte.class,"byte") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return JExpr.cast( ohair@286: outline.getCodeModel().BYTE, alanb@368: JExpr.lit(DatatypeConverter.parseByte(lexical.value))); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo SHORT = new Builtin(Short.class,"short") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return JExpr.cast( ohair@286: outline.getCodeModel().SHORT, alanb@368: JExpr.lit(DatatypeConverter.parseShort(lexical.value))); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo FLOAT = new Builtin(Float.class,"float") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { alanb@368: return JExpr.lit(DatatypeConverter.parseFloat(lexical.value)); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo DOUBLE = new Builtin(Double.class,"double") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { alanb@368: return JExpr.lit(DatatypeConverter.parseDouble(lexical.value)); ohair@286: } ohair@286: }; ohair@286: public static final CBuiltinLeafInfo QNAME = new Builtin(QName.class,"QName") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { alanb@368: QName qn = DatatypeConverter.parseQName(lexical.value,new NamespaceContextAdapter(lexical)); ohair@286: return JExpr._new(outline.getCodeModel().ref(QName.class)) ohair@286: .arg(qn.getNamespaceURI()) ohair@286: .arg(qn.getLocalPart()) ohair@286: .arg(qn.getPrefix()); ohair@286: } ohair@286: }; ohair@286: // XMLGregorianCalendar is mutable, so we can't support default values anyhow. ohair@286: // For CALENAR we are uses a most unlikely name so as to avoid potential name ohair@286: // conflicts in the furture. ohair@286: public static final CBuiltinLeafInfo CALENDAR = new NoConstantBuiltin(XMLGregorianCalendar.class,"\u0000"); ohair@286: public static final CBuiltinLeafInfo DURATION = new NoConstantBuiltin(Duration.class,"duration"); ohair@286: ohair@286: public static final CBuiltinLeafInfo BIG_INTEGER = new Builtin(BigInteger.class,"integer") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return JExpr._new(outline.getCodeModel().ref(BigInteger.class)).arg(lexical.value.trim()); ohair@286: } ohair@286: }; ohair@286: ohair@286: public static final CBuiltinLeafInfo BIG_DECIMAL = new Builtin(BigDecimal.class,"decimal") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return JExpr._new(outline.getCodeModel().ref(BigDecimal.class)).arg(lexical.value.trim()); ohair@286: } ohair@286: }; ohair@286: ohair@286: public static final CBuiltinLeafInfo BASE64_BYTE_ARRAY = new Builtin(byte[].class,"base64Binary") { ohair@286: public JExpression createConstant(Outline outline, XmlString lexical) { ohair@286: return outline.getCodeModel().ref(DatatypeConverter.class).staticInvoke("parseBase64Binary").arg(lexical.value); ohair@286: } ohair@286: }; ohair@286: ohair@286: public static final CBuiltinLeafInfo DATA_HANDLER = new NoConstantBuiltin(DataHandler.class,"base64Binary"); ohair@286: public static final CBuiltinLeafInfo IMAGE = new NoConstantBuiltin(Image.class,"base64Binary"); ohair@286: public static final CBuiltinLeafInfo XML_SOURCE = new NoConstantBuiltin(Source.class,"base64Binary"); ohair@286: ohair@286: public static final TypeUse HEXBIN_BYTE_ARRAY = ohair@286: STRING.makeAdapted(HexBinaryAdapter.class,false); ohair@286: ohair@286: ohair@286: // TODO: not sure if they should belong here, ohair@286: // but I couldn't find other places that fit. ohair@286: public static final TypeUse TOKEN = ohair@286: STRING.makeAdapted(CollapsedStringAdapter.class,false); ohair@286: ohair@286: public static final TypeUse NORMALIZED_STRING = ohair@286: STRING.makeAdapted(NormalizedStringAdapter.class,false); ohair@286: ohair@286: public static final TypeUse ID = TypeUseFactory.makeID(TOKEN,com.sun.xml.internal.bind.v2.model.core.ID.ID); ohair@286: ohair@286: /** ohair@286: * boolean restricted to 0 or 1. ohair@286: */ ohair@286: public static final TypeUse BOOLEAN_ZERO_OR_ONE = ohair@286: STRING.makeAdapted(ZeroOneBooleanAdapter.class,true); ohair@286: ohair@286: /** ohair@286: * IDREF. ohair@286: * ohair@286: * IDREF is has a whitespace normalization semantics of token, but ohair@286: * we don't want {@link XmlJavaTypeAdapter} and {@link XmlIDREF} to interact. ohair@286: */ ohair@286: public static final TypeUse IDREF = TypeUseFactory.makeID(ANYTYPE,com.sun.xml.internal.bind.v2.model.core.ID.IDREF); ohair@286: ohair@286: /** ohair@286: * For all list of strings, such as NMTOKENS, ENTITIES. ohair@286: */ ohair@286: public static final TypeUse STRING_LIST = ohair@286: STRING.makeCollection(); ohair@286: }