1 /*
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
40 import com.sun.codemodel.internal.JExpr;
41 import com.sun.codemodel.internal.JExpression;
42 import com.sun.codemodel.internal.JMethod;
43 import com.sun.codemodel.internal.JMod;
44 import com.sun.codemodel.internal.JPackage;
45 import com.sun.codemodel.internal.JPrimitiveType;
46 import com.sun.codemodel.internal.JType;
47 import com.sun.codemodel.internal.JVar;
48 import com.sun.tools.internal.xjc.model.CAdapter;
49 import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
50 import com.sun.tools.internal.xjc.model.TypeUse;
51 import com.sun.tools.internal.xjc.model.TypeUseFactory;
52
53 import com.sun.xml.internal.bind.v2.util.XmlFactory;
54 import org.w3c.dom.Element;
55 import org.xml.sax.InputSource;
56 import org.xml.sax.Locator;
57 import org.xml.sax.SAXException;
58
59 /**
60 * <conversion> declaration in the binding file.
61 * This declaration declares a conversion by user-specified methods.
62 */
63 public class BIUserConversion implements BIConversion
64 {
65 /**
66 * Wraps a given <conversion> element in the binding file.
67 */
68 BIUserConversion( BindInfo bi, Element _e ) {
69 this.owner = bi;
70 this.e = _e;
71 }
72
73 private static void add( Map<String,BIConversion> m, BIConversion c ) {
74 m.put( c.name(), c );
75 }
76
77 /** Adds all built-in conversions into the given map. */
78 static void addBuiltinConversions( BindInfo bi, Map<String,BIConversion> m ) {
79 add( m, new BIUserConversion( bi, parse("<conversion name='boolean' type='java.lang.Boolean' parse='getBoolean' />")));
80 add( m, new BIUserConversion( bi, parse("<conversion name='byte' type='java.lang.Byte' parse='parseByte' />")));
81 add( m, new BIUserConversion( bi, parse("<conversion name='short' type='java.lang.Short' parse='parseShort' />")));
82 add( m, new BIUserConversion( bi, parse("<conversion name='int' type='java.lang.Integer' parse='parseInt' />")));
83 add( m, new BIUserConversion( bi, parse("<conversion name='long' type='java.lang.Long' parse='parseLong' />")));
84 add( m, new BIUserConversion( bi, parse("<conversion name='float' type='java.lang.Float' parse='parseFloat' />")));
85 add( m, new BIUserConversion( bi, parse("<conversion name='double' type='java.lang.Double' parse='parseDouble' />")));
86 }
87
88 private static Element parse(String text) {
89 try {
90 //this is parsing well known schemas, do not configure secure processing - always true
91 DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(false);
92 InputSource is = new InputSource(new StringReader(text));
93 return dbf.newDocumentBuilder().parse(is).getDocumentElement();
94 } catch (SAXException x) {
95 throw new Error(x);
96 } catch (IOException x) {
97 throw new Error(x);
98 } catch (ParserConfigurationException x) {
99 throw new Error(x);
100 }
101 }
102
103
104 /** The owner {@link BindInfo} object to which this object belongs. */
105 private final BindInfo owner;
106
107 /** <conversion> element which this object is wrapping. */
108 private final Element e;
109
110
111
112 /** Gets the location where this declaration is declared. */
113 public Locator getSourceLocation() {
114 return DOMLocator.getLocationInfo(e);
115 }
116
117 /** Gets the conversion name. */
118 public String name() { return DOMUtil.getAttribute(e,"name"); }
119
120 /** Gets a transducer for this conversion. */
121 public TypeUse getTransducer() {
122
123 String ws = DOMUtil.getAttribute(e,"whitespace");
124 if(ws==null) ws = "collapse";
125
126 String type = DOMUtil.getAttribute(e,"type");
127 if(type==null) type=name();
|
1 /*
2 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
40 import com.sun.codemodel.internal.JExpr;
41 import com.sun.codemodel.internal.JExpression;
42 import com.sun.codemodel.internal.JMethod;
43 import com.sun.codemodel.internal.JMod;
44 import com.sun.codemodel.internal.JPackage;
45 import com.sun.codemodel.internal.JPrimitiveType;
46 import com.sun.codemodel.internal.JType;
47 import com.sun.codemodel.internal.JVar;
48 import com.sun.tools.internal.xjc.model.CAdapter;
49 import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
50 import com.sun.tools.internal.xjc.model.TypeUse;
51 import com.sun.tools.internal.xjc.model.TypeUseFactory;
52
53 import com.sun.xml.internal.bind.v2.util.XmlFactory;
54 import org.w3c.dom.Element;
55 import org.xml.sax.InputSource;
56 import org.xml.sax.Locator;
57 import org.xml.sax.SAXException;
58
59 /**
60 * {@code <conversion>} declaration in the binding file.
61 * This declaration declares a conversion by user-specified methods.
62 */
63 public class BIUserConversion implements BIConversion
64 {
65 /**
66 * Wraps a given {@code <conversion>} element in the binding file.
67 */
68 BIUserConversion( BindInfo bi, Element _e ) {
69 this.owner = bi;
70 this.e = _e;
71 }
72
73 private static void add( Map<String,BIConversion> m, BIConversion c ) {
74 m.put( c.name(), c );
75 }
76
77 /** Adds all built-in conversions into the given map. */
78 static void addBuiltinConversions( BindInfo bi, Map<String,BIConversion> m ) {
79 add( m, new BIUserConversion( bi, parse("<conversion name='boolean' type='java.lang.Boolean' parse='getBoolean' />")));
80 add( m, new BIUserConversion( bi, parse("<conversion name='byte' type='java.lang.Byte' parse='parseByte' />")));
81 add( m, new BIUserConversion( bi, parse("<conversion name='short' type='java.lang.Short' parse='parseShort' />")));
82 add( m, new BIUserConversion( bi, parse("<conversion name='int' type='java.lang.Integer' parse='parseInt' />")));
83 add( m, new BIUserConversion( bi, parse("<conversion name='long' type='java.lang.Long' parse='parseLong' />")));
84 add( m, new BIUserConversion( bi, parse("<conversion name='float' type='java.lang.Float' parse='parseFloat' />")));
85 add( m, new BIUserConversion( bi, parse("<conversion name='double' type='java.lang.Double' parse='parseDouble' />")));
86 }
87
88 private static Element parse(String text) {
89 try {
90 //this is parsing well known schemas, do not configure secure processing - always true
91 DocumentBuilderFactory dbf = XmlFactory.createDocumentBuilderFactory(false);
92 InputSource is = new InputSource(new StringReader(text));
93 return dbf.newDocumentBuilder().parse(is).getDocumentElement();
94 } catch (SAXException x) {
95 throw new Error(x);
96 } catch (IOException x) {
97 throw new Error(x);
98 } catch (ParserConfigurationException x) {
99 throw new Error(x);
100 }
101 }
102
103
104 /** The owner {@link BindInfo} object to which this object belongs. */
105 private final BindInfo owner;
106
107 /** {@code <conversion>} element which this object is wrapping. */
108 private final Element e;
109
110
111
112 /** Gets the location where this declaration is declared. */
113 public Locator getSourceLocation() {
114 return DOMLocator.getLocationInfo(e);
115 }
116
117 /** Gets the conversion name. */
118 public String name() { return DOMUtil.getAttribute(e,"name"); }
119
120 /** Gets a transducer for this conversion. */
121 public TypeUse getTransducer() {
122
123 String ws = DOMUtil.getAttribute(e,"whitespace");
124 if(ws==null) ws = "collapse";
125
126 String type = DOMUtil.getAttribute(e,"type");
127 if(type==null) type=name();
|