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

Tue, 09 Apr 2013 14:51:13 +0100

author
alanb
date
Tue, 09 Apr 2013 14:51:13 +0100
changeset 368
0989ad8c0860
parent 286
f50545b5e2f1
child 637
9c07ef4934dd
permissions
-rw-r--r--

8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com

ohair@286 1 /*
ohair@286 2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
ohair@286 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ohair@286 4 *
ohair@286 5 * This code is free software; you can redistribute it and/or modify it
ohair@286 6 * under the terms of the GNU General Public License version 2 only, as
ohair@286 7 * published by the Free Software Foundation. Oracle designates this
ohair@286 8 * particular file as subject to the "Classpath" exception as provided
ohair@286 9 * by Oracle in the LICENSE file that accompanied this code.
ohair@286 10 *
ohair@286 11 * This code is distributed in the hope that it will be useful, but WITHOUT
ohair@286 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ohair@286 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
ohair@286 14 * version 2 for more details (a copy is included in the LICENSE file that
ohair@286 15 * accompanied this code).
ohair@286 16 *
ohair@286 17 * You should have received a copy of the GNU General Public License version
ohair@286 18 * 2 along with this work; if not, write to the Free Software Foundation,
ohair@286 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ohair@286 20 *
ohair@286 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@286 22 * or visit www.oracle.com if you need additional information or have any
ohair@286 23 * questions.
ohair@286 24 */
ohair@286 25
ohair@286 26 package com.sun.tools.internal.xjc.model;
ohair@286 27
ohair@286 28 import java.awt.*;
ohair@286 29 import java.math.BigDecimal;
ohair@286 30 import java.math.BigInteger;
ohair@286 31 import java.util.HashMap;
ohair@286 32 import java.util.Map;
ohair@286 33
ohair@286 34 import javax.activation.DataHandler;
ohair@286 35 import javax.activation.MimeType;
ohair@286 36 import javax.xml.bind.DatatypeConverter;
ohair@286 37 import javax.xml.bind.annotation.XmlIDREF;
ohair@286 38 import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
ohair@286 39 import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
ohair@286 40 import javax.xml.bind.annotation.adapters.NormalizedStringAdapter;
ohair@286 41 import javax.xml.bind.annotation.adapters.XmlAdapter;
ohair@286 42 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
ohair@286 43 import javax.xml.datatype.Duration;
ohair@286 44 import javax.xml.datatype.XMLGregorianCalendar;
ohair@286 45 import javax.xml.namespace.QName;
ohair@286 46 import javax.xml.transform.Source;
ohair@286 47
ohair@286 48 import com.sun.codemodel.internal.JExpr;
ohair@286 49 import com.sun.codemodel.internal.JExpression;
ohair@286 50 import com.sun.codemodel.internal.JType;
ohair@286 51 import com.sun.tools.internal.xjc.model.nav.NClass;
alanb@368 52 import com.sun.xml.internal.bind.v2.model.annotation.Locatable;
alanb@368 53 import com.sun.xml.internal.bind.v2.model.core.BuiltinLeafInfo;
alanb@368 54 import com.sun.xml.internal.bind.v2.model.core.Element;
alanb@368 55 import com.sun.xml.internal.bind.v2.model.core.LeafInfo;
alanb@368 56 import com.sun.xml.internal.bind.v2.runtime.Location;
ohair@286 57 import com.sun.tools.internal.xjc.model.nav.NType;
ohair@286 58 import com.sun.tools.internal.xjc.model.nav.NavigatorImpl;
ohair@286 59 import com.sun.tools.internal.xjc.outline.Aspect;
ohair@286 60 import com.sun.tools.internal.xjc.outline.Outline;
ohair@286 61 import com.sun.tools.internal.xjc.runtime.ZeroOneBooleanAdapter;
ohair@286 62 import com.sun.tools.internal.xjc.util.NamespaceContextAdapter;
ohair@286 63 import com.sun.xml.internal.bind.v2.WellKnownNamespace;
ohair@286 64 import com.sun.xml.internal.bind.v2.model.core.ID;
ohair@286 65 import com.sun.xml.internal.xsom.XSComponent;
ohair@286 66 import com.sun.xml.internal.xsom.XmlString;
ohair@286 67
ohair@286 68 import org.xml.sax.Locator;
ohair@286 69
ohair@286 70 /**
ohair@286 71 * Encapsulates the default handling for leaf classes (which are bound
ohair@286 72 * to text in XML.) In particular this class knows how to convert
ohair@286 73 * the lexical value into the Java class according to this default rule.
ohair@286 74 *
ohair@286 75 * <p>
ohair@286 76 * This represents the spec-defined default handling for the Java
ohair@286 77 * type ({@link #getType()}.
ohair@286 78 *
ohair@286 79 * <p>
ohair@286 80 * For those Java classes (such as {@link String} or {@link Boolean})
ohair@286 81 * where the spec designates a specific default handling, there are
ohair@286 82 * constants in this class (such as {@link #STRING} or {@link #BOOLEAN}.)
ohair@286 83 *
ohair@286 84 * <p>
ohair@286 85 * The generated type-safe enum classes are also a leaf class,
ohair@286 86 * and as such there are {@link CEnumLeafInfo} that represents it
ohair@286 87 * as {@link CBuiltinLeafInfo}.
ohair@286 88 *
ohair@286 89 * <p>
ohair@286 90 * This class represents the <b>default handling</b>, and therefore
ohair@286 91 * we can only have one instance per one {@link NType}. Handling of
ohair@286 92 * other XML Schema types (such as xs:token) are represented as
ohair@286 93 * a general {@link TypeUse} objects.
ohair@286 94 *
ohair@286 95 *
ohair@286 96 * @author Kohsuke Kawaguchi
ohair@286 97 */
alanb@368 98 public abstract class CBuiltinLeafInfo implements CNonElement, BuiltinLeafInfo<NType,NClass>, LeafInfo<NType,NClass>, Location {
alanb@368 99
alanb@368 100 private final NType type;
alanb@368 101 /**
alanb@368 102 * Can be null for anonymous types.
alanb@368 103 */
alanb@368 104 private final QName typeName;
alanb@368 105
alanb@368 106 private final QName[] typeNames;
ohair@286 107
ohair@286 108 private final ID id;
ohair@286 109
ohair@286 110 // no derived class other than the spec-defined ones. definitely not for enum.
alanb@368 111 private CBuiltinLeafInfo(NType typeToken, ID id, QName... typeNames) {
alanb@368 112 this.type = typeToken;
alanb@368 113 this.typeName = typeNames.length>0?typeNames[0]:null;
alanb@368 114 this.typeNames = typeNames;
ohair@286 115 this.id = id;
ohair@286 116 }
ohair@286 117
ohair@286 118 /**
ohair@286 119 * Gets the code model representation of this type.
ohair@286 120 */
ohair@286 121 public JType toType(Outline o, Aspect aspect) {
ohair@286 122 return getType().toType(o,aspect);
ohair@286 123 }
ohair@286 124
ohair@286 125 /**
ohair@286 126 * Since {@link CBuiltinLeafInfo} represents a default binding,
ohair@286 127 * it is never a collection.
ohair@286 128 */
ohair@286 129 @Deprecated
ohair@286 130 public final boolean isCollection() {
ohair@286 131 return false;
ohair@286 132 }
ohair@286 133
ohair@286 134 /**
ohair@286 135 * Guaranteed to return this.
ohair@286 136 */
ohair@286 137 @Deprecated
ohair@286 138 public CNonElement getInfo() {
ohair@286 139 return this;
ohair@286 140 }
ohair@286 141
ohair@286 142 public ID idUse() {
ohair@286 143 return id;
ohair@286 144 }
ohair@286 145
ohair@286 146 /**
ohair@286 147 * {@link CBuiltinLeafInfo} never has a default associated MIME type.
ohair@286 148 */
ohair@286 149 public MimeType getExpectedMimeType() {
ohair@286 150 return null;
ohair@286 151 }
ohair@286 152
ohair@286 153 @Deprecated
ohair@286 154 public final CAdapter getAdapterUse() {
ohair@286 155 return null;
ohair@286 156 }
ohair@286 157
ohair@286 158 public Locator getLocator() {
ohair@286 159 return Model.EMPTY_LOCATOR;
ohair@286 160 }
ohair@286 161
ohair@286 162 public final XSComponent getSchemaComponent() {
ohair@286 163 throw new UnsupportedOperationException("TODO. If you hit this, let us know.");
ohair@286 164 }
ohair@286 165
ohair@286 166 /**
ohair@286 167 * Creates a {@link TypeUse} that represents a collection of this {@link CBuiltinLeafInfo}.
ohair@286 168 */
ohair@286 169 public final TypeUse makeCollection() {
ohair@286 170 return TypeUseFactory.makeCollection(this);
ohair@286 171 }
ohair@286 172
ohair@286 173 /**
ohair@286 174 * Creates a {@link TypeUse} that represents an adapted use of this {@link CBuiltinLeafInfo}.
ohair@286 175 */
ohair@286 176 public final TypeUse makeAdapted( Class<? extends XmlAdapter> adapter, boolean copy ) {
ohair@286 177 return TypeUseFactory.adapt(this,adapter,copy);
ohair@286 178 }
ohair@286 179
ohair@286 180 /**
ohair@286 181 * Creates a {@link TypeUse} that represents a MIME-type assocaited version of this {@link CBuiltinLeafInfo}.
ohair@286 182 */
ohair@286 183 public final TypeUse makeMimeTyped( MimeType mt ) {
ohair@286 184 return TypeUseFactory.makeMimeTyped(this,mt);
ohair@286 185 }
ohair@286 186
ohair@286 187 /**
alanb@368 188 * @deprecated always return false at this level.
alanb@368 189 */
alanb@368 190 public final boolean isElement() {
alanb@368 191 return false;
alanb@368 192 }
alanb@368 193
alanb@368 194 /**
alanb@368 195 * @deprecated always return null at this level.
alanb@368 196 */
alanb@368 197 public final QName getElementName() {
alanb@368 198 return null;
alanb@368 199 }
alanb@368 200
alanb@368 201 /**
alanb@368 202 * @deprecated always return null at this level.
alanb@368 203 */
alanb@368 204 public final Element<NType,NClass> asElement() {
alanb@368 205 return null;
alanb@368 206 }
alanb@368 207
alanb@368 208 /**
alanb@368 209 * A reference to the representation of the type.
alanb@368 210 */
alanb@368 211 public NType getType() {
alanb@368 212 return type;
alanb@368 213 }
alanb@368 214
alanb@368 215 /**
alanb@368 216 * Returns all the type names recognized by this bean info.
alanb@368 217 *
alanb@368 218 * @return
alanb@368 219 * do not modify the returned array.
alanb@368 220 */
alanb@368 221 public final QName[] getTypeNames() {
alanb@368 222 return typeNames;
alanb@368 223 }
alanb@368 224
alanb@368 225 /**
alanb@368 226 * Leaf-type cannot be referenced from IDREF.
alanb@368 227 *
alanb@368 228 * @deprecated
alanb@368 229 * why are you calling a method whose return value is always known?
alanb@368 230 */
alanb@368 231 public final boolean canBeReferencedByIDREF() {
alanb@368 232 return false;
alanb@368 233 }
alanb@368 234
alanb@368 235 public QName getTypeName() {
alanb@368 236 return typeName;
alanb@368 237 }
alanb@368 238
alanb@368 239 public Locatable getUpstream() {
alanb@368 240 return null;
alanb@368 241 }
alanb@368 242
alanb@368 243 public Location getLocation() {
alanb@368 244 // this isn't very accurate, but it's not too bad
alanb@368 245 // doing it correctly need leaves to hold navigator.
alanb@368 246 // otherwise revisit the design so that we take navigator as a parameter
alanb@368 247 return this;
alanb@368 248 }
alanb@368 249
alanb@368 250 public boolean isSimpleType() {
alanb@368 251 return true;
alanb@368 252 }
alanb@368 253
alanb@368 254 /**
ohair@286 255 * {@link CBuiltinLeafInfo} for Java classes that have
ohair@286 256 * the spec defined built-in binding semantics.
ohair@286 257 */
ohair@286 258 private static abstract class Builtin extends CBuiltinLeafInfo {
ohair@286 259 protected Builtin(Class c, String typeName) {
ohair@286 260 this(c,typeName,com.sun.xml.internal.bind.v2.model.core.ID.NONE);
ohair@286 261 }
ohair@286 262 protected Builtin(Class c, String typeName, ID id) {
alanb@368 263 super(NavigatorImpl.theInstance.ref(c), id, new QName(WellKnownNamespace.XML_SCHEMA,typeName));
ohair@286 264 LEAVES.put(getType(),this);
ohair@286 265 }
ohair@286 266
ohair@286 267 /**
ohair@286 268 * No vendor customization in the built-in classes.
ohair@286 269 */
ohair@286 270 public CCustomizations getCustomizations() {
ohair@286 271 return CCustomizations.EMPTY;
ohair@286 272 }
ohair@286 273 }
ohair@286 274
ohair@286 275 private static final class NoConstantBuiltin extends Builtin {
ohair@286 276 public NoConstantBuiltin(Class c, String typeName) {
ohair@286 277 super(c, typeName);
ohair@286 278 }
ohair@286 279 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 280 return null;
ohair@286 281 }
ohair@286 282 }
ohair@286 283
ohair@286 284 /**
ohair@286 285 * All built-in leaves.
ohair@286 286 */
ohair@286 287 public static final Map<NType,CBuiltinLeafInfo> LEAVES = new HashMap<NType,CBuiltinLeafInfo>();
ohair@286 288
ohair@286 289
ohair@286 290 public static final CBuiltinLeafInfo ANYTYPE = new NoConstantBuiltin(Object.class,"anyType");
ohair@286 291 public static final CBuiltinLeafInfo STRING = new Builtin(String.class,"string") {
ohair@286 292 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 293 return JExpr.lit(lexical.value);
ohair@286 294 }
ohair@286 295 };
ohair@286 296 public static final CBuiltinLeafInfo BOOLEAN = new Builtin(Boolean.class,"boolean") {
ohair@286 297 public JExpression createConstant(Outline outline, XmlString lexical) {
alanb@368 298 return JExpr.lit(DatatypeConverter.parseBoolean(lexical.value));
ohair@286 299 }
ohair@286 300 };
ohair@286 301 public static final CBuiltinLeafInfo INT = new Builtin(Integer.class,"int") {
ohair@286 302 public JExpression createConstant(Outline outline, XmlString lexical) {
alanb@368 303 return JExpr.lit(DatatypeConverter.parseInt(lexical.value));
ohair@286 304 }
ohair@286 305 };
ohair@286 306 public static final CBuiltinLeafInfo LONG = new Builtin(Long.class,"long") {
ohair@286 307 public JExpression createConstant(Outline outline, XmlString lexical) {
alanb@368 308 return JExpr.lit(DatatypeConverter.parseLong(lexical.value));
ohair@286 309 }
ohair@286 310 };
ohair@286 311 public static final CBuiltinLeafInfo BYTE = new Builtin(Byte.class,"byte") {
ohair@286 312 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 313 return JExpr.cast(
ohair@286 314 outline.getCodeModel().BYTE,
alanb@368 315 JExpr.lit(DatatypeConverter.parseByte(lexical.value)));
ohair@286 316 }
ohair@286 317 };
ohair@286 318 public static final CBuiltinLeafInfo SHORT = new Builtin(Short.class,"short") {
ohair@286 319 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 320 return JExpr.cast(
ohair@286 321 outline.getCodeModel().SHORT,
alanb@368 322 JExpr.lit(DatatypeConverter.parseShort(lexical.value)));
ohair@286 323 }
ohair@286 324 };
ohair@286 325 public static final CBuiltinLeafInfo FLOAT = new Builtin(Float.class,"float") {
ohair@286 326 public JExpression createConstant(Outline outline, XmlString lexical) {
alanb@368 327 return JExpr.lit(DatatypeConverter.parseFloat(lexical.value));
ohair@286 328 }
ohair@286 329 };
ohair@286 330 public static final CBuiltinLeafInfo DOUBLE = new Builtin(Double.class,"double") {
ohair@286 331 public JExpression createConstant(Outline outline, XmlString lexical) {
alanb@368 332 return JExpr.lit(DatatypeConverter.parseDouble(lexical.value));
ohair@286 333 }
ohair@286 334 };
ohair@286 335 public static final CBuiltinLeafInfo QNAME = new Builtin(QName.class,"QName") {
ohair@286 336 public JExpression createConstant(Outline outline, XmlString lexical) {
alanb@368 337 QName qn = DatatypeConverter.parseQName(lexical.value,new NamespaceContextAdapter(lexical));
ohair@286 338 return JExpr._new(outline.getCodeModel().ref(QName.class))
ohair@286 339 .arg(qn.getNamespaceURI())
ohair@286 340 .arg(qn.getLocalPart())
ohair@286 341 .arg(qn.getPrefix());
ohair@286 342 }
ohair@286 343 };
ohair@286 344 // XMLGregorianCalendar is mutable, so we can't support default values anyhow.
ohair@286 345 // For CALENAR we are uses a most unlikely name so as to avoid potential name
ohair@286 346 // conflicts in the furture.
ohair@286 347 public static final CBuiltinLeafInfo CALENDAR = new NoConstantBuiltin(XMLGregorianCalendar.class,"\u0000");
ohair@286 348 public static final CBuiltinLeafInfo DURATION = new NoConstantBuiltin(Duration.class,"duration");
ohair@286 349
ohair@286 350 public static final CBuiltinLeafInfo BIG_INTEGER = new Builtin(BigInteger.class,"integer") {
ohair@286 351 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 352 return JExpr._new(outline.getCodeModel().ref(BigInteger.class)).arg(lexical.value.trim());
ohair@286 353 }
ohair@286 354 };
ohair@286 355
ohair@286 356 public static final CBuiltinLeafInfo BIG_DECIMAL = new Builtin(BigDecimal.class,"decimal") {
ohair@286 357 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 358 return JExpr._new(outline.getCodeModel().ref(BigDecimal.class)).arg(lexical.value.trim());
ohair@286 359 }
ohair@286 360 };
ohair@286 361
ohair@286 362 public static final CBuiltinLeafInfo BASE64_BYTE_ARRAY = new Builtin(byte[].class,"base64Binary") {
ohair@286 363 public JExpression createConstant(Outline outline, XmlString lexical) {
ohair@286 364 return outline.getCodeModel().ref(DatatypeConverter.class).staticInvoke("parseBase64Binary").arg(lexical.value);
ohair@286 365 }
ohair@286 366 };
ohair@286 367
ohair@286 368 public static final CBuiltinLeafInfo DATA_HANDLER = new NoConstantBuiltin(DataHandler.class,"base64Binary");
ohair@286 369 public static final CBuiltinLeafInfo IMAGE = new NoConstantBuiltin(Image.class,"base64Binary");
ohair@286 370 public static final CBuiltinLeafInfo XML_SOURCE = new NoConstantBuiltin(Source.class,"base64Binary");
ohair@286 371
ohair@286 372 public static final TypeUse HEXBIN_BYTE_ARRAY =
ohair@286 373 STRING.makeAdapted(HexBinaryAdapter.class,false);
ohair@286 374
ohair@286 375
ohair@286 376 // TODO: not sure if they should belong here,
ohair@286 377 // but I couldn't find other places that fit.
ohair@286 378 public static final TypeUse TOKEN =
ohair@286 379 STRING.makeAdapted(CollapsedStringAdapter.class,false);
ohair@286 380
ohair@286 381 public static final TypeUse NORMALIZED_STRING =
ohair@286 382 STRING.makeAdapted(NormalizedStringAdapter.class,false);
ohair@286 383
ohair@286 384 public static final TypeUse ID = TypeUseFactory.makeID(TOKEN,com.sun.xml.internal.bind.v2.model.core.ID.ID);
ohair@286 385
ohair@286 386 /**
ohair@286 387 * boolean restricted to 0 or 1.
ohair@286 388 */
ohair@286 389 public static final TypeUse BOOLEAN_ZERO_OR_ONE =
ohair@286 390 STRING.makeAdapted(ZeroOneBooleanAdapter.class,true);
ohair@286 391
ohair@286 392 /**
ohair@286 393 * IDREF.
ohair@286 394 *
ohair@286 395 * IDREF is has a whitespace normalization semantics of token, but
ohair@286 396 * we don't want {@link XmlJavaTypeAdapter} and {@link XmlIDREF} to interact.
ohair@286 397 */
ohair@286 398 public static final TypeUse IDREF = TypeUseFactory.makeID(ANYTYPE,com.sun.xml.internal.bind.v2.model.core.ID.IDREF);
ohair@286 399
ohair@286 400 /**
ohair@286 401 * For all list of strings, such as NMTOKENS, ENTITIES.
ohair@286 402 */
ohair@286 403 public static final TypeUse STRING_LIST =
ohair@286 404 STRING.makeCollection();
ohair@286 405 }

mercurial