Debian Codegraph @

mod/convert-to-dtd/src/main/com/thaiopensource/relaxng/output/dtd/Analysis.java

raw
1 package com.thaiopensource.relaxng.output.dtd;
2
3 import com.thaiopensource.relaxng.edit.AbstractPatternVisitor;
4 import com.thaiopensource.relaxng.edit.AnyNameNameClass;
5 import com.thaiopensource.relaxng.edit.AttributePattern;
6 import com.thaiopensource.relaxng.edit.ChoiceNameClass;
7 import com.thaiopensource.relaxng.edit.ChoicePattern;
8 import com.thaiopensource.relaxng.edit.Component;
9 import com.thaiopensource.relaxng.edit.ComponentVisitor;
10 import com.thaiopensource.relaxng.edit.CompositePattern;
11 import com.thaiopensource.relaxng.edit.Container;
12 import com.thaiopensource.relaxng.edit.DataPattern;
13 import com.thaiopensource.relaxng.edit.DefineComponent;
14 import com.thaiopensource.relaxng.edit.DivComponent;
15 import com.thaiopensource.relaxng.edit.ElementPattern;
16 import com.thaiopensource.relaxng.edit.EmptyPattern;
17 import com.thaiopensource.relaxng.edit.ExternalRefPattern;
18 import com.thaiopensource.relaxng.edit.GrammarPattern;
19 import com.thaiopensource.relaxng.edit.GroupPattern;
20 import com.thaiopensource.relaxng.edit.IncludeComponent;
21 import com.thaiopensource.relaxng.edit.InterleavePattern;
22 import com.thaiopensource.relaxng.edit.ListPattern;
23 import com.thaiopensource.relaxng.edit.MixedPattern;
24 import com.thaiopensource.relaxng.edit.NameClass;
25 import com.thaiopensource.relaxng.edit.NameClassVisitor;
26 import com.thaiopensource.relaxng.edit.NameNameClass;
27 import com.thaiopensource.relaxng.edit.NotAllowedPattern;
28 import com.thaiopensource.relaxng.edit.NsNameNameClass;
29 import com.thaiopensource.relaxng.edit.OneOrMorePattern;
30 import com.thaiopensource.relaxng.edit.OptionalPattern;
31 import com.thaiopensource.relaxng.edit.ParentRefPattern;
32 import com.thaiopensource.relaxng.edit.Pattern;
33 import com.thaiopensource.relaxng.edit.PatternVisitor;
34 import com.thaiopensource.relaxng.edit.RefPattern;
35 import com.thaiopensource.relaxng.edit.SchemaCollection;
36 import com.thaiopensource.relaxng.edit.TextPattern;
37 import com.thaiopensource.relaxng.edit.ValuePattern;
38 import com.thaiopensource.relaxng.edit.ZeroOrMorePattern;
39 import com.thaiopensource.relaxng.output.common.ErrorReporter;
40 import com.thaiopensource.relaxng.output.common.NameClassSplitter;
41 import com.thaiopensource.util.VoidValue;
42 import com.thaiopensource.xml.util.Name;
43 import com.thaiopensource.xml.util.Naming;
44 import com.thaiopensource.xml.util.WellKnownNamespaces;
45
46 import java.util.Collections;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.List;
51 import java.util.Map;
52 import java.util.Set;
53
54 class Analysis {
55 private final NamespaceManager nsm = new NamespaceManager();
56 private final AttlistMapper am = new AttlistMapper();
57 private final ErrorReporter er;
58 private final Map<Pattern, ContentType> contentTypes = new HashMap<Pattern, ContentType>();
59 private final Map<Pattern, AttributeType> attributeTypes = new HashMap<Pattern, AttributeType>();
60 private final Map<Pattern, Set<Name>> attributeAlphabets = new HashMap<Pattern, Set<Name>>();
61 private final Map<Pattern, Set<String>> attributeNamespaces = new HashMap<Pattern, Set<String>>();
62 private Map<String, Pattern> defines = null;
63 private final Set<String> attlists = new HashSet<String>();
64 private final Map<String, GrammarPart> parts = new HashMap<String, GrammarPart>();
65 private final Map<Pattern, Pattern> seenTable = new HashMap<Pattern, Pattern>();
66 private final Map<Name, ElementPattern> elementDecls = new HashMap<Name, ElementPattern>();
67 private ContentType startType = ContentType.ERROR;
68 private GrammarPart mainPart;
69 private final SchemaCollection schemas;
70 private GrammarPattern grammarPattern;
71 private final AttributeTyper attributeTyper = new AttributeTyper();
72 private final AttributeAlphabetComputer attributeAlphabetComputer = new AttributeAlphabetComputer();
73 private final AttributeNamespacesComputer attributeNamespacesComputer = new AttributeNamespacesComputer();
74 private final IncludeContentChecker includeContentChecker = new IncludeContentChecker();
75 private final static Set<String> EMPTY_STRING_SET = Collections.emptySet();
76 private final static Set<Name> EMPTY_NAME_SET = Collections.emptySet();
77
78 private class Analyzer implements PatternVisitor<ContentType>,
79 ComponentVisitor<VoidValue>, NameClassVisitor<VoidValue> {
80 private ElementPattern ancestorPattern;
81 private final Set<String> pendingRefs;
82
83 public Analyzer() {
84 pendingRefs = new HashSet<String>();
85 }
86
87 private Analyzer(ElementPattern ancestorPattern) {
88 this.ancestorPattern = ancestorPattern;
89 pendingRefs = new HashSet<String>();
90 }
91
92 private Analyzer(Set<String> pendingRefs) {
93 this.pendingRefs = pendingRefs;
94 }
95
96 public ContentType visitEmpty(EmptyPattern p) {
97 return ContentType.EMPTY;
98 }
99
100 public ContentType visitData(DataPattern p) {
101 return ContentType.SIMPLE_TYPE;
102 }
103
104 public ContentType visitValue(ValuePattern p) {
105 Datatypes.Info info = Datatypes.getInfo(p.getDatatypeLibrary(), p.getType());
106 if (info.usesTokenEquality() && Naming.isNmtoken(p.getValue()))
107 return ContentType.ENUM;
108 if (info.usesCdataEquality())
109 return ContentType.VALUE;
110 return ContentType.SIMPLE_TYPE;
111 }
112
113 public ContentType visitElement(ElementPattern p) {
114 int len;
115 if (seen(p))
116 len = NameClassSplitter.split(p.getNameClass()).size();
117 else {
118 new Analyzer(p).analyzeContentType(p.getChild());
119 List<NameNameClass> names = noteNames(p.getNameClass(), true);
120 len = names.size();
121 for (int i = 0; i < len; i++) {
122 NameNameClass nnc = names.get(i);
123 String ns = nnc.getNamespaceUri();
124 if (ns == NameClass.INHERIT_NS)
125 ns = "";
126 Name name = new Name(ns, nnc.getLocalName());
127 ElementPattern prev = elementDecls.get(name);
128 if (prev != null) {
129 er.error("sorry_multiple_element", ns, name.getLocalName(), p.getSourceLocation());
130 er.error("other_element", prev.getSourceLocation());
131 }
132 else
133 elementDecls.put(name, p);
134 }
135 }
136 return len == 1 ? ContentType.DIRECT_SINGLE_ELEMENT : ContentType.ELEMENT_CLASS;
137 }
138
139 public ContentType visitAttribute(AttributePattern p) {
140 noteNames(p.getNameClass(), false);
141 ContentType t = analyzeContentType(p.getChild());
142 if (t.isA(ContentType.MODEL_GROUP) || t == ContentType.MIXED_ELEMENT_CLASS || t == ContentType.MIXED_MODEL)
143 er.error("bad_attribute_type", p.getSourceLocation());
144 if (ancestorPattern != null)
145 am.noteAttribute(ancestorPattern);
146 return ContentType.EMPTY;
147 }
148
149 private List<NameNameClass> noteNames(NameClass nc, boolean defaultable) {
150 nc.accept(this);
151 List<NameNameClass> names = NameClassSplitter.split(nc);
152 int len = names.size();
153 for (int i = 0; i < len; i++)
154 nsm.noteName(names.get(i), defaultable);
155 return names;
156 }
157
158 public ContentType visitNotAllowed(NotAllowedPattern p) {
159 return ContentType.NOT_ALLOWED;
160 }
161
162 public ContentType visitText(TextPattern p) {
163 return ContentType.TEXT;
164 }
165
166 public ContentType visitList(ListPattern p) {
167 return ContentType.SIMPLE_TYPE;
168 }
169
170 public ContentType visitOneOrMore(OneOrMorePattern p) {
171 return checkContentType("sorry_one_or_more", ContentType.oneOrMore(analyzeContentTypeNullAncestorPattern(p.getChild())), p);
172 }
173
174 public ContentType visitZeroOrMore(ZeroOrMorePattern p) {
175 return checkContentType("sorry_zero_or_more", ContentType.zeroOrMore(analyzeContentTypeNullAncestorPattern(p.getChild())), p);
176 }
177
178 public ContentType visitChoice(ChoicePattern p) {
179 List<Pattern> children = p.getChildren();
180 Iterator<Pattern> iter = children.iterator();
181 ContentType tem = analyzeContentType(iter.next());
182 while (iter.hasNext())
183 tem = checkContentType("sorry_choice",
184 ContentType.choice(tem, analyzeContentType(iter.next())),
185 p);
186 if (getAttributeType(p) == AttributeType.MULTI) {
187 Set<Name> attributeNames = new HashSet<Name>();
188 for (Pattern child : children) {
189 Set<Name> childAttributeNames = getAttributeAlphabet(child);
190 for (Name name : childAttributeNames) {
191 if (attributeNames.contains(name))
192 er.error("sorry_choice_attribute_name",
193 name.getNamespaceUri(),
194 name.getLocalName(),
195 p.getSourceLocation());
196 else
197 attributeNames.add(name);
198 }
199 }
200 }
201 return tem;
202 }
203
204 public ContentType visitInterleave(InterleavePattern p) {
205 List<Pattern> children = p.getChildren();
206 ContentType tem = analyzeContentType(children.get(0));
207 for (int i = 1, len = children.size(); i < len; i++)
208 tem = checkContentType("sorry_interleave", ContentType.interleave(tem, analyzeContentType(children.get(i))), p);
209 return tem;
210 }
211
212 public ContentType visitGroup(GroupPattern p) {
213 List<Pattern> children = p.getChildren();
214 ContentType tem = analyzeContentType(children.get(0));
215 for (int i = 1, len = children.size(); i < len; i++)
216 tem = checkContentType("sorry_group", ContentType.group(tem, analyzeContentType(children.get(i))), p);
217 return tem;
218 }
219
220 public ContentType visitRef(RefPattern p) {
221 String name = p.getName();
222 Pattern def = getBody(name);
223 if (def == null) {
224 er.error("undefined_ref", p.getSourceLocation());
225 return ContentType.ERROR;
226 }
227 if (pendingRefs.contains(name)) {
228 er.error("ref_loop", p.getSourceLocation());
229 return ContentType.ERROR;
230 }
231 pendingRefs.add(name);
232 ContentType t = ContentType.ref(new Analyzer(pendingRefs).analyzeContentType(def));
233 pendingRefs.remove(name);
234 if (t.isA(ContentType.EMPTY))
235 am.noteAttributeGroupRef(ancestorPattern, p.getName());
236 return ContentType.ref(t);
237 }
238
239 public ContentType visitParentRef(ParentRefPattern p) {
240 er.error("sorry_parent_ref", p.getSourceLocation());
241 return null;
242 }
243
244 public ContentType visitGrammar(GrammarPattern p) {
245 if (defines != null) {
246 er.error("sorry_nested_grammar", p.getSourceLocation());
247 return ContentType.ERROR;
248 }
249 defines = new HashMap<String, Pattern>();
250 try {
251 mainPart = new GrammarPart(er, defines, attlists, schemas, parts, p);
252 }
253 catch (GrammarPart.IncludeLoopException e) {
254 er.error("include_loop", e.getInclude().getSourceLocation());
255 return ContentType.ERROR;
256 }
257 grammarPattern = p;
258 visitContainer(p);
259 return startType;
260 }
261
262 public ContentType visitExternalRef(ExternalRefPattern p) {
263 er.error("sorry_external_ref", p.getSourceLocation());
264 return null;
265 }
266
267 public ContentType visitMixed(MixedPattern p) {
268 return checkContentType("sorry_mixed", ContentType.mixed(analyzeContentType(p.getChild())), p);
269 }
270
271 public ContentType visitOptional(OptionalPattern p) {
272 return checkContentType("sorry_optional", ContentType.optional(analyzeContentTypeNullAncestorPattern(p.getChild())), p);
273 }
274
275 public VoidValue visitContainer(Container c) {
276 List<Component> list = c.getComponents();
277 for (int i = 0, len = list.size(); i < len; i++)
278 list.get(i).accept(this);
279 return VoidValue.VOID;
280 }
281
282 public VoidValue visitDiv(DivComponent c) {
283 return visitContainer(c);
284 }
285
286 public VoidValue visitDefine(DefineComponent c) {
287 if (c.getName() == DefineComponent.START)
288 startType = analyzeContentType(c.getBody());
289 else
290 new Analyzer().analyzeContentType(c.getBody());
291 if (attlists.contains(c.getName()) && getContentType(c.getBody()) != ContentType.EMPTY) {
292 er.error("not_attlist", c.getName(), c.getSourceLocation());
293 attlists.remove(c.getName());
294 }
295 return VoidValue.VOID;
296 }
297
298 public VoidValue visitInclude(IncludeComponent c) {
299 includeContentChecker.visitContainer(c);
300 visitContainer((GrammarPattern)(schemas.getSchemaDocumentMap().get(c.getUri())).getPattern());
301 return VoidValue.VOID;
302 }
303
304 public VoidValue visitChoice(ChoiceNameClass nc) {
305 List<NameClass> list = nc.getChildren();
306 for (int i = 0, len = list.size(); i < len; i++)
307 list.get(i).accept(this);
308 return VoidValue.VOID;
309 }
310
311 public VoidValue visitAnyName(AnyNameNameClass nc) {
312 er.error("sorry_wildcard", nc.getSourceLocation());
313 return VoidValue.VOID;
314 }
315
316 public VoidValue visitNsName(NsNameNameClass nc) {
317 er.error("sorry_wildcard", nc.getSourceLocation());
318 return VoidValue.VOID;
319 }
320
321 public VoidValue visitName(NameNameClass nc) {
322 nsm.noteName(nc, true);
323 return VoidValue.VOID;
324 }
325
326 ContentType checkContentType(String key, ContentType t, Pattern p) {
327 if (t != null)
328 return t;
329 er.error(key, p.getSourceLocation());
330 return ContentType.ERROR;
331 }
332
333 ContentType analyzeContentType(Pattern p) {
334 ContentType t = contentTypes.get(p);
335 if (t == null) {
336 t = p.accept(this);
337 contentTypes.put(p, t);
338 }
339 return t;
340 }
341
342 ContentType analyzeContentTypeNullAncestorPattern(Pattern p) {
343 return (ancestorPattern == null ? this : new Analyzer(pendingRefs)).analyzeContentType(p);
344 }
345
346 }
347
348 class IncludeContentChecker implements ComponentVisitor<VoidValue> {
349 public VoidValue visitContainer(Container c) {
350 List<Component> list = c.getComponents();
351 for (int i = 0, len = list.size(); i < len; i++)
352 list.get(i).accept(this);
353 return VoidValue.VOID;
354 }
355
356 public VoidValue visitDefine(DefineComponent c) {
357 er.error("sorry_include_override", c.getSourceLocation());
358 return VoidValue.VOID;
359 }
360
361 public VoidValue visitDiv(DivComponent c) {
362 return visitContainer(c);
363 }
364
365 public VoidValue visitInclude(IncludeComponent c) {
366 return VoidValue.VOID;
367 }
368 }
369
370 private class AttributeTyper extends AbstractPatternVisitor<AttributeType> {
371 public AttributeType visitPattern(Pattern p) {
372 return AttributeType.EMPTY;
373 }
374
375 public AttributeType visitMixed(MixedPattern p) {
376 return getAttributeType(p.getChild());
377 }
378
379 public AttributeType visitOneOrMore(OneOrMorePattern p) {
380 return getAttributeType(p.getChild());
381 }
382
383 public AttributeType visitZeroOrMore(ZeroOrMorePattern p) {
384 return getAttributeType(p.getChild());
385 }
386
387 public AttributeType visitOptional(OptionalPattern p) {
388 return getAttributeType(p.getChild());
389 }
390
391 public AttributeType visitComposite(CompositePattern p) {
392 List<Pattern> list = p.getChildren();
393 AttributeType at = getAttributeType(list.get(0));
394 for (int i = 1, len = list.size(); i < len; i++)
395 at = AttributeType.group(at, getAttributeType(list.get(i)));
396 return at;
397 }
398
399 public AttributeType visitAttribute(AttributePattern p) {
400 return AttributeType.SINGLE;
401 }
402
403 public AttributeType visitEmpty(EmptyPattern p) {
404 return AttributeType.MULTI;
405 }
406
407 public AttributeType visitRef(RefPattern p) {
408 return getAttributeType(getBody(p.getName()));
409 }
410 }
411
412
413 private class AttributeAlphabetComputer extends AbstractPatternVisitor<Set<Name>> {
414 public Set<Name> visitPattern(Pattern p) {
415 return EMPTY_NAME_SET;
416 }
417
418 public Set<Name> visitMixed(MixedPattern p) {
419 return getAttributeAlphabet(p.getChild());
420 }
421
422 public Set<Name> visitOneOrMore(OneOrMorePattern p) {
423 return getAttributeAlphabet(p.getChild());
424 }
425
426 public Set<Name> visitZeroOrMore(ZeroOrMorePattern p) {
427 return getAttributeAlphabet(p.getChild());
428 }
429
430 public Set<Name> visitOptional(OptionalPattern p) {
431 return getAttributeAlphabet(p.getChild());
432 }
433
434 public Set<Name> visitComposite(CompositePattern p) {
435 List<Pattern> list = p.getChildren();
436 Set<Name> result = new HashSet<Name>();
437 for (int i = 0, len = list.size(); i < len; i++)
438 result.addAll(getAttributeAlphabet(list.get(i)));
439 return result;
440 }
441
442 public Set<Name> visitAttribute(AttributePattern p) {
443 Set<Name> result = new HashSet<Name>();
444 List<NameNameClass> names = NameClassSplitter.split(p.getNameClass());
445 for (int i = 0, len = names.size(); i < len; i++) {
446 NameNameClass nnc = names.get(i);
447 String ns = nnc.getNamespaceUri();
448 if (ns == NameClass.INHERIT_NS)
449 ns = "";
450 result.add(new Name(ns, nnc.getLocalName()));
451 }
452 return result;
453 }
454
455 public Set<Name> visitRef(RefPattern p) {
456 return getAttributeAlphabet(getBody(p.getName()));
457 }
458 }
459
460 private class AttributeNamespacesComputer extends AbstractPatternVisitor<Set<String>> {
461 public Set<String> visitPattern(Pattern p) {
462 return EMPTY_STRING_SET;
463 }
464
465 public Set<String> visitMixed(MixedPattern p) {
466 return getAttributeNamespaces(p.getChild());
467 }
468
469 public Set<String> visitOneOrMore(OneOrMorePattern p) {
470 return getAttributeNamespaces(p.getChild());
471 }
472
473 public Set<String> visitZeroOrMore(ZeroOrMorePattern p) {
474 return getAttributeNamespaces(p.getChild());
475 }
476
477 public Set<String> visitOptional(OptionalPattern p) {
478 return getAttributeNamespaces(p.getChild());
479 }
480
481 public Set<String> visitComposite(CompositePattern p) {
482 Set<String> result = EMPTY_STRING_SET;
483 boolean newResult = false;
484 for (Pattern child : p.getChildren()) {
485 Set<String> tem = getAttributeNamespaces(child);
486 if (tem != EMPTY_STRING_SET && !result.containsAll(tem)) {
487 if (result == EMPTY_STRING_SET)
488 result = tem;
489 else {
490 if (!newResult) {
491 result = new HashSet<String>(result);
492 newResult = true;
493 }
494 result.addAll(tem);
495 }
496 }
497 }
498 if (newResult)
499 result = Collections.unmodifiableSet(result);
500 return result;
501 }
502
503 public Set<String> visitAttribute(AttributePattern p) {
504 Set<String> result = null;
505 List<NameNameClass> names = NameClassSplitter.split(p.getNameClass());
506 for (NameNameClass name : names) {
507 String ns = name.getNamespaceUri();
508 if (ns.length() != 0 && ns != NameClass.INHERIT_NS && !ns.equals(WellKnownNamespaces.XML)) {
509 if (result == null)
510 result = new HashSet<String>();
511 result.add(ns);
512 }
513 }
514 if (result == null)
515 return EMPTY_STRING_SET;
516 return Collections.unmodifiableSet(result);
517 }
518
519 public Set<String> visitRef(RefPattern p) {
520 return getAttributeNamespaces(getBody(p.getName()));
521 }
522 }
523
524 private boolean seen(Pattern p) {
525 if (seenTable.get(p) != null)
526 return true;
527 seenTable.put(p, p);
528 return false;
529 }
530
531 Analysis(SchemaCollection schemas, ErrorReporter er) {
532 this.schemas = schemas;
533 this.er = er;
534 new Analyzer().analyzeContentType(getPattern());
535 checkAttlists();
536 if (!er.getHadError())
537 nsm.assignPrefixes();
538 }
539
540 private void checkAttlists() {
541 for (String name : attlists)
542 if (getParamEntityElementName(name) == null)
543 er.error("not_attlist", name, getBody(name).getSourceLocation());
544 }
545
546 Pattern getPattern() {
547 return (schemas.getSchemaDocumentMap().get(schemas.getMainUri())).getPattern();
548 }
549
550 String getPrefixForNamespaceUri(String ns) {
551 return nsm.getPrefixForNamespaceUri(ns);
552 }
553
554 String getElementPrefixForNamespaceUri(String ns) {
555 if (ns.equals("") || ns.equals(nsm.getDefaultNamespaceUri()) || ns == NameClass.INHERIT_NS)
556 return null;
557 return nsm.getPrefixForNamespaceUri(ns);
558 }
559
560 String getParamEntityElementName(String name) {
561 NameNameClass nc = am.getParamEntityElementName(name);
562 if (nc == null)
563 return null;
564 String prefix = getElementPrefixForNamespaceUri(nc.getNamespaceUri());
565 String localName = nc.getLocalName();
566 if (prefix == null)
567 return localName;
568 return prefix + ":" + localName;
569 }
570
571 ContentType getContentType(Pattern p) {
572 return contentTypes.get(p);
573 }
574
575 AttributeType getAttributeType(Pattern p) {
576 AttributeType at = attributeTypes.get(p);
577 if (at == null) {
578 at = p.accept(attributeTyper);
579 attributeTypes.put(p, at);
580 }
581 return at;
582 }
583
584 Set<Name> getAttributeAlphabet(Pattern p) {
585 Set<Name> aa = attributeAlphabets.get(p);
586 if (aa == null) {
587 aa = Collections.unmodifiableSet(p.accept(attributeAlphabetComputer));
588 attributeAlphabets.put(p, aa);
589 }
590 return aa;
591 }
592
593
594 Set<String> getAttributeNamespaces(Pattern p) {
595 Set<String> aa = attributeNamespaces.get(p);
596 if (aa == null) {
597 aa = p.accept(attributeNamespacesComputer);
598 attributeNamespaces.put(p, aa);
599 }
600 return aa;
601 }
602
603 Pattern getBody(String name) {
604 return defines.get(name);
605 }
606
607 GrammarPattern getGrammarPattern() {
608 return grammarPattern;
609 }
610
611 String getMainUri() {
612 return schemas.getMainUri();
613 }
614
615 GrammarPart getGrammarPart(String sourceUri) {
616 if (sourceUri.equals(schemas.getMainUri()))
617 return mainPart;
618 else
619 return parts.get(sourceUri);
620 }
621
622 Pattern getSchema(String sourceUri) {
623 return (schemas.getSchemaDocumentMap().get(sourceUri)).getPattern();
624 }
625
626 String getEncoding(String sourceUri) {
627 return (schemas.getSchemaDocumentMap().get(sourceUri)).getEncoding();
628 }
629 }