src/share/jaxws_classes/com/sun/tools/internal/xjc/model/CBuiltinLeafInfo.java

changeset 286
f50545b5e2f1
child 368
0989ad8c0860
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/model/CBuiltinLeafInfo.java	Tue Mar 06 16:09:35 2012 -0800
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.xjc.model;
    1.30 +
    1.31 +import java.awt.*;
    1.32 +import java.math.BigDecimal;
    1.33 +import java.math.BigInteger;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.Map;
    1.36 +
    1.37 +import javax.activation.DataHandler;
    1.38 +import javax.activation.MimeType;
    1.39 +import javax.xml.bind.DatatypeConverter;
    1.40 +import javax.xml.bind.annotation.XmlIDREF;
    1.41 +import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
    1.42 +import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
    1.43 +import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
    1.44 +import javax.xml.bind.annotation.adapters.XmlAdapter;
    1.45 +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
    1.46 +import javax.xml.datatype.Duration;
    1.47 +import javax.xml.datatype.XMLGregorianCalendar;
    1.48 +import javax.xml.namespace.QName;
    1.49 +import javax.xml.transform.Source;
    1.50 +
    1.51 +import com.sun.codemodel.internal.JExpr;
    1.52 +import com.sun.codemodel.internal.JExpression;
    1.53 +import com.sun.codemodel.internal.JType;
    1.54 +import com.sun.tools.internal.xjc.model.nav.NClass;
    1.55 +import com.sun.tools.internal.xjc.model.nav.NType;
    1.56 +import com.sun.tools.internal.xjc.model.nav.NavigatorImpl;
    1.57 +import com.sun.tools.internal.xjc.outline.Aspect;
    1.58 +import com.sun.tools.internal.xjc.outline.Outline;
    1.59 +import com.sun.tools.internal.xjc.runtime.ZeroOneBooleanAdapter;
    1.60 +import com.sun.tools.internal.xjc.util.NamespaceContextAdapter;
    1.61 +import com.sun.xml.internal.bind.DatatypeConverterImpl;
    1.62 +import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.63 +import com.sun.xml.internal.bind.v2.model.core.ID;
    1.64 +import com.sun.xml.internal.bind.v2.model.impl.BuiltinLeafInfoImpl;
    1.65 +import com.sun.xml.internal.xsom.XSComponent;
    1.66 +import com.sun.xml.internal.xsom.XmlString;
    1.67 +
    1.68 +import org.xml.sax.Locator;
    1.69 +
    1.70 +/**
    1.71 + * Encapsulates the default handling for leaf classes (which are bound
    1.72 + * to text in XML.) In particular this class knows how to convert
    1.73 + * the lexical value into the Java class according to this default rule.
    1.74 + *
    1.75 + * <p>
    1.76 + * This represents the spec-defined default handling for the Java
    1.77 + * type ({@link #getType()}.
    1.78 + *
    1.79 + * <p>
    1.80 + * For those Java classes (such as {@link String} or {@link Boolean})
    1.81 + * where the spec designates a specific default handling, there are
    1.82 + * constants in this class (such as {@link #STRING} or {@link #BOOLEAN}.)
    1.83 + *
    1.84 + * <p>
    1.85 + * The generated type-safe enum classes are also a leaf class,
    1.86 + * and as such there are {@link CEnumLeafInfo} that represents it
    1.87 + * as {@link CBuiltinLeafInfo}.
    1.88 + *
    1.89 + * <p>
    1.90 + * This class represents the <b>default handling</b>, and therefore
    1.91 + * we can only have one instance per one {@link NType}. Handling of
    1.92 + * other XML Schema types (such as xs:token) are represented as
    1.93 + * a general {@link TypeUse} objects.
    1.94 + *
    1.95 + *
    1.96 + * @author Kohsuke Kawaguchi
    1.97 + */
    1.98 +public abstract class CBuiltinLeafInfo extends BuiltinLeafInfoImpl<NType,NClass> implements CNonElement {
    1.99 +
   1.100 +    private final ID id;
   1.101 +
   1.102 +    // no derived class other than the spec-defined ones. definitely not for enum.
   1.103 +    private CBuiltinLeafInfo(NType typeToken, QName typeName, ID id) {
   1.104 +        super(typeToken,typeName);
   1.105 +        this.id = id;
   1.106 +    }
   1.107 +
   1.108 +    /**
   1.109 +     * Gets the code model representation of this type.
   1.110 +     */
   1.111 +    public JType toType(Outline o, Aspect aspect) {
   1.112 +        return getType().toType(o,aspect);
   1.113 +    }
   1.114 +
   1.115 +    /**
   1.116 +     * Since {@link CBuiltinLeafInfo} represents a default binding,
   1.117 +     * it is never a collection.
   1.118 +     */
   1.119 +    @Deprecated
   1.120 +    public final boolean isCollection() {
   1.121 +        return false;
   1.122 +    }
   1.123 +
   1.124 +    /**
   1.125 +     * Guaranteed to return this.
   1.126 +     */
   1.127 +    @Deprecated
   1.128 +    public CNonElement getInfo() {
   1.129 +        return this;
   1.130 +    }
   1.131 +
   1.132 +    public ID idUse() {
   1.133 +        return id;
   1.134 +    }
   1.135 +
   1.136 +    /**
   1.137 +     * {@link CBuiltinLeafInfo} never has a default associated MIME type.
   1.138 +     */
   1.139 +    public MimeType getExpectedMimeType() {
   1.140 +        return null;
   1.141 +    }
   1.142 +
   1.143 +    @Deprecated
   1.144 +    public final CAdapter getAdapterUse() {
   1.145 +        return null;
   1.146 +    }
   1.147 +
   1.148 +    public Locator getLocator() {
   1.149 +        return Model.EMPTY_LOCATOR;
   1.150 +    }
   1.151 +
   1.152 +    public final XSComponent getSchemaComponent() {
   1.153 +        throw new UnsupportedOperationException("TODO. If you hit this, let us know.");
   1.154 +    }
   1.155 +
   1.156 +    /**
   1.157 +     * Creates a {@link TypeUse} that represents a collection of this {@link CBuiltinLeafInfo}.
   1.158 +     */
   1.159 +    public final TypeUse makeCollection() {
   1.160 +        return TypeUseFactory.makeCollection(this);
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * Creates a {@link TypeUse} that represents an adapted use of this {@link CBuiltinLeafInfo}.
   1.165 +     */
   1.166 +    public final TypeUse makeAdapted( Class<? extends XmlAdapter> adapter, boolean copy ) {
   1.167 +        return TypeUseFactory.adapt(this,adapter,copy);
   1.168 +    }
   1.169 +
   1.170 +    /**
   1.171 +     * Creates a {@link TypeUse} that represents a MIME-type assocaited version of this {@link CBuiltinLeafInfo}.
   1.172 +     */
   1.173 +    public final TypeUse makeMimeTyped( MimeType mt ) {
   1.174 +        return TypeUseFactory.makeMimeTyped(this,mt);
   1.175 +    }
   1.176 +
   1.177 +    /**
   1.178 +     * {@link CBuiltinLeafInfo} for Java classes that have
   1.179 +     * the spec defined built-in binding semantics.
   1.180 +     */
   1.181 +    private static abstract class Builtin extends CBuiltinLeafInfo {
   1.182 +        protected Builtin(Class c, String typeName) {
   1.183 +            this(c,typeName,com.sun.xml.internal.bind.v2.model.core.ID.NONE);
   1.184 +        }
   1.185 +        protected Builtin(Class c, String typeName, ID id) {
   1.186 +            super(NavigatorImpl.theInstance.ref(c), new QName(WellKnownNamespace.XML_SCHEMA,typeName),id);
   1.187 +            LEAVES.put(getType(),this);
   1.188 +        }
   1.189 +
   1.190 +        /**
   1.191 +         * No vendor customization in the built-in classes.
   1.192 +         */
   1.193 +        public CCustomizations getCustomizations() {
   1.194 +            return CCustomizations.EMPTY;
   1.195 +        }
   1.196 +    }
   1.197 +
   1.198 +    private static final class NoConstantBuiltin extends Builtin {
   1.199 +        public NoConstantBuiltin(Class c, String typeName) {
   1.200 +            super(c, typeName);
   1.201 +        }
   1.202 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.203 +            return null;
   1.204 +        }
   1.205 +    }
   1.206 +
   1.207 +    /**
   1.208 +     * All built-in leaves.
   1.209 +     */
   1.210 +    public static final Map<NType,CBuiltinLeafInfo> LEAVES = new HashMap<NType,CBuiltinLeafInfo>();
   1.211 +
   1.212 +
   1.213 +    public static final CBuiltinLeafInfo ANYTYPE = new NoConstantBuiltin(Object.class,"anyType");
   1.214 +    public static final CBuiltinLeafInfo STRING = new Builtin(String.class,"string") {
   1.215 +            public JExpression createConstant(Outline outline, XmlString lexical) {
   1.216 +                return JExpr.lit(lexical.value);
   1.217 +            }
   1.218 +    };
   1.219 +    public static final CBuiltinLeafInfo BOOLEAN = new Builtin(Boolean.class,"boolean") {
   1.220 +            public JExpression createConstant(Outline outline, XmlString lexical) {
   1.221 +                return JExpr.lit(DatatypeConverterImpl._parseBoolean(lexical.value));
   1.222 +            }
   1.223 +    };
   1.224 +    public static final CBuiltinLeafInfo INT = new Builtin(Integer.class,"int") {
   1.225 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.226 +            return JExpr.lit(DatatypeConverterImpl._parseInt(lexical.value));
   1.227 +        }
   1.228 +    };
   1.229 +    public static final CBuiltinLeafInfo LONG = new Builtin(Long.class,"long") {
   1.230 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.231 +            return JExpr.lit(DatatypeConverterImpl._parseLong(lexical.value));
   1.232 +        }
   1.233 +    };
   1.234 +    public static final CBuiltinLeafInfo BYTE = new Builtin(Byte.class,"byte") {
   1.235 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.236 +            return JExpr.cast(
   1.237 +                    outline.getCodeModel().BYTE,
   1.238 +                    JExpr.lit(DatatypeConverterImpl._parseByte(lexical.value)));
   1.239 +        }
   1.240 +    };
   1.241 +    public static final CBuiltinLeafInfo SHORT = new Builtin(Short.class,"short") {
   1.242 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.243 +            return JExpr.cast(
   1.244 +                    outline.getCodeModel().SHORT,
   1.245 +                    JExpr.lit(DatatypeConverterImpl._parseShort(lexical.value)));
   1.246 +        }
   1.247 +    };
   1.248 +    public static final CBuiltinLeafInfo FLOAT = new Builtin(Float.class,"float") {
   1.249 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.250 +            return JExpr.lit(DatatypeConverterImpl._parseFloat(lexical.value));
   1.251 +        }
   1.252 +    };
   1.253 +    public static final CBuiltinLeafInfo DOUBLE = new Builtin(Double.class,"double") {
   1.254 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.255 +            return JExpr.lit(DatatypeConverterImpl._parseDouble(lexical.value));
   1.256 +        }
   1.257 +    };
   1.258 +    public static final CBuiltinLeafInfo QNAME = new Builtin(QName.class,"QName") {
   1.259 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.260 +            QName qn = DatatypeConverterImpl._parseQName(lexical.value,new NamespaceContextAdapter(lexical));
   1.261 +            return JExpr._new(outline.getCodeModel().ref(QName.class))
   1.262 +                .arg(qn.getNamespaceURI())
   1.263 +                .arg(qn.getLocalPart())
   1.264 +                .arg(qn.getPrefix());
   1.265 +        }
   1.266 +    };
   1.267 +    // XMLGregorianCalendar is mutable, so we can't support default values anyhow.
   1.268 +        // For CALENAR we are uses a most unlikely name so as to avoid potential name
   1.269 +        // conflicts in the furture.
   1.270 +        public static final CBuiltinLeafInfo CALENDAR = new NoConstantBuiltin(XMLGregorianCalendar.class,"\u0000");
   1.271 +    public static final CBuiltinLeafInfo DURATION = new NoConstantBuiltin(Duration.class,"duration");
   1.272 +
   1.273 +    public static final CBuiltinLeafInfo BIG_INTEGER = new Builtin(BigInteger.class,"integer") {
   1.274 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.275 +            return JExpr._new(outline.getCodeModel().ref(BigInteger.class)).arg(lexical.value.trim());
   1.276 +        }
   1.277 +    };
   1.278 +
   1.279 +    public static final CBuiltinLeafInfo BIG_DECIMAL = new Builtin(BigDecimal.class,"decimal") {
   1.280 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.281 +            return JExpr._new(outline.getCodeModel().ref(BigDecimal.class)).arg(lexical.value.trim());
   1.282 +        }
   1.283 +    };
   1.284 +
   1.285 +    public static final CBuiltinLeafInfo BASE64_BYTE_ARRAY = new Builtin(byte[].class,"base64Binary") {
   1.286 +        public JExpression createConstant(Outline outline, XmlString lexical) {
   1.287 +            return outline.getCodeModel().ref(DatatypeConverter.class).staticInvoke("parseBase64Binary").arg(lexical.value);
   1.288 +        }
   1.289 +    };
   1.290 +
   1.291 +    public static final CBuiltinLeafInfo DATA_HANDLER = new NoConstantBuiltin(DataHandler.class,"base64Binary");
   1.292 +    public static final CBuiltinLeafInfo IMAGE = new NoConstantBuiltin(Image.class,"base64Binary");
   1.293 +    public static final CBuiltinLeafInfo XML_SOURCE = new NoConstantBuiltin(Source.class,"base64Binary");
   1.294 +
   1.295 +    public static final TypeUse HEXBIN_BYTE_ARRAY =
   1.296 +        STRING.makeAdapted(HexBinaryAdapter.class,false);
   1.297 +
   1.298 +
   1.299 +    // TODO: not sure if they should belong here,
   1.300 +    // but I couldn't find other places that fit.
   1.301 +    public static final TypeUse TOKEN =
   1.302 +            STRING.makeAdapted(CollapsedStringAdapter.class,false);
   1.303 +
   1.304 +    public static final TypeUse NORMALIZED_STRING =
   1.305 +            STRING.makeAdapted(NormalizedStringAdapter.class,false);
   1.306 +
   1.307 +    public static final TypeUse ID = TypeUseFactory.makeID(TOKEN,com.sun.xml.internal.bind.v2.model.core.ID.ID);
   1.308 +
   1.309 +    /**
   1.310 +     * boolean restricted to 0 or 1.
   1.311 +     */
   1.312 +    public static final TypeUse BOOLEAN_ZERO_OR_ONE =
   1.313 +            STRING.makeAdapted(ZeroOneBooleanAdapter.class,true);
   1.314 +
   1.315 +    /**
   1.316 +     * IDREF.
   1.317 +     *
   1.318 +     * IDREF is has a whitespace normalization semantics of token, but
   1.319 +     * we don't want {@link XmlJavaTypeAdapter} and {@link XmlIDREF} to interact.
   1.320 +     */
   1.321 +    public static final TypeUse IDREF = TypeUseFactory.makeID(ANYTYPE,com.sun.xml.internal.bind.v2.model.core.ID.IDREF);
   1.322 +
   1.323 +    /**
   1.324 +     * For all list of strings, such as NMTOKENS, ENTITIES.
   1.325 +     */
   1.326 +    public static final TypeUse STRING_LIST =
   1.327 +            STRING.makeCollection();
   1.328 +}

mercurial