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.reader.xmlschema.bindinfo;
325N/A
325N/Aimport javax.xml.bind.DatatypeConverter;
325N/Aimport javax.xml.bind.annotation.XmlAttribute;
325N/Aimport javax.xml.bind.annotation.XmlRootElement;
325N/Aimport javax.xml.bind.annotation.adapters.XmlAdapter;
325N/Aimport javax.xml.namespace.QName;
325N/A
325N/Aimport com.sun.codemodel.internal.JClass;
325N/Aimport com.sun.codemodel.internal.JClassAlreadyExistsException;
325N/Aimport com.sun.codemodel.internal.JCodeModel;
325N/Aimport com.sun.codemodel.internal.JDefinedClass;
325N/Aimport com.sun.codemodel.internal.JExpr;
325N/Aimport com.sun.codemodel.internal.JExpression;
325N/Aimport com.sun.codemodel.internal.JMethod;
325N/Aimport com.sun.codemodel.internal.JMod;
325N/Aimport com.sun.codemodel.internal.JPackage;
325N/Aimport com.sun.codemodel.internal.JType;
325N/Aimport com.sun.codemodel.internal.JVar;
325N/Aimport com.sun.codemodel.internal.JConditional;
325N/Aimport com.sun.tools.internal.xjc.ErrorReceiver;
325N/Aimport com.sun.tools.internal.xjc.model.CAdapter;
325N/Aimport com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
325N/Aimport com.sun.tools.internal.xjc.model.TypeUse;
325N/Aimport com.sun.tools.internal.xjc.model.TypeUseFactory;
325N/Aimport com.sun.tools.internal.xjc.reader.Const;
325N/Aimport com.sun.tools.internal.xjc.reader.Ring;
325N/Aimport com.sun.tools.internal.xjc.reader.TypeUtil;
325N/Aimport com.sun.tools.internal.xjc.reader.xmlschema.ClassSelector;
325N/Aimport com.sun.xml.internal.bind.v2.WellKnownNamespace;
325N/Aimport com.sun.xml.internal.xsom.XSSimpleType;
325N/A
325N/Aimport org.xml.sax.Locator;
325N/A
325N/A/**
325N/A * Conversion declaration.
325N/A *
325N/A * <p>
325N/A * A conversion declaration specifies how an XML type gets mapped
325N/A * to a Java type.
325N/A *
325N/A * @author
325N/A * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
325N/A */
325N/Apublic abstract class BIConversion extends AbstractDeclarationImpl {
325N/A @Deprecated
325N/A public BIConversion( Locator loc ) {
325N/A super(loc);
325N/A }
325N/A
325N/A protected BIConversion() {
325N/A }
325N/A
325N/A /**
325N/A * Gets the {@link TypeUse} object that this conversion represents.
325N/A * <p>
325N/A * The returned {@link TypeUse} object is properly adapted.
325N/A *
325N/A * @param owner
325N/A * A {@link BIConversion} is always associated with one
325N/A * {@link XSSimpleType}, but that's not always available
325N/A * when a {@link BIConversion} is built. So we pass this
325N/A * as a parameter to this method.
325N/A */
325N/A public abstract TypeUse getTypeUse( XSSimpleType owner );
325N/A
325N/A public QName getName() { return NAME; }
325N/A
325N/A /** Name of the conversion declaration. */
325N/A public static final QName NAME = new QName(
325N/A Const.JAXB_NSURI, "conversion" );
325N/A
325N/A /**
325N/A * Implementation that returns a statically-determined constant {@link TypeUse}.
325N/A */
325N/A public static final class Static extends BIConversion {
325N/A /**
325N/A * Always non-null.
325N/A */
325N/A private final TypeUse transducer;
325N/A
325N/A public Static(Locator loc, TypeUse transducer) {
325N/A super(loc);
325N/A this.transducer = transducer;
325N/A }
325N/A
325N/A public TypeUse getTypeUse(XSSimpleType owner) {
325N/A return transducer;
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * User-specified &lt;javaType> customization.
325N/A *
325N/A * The parse/print methods are allowed to be null,
325N/A * and their default values are determined based on the
325N/A * owner of the token.
325N/A */
325N/A @XmlRootElement(name="javaType")
325N/A public static class User extends BIConversion {
325N/A @XmlAttribute
325N/A private String parseMethod;
325N/A @XmlAttribute
325N/A private String printMethod;
325N/A @XmlAttribute(name="name")
325N/A private String type = "java.lang.String";
325N/A
325N/A /**
325N/A * If null, computed from {@link #type}.
325N/A * Sometimes this can be set instead of {@link #type}.
325N/A */
325N/A private JType inMemoryType;
325N/A
325N/A public User(Locator loc, String parseMethod, String printMethod, JType inMemoryType) {
325N/A super(loc);
325N/A this.parseMethod = parseMethod;
325N/A this.printMethod = printMethod;
325N/A this.inMemoryType = inMemoryType;
325N/A }
325N/A
325N/A public User() {
325N/A }
325N/A
325N/A /**
325N/A * Cache used by {@link #getTypeUse(XSSimpleType)} to improve the performance.
325N/A */
325N/A private TypeUse typeUse;
325N/A
325N/A public TypeUse getTypeUse(XSSimpleType owner) {
325N/A if(typeUse!=null)
325N/A return typeUse;
325N/A
325N/A JCodeModel cm = getCodeModel();
325N/A
325N/A if(inMemoryType==null)
325N/A inMemoryType = TypeUtil.getType(cm,type,Ring.get(ErrorReceiver.class),getLocation());
325N/A
325N/A JDefinedClass adapter = generateAdapter(parseMethodFor(owner),printMethodFor(owner),owner);
325N/A
325N/A // XmlJavaType customization always converts between string and an user-defined type.
325N/A typeUse = TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,new CAdapter(adapter));
325N/A
325N/A return typeUse;
325N/A }
325N/A
325N/A /**
325N/A * generate the adapter class.
325N/A */
325N/A private JDefinedClass generateAdapter(String parseMethod, String printMethod,XSSimpleType owner) {
325N/A JDefinedClass adapter = null;
325N/A
325N/A int id = 1;
325N/A while(adapter==null) {
325N/A try {
325N/A JPackage pkg = Ring.get(ClassSelector.class).getClassScope().getOwnerPackage();
325N/A adapter = pkg._class("Adapter"+id);
325N/A } catch (JClassAlreadyExistsException e) {
325N/A // try another name in search for an unique name.
325N/A // this isn't too efficient, but we expect people to usually use
325N/A // a very small number of adapters.
325N/A id++;
325N/A }
325N/A }
325N/A
325N/A JClass bim = inMemoryType.boxify();
325N/A
325N/A adapter._extends(getCodeModel().ref(XmlAdapter.class).narrow(String.class).narrow(bim));
325N/A
325N/A JMethod unmarshal = adapter.method(JMod.PUBLIC, bim, "unmarshal");
325N/A JVar $value = unmarshal.param(String.class, "value");
325N/A
325N/A JExpression inv;
325N/A
325N/A if( parseMethod.equals("new") ) {
325N/A // "new" indicates that the constructor of the target type
325N/A // will do the unmarshalling.
325N/A
325N/A // RESULT: new <type>()
325N/A inv = JExpr._new(bim).arg($value);
325N/A } else {
325N/A int idx = parseMethod.lastIndexOf('.');
325N/A if(idx<0) {
325N/A // parseMethod specifies the static method of the target type
325N/A // which will do the unmarshalling.
325N/A
325N/A // because of an error check at the constructor,
325N/A // we can safely assume that this cast works.
325N/A inv = bim.staticInvoke(parseMethod).arg($value);
325N/A } else {
325N/A inv = JExpr.direct(parseMethod+"(value)");
325N/A }
325N/A }
325N/A unmarshal.body()._return(inv);
325N/A
325N/A
325N/A JMethod marshal = adapter.method(JMod.PUBLIC, String.class, "marshal");
325N/A $value = marshal.param(bim,"value");
325N/A
325N/A if(printMethod.startsWith("javax.xml.bind.DatatypeConverter.")) {
325N/A // UGLY: if this conversion is the system-driven conversion,
325N/A // check for null
325N/A marshal.body()._if($value.eq(JExpr._null()))._then()._return(JExpr._null());
325N/A }
325N/A
325N/A int idx = printMethod.lastIndexOf('.');
325N/A if(idx<0) {
325N/A // printMethod specifies a method in the target type
325N/A // which performs the serialization.
325N/A
325N/A // RESULT: <value>.<method>()
325N/A inv = $value.invoke(printMethod);
325N/A
325N/A // check value is not null ... if(value == null) return null;
325N/A JConditional jcon = marshal.body()._if($value.eq(JExpr._null()));
325N/A jcon._then()._return(JExpr._null());
325N/A } else {
325N/A // RESULT: <className>.<method>(<value>)
325N/A if(this.printMethod==null) {
325N/A // HACK HACK HACK
325N/A JType t = inMemoryType.unboxify();
325N/A inv = JExpr.direct(printMethod+"(("+findBaseConversion(owner).toLowerCase()+")("+t.fullName()+")value)");
325N/A } else
325N/A inv = JExpr.direct(printMethod+"(value)");
325N/A }
325N/A marshal.body()._return(inv);
325N/A
325N/A return adapter;
325N/A }
325N/A
325N/A private String printMethodFor(XSSimpleType owner) {
325N/A if(printMethod!=null) return printMethod;
325N/A
325N/A if(inMemoryType.unboxify().isPrimitive()) {
325N/A String method = getConversionMethod("print",owner);
325N/A if(method!=null)
325N/A return method;
325N/A }
325N/A
325N/A return "toString";
325N/A }
325N/A
325N/A private String parseMethodFor(XSSimpleType owner) {
325N/A if(parseMethod!=null) return parseMethod;
325N/A
325N/A if(inMemoryType.unboxify().isPrimitive()) {
325N/A String method = getConversionMethod("parse", owner);
325N/A if(method!=null) {
325N/A // this cast is necessary for conversion between primitive Java types
325N/A return '('+inMemoryType.unboxify().fullName()+')'+method;
325N/A }
325N/A }
325N/A
325N/A return "new";
325N/A }
325N/A
325N/A private static final String[] knownBases = new String[]{
325N/A "Float", "Double", "Byte", "Short", "Int", "Long", "Boolean"
325N/A };
325N/A
325N/A private String getConversionMethod(String methodPrefix, XSSimpleType owner) {
325N/A String bc = findBaseConversion(owner);
325N/A if(bc==null) return null;
325N/A
325N/A return DatatypeConverter.class.getName()+'.'+methodPrefix+bc;
325N/A }
325N/A
325N/A private String findBaseConversion(XSSimpleType owner) {
325N/A // find the base simple type mapping.
325N/A for( XSSimpleType st=owner; st!=null; st = st.getSimpleBaseType() ) {
325N/A if( !WellKnownNamespace.XML_SCHEMA.equals(st.getTargetNamespace()) )
325N/A continue; // user-defined type
325N/A
325N/A String name = st.getName().intern();
325N/A for( String s : knownBases )
325N/A if(name.equalsIgnoreCase(s))
325N/A return s;
325N/A }
325N/A
325N/A return null;
325N/A }
325N/A
325N/A public QName getName() { return NAME; }
325N/A
325N/A /** Name of the conversion declaration. */
325N/A public static final QName NAME = new QName(
325N/A Const.JAXB_NSURI, "javaType" );
325N/A }
325N/A
325N/A @XmlRootElement(name="javaType",namespace=Const.XJC_EXTENSION_URI)
325N/A public static class UserAdapter extends BIConversion {
325N/A @XmlAttribute(name="name")
325N/A private String type = null;
325N/A
325N/A @XmlAttribute
325N/A private String adapter = null;
325N/A
325N/A private TypeUse typeUse;
325N/A
325N/A public TypeUse getTypeUse(XSSimpleType owner) {
325N/A if(typeUse!=null)
325N/A return typeUse;
325N/A
325N/A JCodeModel cm = getCodeModel();
325N/A
325N/A JDefinedClass a;
325N/A try {
325N/A a = cm._class(adapter);
325N/A a.hide(); // we assume this is given by the user
325N/A a._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(
325N/A cm.ref(type)));
325N/A } catch (JClassAlreadyExistsException e) {
325N/A a = e.getExistingClass();
325N/A }
325N/A
325N/A // TODO: it's not correct to say that it adapts from String,
325N/A // but OTOH I don't think we can compute that.
325N/A typeUse = TypeUseFactory.adapt(
325N/A CBuiltinLeafInfo.STRING,
325N/A new CAdapter(a));
325N/A
325N/A return typeUse;
325N/A }
325N/A }
325N/A}