|
1 /* |
|
2 * Copyright (c) 1997, 2011, 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 |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.tools.internal.xjc.model; |
|
27 |
|
28 import java.awt.*; |
|
29 import java.math.BigDecimal; |
|
30 import java.math.BigInteger; |
|
31 import java.util.HashMap; |
|
32 import java.util.Map; |
|
33 |
|
34 import javax.activation.DataHandler; |
|
35 import javax.activation.MimeType; |
|
36 import javax.xml.bind.DatatypeConverter; |
|
37 import javax.xml.bind.annotation.XmlIDREF; |
|
38 import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; |
|
39 import javax.xml.bind.annotation.adapters.HexBinaryAdapter; |
|
40 import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; |
|
41 import javax.xml.bind.annotation.adapters.XmlAdapter; |
|
42 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; |
|
43 import javax.xml.datatype.Duration; |
|
44 import javax.xml.datatype.XMLGregorianCalendar; |
|
45 import javax.xml.namespace.QName; |
|
46 import javax.xml.transform.Source; |
|
47 |
|
48 import com.sun.codemodel.internal.JExpr; |
|
49 import com.sun.codemodel.internal.JExpression; |
|
50 import com.sun.codemodel.internal.JType; |
|
51 import com.sun.tools.internal.xjc.model.nav.NClass; |
|
52 import com.sun.tools.internal.xjc.model.nav.NType; |
|
53 import com.sun.tools.internal.xjc.model.nav.NavigatorImpl; |
|
54 import com.sun.tools.internal.xjc.outline.Aspect; |
|
55 import com.sun.tools.internal.xjc.outline.Outline; |
|
56 import com.sun.tools.internal.xjc.runtime.ZeroOneBooleanAdapter; |
|
57 import com.sun.tools.internal.xjc.util.NamespaceContextAdapter; |
|
58 import com.sun.xml.internal.bind.DatatypeConverterImpl; |
|
59 import com.sun.xml.internal.bind.v2.WellKnownNamespace; |
|
60 import com.sun.xml.internal.bind.v2.model.core.ID; |
|
61 import com.sun.xml.internal.bind.v2.model.impl.BuiltinLeafInfoImpl; |
|
62 import com.sun.xml.internal.xsom.XSComponent; |
|
63 import com.sun.xml.internal.xsom.XmlString; |
|
64 |
|
65 import org.xml.sax.Locator; |
|
66 |
|
67 /** |
|
68 * Encapsulates the default handling for leaf classes (which are bound |
|
69 * to text in XML.) In particular this class knows how to convert |
|
70 * the lexical value into the Java class according to this default rule. |
|
71 * |
|
72 * <p> |
|
73 * This represents the spec-defined default handling for the Java |
|
74 * type ({@link #getType()}. |
|
75 * |
|
76 * <p> |
|
77 * For those Java classes (such as {@link String} or {@link Boolean}) |
|
78 * where the spec designates a specific default handling, there are |
|
79 * constants in this class (such as {@link #STRING} or {@link #BOOLEAN}.) |
|
80 * |
|
81 * <p> |
|
82 * The generated type-safe enum classes are also a leaf class, |
|
83 * and as such there are {@link CEnumLeafInfo} that represents it |
|
84 * as {@link CBuiltinLeafInfo}. |
|
85 * |
|
86 * <p> |
|
87 * This class represents the <b>default handling</b>, and therefore |
|
88 * we can only have one instance per one {@link NType}. Handling of |
|
89 * other XML Schema types (such as xs:token) are represented as |
|
90 * a general {@link TypeUse} objects. |
|
91 * |
|
92 * |
|
93 * @author Kohsuke Kawaguchi |
|
94 */ |
|
95 public abstract class CBuiltinLeafInfo extends BuiltinLeafInfoImpl<NType,NClass> implements CNonElement { |
|
96 |
|
97 private final ID id; |
|
98 |
|
99 // no derived class other than the spec-defined ones. definitely not for enum. |
|
100 private CBuiltinLeafInfo(NType typeToken, QName typeName, ID id) { |
|
101 super(typeToken,typeName); |
|
102 this.id = id; |
|
103 } |
|
104 |
|
105 /** |
|
106 * Gets the code model representation of this type. |
|
107 */ |
|
108 public JType toType(Outline o, Aspect aspect) { |
|
109 return getType().toType(o,aspect); |
|
110 } |
|
111 |
|
112 /** |
|
113 * Since {@link CBuiltinLeafInfo} represents a default binding, |
|
114 * it is never a collection. |
|
115 */ |
|
116 @Deprecated |
|
117 public final boolean isCollection() { |
|
118 return false; |
|
119 } |
|
120 |
|
121 /** |
|
122 * Guaranteed to return this. |
|
123 */ |
|
124 @Deprecated |
|
125 public CNonElement getInfo() { |
|
126 return this; |
|
127 } |
|
128 |
|
129 public ID idUse() { |
|
130 return id; |
|
131 } |
|
132 |
|
133 /** |
|
134 * {@link CBuiltinLeafInfo} never has a default associated MIME type. |
|
135 */ |
|
136 public MimeType getExpectedMimeType() { |
|
137 return null; |
|
138 } |
|
139 |
|
140 @Deprecated |
|
141 public final CAdapter getAdapterUse() { |
|
142 return null; |
|
143 } |
|
144 |
|
145 public Locator getLocator() { |
|
146 return Model.EMPTY_LOCATOR; |
|
147 } |
|
148 |
|
149 public final XSComponent getSchemaComponent() { |
|
150 throw new UnsupportedOperationException("TODO. If you hit this, let us know."); |
|
151 } |
|
152 |
|
153 /** |
|
154 * Creates a {@link TypeUse} that represents a collection of this {@link CBuiltinLeafInfo}. |
|
155 */ |
|
156 public final TypeUse makeCollection() { |
|
157 return TypeUseFactory.makeCollection(this); |
|
158 } |
|
159 |
|
160 /** |
|
161 * Creates a {@link TypeUse} that represents an adapted use of this {@link CBuiltinLeafInfo}. |
|
162 */ |
|
163 public final TypeUse makeAdapted( Class<? extends XmlAdapter> adapter, boolean copy ) { |
|
164 return TypeUseFactory.adapt(this,adapter,copy); |
|
165 } |
|
166 |
|
167 /** |
|
168 * Creates a {@link TypeUse} that represents a MIME-type assocaited version of this {@link CBuiltinLeafInfo}. |
|
169 */ |
|
170 public final TypeUse makeMimeTyped( MimeType mt ) { |
|
171 return TypeUseFactory.makeMimeTyped(this,mt); |
|
172 } |
|
173 |
|
174 /** |
|
175 * {@link CBuiltinLeafInfo} for Java classes that have |
|
176 * the spec defined built-in binding semantics. |
|
177 */ |
|
178 private static abstract class Builtin extends CBuiltinLeafInfo { |
|
179 protected Builtin(Class c, String typeName) { |
|
180 this(c,typeName,com.sun.xml.internal.bind.v2.model.core.ID.NONE); |
|
181 } |
|
182 protected Builtin(Class c, String typeName, ID id) { |
|
183 super(NavigatorImpl.theInstance.ref(c), new QName(WellKnownNamespace.XML_SCHEMA,typeName),id); |
|
184 LEAVES.put(getType(),this); |
|
185 } |
|
186 |
|
187 /** |
|
188 * No vendor customization in the built-in classes. |
|
189 */ |
|
190 public CCustomizations getCustomizations() { |
|
191 return CCustomizations.EMPTY; |
|
192 } |
|
193 } |
|
194 |
|
195 private static final class NoConstantBuiltin extends Builtin { |
|
196 public NoConstantBuiltin(Class c, String typeName) { |
|
197 super(c, typeName); |
|
198 } |
|
199 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
200 return null; |
|
201 } |
|
202 } |
|
203 |
|
204 /** |
|
205 * All built-in leaves. |
|
206 */ |
|
207 public static final Map<NType,CBuiltinLeafInfo> LEAVES = new HashMap<NType,CBuiltinLeafInfo>(); |
|
208 |
|
209 |
|
210 public static final CBuiltinLeafInfo ANYTYPE = new NoConstantBuiltin(Object.class,"anyType"); |
|
211 public static final CBuiltinLeafInfo STRING = new Builtin(String.class,"string") { |
|
212 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
213 return JExpr.lit(lexical.value); |
|
214 } |
|
215 }; |
|
216 public static final CBuiltinLeafInfo BOOLEAN = new Builtin(Boolean.class,"boolean") { |
|
217 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
218 return JExpr.lit(DatatypeConverterImpl._parseBoolean(lexical.value)); |
|
219 } |
|
220 }; |
|
221 public static final CBuiltinLeafInfo INT = new Builtin(Integer.class,"int") { |
|
222 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
223 return JExpr.lit(DatatypeConverterImpl._parseInt(lexical.value)); |
|
224 } |
|
225 }; |
|
226 public static final CBuiltinLeafInfo LONG = new Builtin(Long.class,"long") { |
|
227 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
228 return JExpr.lit(DatatypeConverterImpl._parseLong(lexical.value)); |
|
229 } |
|
230 }; |
|
231 public static final CBuiltinLeafInfo BYTE = new Builtin(Byte.class,"byte") { |
|
232 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
233 return JExpr.cast( |
|
234 outline.getCodeModel().BYTE, |
|
235 JExpr.lit(DatatypeConverterImpl._parseByte(lexical.value))); |
|
236 } |
|
237 }; |
|
238 public static final CBuiltinLeafInfo SHORT = new Builtin(Short.class,"short") { |
|
239 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
240 return JExpr.cast( |
|
241 outline.getCodeModel().SHORT, |
|
242 JExpr.lit(DatatypeConverterImpl._parseShort(lexical.value))); |
|
243 } |
|
244 }; |
|
245 public static final CBuiltinLeafInfo FLOAT = new Builtin(Float.class,"float") { |
|
246 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
247 return JExpr.lit(DatatypeConverterImpl._parseFloat(lexical.value)); |
|
248 } |
|
249 }; |
|
250 public static final CBuiltinLeafInfo DOUBLE = new Builtin(Double.class,"double") { |
|
251 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
252 return JExpr.lit(DatatypeConverterImpl._parseDouble(lexical.value)); |
|
253 } |
|
254 }; |
|
255 public static final CBuiltinLeafInfo QNAME = new Builtin(QName.class,"QName") { |
|
256 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
257 QName qn = DatatypeConverterImpl._parseQName(lexical.value,new NamespaceContextAdapter(lexical)); |
|
258 return JExpr._new(outline.getCodeModel().ref(QName.class)) |
|
259 .arg(qn.getNamespaceURI()) |
|
260 .arg(qn.getLocalPart()) |
|
261 .arg(qn.getPrefix()); |
|
262 } |
|
263 }; |
|
264 // XMLGregorianCalendar is mutable, so we can't support default values anyhow. |
|
265 // For CALENAR we are uses a most unlikely name so as to avoid potential name |
|
266 // conflicts in the furture. |
|
267 public static final CBuiltinLeafInfo CALENDAR = new NoConstantBuiltin(XMLGregorianCalendar.class,"\u0000"); |
|
268 public static final CBuiltinLeafInfo DURATION = new NoConstantBuiltin(Duration.class,"duration"); |
|
269 |
|
270 public static final CBuiltinLeafInfo BIG_INTEGER = new Builtin(BigInteger.class,"integer") { |
|
271 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
272 return JExpr._new(outline.getCodeModel().ref(BigInteger.class)).arg(lexical.value.trim()); |
|
273 } |
|
274 }; |
|
275 |
|
276 public static final CBuiltinLeafInfo BIG_DECIMAL = new Builtin(BigDecimal.class,"decimal") { |
|
277 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
278 return JExpr._new(outline.getCodeModel().ref(BigDecimal.class)).arg(lexical.value.trim()); |
|
279 } |
|
280 }; |
|
281 |
|
282 public static final CBuiltinLeafInfo BASE64_BYTE_ARRAY = new Builtin(byte[].class,"base64Binary") { |
|
283 public JExpression createConstant(Outline outline, XmlString lexical) { |
|
284 return outline.getCodeModel().ref(DatatypeConverter.class).staticInvoke("parseBase64Binary").arg(lexical.value); |
|
285 } |
|
286 }; |
|
287 |
|
288 public static final CBuiltinLeafInfo DATA_HANDLER = new NoConstantBuiltin(DataHandler.class,"base64Binary"); |
|
289 public static final CBuiltinLeafInfo IMAGE = new NoConstantBuiltin(Image.class,"base64Binary"); |
|
290 public static final CBuiltinLeafInfo XML_SOURCE = new NoConstantBuiltin(Source.class,"base64Binary"); |
|
291 |
|
292 public static final TypeUse HEXBIN_BYTE_ARRAY = |
|
293 STRING.makeAdapted(HexBinaryAdapter.class,false); |
|
294 |
|
295 |
|
296 // TODO: not sure if they should belong here, |
|
297 // but I couldn't find other places that fit. |
|
298 public static final TypeUse TOKEN = |
|
299 STRING.makeAdapted(CollapsedStringAdapter.class,false); |
|
300 |
|
301 public static final TypeUse NORMALIZED_STRING = |
|
302 STRING.makeAdapted(NormalizedStringAdapter.class,false); |
|
303 |
|
304 public static final TypeUse ID = TypeUseFactory.makeID(TOKEN,com.sun.xml.internal.bind.v2.model.core.ID.ID); |
|
305 |
|
306 /** |
|
307 * boolean restricted to 0 or 1. |
|
308 */ |
|
309 public static final TypeUse BOOLEAN_ZERO_OR_ONE = |
|
310 STRING.makeAdapted(ZeroOneBooleanAdapter.class,true); |
|
311 |
|
312 /** |
|
313 * IDREF. |
|
314 * |
|
315 * IDREF is has a whitespace normalization semantics of token, but |
|
316 * we don't want {@link XmlJavaTypeAdapter} and {@link XmlIDREF} to interact. |
|
317 */ |
|
318 public static final TypeUse IDREF = TypeUseFactory.makeID(ANYTYPE,com.sun.xml.internal.bind.v2.model.core.ID.IDREF); |
|
319 |
|
320 /** |
|
321 * For all list of strings, such as NMTOKENS, ENTITIES. |
|
322 */ |
|
323 public static final TypeUse STRING_LIST = |
|
324 STRING.makeCollection(); |
|
325 } |