author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 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.reader.dtd; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.util.Collections; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.Map; |
|
32 |
import java.util.Stack; |
|
33 |
||
34 |
import javax.xml.namespace.QName; |
|
35 |
||
36 |
import com.sun.codemodel.internal.JClass; |
|
37 |
import com.sun.codemodel.internal.JCodeModel; |
|
38 |
import com.sun.codemodel.internal.JDefinedClass; |
|
39 |
import com.sun.codemodel.internal.JPackage; |
|
40 |
import com.sun.tools.internal.xjc.AbortException; |
|
41 |
import com.sun.tools.internal.xjc.ErrorReceiver; |
|
42 |
import com.sun.tools.internal.xjc.Options; |
|
43 |
import com.sun.tools.internal.xjc.model.CAttributePropertyInfo; |
|
44 |
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo; |
|
45 |
import com.sun.tools.internal.xjc.model.CClassInfo; |
|
46 |
import com.sun.tools.internal.xjc.model.CPropertyInfo; |
|
47 |
import com.sun.tools.internal.xjc.model.Model; |
|
48 |
import com.sun.tools.internal.xjc.model.TypeUse; |
|
49 |
import com.sun.tools.internal.xjc.model.TypeUseFactory; |
|
50 |
import com.sun.tools.internal.xjc.model.CDefaultValue; |
|
51 |
import com.sun.tools.internal.xjc.reader.ModelChecker; |
|
52 |
import com.sun.tools.internal.xjc.reader.Ring; |
|
53 |
import com.sun.tools.internal.xjc.reader.dtd.bindinfo.BIAttribute; |
|
54 |
import com.sun.tools.internal.xjc.reader.dtd.bindinfo.BIElement; |
|
55 |
import com.sun.tools.internal.xjc.reader.dtd.bindinfo.BIInterface; |
|
56 |
import com.sun.tools.internal.xjc.reader.dtd.bindinfo.BindInfo; |
|
57 |
import com.sun.tools.internal.xjc.util.CodeModelClassFactory; |
|
58 |
import com.sun.tools.internal.xjc.util.ErrorReceiverFilter; |
|
59 |
import com.sun.xml.internal.bind.api.impl.NameConverter; |
|
60 |
import com.sun.xml.internal.dtdparser.DTDHandlerBase; |
|
61 |
import com.sun.xml.internal.dtdparser.DTDParser; |
|
62 |
import com.sun.xml.internal.dtdparser.InputEntity; |
|
63 |
import com.sun.xml.internal.xsom.XmlString; |
|
64 |
import com.sun.istack.internal.SAXParseException2; |
|
65 |
||
66 |
import org.xml.sax.EntityResolver; |
|
67 |
import org.xml.sax.InputSource; |
|
68 |
import org.xml.sax.Locator; |
|
69 |
import org.xml.sax.SAXException; |
|
70 |
import org.xml.sax.SAXParseException; |
|
71 |
import org.xml.sax.helpers.LocatorImpl; |
|
72 |
||
73 |
/** |
|
74 |
* Parses DTD grammar along with binding information into BGM. |
|
75 |
* |
|
76 |
* @author |
|
77 |
* <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a> |
|
78 |
*/ |
|
79 |
public class TDTDReader extends DTDHandlerBase |
|
80 |
{ |
|
81 |
/** |
|
82 |
* Parses DTD grammar and a binding information into BGM. |
|
83 |
* |
|
84 |
* <p> |
|
85 |
* This method is just a utility method that covers 80% of the use |
|
86 |
* cases. |
|
87 |
* |
|
88 |
* @param bindingInfo |
|
89 |
* binding information file, if any. Can be null. |
|
90 |
*/ |
|
91 |
public static Model parse( |
|
92 |
InputSource dtd, |
|
93 |
InputSource bindingInfo, |
|
94 |
ErrorReceiver errorReceiver, |
|
95 |
Options opts) { |
|
96 |
||
97 |
try { |
|
98 |
// set up a ring |
|
99 |
final Ring old = Ring.begin(); |
|
100 |
try { |
|
101 |
ErrorReceiverFilter ef = new ErrorReceiverFilter(errorReceiver); |
|
102 |
||
103 |
JCodeModel cm = new JCodeModel(); |
|
104 |
Model model = new Model(opts,cm,NameConverter.standard,opts.classNameAllocator,null); |
|
105 |
||
106 |
Ring.add(cm); |
|
107 |
Ring.add(model); |
|
108 |
Ring.add(ErrorReceiver.class,ef); |
|
109 |
||
110 |
TDTDReader reader = new TDTDReader( ef, opts, bindingInfo); |
|
111 |
||
112 |
DTDParser parser = new DTDParser(); |
|
113 |
parser.setDtdHandler(reader); |
|
114 |
if( opts.entityResolver!=null ) |
|
115 |
parser.setEntityResolver(opts.entityResolver); |
|
116 |
||
117 |
try { |
|
118 |
parser.parse(dtd); |
|
119 |
} catch (SAXParseException e) { |
|
120 |
return null; // this error was already handled by GrammarReaderController |
|
121 |
} |
|
122 |
||
123 |
Ring.get(ModelChecker.class).check(); |
|
124 |
||
125 |
if(ef.hadError()) return null; |
|
126 |
else return model; |
|
127 |
} finally { |
|
128 |
Ring.end(old); |
|
129 |
} |
|
130 |
} catch (IOException e) { |
|
131 |
errorReceiver.error(new SAXParseException2(e.getMessage(),null,e)); |
|
132 |
return null; |
|
133 |
} catch (SAXException e) { |
|
134 |
errorReceiver.error(new SAXParseException2(e.getMessage(),null,e)); |
|
135 |
return null; |
|
136 |
} catch (AbortException e) { |
|
137 |
// parsing was aborted but the error was already reported |
|
138 |
return null; |
|
139 |
} |
|
140 |
} |
|
141 |
protected TDTDReader(ErrorReceiver errorReceiver, Options opts, InputSource _bindInfo) |
|
142 |
throws AbortException { |
|
143 |
this.entityResolver = opts.entityResolver; |
|
144 |
this.errorReceiver = new ErrorReceiverFilter(errorReceiver); |
|
145 |
bindInfo = new BindInfo(model,_bindInfo, this.errorReceiver); |
|
146 |
classFactory = new CodeModelClassFactory(errorReceiver); |
|
147 |
} |
|
148 |
||
149 |
private final EntityResolver entityResolver; |
|
150 |
||
151 |
/** |
|
152 |
* binding information. |
|
153 |
* |
|
154 |
* <p> |
|
155 |
* This is always non-null even if no binding information was specified. |
|
156 |
* (In that case, a dummy object will be provided.) |
|
157 |
*/ |
|
158 |
final BindInfo bindInfo; |
|
159 |
||
160 |
final Model model = Ring.get(Model.class); |
|
161 |
||
162 |
private final CodeModelClassFactory classFactory; |
|
163 |
||
164 |
private final ErrorReceiverFilter errorReceiver; |
|
165 |
||
166 |
/** |
|
167 |
* Element name to its content model definition. |
|
168 |
*/ |
|
169 |
private final Map<String,Element> elements = new HashMap<String,Element>(); |
|
170 |
||
171 |
||
172 |
public void startDTD(InputEntity entity) throws SAXException { |
|
173 |
} |
|
174 |
||
175 |
public void endDTD() throws SAXException { |
|
176 |
||
177 |
// bind them all. |
|
178 |
// we need to know how elements are referencing each other before we do this, |
|
179 |
// so this can be only done at the endDTD method |
|
180 |
for( Element e : elements.values() ) |
|
181 |
e.bind(); |
|
182 |
||
183 |
// if there was an error by now, just abort. |
|
184 |
if (errorReceiver.hadError()) |
|
185 |
return; |
|
186 |
||
187 |
processInterfaceDeclarations(); |
|
188 |
||
189 |
// check XJC extensions and realize them |
|
190 |
model.serialVersionUID = bindInfo.getSerialVersionUID(); |
|
191 |
if(model.serialVersionUID!=null) |
|
192 |
model.serializable=true; |
|
193 |
model.rootClass = bindInfo.getSuperClass(); |
|
194 |
model.rootInterface = bindInfo.getSuperInterface(); |
|
195 |
||
196 |
// TODO: do we need to reimplement them? |
|
197 |
// // performs annotation |
|
198 |
// Annotator.annotate(model, this); |
|
199 |
// FieldCollisionChecker.check( model, this ); |
|
200 |
||
201 |
||
202 |
processConstructorDeclarations(); |
|
203 |
} |
|
204 |
||
205 |
/** Processes interface declarations. */ |
|
206 |
private void processInterfaceDeclarations() { |
|
207 |
||
208 |
||
209 |
Map<String,InterfaceAcceptor> fromName = new HashMap<String,InterfaceAcceptor>(); |
|
210 |
||
211 |
// first, create empty InterfaceItem declaration for all interfaces |
|
212 |
Map<BIInterface,JClass> decls = new HashMap<BIInterface,JClass>(); |
|
213 |
||
214 |
for( BIInterface decl : bindInfo.interfaces() ) { |
|
215 |
final JDefinedClass intf = classFactory.createInterface( |
|
216 |
bindInfo.getTargetPackage(), decl.name(), copyLocator() ); |
|
217 |
decls.put(decl,intf); |
|
218 |
fromName.put(decl.name(),new InterfaceAcceptor() { |
|
219 |
public void implement(JClass c) { |
|
220 |
intf._implements(c); |
|
221 |
} |
|
222 |
}); |
|
223 |
} |
|
224 |
||
225 |
for( final CClassInfo ci : model.beans().values() ) { |
|
226 |
fromName.put(ci.getName(),new InterfaceAcceptor() { |
|
227 |
public void implement(JClass c) { |
|
228 |
ci._implements(c); |
|
229 |
} |
|
230 |
}); |
|
231 |
} |
|
232 |
||
233 |
// traverse the interface declarations again |
|
234 |
// and populate its expression according to the members attribute. |
|
235 |
for( Map.Entry<BIInterface,JClass> e : decls.entrySet() ) { |
|
236 |
BIInterface decl = e.getKey(); |
|
237 |
JClass c = e.getValue(); |
|
238 |
||
239 |
for (String member : decl.members()) { |
|
240 |
InterfaceAcceptor acc = fromName.get(member); |
|
241 |
if (acc == null) { |
|
242 |
// there is no such class/interface |
|
243 |
// TODO: error location |
|
244 |
error(decl.getSourceLocation(), |
|
245 |
Messages.ERR_BINDINFO_NON_EXISTENT_INTERFACE_MEMBER, |
|
246 |
member); |
|
247 |
continue; |
|
248 |
} |
|
249 |
||
250 |
acc.implement(c); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
// TODO: check the cyclic interface definition |
|
255 |
} |
|
256 |
||
257 |
private static interface InterfaceAcceptor { |
|
258 |
void implement( JClass c ); |
|
259 |
} |
|
260 |
||
261 |
||
262 |
JPackage getTargetPackage() { |
|
263 |
return bindInfo.getTargetPackage(); |
|
264 |
} |
|
265 |
||
266 |
||
267 |
/** |
|
268 |
* Creates constructor declarations as specified in the |
|
269 |
* binding information. |
|
270 |
* |
|
271 |
* <p> |
|
272 |
* Also checks that the binding file does not contain |
|
273 |
* declarations for non-existent elements. |
|
274 |
*/ |
|
275 |
private void processConstructorDeclarations() { |
|
276 |
for( BIElement decl: bindInfo.elements() ) { |
|
277 |
Element e = elements.get(decl.name()); |
|
278 |
if(e==null) { |
|
279 |
error(decl.getSourceLocation(), |
|
280 |
Messages.ERR_BINDINFO_NON_EXISTENT_ELEMENT_DECLARATION,decl.name()); |
|
281 |
continue; // continue to process next declaration |
|
282 |
} |
|
283 |
||
284 |
if(!decl.isClass()) |
|
285 |
// only element-class declaration has constructor definitions |
|
286 |
continue; |
|
287 |
||
288 |
decl.declareConstructors(e.getClassInfo()); |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
public void attributeDecl(String elementName, String attributeName, String attributeType, String[] enumeration, short attributeUse, String defaultValue) throws SAXException { |
|
293 |
getOrCreateElement(elementName).attributes.add( |
|
294 |
createAttribute(elementName, attributeName, attributeType, enumeration, attributeUse, defaultValue) |
|
295 |
); |
|
296 |
} |
|
297 |
||
298 |
protected CPropertyInfo createAttribute( |
|
299 |
String elementName, String attributeName, String attributeType, |
|
300 |
String[] enums, short attributeUse, String defaultValue ) |
|
301 |
throws SAXException { |
|
302 |
||
303 |
boolean required = attributeUse==USE_REQUIRED; |
|
304 |
||
305 |
// get the attribute-property declaration |
|
306 |
BIElement edecl = bindInfo.element(elementName); |
|
307 |
BIAttribute decl=null; |
|
308 |
if(edecl!=null) decl=edecl.attribute(attributeName); |
|
309 |
||
310 |
String propName; |
|
311 |
if(decl==null) propName = model.getNameConverter().toPropertyName(attributeName); |
|
312 |
else propName = decl.getPropertyName(); |
|
313 |
||
314 |
QName qname = new QName("",attributeName); |
|
315 |
||
316 |
// if no declaration is specified, just wrap it by |
|
317 |
// a FieldItem and let the normalizer handle its content. |
|
318 |
TypeUse use; |
|
319 |
||
320 |
if(decl!=null && decl.getConversion()!=null) |
|
321 |
use = decl.getConversion().getTransducer(); |
|
322 |
else |
|
323 |
use = builtinConversions.get(attributeType); |
|
324 |
||
325 |
CPropertyInfo r = new CAttributePropertyInfo( |
|
326 |
propName, null,null/*TODO*/, copyLocator(), qname, use, null, required ); |
|
327 |
||
328 |
if(defaultValue!=null) |
|
329 |
r.defaultValue = CDefaultValue.create( use, new XmlString(defaultValue) ); |
|
330 |
||
331 |
return r; |
|
332 |
} |
|
333 |
||
334 |
||
335 |
||
336 |
Element getOrCreateElement( String elementName ) { |
|
337 |
Element r = elements.get(elementName); |
|
338 |
if(r==null) { |
|
339 |
r = new Element(this,elementName); |
|
340 |
elements.put(elementName,r); |
|
341 |
} |
|
342 |
||
343 |
return r; |
|
344 |
} |
|
345 |
||
346 |
||
347 |
public void startContentModel(String elementName, short contentModelType) throws SAXException { |
|
348 |
assert modelGroups.isEmpty(); |
|
349 |
modelGroups.push(new ModelGroup()); |
|
350 |
} |
|
351 |
||
352 |
public void endContentModel(String elementName, short contentModelType) throws SAXException { |
|
353 |
assert modelGroups.size()==1; |
|
354 |
Term term = modelGroups.pop().wrapUp(); |
|
355 |
||
356 |
Element e = getOrCreateElement(elementName); |
|
357 |
e.define( contentModelType, term, copyLocator() ); |
|
358 |
} |
|
359 |
||
360 |
||
361 |
private final Stack<ModelGroup> modelGroups = new Stack<ModelGroup>(); |
|
362 |
||
363 |
public void startModelGroup() throws SAXException { |
|
364 |
modelGroups.push(new ModelGroup()); |
|
365 |
} |
|
366 |
||
367 |
public void endModelGroup(short occurence) throws SAXException { |
|
368 |
Term t = Occurence.wrap( modelGroups.pop().wrapUp(), occurence ); |
|
369 |
modelGroups.peek().addTerm(t); |
|
370 |
} |
|
371 |
||
372 |
public void connector(short connectorType) throws SAXException { |
|
373 |
modelGroups.peek().setKind(connectorType); |
|
374 |
} |
|
375 |
||
376 |
// TODO: for now, we just ignore all the content model specification |
|
377 |
// and treat it as (A,B,C,....) |
|
378 |
||
379 |
public void childElement(String elementName, short occurence) throws SAXException { |
|
380 |
Element child = getOrCreateElement(elementName); |
|
381 |
modelGroups.peek().addTerm( Occurence.wrap( child, occurence ) ); |
|
382 |
child.isReferenced = true; |
|
383 |
} |
|
384 |
||
385 |
||
386 |
||
387 |
||
388 |
||
389 |
/** |
|
390 |
* Mutable {@link Locator} instance that points to |
|
391 |
* the current source line. |
|
392 |
* <p> |
|
393 |
* Use {@link #copyLocator()} to get a immutable clone. |
|
394 |
*/ |
|
395 |
private Locator locator; |
|
396 |
||
397 |
public void setDocumentLocator(Locator loc) { |
|
398 |
this.locator = loc; |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* Creates a snapshot of the current {@link #locator} values. |
|
403 |
*/ |
|
404 |
private Locator copyLocator(){ |
|
405 |
return new LocatorImpl(locator); |
|
406 |
} |
|
407 |
||
408 |
||
409 |
||
410 |
// |
|
411 |
// |
|
412 |
// builtin datatype handling |
|
413 |
// |
|
414 |
// |
|
415 |
||
416 |
/** Transducers for the built-in types. Read-only. */ |
|
417 |
private static final Map<String,TypeUse> builtinConversions; |
|
418 |
||
419 |
||
420 |
static { |
|
421 |
// list of datatypes which have built-in conversions. |
|
422 |
// note that although xs:token and xs:normalizedString are not |
|
423 |
// specified in the spec, they need to be here because they |
|
424 |
// have different whitespace normalization semantics. |
|
425 |
Map<String,TypeUse> m = new HashMap<String,TypeUse>(); |
|
426 |
||
427 |
m.put("CDATA", CBuiltinLeafInfo.NORMALIZED_STRING); |
|
428 |
m.put("ENTITY", CBuiltinLeafInfo.TOKEN); |
|
429 |
m.put("ENTITIES", CBuiltinLeafInfo.STRING.makeCollection()); |
|
430 |
m.put("NMTOKEN", CBuiltinLeafInfo.TOKEN); |
|
431 |
m.put("NMTOKENS", CBuiltinLeafInfo.STRING.makeCollection()); |
|
432 |
m.put("ID", CBuiltinLeafInfo.ID); |
|
433 |
m.put("IDREF", CBuiltinLeafInfo.IDREF); |
|
434 |
m.put("IDREFS", TypeUseFactory.makeCollection(CBuiltinLeafInfo.IDREF)); |
|
435 |
m.put("ENUMERATION",CBuiltinLeafInfo.TOKEN); |
|
436 |
||
437 |
builtinConversions = Collections.unmodifiableMap(m); |
|
438 |
} |
|
439 |
||
440 |
||
441 |
// |
|
442 |
// |
|
443 |
// error related utility methods |
|
444 |
// |
|
445 |
// |
|
446 |
public void error(SAXParseException e) throws SAXException { |
|
447 |
errorReceiver.error(e); |
|
448 |
} |
|
449 |
||
450 |
public void fatalError(SAXParseException e) throws SAXException { |
|
451 |
errorReceiver.fatalError(e); |
|
452 |
} |
|
453 |
||
454 |
public void warning(SAXParseException e) throws SAXException { |
|
455 |
errorReceiver.warning(e); |
|
456 |
} |
|
457 |
||
458 |
protected final void error( Locator loc, String prop, Object... args ) { |
|
459 |
errorReceiver.error(loc,Messages.format(prop,args)); |
|
460 |
} |
|
461 |
||
462 |
||
463 |
} |