Tue, 09 Apr 2013 14:51:13 +0100
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 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, 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.reader.dtd.bindinfo; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.io.IOException; |
ohair@286 | 29 | import java.io.StringReader; |
ohair@286 | 30 | import java.util.Map; |
ohair@286 | 31 | |
ohair@286 | 32 | import javax.xml.bind.annotation.adapters.XmlAdapter; |
ohair@286 | 33 | import javax.xml.parsers.DocumentBuilderFactory; |
ohair@286 | 34 | import javax.xml.parsers.ParserConfigurationException; |
ohair@286 | 35 | |
ohair@286 | 36 | import com.sun.codemodel.internal.JClass; |
ohair@286 | 37 | import com.sun.codemodel.internal.JClassAlreadyExistsException; |
ohair@286 | 38 | import com.sun.codemodel.internal.JCodeModel; |
ohair@286 | 39 | import com.sun.codemodel.internal.JDefinedClass; |
ohair@286 | 40 | import com.sun.codemodel.internal.JExpr; |
ohair@286 | 41 | import com.sun.codemodel.internal.JExpression; |
ohair@286 | 42 | import com.sun.codemodel.internal.JMethod; |
ohair@286 | 43 | import com.sun.codemodel.internal.JMod; |
ohair@286 | 44 | import com.sun.codemodel.internal.JPackage; |
ohair@286 | 45 | import com.sun.codemodel.internal.JPrimitiveType; |
ohair@286 | 46 | import com.sun.codemodel.internal.JType; |
ohair@286 | 47 | import com.sun.codemodel.internal.JVar; |
ohair@286 | 48 | import com.sun.tools.internal.xjc.model.CAdapter; |
ohair@286 | 49 | import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo; |
ohair@286 | 50 | import com.sun.tools.internal.xjc.model.TypeUse; |
ohair@286 | 51 | import com.sun.tools.internal.xjc.model.TypeUseFactory; |
ohair@286 | 52 | |
alanb@368 | 53 | import com.sun.xml.internal.bind.v2.util.XmlFactory; |
ohair@286 | 54 | import org.w3c.dom.Element; |
ohair@286 | 55 | import org.xml.sax.InputSource; |
ohair@286 | 56 | import org.xml.sax.Locator; |
ohair@286 | 57 | import org.xml.sax.SAXException; |
ohair@286 | 58 | |
ohair@286 | 59 | /** |
ohair@286 | 60 | * <conversion> declaration in the binding file. |
ohair@286 | 61 | * This declaration declares a conversion by user-specified methods. |
ohair@286 | 62 | */ |
ohair@286 | 63 | public class BIUserConversion implements BIConversion |
ohair@286 | 64 | { |
ohair@286 | 65 | /** |
ohair@286 | 66 | * Wraps a given <conversion> element in the binding file. |
ohair@286 | 67 | */ |
ohair@286 | 68 | BIUserConversion( BindInfo bi, Element _e ) { |
ohair@286 | 69 | this.owner = bi; |
ohair@286 | 70 | this.e = _e; |
ohair@286 | 71 | } |
ohair@286 | 72 | |
ohair@286 | 73 | private static void add( Map<String,BIConversion> m, BIConversion c ) { |
ohair@286 | 74 | m.put( c.name(), c ); |
ohair@286 | 75 | } |
ohair@286 | 76 | |
ohair@286 | 77 | /** Adds all built-in conversions into the given map. */ |
ohair@286 | 78 | static void addBuiltinConversions( BindInfo bi, Map<String,BIConversion> m ) { |
ohair@286 | 79 | add( m, new BIUserConversion( bi, parse("<conversion name='boolean' type='java.lang.Boolean' parse='getBoolean' />"))); |
ohair@286 | 80 | add( m, new BIUserConversion( bi, parse("<conversion name='byte' type='java.lang.Byte' parse='parseByte' />"))); |
ohair@286 | 81 | add( m, new BIUserConversion( bi, parse("<conversion name='short' type='java.lang.Short' parse='parseShort' />"))); |
ohair@286 | 82 | add( m, new BIUserConversion( bi, parse("<conversion name='int' type='java.lang.Integer' parse='parseInt' />"))); |
ohair@286 | 83 | add( m, new BIUserConversion( bi, parse("<conversion name='long' type='java.lang.Long' parse='parseLong' />"))); |
ohair@286 | 84 | add( m, new BIUserConversion( bi, parse("<conversion name='float' type='java.lang.Float' parse='parseFloat' />"))); |
ohair@286 | 85 | add( m, new BIUserConversion( bi, parse("<conversion name='double' type='java.lang.Double' parse='parseDouble' />"))); |
ohair@286 | 86 | } |
ohair@286 | 87 | |
ohair@286 | 88 | private static Element parse(String text) { |
ohair@286 | 89 | try { |
alanb@368 | 90 | //this is parsing well known schemas, do not configure secure processing - always true |
alanb@368 | 91 | DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(false); |
ohair@286 | 92 | InputSource is = new InputSource(new StringReader(text)); |
ohair@286 | 93 | return dbf.newDocumentBuilder().parse(is).getDocumentElement(); |
ohair@286 | 94 | } catch (SAXException x) { |
ohair@286 | 95 | throw new Error(x); |
ohair@286 | 96 | } catch (IOException x) { |
ohair@286 | 97 | throw new Error(x); |
ohair@286 | 98 | } catch (ParserConfigurationException x) { |
ohair@286 | 99 | throw new Error(x); |
ohair@286 | 100 | } |
ohair@286 | 101 | } |
ohair@286 | 102 | |
ohair@286 | 103 | |
ohair@286 | 104 | /** The owner {@link BindInfo} object to which this object belongs. */ |
ohair@286 | 105 | private final BindInfo owner; |
ohair@286 | 106 | |
ohair@286 | 107 | /** <conversion> element which this object is wrapping. */ |
ohair@286 | 108 | private final Element e; |
ohair@286 | 109 | |
ohair@286 | 110 | |
ohair@286 | 111 | |
ohair@286 | 112 | /** Gets the location where this declaration is declared. */ |
ohair@286 | 113 | public Locator getSourceLocation() { |
ohair@286 | 114 | return DOMLocator.getLocationInfo(e); |
ohair@286 | 115 | } |
ohair@286 | 116 | |
ohair@286 | 117 | /** Gets the conversion name. */ |
ohair@286 | 118 | public String name() { return DOMUtil.getAttribute(e,"name"); } |
ohair@286 | 119 | |
ohair@286 | 120 | /** Gets a transducer for this conversion. */ |
ohair@286 | 121 | public TypeUse getTransducer() { |
ohair@286 | 122 | |
ohair@286 | 123 | String ws = DOMUtil.getAttribute(e,"whitespace"); |
ohair@286 | 124 | if(ws==null) ws = "collapse"; |
ohair@286 | 125 | |
ohair@286 | 126 | String type = DOMUtil.getAttribute(e,"type"); |
ohair@286 | 127 | if(type==null) type=name(); |
ohair@286 | 128 | JType t=null; |
ohair@286 | 129 | |
ohair@286 | 130 | int idx = type.lastIndexOf('.'); |
ohair@286 | 131 | if(idx<0) { |
ohair@286 | 132 | // no package name is specified. |
ohair@286 | 133 | try { |
ohair@286 | 134 | t = JPrimitiveType.parse(owner.codeModel,type); |
alanb@368 | 135 | } catch( IllegalArgumentException ex ) { |
ohair@286 | 136 | // otherwise treat it as a class name in the current package |
ohair@286 | 137 | type = owner.getTargetPackage().name()+'.'+type; |
ohair@286 | 138 | } |
ohair@286 | 139 | } |
ohair@286 | 140 | if(t==null) { |
ohair@286 | 141 | try { |
ohair@286 | 142 | // TODO: revisit this later |
ohair@286 | 143 | JDefinedClass cls = owner.codeModel._class(type); |
ohair@286 | 144 | cls.hide(); |
ohair@286 | 145 | t = cls; |
alanb@368 | 146 | } catch( JClassAlreadyExistsException ex ) { |
alanb@368 | 147 | t = ex.getExistingClass(); |
ohair@286 | 148 | } |
ohair@286 | 149 | } |
ohair@286 | 150 | |
ohair@286 | 151 | String parse = DOMUtil.getAttribute(e,"parse"); |
ohair@286 | 152 | if(parse==null) parse="new"; |
ohair@286 | 153 | |
ohair@286 | 154 | String print = DOMUtil.getAttribute(e,"print"); |
ohair@286 | 155 | if(print==null) print="toString"; |
ohair@286 | 156 | |
ohair@286 | 157 | JDefinedClass adapter = generateAdapter(owner.codeModel, parse, print, t.boxify()); |
ohair@286 | 158 | |
ohair@286 | 159 | // XmlJavaType customization always converts between string and an user-defined type. |
ohair@286 | 160 | return TypeUseFactory.adapt(CBuiltinLeafInfo.STRING,new CAdapter(adapter)); |
ohair@286 | 161 | } |
ohair@286 | 162 | |
ohair@286 | 163 | // TODO: anyway to reuse this code between XML Schema compiler? |
ohair@286 | 164 | private JDefinedClass generateAdapter(JCodeModel cm, String parseMethod, String printMethod, JClass inMemoryType) { |
ohair@286 | 165 | JDefinedClass adapter = null; |
ohair@286 | 166 | |
ohair@286 | 167 | int id = 1; |
ohair@286 | 168 | while(adapter==null) { |
ohair@286 | 169 | try { |
ohair@286 | 170 | JPackage pkg = owner.getTargetPackage(); |
ohair@286 | 171 | adapter = pkg._class("Adapter"+id); |
alanb@368 | 172 | } catch (JClassAlreadyExistsException ex) { |
ohair@286 | 173 | // try another name in search for an unique name. |
ohair@286 | 174 | // this isn't too efficient, but we expect people to usually use |
ohair@286 | 175 | // a very small number of adapters. |
ohair@286 | 176 | id++; |
ohair@286 | 177 | } |
ohair@286 | 178 | } |
ohair@286 | 179 | |
ohair@286 | 180 | adapter._extends(cm.ref(XmlAdapter.class).narrow(String.class).narrow(inMemoryType)); |
ohair@286 | 181 | |
ohair@286 | 182 | JMethod unmarshal = adapter.method(JMod.PUBLIC, inMemoryType, "unmarshal"); |
ohair@286 | 183 | JVar $value = unmarshal.param(String.class, "value"); |
ohair@286 | 184 | |
ohair@286 | 185 | JExpression inv; |
ohair@286 | 186 | |
ohair@286 | 187 | if( parseMethod.equals("new") ) { |
ohair@286 | 188 | // "new" indicates that the constructor of the target type |
ohair@286 | 189 | // will do the unmarshalling. |
ohair@286 | 190 | |
ohair@286 | 191 | // RESULT: new <type>() |
ohair@286 | 192 | inv = JExpr._new(inMemoryType).arg($value); |
ohair@286 | 193 | } else { |
ohair@286 | 194 | int idx = parseMethod.lastIndexOf('.'); |
ohair@286 | 195 | if(idx<0) { |
ohair@286 | 196 | // parseMethod specifies the static method of the target type |
ohair@286 | 197 | // which will do the unmarshalling. |
ohair@286 | 198 | |
ohair@286 | 199 | // because of an error check at the constructor, |
ohair@286 | 200 | // we can safely assume that this cast works. |
ohair@286 | 201 | inv = inMemoryType.staticInvoke(parseMethod).arg($value); |
ohair@286 | 202 | } else { |
ohair@286 | 203 | inv = JExpr.direct(parseMethod+"(value)"); |
ohair@286 | 204 | } |
ohair@286 | 205 | } |
ohair@286 | 206 | unmarshal.body()._return(inv); |
ohair@286 | 207 | |
ohair@286 | 208 | |
ohair@286 | 209 | JMethod marshal = adapter.method(JMod.PUBLIC, String.class, "marshal"); |
ohair@286 | 210 | $value = marshal.param(inMemoryType,"value"); |
ohair@286 | 211 | |
ohair@286 | 212 | int idx = printMethod.lastIndexOf('.'); |
ohair@286 | 213 | if(idx<0) { |
ohair@286 | 214 | // printMethod specifies a method in the target type |
ohair@286 | 215 | // which performs the serialization. |
ohair@286 | 216 | |
ohair@286 | 217 | // RESULT: <value>.<method>() |
ohair@286 | 218 | inv = $value.invoke(printMethod); |
ohair@286 | 219 | } else { |
ohair@286 | 220 | // RESULT: <className>.<method>(<value>) |
ohair@286 | 221 | inv = JExpr.direct(printMethod+"(value)"); |
ohair@286 | 222 | } |
ohair@286 | 223 | marshal.body()._return(inv); |
ohair@286 | 224 | |
ohair@286 | 225 | return adapter; |
ohair@286 | 226 | } |
ohair@286 | 227 | } |