src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/dtd/bindinfo/BIUserConversion.java

Thu, 31 Aug 2017 15:18:52 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:18:52 +0800
changeset 637
9c07ef4934dd
parent 368
0989ad8c0860
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.internal.xjc.reader.dtd.bindinfo;
aoqi@0 27
aoqi@0 28 import java.io.IOException;
aoqi@0 29 import java.io.StringReader;
aoqi@0 30 import java.util.Map;
aoqi@0 31
aoqi@0 32 import javax.xml.bind.annotation.adapters.XmlAdapter;
aoqi@0 33 import javax.xml.parsers.DocumentBuilderFactory;
aoqi@0 34 import javax.xml.parsers.ParserConfigurationException;
aoqi@0 35
aoqi@0 36 import com.sun.codemodel.internal.JClass;
aoqi@0 37 import com.sun.codemodel.internal.JClassAlreadyExistsException;
aoqi@0 38 import com.sun.codemodel.internal.JCodeModel;
aoqi@0 39 import com.sun.codemodel.internal.JDefinedClass;
aoqi@0 40 import com.sun.codemodel.internal.JExpr;
aoqi@0 41 import com.sun.codemodel.internal.JExpression;
aoqi@0 42 import com.sun.codemodel.internal.JMethod;
aoqi@0 43 import com.sun.codemodel.internal.JMod;
aoqi@0 44 import com.sun.codemodel.internal.JPackage;
aoqi@0 45 import com.sun.codemodel.internal.JPrimitiveType;
aoqi@0 46 import com.sun.codemodel.internal.JType;
aoqi@0 47 import com.sun.codemodel.internal.JVar;
aoqi@0 48 import com.sun.tools.internal.xjc.model.CAdapter;
aoqi@0 49 import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
aoqi@0 50 import com.sun.tools.internal.xjc.model.TypeUse;
aoqi@0 51 import com.sun.tools.internal.xjc.model.TypeUseFactory;
aoqi@0 52
aoqi@0 53 import com.sun.xml.internal.bind.v2.util.XmlFactory;
aoqi@0 54 import org.w3c.dom.Element;
aoqi@0 55 import org.xml.sax.InputSource;
aoqi@0 56 import org.xml.sax.Locator;
aoqi@0 57 import org.xml.sax.SAXException;
aoqi@0 58
aoqi@0 59 /**
aoqi@0 60 * <conversion> declaration in the binding file.
aoqi@0 61 * This declaration declares a conversion by user-specified methods.
aoqi@0 62 */
aoqi@0 63 public class BIUserConversion implements BIConversion
aoqi@0 64 {
aoqi@0 65 /**
aoqi@0 66 * Wraps a given <conversion> element in the binding file.
aoqi@0 67 */
aoqi@0 68 BIUserConversion( BindInfo bi, Element _e ) {
aoqi@0 69 this.owner = bi;
aoqi@0 70 this.e = _e;
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 private static void add( Map<String,BIConversion> m, BIConversion c ) {
aoqi@0 74 m.put( c.name(), c );
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 /** Adds all built-in conversions into the given map. */
aoqi@0 78 static void addBuiltinConversions( BindInfo bi, Map<String,BIConversion> m ) {
aoqi@0 79 add( m, new BIUserConversion( bi, parse("<conversion name='boolean' type='java.lang.Boolean' parse='getBoolean' />")));
aoqi@0 80 add( m, new BIUserConversion( bi, parse("<conversion name='byte' type='java.lang.Byte' parse='parseByte' />")));
aoqi@0 81 add( m, new BIUserConversion( bi, parse("<conversion name='short' type='java.lang.Short' parse='parseShort' />")));
aoqi@0 82 add( m, new BIUserConversion( bi, parse("<conversion name='int' type='java.lang.Integer' parse='parseInt' />")));
aoqi@0 83 add( m, new BIUserConversion( bi, parse("<conversion name='long' type='java.lang.Long' parse='parseLong' />")));
aoqi@0 84 add( m, new BIUserConversion( bi, parse("<conversion name='float' type='java.lang.Float' parse='parseFloat' />")));
aoqi@0 85 add( m, new BIUserConversion( bi, parse("<conversion name='double' type='java.lang.Double' parse='parseDouble' />")));
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 private static Element parse(String text) {
aoqi@0 89 try {
aoqi@0 90 //this is parsing well known schemas, do not configure secure processing - always true
aoqi@0 91 DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(false);
aoqi@0 92 InputSource is = new InputSource(new StringReader(text));
aoqi@0 93 return dbf.newDocumentBuilder().parse(is).getDocumentElement();
aoqi@0 94 } catch (SAXException x) {
aoqi@0 95 throw new Error(x);
aoqi@0 96 } catch (IOException x) {
aoqi@0 97 throw new Error(x);
aoqi@0 98 } catch (ParserConfigurationException x) {
aoqi@0 99 throw new Error(x);
aoqi@0 100 }
aoqi@0 101 }
aoqi@0 102
aoqi@0 103
aoqi@0 104 /** The owner {@link BindInfo} object to which this object belongs. */
aoqi@0 105 private final BindInfo owner;
aoqi@0 106
aoqi@0 107 /** &lt;conversion> element which this object is wrapping. */
aoqi@0 108 private final Element e;
aoqi@0 109
aoqi@0 110
aoqi@0 111
aoqi@0 112 /** Gets the location where this declaration is declared. */
aoqi@0 113 public Locator getSourceLocation() {
aoqi@0 114 return DOMLocator.getLocationInfo(e);
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 /** Gets the conversion name. */
aoqi@0 118 public String name() { return DOMUtil.getAttribute(e,"name"); }
aoqi@0 119
aoqi@0 120 /** Gets a transducer for this conversion. */
aoqi@0 121 public TypeUse getTransducer() {
aoqi@0 122
aoqi@0 123 String ws = DOMUtil.getAttribute(e,"whitespace");
aoqi@0 124 if(ws==null) ws = "collapse";
aoqi@0 125
aoqi@0 126 String type = DOMUtil.getAttribute(e,"type");
aoqi@0 127 if(type==null) type=name();
aoqi@0 128 JType t=null;
aoqi@0 129
aoqi@0 130 int idx = type.lastIndexOf('.');
aoqi@0 131 if(idx<0) {
aoqi@0 132 // no package name is specified.
aoqi@0 133 try {
aoqi@0 134 t = JPrimitiveType.parse(owner.codeModel,type);
aoqi@0 135 } catch( IllegalArgumentException ex ) {
aoqi@0 136 // otherwise treat it as a class name in the current package
aoqi@0 137 type = owner.getTargetPackage().name()+'.'+type;
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140 if(t==null) {
aoqi@0 141 try {
aoqi@0 142 // TODO: revisit this later
aoqi@0 143 JDefinedClass cls = owner.codeModel._class(type);
aoqi@0 144 cls.hide();
aoqi@0 145 t = cls;
aoqi@0 146 } catch( JClassAlreadyExistsException ex ) {
aoqi@0 147 t = ex.getExistingClass();
aoqi@0 148 }
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 String parse = DOMUtil.getAttribute(e,"parse");
aoqi@0 152 if(parse==null) parse="new";
aoqi@0 153
aoqi@0 154 String print = DOMUtil.getAttribute(e,"print");
aoqi@0 155 if(print==null) print="toString";
aoqi@0 156
aoqi@0 157 JDefinedClass adapter = generateAdapter(owner.codeModel, parse, print, t.boxify());
aoqi@0 158
aoqi@0 159 // XmlJavaType customization always converts between string and an user-defined type.
aoqi@0 160 return TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,new CAdapter(adapter));
aoqi@0 161 }
aoqi@0 162
aoqi@0 163 // TODO: anyway to reuse this code between XML Schema compiler?
aoqi@0 164 private JDefinedClass generateAdapter(JCodeModel cm, String parseMethod, String printMethod, JClass inMemoryType) {
aoqi@0 165 JDefinedClass adapter = null;
aoqi@0 166
aoqi@0 167 int id = 1;
aoqi@0 168 while(adapter==null) {
aoqi@0 169 try {
aoqi@0 170 JPackage pkg = owner.getTargetPackage();
aoqi@0 171 adapter = pkg._class("Adapter"+id);
aoqi@0 172 } catch (JClassAlreadyExistsException ex) {
aoqi@0 173 // try another name in search for an unique name.
aoqi@0 174 // this isn't too efficient, but we expect people to usually use
aoqi@0 175 // a very small number of adapters.
aoqi@0 176 id++;
aoqi@0 177 }
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 adapter._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(inMemoryType));
aoqi@0 181
aoqi@0 182 JMethod unmarshal = adapter.method(JMod.PUBLIC, inMemoryType, "unmarshal");
aoqi@0 183 JVar $value = unmarshal.param(String.class, "value");
aoqi@0 184
aoqi@0 185 JExpression inv;
aoqi@0 186
aoqi@0 187 if( parseMethod.equals("new") ) {
aoqi@0 188 // "new" indicates that the constructor of the target type
aoqi@0 189 // will do the unmarshalling.
aoqi@0 190
aoqi@0 191 // RESULT: new <type>()
aoqi@0 192 inv = JExpr._new(inMemoryType).arg($value);
aoqi@0 193 } else {
aoqi@0 194 int idx = parseMethod.lastIndexOf('.');
aoqi@0 195 if(idx<0) {
aoqi@0 196 // parseMethod specifies the static method of the target type
aoqi@0 197 // which will do the unmarshalling.
aoqi@0 198
aoqi@0 199 // because of an error check at the constructor,
aoqi@0 200 // we can safely assume that this cast works.
aoqi@0 201 inv = inMemoryType.staticInvoke(parseMethod).arg($value);
aoqi@0 202 } else {
aoqi@0 203 inv = JExpr.direct(parseMethod+"(value)");
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206 unmarshal.body()._return(inv);
aoqi@0 207
aoqi@0 208
aoqi@0 209 JMethod marshal = adapter.method(JMod.PUBLIC, String.class, "marshal");
aoqi@0 210 $value = marshal.param(inMemoryType,"value");
aoqi@0 211
aoqi@0 212 int idx = printMethod.lastIndexOf('.');
aoqi@0 213 if(idx<0) {
aoqi@0 214 // printMethod specifies a method in the target type
aoqi@0 215 // which performs the serialization.
aoqi@0 216
aoqi@0 217 // RESULT: <value>.<method>()
aoqi@0 218 inv = $value.invoke(printMethod);
aoqi@0 219 } else {
aoqi@0 220 // RESULT: <className>.<method>(<value>)
aoqi@0 221 inv = JExpr.direct(printMethod+"(value)");
aoqi@0 222 }
aoqi@0 223 marshal.body()._return(inv);
aoqi@0 224
aoqi@0 225 return adapter;
aoqi@0 226 }
aoqi@0 227 }

mercurial