Debian Codegraph @

mod/convert-to-xsd/src/main/com/thaiopensource/relaxng/output/xsd/BasicOutput.java

raw
1 package com.thaiopensource.relaxng.output.xsd;
2
3 import com.thaiopensource.relaxng.output.OutputDirectory;
4 import com.thaiopensource.relaxng.output.common.ErrorReporter;
5 import com.thaiopensource.relaxng.output.common.XmlWriter;
6 import com.thaiopensource.relaxng.output.xsd.basic.AbstractAttributeUseVisitor;
7 import com.thaiopensource.relaxng.output.xsd.basic.AbstractSchemaVisitor;
8 import com.thaiopensource.relaxng.output.xsd.basic.Annotated;
9 import com.thaiopensource.relaxng.output.xsd.basic.Annotation;
10 import com.thaiopensource.relaxng.output.xsd.basic.Attribute;
11 import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroup;
12 import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroupDefinition;
13 import com.thaiopensource.relaxng.output.xsd.basic.AttributeGroupRef;
14 import com.thaiopensource.relaxng.output.xsd.basic.AttributeUse;
15 import com.thaiopensource.relaxng.output.xsd.basic.AttributeUseVisitor;
16 import com.thaiopensource.relaxng.output.xsd.basic.Comment;
17 import com.thaiopensource.relaxng.output.xsd.basic.ComplexType;
18 import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeComplexContent;
19 import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeNotAllowedContent;
20 import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeSimpleContent;
21 import com.thaiopensource.relaxng.output.xsd.basic.ComplexTypeVisitor;
22 import com.thaiopensource.relaxng.output.xsd.basic.Element;
23 import com.thaiopensource.relaxng.output.xsd.basic.Facet;
24 import com.thaiopensource.relaxng.output.xsd.basic.GroupDefinition;
25 import com.thaiopensource.relaxng.output.xsd.basic.GroupRef;
26 import com.thaiopensource.relaxng.output.xsd.basic.Occurs;
27 import com.thaiopensource.relaxng.output.xsd.basic.OptionalAttribute;
28 import com.thaiopensource.relaxng.output.xsd.basic.Particle;
29 import com.thaiopensource.relaxng.output.xsd.basic.ParticleAll;
30 import com.thaiopensource.relaxng.output.xsd.basic.ParticleChoice;
31 import com.thaiopensource.relaxng.output.xsd.basic.ParticleRepeat;
32 import com.thaiopensource.relaxng.output.xsd.basic.ParticleSequence;
33 import com.thaiopensource.relaxng.output.xsd.basic.ParticleVisitor;
34 import com.thaiopensource.relaxng.output.xsd.basic.RootDeclaration;
35 import com.thaiopensource.relaxng.output.xsd.basic.Schema;
36 import com.thaiopensource.relaxng.output.xsd.basic.SchemaVisitor;
37 import com.thaiopensource.relaxng.output.xsd.basic.SchemaWalker;
38 import com.thaiopensource.relaxng.output.xsd.basic.SimpleType;
39 import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeDefinition;
40 import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeList;
41 import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeRef;
42 import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeRestriction;
43 import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeUnion;
44 import com.thaiopensource.relaxng.output.xsd.basic.SimpleTypeVisitor;
45 import com.thaiopensource.relaxng.output.xsd.basic.Structure;
46 import com.thaiopensource.relaxng.output.xsd.basic.StructureVisitor;
47 import com.thaiopensource.relaxng.output.xsd.basic.Wildcard;
48 import com.thaiopensource.relaxng.output.xsd.basic.WildcardAttribute;
49 import com.thaiopensource.relaxng.output.xsd.basic.WildcardElement;
50 import com.thaiopensource.util.VoidValue;
51 import com.thaiopensource.xml.util.Name;
52 import com.thaiopensource.xml.util.WellKnownNamespaces;
53
54 import java.io.IOException;
55 import java.util.Collections;
56 import java.util.HashSet;
57 import java.util.Iterator;
58 import java.util.List;
59 import java.util.Set;
60 import java.util.Vector;
61
62 public class BasicOutput {
63 static class Options {
64 String anyProcessContents = "skip";
65 String anyAttributeProcessContents = "skip";
66 }
67 private final XmlWriter xw;
68 private final Schema schema;
69 private final SimpleTypeOutput simpleTypeOutput = new SimpleTypeOutput();
70 private final ComplexTypeOutput complexTypeOutput = new ComplexTypeOutput();
71 private final AttributeUseOutput attributeUseOutput = new AttributeUseOutput();
72 private final AttributeUseVisitor<VoidValue> attributeWildcardOutput = new AttributeWildcardOutput();
73 private final ParticleOutput particleOutput = new ParticleOutput();
74 private final ParticleVisitor<VoidValue> globalElementOutput = new GlobalElementOutput();
75 private final GlobalAttributeOutput globalAttributeOutput = new GlobalAttributeOutput();
76 private final SchemaVisitor schemaOutput = new SchemaOutput();
77 private final StructureVisitor<VoidValue> movedStructureOutput = new MovedStructureOutput();
78 private final SimpleTypeVisitor<String> simpleTypeNamer = new SimpleTypeNamer();
79 private final NamespaceManager nsm;
80 private final PrefixManager pm;
81 private final String targetNamespace;
82 private final OutputDirectory od;
83 private final String sourceUri;
84 private final ComplexTypeSelector complexTypeSelector;
85 private final AbstractElementTypeSelector abstractElementTypeSelector;
86 private final Set<Name> globalElementsDefined;
87 private final Set<Name> globalAttributesDefined;
88 private final String xsPrefix;
89 private final Options options;
90
91 class SimpleTypeOutput implements SimpleTypeVisitor<VoidValue> {
92 public VoidValue visitRestriction(SimpleTypeRestriction t) {
93 boolean hadPatternFacet = false;
94 for (Facet facet : t.getFacets()) {
95 if (facet.getName().equals("pattern")) {
96 if (!hadPatternFacet)
97 hadPatternFacet = true;
98 else {
99 xw.startElement(xs("restriction"));
100 xw.startElement(xs("simpleType"));
101 }
102 }
103 }
104 xw.startElement(xs("restriction"));
105 xw.attribute("base", xs(t.getName()));
106 hadPatternFacet = false;
107 for (Facet facet : t.getFacets()) {
108 if (facet.getName().equals("pattern")) {
109 if (!hadPatternFacet) {
110 hadPatternFacet = true;
111 outputFacet(facet);
112 }
113 }
114 else
115 outputFacet(facet);
116 }
117 xw.endElement();
118 hadPatternFacet = false;
119 for (Facet facet : t.getFacets()) {
120 if (facet.getName().equals("pattern")) {
121 if (!hadPatternFacet)
122 hadPatternFacet = true;
123 else {
124 xw.endElement();
125 outputFacet(facet);
126 xw.endElement();
127 }
128 }
129 }
130 return null;
131 }
132
133 private void outputFacet(Facet facet) {
134 xw.startElement(xs(facet.getName()));
135 xw.attribute("value", facet.getValue());
136 String prefix = facet.getPrefix();
137 if (prefix != null && !prefix.equals(topLevelPrefix(facet.getNamespace())))
138 xw.attribute(prefix.equals("") ? "xmlns" : "xmlns:" + prefix, facet.getNamespace());
139 outputAnnotation(facet);
140 xw.endElement();
141 }
142
143
144 public VoidValue visitRef(SimpleTypeRef t) {
145 xw.startElement(xs("restriction"));
146 xw.attribute("base", qualifyRef(schema.getSimpleType(t.getName()).getParentSchema().getUri(),
147 t.getName()));
148 xw.endElement();
149 return null;
150 }
151
152 public VoidValue visitUnion(SimpleTypeUnion t) {
153 xw.startElement(xs("union"));
154 StringBuffer buf = new StringBuffer();
155 for (SimpleType child : t.getChildren()) {
156 String typeName = child.accept(simpleTypeNamer);
157 if (typeName != null) {
158 if (buf.length() != 0)
159 buf.append(' ');
160 buf.append(typeName);
161 }
162 }
163 if (buf.length() != 0)
164 xw.attribute("memberTypes", buf.toString());
165 outputAnnotation(t);
166 for (SimpleType child : t.getChildren()) {
167 if (child.accept(simpleTypeNamer) == null)
168 outputWrap(child, null);
169 }
170 xw.endElement();
171 return null;
172 }
173
174 public VoidValue visitList(SimpleTypeList t) {
175 Occurs occ = t.getOccurs();
176 if (!occ.equals(Occurs.ZERO_OR_MORE)) {
177 xw.startElement(xs("restriction"));
178 xw.startElement(xs("simpleType"));
179 }
180 xw.startElement(xs("list"));
181 outputWrap(t.getItemType(), "itemType", t);
182 xw.endElement();
183 if (!occ.equals(Occurs.ZERO_OR_MORE)) {
184 xw.endElement();
185 if (occ.getMin() == occ.getMax()) {
186 xw.startElement(xs("length"));
187 xw.attribute("value", Integer.toString(occ.getMin()));
188 xw.endElement();
189 }
190 else {
191 if (occ.getMin() != 0) {
192 xw.startElement(xs("minLength"));
193 xw.attribute("value", Integer.toString(occ.getMin()));
194 xw.endElement();
195 }
196 if (occ.getMax() != Occurs.UNBOUNDED) {
197 xw.startElement(xs("maxLength"));
198 xw.attribute("value", Integer.toString(occ.getMax()));
199 xw.endElement();
200 }
201 }
202 xw.endElement();
203 }
204 return null;
205 }
206
207 void outputWrap(SimpleType t, Annotated parent) {
208 outputWrap(t, "type", parent);
209 }
210
211 void outputWrap(SimpleType t, String attributeName, Annotated parent) {
212 String typeName = t.accept(simpleTypeNamer);
213 if (typeName != null) {
214 xw.attribute(attributeName, typeName);
215 if (parent != null)
216 outputAnnotation(parent);
217 }
218 else {
219 if (parent != null)
220 outputAnnotation(parent);
221 xw.startElement(xs("simpleType"));
222 t.accept(this);
223 xw.endElement();
224 }
225 }
226 }
227
228 class SimpleTypeNamer implements SimpleTypeVisitor<String> {
229 public String visitRestriction(SimpleTypeRestriction t) {
230 if (t.getFacets().size() > 0)
231 return null;
232 if (t.getAnnotation() != null)
233 return null;
234 return xs(t.getName());
235 }
236
237 public String visitRef(SimpleTypeRef t) {
238 if (t.getAnnotation() != null)
239 return null;
240 return qualifyRef(schema.getSimpleType(t.getName()).getParentSchema().getUri(),
241 t.getName());
242 }
243
244 public String visitList(SimpleTypeList t) {
245 return null;
246 }
247
248 public String visitUnion(SimpleTypeUnion t) {
249 return null;
250 }
251 }
252
253 private static final int NORMAL_CONTEXT = 0;
254 private static final int COMPLEX_TYPE_CONTEXT = 1;
255 private static final int NAMED_GROUP_CONTEXT = 2;
256
257 class ParticleOutput implements ParticleVisitor<VoidValue> {
258 private Occurs occ = Occurs.EXACTLY_ONE;
259 private int context = NORMAL_CONTEXT;
260
261 private boolean startWrapperForElement() {
262 boolean needWrapper = context >= COMPLEX_TYPE_CONTEXT;
263 context = NORMAL_CONTEXT;
264 if (needWrapper)
265 xw.startElement(xs("sequence"));
266 xw.startElement(xs("element"));
267 outputOccurAttributes();
268 return needWrapper;
269 }
270
271 private boolean startWrapperForAny() {
272 boolean needWrapper = context >= COMPLEX_TYPE_CONTEXT;
273 context = NORMAL_CONTEXT;
274 if (needWrapper)
275 xw.startElement(xs("sequence"));
276 xw.startElement(xs("any"));
277 outputOccurAttributes();
278 return needWrapper;
279 }
280
281 private boolean startWrapperForGroupRef() {
282 boolean needWrapper = context == NAMED_GROUP_CONTEXT;
283 context = NORMAL_CONTEXT;
284 if (needWrapper)
285 xw.startElement(xs("sequence"));
286 xw.startElement(xs("group"));
287 outputOccurAttributes();
288 return needWrapper;
289 }
290
291 private boolean startWrapperForGroup(String groupType) {
292 boolean needWrapper = context == NAMED_GROUP_CONTEXT && !occ.equals(Occurs.EXACTLY_ONE);
293 context = NORMAL_CONTEXT;
294 if (needWrapper)
295 xw.startElement(xs("sequence"));
296 xw.startElement(xs(groupType));
297 outputOccurAttributes();
298 return needWrapper;
299 }
300
301 private void endWrapper(boolean extra) {
302 xw.endElement();
303 if (extra)
304 xw.endElement();
305 }
306
307 public VoidValue visitElement(Element p) {
308 boolean usedWrapper;
309 if (nsm.isGlobal(p)) {
310 usedWrapper = startWrapperForElement();
311 xw.attribute("ref", qualifyName(p.getName()));
312 }
313 else if (!namespaceIsLocal(p.getName().getNamespaceUri())) {
314 usedWrapper = startWrapperForGroupRef();
315 xw.attribute("ref", qualifyName(p.getName().getNamespaceUri(),
316 nsm.getProxyName(p)));
317 }
318 else {
319 usedWrapper = startWrapperForElement();
320 xw.attribute("name", p.getName().getLocalName());
321 if (!p.getName().getNamespaceUri().equals(targetNamespace))
322 xw.attribute("form", "unqualified");
323 complexTypeOutput.parent = p;
324 p.getComplexType().accept(complexTypeOutput);
325 }
326 endWrapper(usedWrapper);
327 return null;
328 }
329
330 public VoidValue visitWildcardElement(WildcardElement p) {
331 String ns = NamespaceManager.otherNamespace(p.getWildcard());
332 boolean usedWrapper;
333 if (ns != null && !ns.equals(targetNamespace)) {
334 usedWrapper = startWrapperForGroupRef();
335 xw.attribute("ref", qualifyName(ns, nsm.getOtherElementName(ns)));
336 }
337 else {
338 usedWrapper = startWrapperForAny();
339 namespaceAttribute(p.getWildcard());
340 xw.attribute("processContents", options.anyProcessContents);
341 outputAnnotation(p);
342 }
343 endWrapper(usedWrapper);
344 return null;
345 }
346
347 public VoidValue visitRepeat(ParticleRepeat p) {
348 occ = Occurs.multiply(occ, p.getOccurs());
349 p.getChild().accept(this);
350 return null;
351 }
352
353 public VoidValue visitSequence(ParticleSequence p) {
354 boolean usedWrapper = startWrapperForGroup("sequence");
355 outputAnnotation(p);
356 outputParticles(p.getChildren());
357 endWrapper(usedWrapper);
358 return null;
359 }
360
361 public VoidValue visitChoice(ParticleChoice p) {
362 boolean usedWrapper = startWrapperForGroup("choice");
363 outputAnnotation(p);
364 outputParticles(p.getChildren());
365 endWrapper(usedWrapper);
366 return null;
367 }
368
369 public VoidValue visitAll(ParticleAll p) {
370 boolean usedWrapper = startWrapperForGroup("all");
371 outputAnnotation(p);
372 outputParticles(p.getChildren());
373 endWrapper(usedWrapper);
374 return null;
375 }
376
377 private void outputParticles(List<Particle> particles) {
378 for (Particle particle : particles)
379 particle.accept(this);
380 }
381
382 public VoidValue visitGroupRef(GroupRef p) {
383 String groupName = p.getName();
384 GroupDefinition def = schema.getGroup(groupName);
385 Name elementName = nsm.getElementNameForGroupRef(def);
386 boolean usedWrapper;
387 if (elementName != null) {
388 usedWrapper = startWrapperForElement();
389 xw.attribute("ref", qualifyName(elementName));
390 }
391 else {
392 usedWrapper = startWrapperForGroupRef();
393 xw.attribute("ref", qualifyRef(def.getParentSchema().getUri(), groupName));
394 }
395 outputAnnotation(p);
396 endWrapper(usedWrapper);
397 return null;
398 }
399
400 void outputOccurAttributes() {
401 if (occ.getMin() != 1)
402 xw.attribute("minOccurs", Integer.toString(occ.getMin()));
403 if (occ.getMax() != 1)
404 xw.attribute("maxOccurs",
405 occ.getMax() == Occurs.UNBOUNDED ? "unbounded" : Integer.toString(occ.getMax()));
406 occ = Occurs.EXACTLY_ONE;
407 }
408 }
409
410 class ComplexTypeOutput implements ComplexTypeVisitor<VoidValue> {
411 Annotated parent;
412
413 public VoidValue visitComplexContent(ComplexTypeComplexContent t) {
414 outputComplexTypeComplexContent(complexTypeSelector.transformComplexContent(t), null, parent);
415 return null;
416 }
417
418 public VoidValue visitSimpleContent(ComplexTypeSimpleContent t) {
419 outputComplexTypeSimpleContent(complexTypeSelector.transformSimpleContent(t), null, parent);
420 return null;
421 }
422
423 public VoidValue visitNotAllowedContent(ComplexTypeNotAllowedContent t) {
424 xw.startElement(xs("complexType"));
425 xw.startElement(xs("choice"));
426 xw.endElement();
427 xw.endElement();
428 return null;
429 }
430 }
431
432 class AttributeUseOutput extends SchemaWalker {
433 boolean isOptional = false;
434 String defaultValue = null;
435
436 public VoidValue visitOptionalAttribute(OptionalAttribute a) {
437 isOptional = true;
438 defaultValue = a.getDefaultValue();
439 a.getAttribute().accept(this);
440 isOptional = false;
441 defaultValue = null;
442 return null;
443 }
444
445 public VoidValue visitAttribute(Attribute a) {
446 if (nsm.isGlobal(a)) {
447 xw.startElement(xs("attribute"));
448 xw.attribute("ref", qualifyName(a.getName()));
449 if (!isOptional)
450 xw.attribute("use", "required");
451 else if (defaultValue != null)
452 xw.attribute("default", defaultValue);
453 xw.endElement();
454 }
455 else if (namespaceIsLocal(a.getName().getNamespaceUri())) {
456 xw.startElement(xs("attribute"));
457 xw.attribute("name", a.getName().getLocalName());
458 if (!isOptional)
459 xw.attribute("use", "required");
460 else if (defaultValue != null)
461 xw.attribute("default", defaultValue);
462 if (!a.getName().getNamespaceUri().equals(""))
463 xw.attribute("form", "qualified");
464 if (a.getType() != null)
465 simpleTypeOutput.outputWrap(a.getType(), a);
466 else
467 outputAnnotation(a);
468 xw.endElement();
469 }
470 else {
471 xw.startElement(xs("attributeGroup"));
472 xw.attribute("ref",
473 qualifyName(a.getName().getNamespaceUri(),
474 nsm.getProxyName(a)));
475 xw.endElement();
476 }
477 return null;
478 }
479
480 public VoidValue visitAttributeGroupRef(AttributeGroupRef a) {
481 xw.startElement(xs("attributeGroup"));
482 String name = a.getName();
483 xw.attribute("ref",
484 qualifyRef(schema.getAttributeGroup(name).getParentSchema().getUri(), name));
485 xw.endElement();
486 return null;
487 }
488 }
489
490 class AttributeWildcardOutput extends SchemaWalker {
491 public VoidValue visitWildcardAttribute(WildcardAttribute a) {
492 String ns = NamespaceManager.otherNamespace(a.getWildcard());
493 if (ns != null && !ns.equals(targetNamespace)) {
494 xw.startElement(xs("attributeGroup"));
495 xw.attribute("ref", qualifyName(ns, nsm.getOtherAttributeName(ns)));
496 xw.endElement();
497 }
498 else {
499 xw.startElement(xs("anyAttribute"));
500 namespaceAttribute(a.getWildcard());
501 xw.attribute("processContents", options.anyAttributeProcessContents);
502 xw.endElement();
503 }
504 return null;
505 }
506 }
507
508 class GlobalElementOutput implements ParticleVisitor<VoidValue>, ComplexTypeVisitor<VoidValue> {
509 public VoidValue visitElement(Element p) {
510 Name name = p.getName();
511 if (nsm.isGlobal(p)
512 && name.getNamespaceUri().equals(targetNamespace)
513 && !globalElementsDefined.contains(name)) {
514 globalElementsDefined.add(name);
515 xw.startElement(xs("element"));
516 xw.attribute("name", name.getLocalName());
517 outputComplexType(name, p.getComplexType(), p);
518 xw.endElement();
519 }
520 return p.getComplexType().accept(this);
521 }
522
523 public VoidValue visitRepeat(ParticleRepeat p) {
524 return p.getChild().accept(this);
525 }
526
527 void visitList(List<Particle> list) {
528 for (Particle p : list)
529 p.accept(this);
530 }
531
532 public VoidValue visitSequence(ParticleSequence p) {
533 visitList(p.getChildren());
534 return null;
535 }
536
537 public VoidValue visitChoice(ParticleChoice p) {
538 visitList(p.getChildren());
539 return null;
540 }
541
542 public VoidValue visitAll(ParticleAll p) {
543 visitList(p.getChildren());
544 return null;
545 }
546
547 public VoidValue visitGroupRef(GroupRef p) {
548 return null;
549 }
550
551 public VoidValue visitWildcardElement(WildcardElement p) {
552 return null;
553 }
554
555 public VoidValue visitComplexContent(ComplexTypeComplexContent t) {
556 if (t.getParticle() == null)
557 return null;
558 return t.getParticle().accept(this);
559 }
560
561 public VoidValue visitSimpleContent(ComplexTypeSimpleContent t) {
562 return null;
563 }
564
565 public VoidValue visitNotAllowedContent(ComplexTypeNotAllowedContent t) {
566 return null;
567 }
568 }
569
570 class GlobalAttributeOutput extends AbstractAttributeUseVisitor<VoidValue> {
571 public VoidValue visitAttributeGroup(AttributeGroup a) {
572 for (AttributeUse child : a.getChildren())
573 child.accept(this);
574 return null;
575 }
576
577 public VoidValue visitAttribute(Attribute a) {
578 Name name = a.getName();
579 if (nsm.isGlobal(a)
580 && name.getNamespaceUri().equals(targetNamespace)
581 && !globalAttributesDefined.contains(name)) {
582 globalAttributesDefined.add(name);
583 xw.startElement(xs("attribute"));
584 xw.attribute("name", name.getLocalName());
585 if (a.getType() != null)
586 simpleTypeOutput.outputWrap(a.getType(), a);
587 xw.endElement();
588 }
589 return null;
590 }
591
592 public VoidValue visitOptionalAttribute(OptionalAttribute a) {
593 return a.getAttribute().accept(this);
594 }
595
596 public VoidValue visitAttributeGroupRef(AttributeGroupRef a) {
597 return null;
598 }
599
600 public VoidValue visitWildcardAttribute(WildcardAttribute a) {
601 return null;
602 }
603 }
604
605 class SchemaOutput extends AbstractSchemaVisitor {
606 public void visitGroup(GroupDefinition def) {
607 Particle particle = def.getParticle();
608 ComplexTypeComplexContentExtension ct = complexTypeSelector.createComplexTypeForGroup(def.getName(), nsm);
609 if (ct != null) {
610 Annotated anno;
611 if (tryAbstractElement(def))
612 anno = null;
613 else
614 anno = def;
615 outputComplexTypeComplexContent(ct, def.getName(), anno);
616 }
617 else if (!nsm.isGroupDefinitionOmitted(def)
618 && !tryAbstractElement(def)
619 && !tryElementChoiceSameType(def)) {
620 xw.startElement(xs("group"));
621 xw.attribute("name", def.getName());
622 outputAnnotation(def);
623 particleOutput.context = NAMED_GROUP_CONTEXT;
624 particle.accept(particleOutput);
625 xw.endElement();
626 }
627 particle.accept(globalElementOutput);
628 }
629
630 private boolean tryAbstractElement(GroupDefinition def) {
631 Name name = nsm.getGroupDefinitionAbstractElementName(def);
632 if (name == null)
633 return false;
634 xw.startElement(xs("element"));
635 xw.attribute("name", name.getLocalName());
636 xw.attribute("abstract", "true");
637 outputComplexType(name, abstractElementTypeSelector.getAbstractElementType(name), def);
638 xw.endElement();
639 return true;
640 }
641
642 private boolean tryElementChoiceSameType(GroupDefinition def) {
643 Particle particle = def.getParticle();
644 if (!(particle instanceof ParticleChoice))
645 return false;
646 List<Particle> children = ((ParticleChoice)particle).getChildren();
647 if (children.size() <= 1)
648 return false;
649 Iterator<Particle> iter = children.iterator();
650 Particle first = iter.next();
651 if (!(first instanceof Element))
652 return false;
653 if (!((Element)first).getName().getNamespaceUri().equals(targetNamespace))
654 return false;
655 ComplexType type = ((Element)first).getComplexType();
656 do {
657 Particle tem = iter.next();
658 if (!(tem instanceof Element))
659 return false;
660 if (!((Element)tem).getComplexType().equals(type))
661 return false;
662 if (!((Element)tem).getName().getNamespaceUri().equals(targetNamespace))
663 return false;
664 } while (iter.hasNext());
665 if (type instanceof ComplexTypeComplexContent) {
666 ComplexTypeComplexContentExtension t = complexTypeSelector.transformComplexContent((ComplexTypeComplexContent)type);
667 if (t.getBase() != null && t.getParticle() == null && !t.isMixed() && t.getAttributeUses().equals(AttributeGroup.EMPTY))
668 return false;
669 outputComplexTypeComplexContent(t, def.getName(), null);
670 }
671 else {
672 ComplexTypeSimpleContentExtension t = complexTypeSelector.transformSimpleContent((ComplexTypeSimpleContent)type);
673 if (t.getAttributeUses().equals(AttributeGroup.EMPTY)
674 && (t.getBase() != null || t.getSimpleType().accept(simpleTypeNamer) != null))
675 return false;
676 outputComplexTypeSimpleContent(t, def.getName(), null);
677 }
678 xw.startElement(xs("group"));
679 xw.attribute("name", def.getName());
680 outputAnnotation(def);
681 xw.startElement(xs("choice"));
682 for (iter = children.iterator(); iter.hasNext();) {
683 Element element = (Element)iter.next();
684 xw.startElement(xs("element"));
685 if (nsm.isGlobal(element))
686 xw.attribute("ref", qualifyName(element.getName()));
687 else {
688 xw.attribute("name", element.getName().getLocalName());
689 xw.attribute("type", def.getName());
690 outputAnnotation(element);
691 }
692 xw.endElement();
693 }
694 xw.endElement();
695 xw.endElement();
696 for (iter = children.iterator(); iter.hasNext();) {
697 Element element = (Element)iter.next();
698 if (nsm.isGlobal(element) && !globalElementsDefined.contains(element.getName())) {
699 globalElementsDefined.add(element.getName());
700 xw.startElement(xs("element"));
701 xw.attribute("name", element.getName().getLocalName());
702 xw.attribute("type", def.getName());
703 outputAnnotation(element);
704 xw.endElement();
705 }
706 }
707 return true;
708 }
709
710 public void visitSimpleType(SimpleTypeDefinition def) {
711 ComplexTypeSimpleContentExtension ct = complexTypeSelector.createComplexTypeForSimpleType(def.getName());
712 if (ct != null)
713 outputComplexTypeSimpleContent(ct, def.getName(), def);
714 else {
715 xw.startElement(xs("simpleType"));
716 xw.attribute("name", def.getName());
717 outputAnnotation(def);
718 def.getSimpleType().accept(simpleTypeOutput);
719 xw.endElement();
720 }
721 }
722
723 public void visitAttributeGroup(AttributeGroupDefinition def) {
724 if (complexTypeSelector.isComplexType(def.getName()))
725 return;
726 xw.startElement(xs("attributeGroup"));
727 xw.attribute("name", def.getName());
728 outputAnnotation(def);
729 outputAttributeUse(def.getAttributeUses());
730 xw.endElement();
731 def.getAttributeUses().accept(globalAttributeOutput);
732 }
733
734 public void visitRoot(RootDeclaration decl) {
735 decl.getParticle().accept(globalElementOutput);
736 }
737
738 public void visitComment(Comment comment) {
739 xw.comment(comment.getContent());
740 }
741 }
742
743 class MovedStructureOutput implements StructureVisitor<VoidValue> {
744 public VoidValue visitElement(Element element) {
745 if (!nsm.isGlobal(element)) {
746 xw.startElement(xs("group"));
747 xw.attribute("name", nsm.getProxyName(element));
748 particleOutput.context = NAMED_GROUP_CONTEXT;
749 particleOutput.visitElement(element);
750 xw.endElement();
751 }
752 globalElementOutput.visitElement(element);
753 return null;
754 }
755
756 public VoidValue visitAttribute(Attribute attribute) {
757 if (!nsm.isGlobal(attribute)) {
758 xw.startElement(xs("attributeGroup"));
759 xw.attribute("name", nsm.getProxyName(attribute));
760 attributeUseOutput.visitAttribute(attribute);
761 xw.endElement();
762 }
763 globalAttributeOutput.visitAttribute(attribute);
764 return null;
765 }
766 }
767
768 static void output(Schema schema, Guide guide, PrefixManager pm, OutputDirectory od,
769 Options options, ErrorReporter er) throws IOException {
770 NamespaceManager nsm = new NamespaceManager(schema, guide, pm);
771 ComplexTypeSelector cts = new ComplexTypeSelector(schema);
772 AbstractElementTypeSelector aets = new AbstractElementTypeSelector(schema, nsm, cts);
773 Set<Name> globalElementsDefined = new HashSet<Name>();
774 Set<Name> globalAttributesDefined = new HashSet<Name>();
775 try {
776 for (Schema sch : schema.getSubSchemas())
777 new BasicOutput(sch, er, od, options, nsm, pm, cts, aets,
778 globalElementsDefined, globalAttributesDefined).output();
779 }
780 catch (XmlWriter.WrappedException e) {
781 throw e.getIOException();
782 }
783 }
784
785 private BasicOutput(Schema schema, ErrorReporter er, OutputDirectory od, Options options,
786 NamespaceManager nsm, PrefixManager pm, ComplexTypeSelector complexTypeSelector,
787 AbstractElementTypeSelector abstractElementTypeSelector,
788 Set<Name> globalElementsDefined, Set<Name> globalAttributesDefined) throws IOException {
789 this.schema = schema;
790 this.nsm = nsm;
791 this.pm = pm;
792 this.complexTypeSelector = complexTypeSelector;
793 this.abstractElementTypeSelector = abstractElementTypeSelector;
794 this.globalElementsDefined = globalElementsDefined;
795 this.globalAttributesDefined = globalAttributesDefined;
796 this.sourceUri = schema.getUri();
797 this.od = od;
798 this.targetNamespace = nsm.getTargetNamespace(schema.getUri());
799 this.xsPrefix = pm.getPrefix(WellKnownNamespaces.XML_SCHEMA);
800 this.options = options;
801 OutputDirectory.Stream stream = od.open(schema.getUri(), schema.getEncoding());
802 xw = new XmlWriter(stream.getWriter(),
803 stream.getEncoding(),
804 stream.getCharRepertoire(),
805 od.getLineSeparator(),
806 od.getIndent(),
807 new String[0]);
808 }
809
810 private String topLevelPrefix(String ns) {
811 if (!nsm.isTargetNamespace(ns))
812 return null;
813 if (ns.equals(""))
814 return "";
815 return pm.getPrefix(ns);
816 }
817
818 private void output() {
819 outputCommentList(schema.getLeadingComments());
820 xw.startElement(xs("schema"));
821 xw.attribute("xmlns:" + xsPrefix, WellKnownNamespaces.XML_SCHEMA);
822 xw.attribute("elementFormDefault", "qualified");
823 if (!targetNamespace.equals(""))
824 xw.attribute("targetNamespace", targetNamespace);
825 for (String ns : nsm.getTargetNamespaces()) {
826 if (!ns.equals("")) {
827 String prefix = pm.getPrefix(ns);
828 if (!prefix.equals("xml"))
829 xw.attribute("xmlns:" + pm.getPrefix(ns), ns);
830 }
831 }
832 for (String uri : nsm.effectiveIncludes(schema.getUri()))
833 outputInclude(uri);
834 List<String> targetNamespaces = new Vector<String>();
835 targetNamespaces.addAll(nsm.getTargetNamespaces());
836 Collections.sort(targetNamespaces);
837 for (String ns : targetNamespaces) {
838 if (!ns.equals(targetNamespace))
839 outputImport(ns, nsm.getRootSchema(ns));
840 }
841 schema.accept(schemaOutput);
842 if (nsm.getRootSchema(targetNamespace).equals(sourceUri)) {
843 for (Structure structure : nsm.getMovedStructures(targetNamespace))
844 structure.accept(movedStructureOutput);
845 outputOther();
846 }
847 xw.endElement();
848 outputCommentList(schema.getTrailingComments());
849 xw.close();
850 }
851
852 private String xs(String name) {
853 return xsPrefix + ":" + name;
854 }
855
856 private boolean namespaceIsLocal(String ns) {
857 return ns.equals(targetNamespace) || ns.equals("");
858 }
859
860 private void outputAttributeUse(AttributeUse use) {
861 use.accept(attributeUseOutput);
862 use.accept(attributeWildcardOutput);
863 }
864
865 private void namespaceAttribute(Wildcard wc) {
866 if (wc.isPositive()) {
867 StringBuffer buf = new StringBuffer();
868 List<String> namespaces = new Vector<String>(wc.getNamespaces());
869 Collections.sort(namespaces);
870 for (String ns : namespaces) {
871 if (buf.length() > 0)
872 buf.append(' ');
873 if (ns.equals(""))
874 buf.append("##local");
875 else if (ns.equals(targetNamespace))
876 buf.append("##targetNamespace");
877 else
878 buf.append(ns);
879 }
880 xw.attribute("namespace", buf.toString());
881 }
882 else {
883 if (targetNamespace.equals(NamespaceManager.otherNamespace(wc)))
884 xw.attribute("namespace", "##other");
885 }
886 }
887
888 private String qualifyRef(String schemaUri, String localName) {
889 return qualifyName(nsm.getTargetNamespace(schemaUri), localName);
890 }
891
892 private String qualifyName(Name name) {
893 return qualifyName(name.getNamespaceUri(), name.getLocalName());
894 }
895
896 private String qualifyName(String ns, String localName) {
897 if (ns.equals(""))
898 return localName;
899 return pm.getPrefix(ns) + ":" + localName;
900 }
901
902 private void outputOther() {
903 String name = nsm.getOtherElementName(targetNamespace);
904 if (name != null) {
905 xw.startElement(xs("group"));
906 xw.attribute("name", name);
907 xw.startElement(xs("sequence"));
908 xw.startElement(xs("any"));
909 xw.attribute("namespace", "##other");
910 xw.attribute("processContents", options.anyProcessContents);
911 xw.endElement();
912 xw.endElement();
913 xw.endElement();
914 }
915 name = nsm.getOtherAttributeName(targetNamespace);
916 if (name != null) {
917 xw.startElement(xs("attributeGroup"));
918 xw.attribute("name", name);
919 xw.startElement(xs("anyAttribute"));
920 xw.attribute("namespace", "##other");
921 xw.attribute("processContents", options.anyAttributeProcessContents);
922 xw.endElement();
923 xw.endElement();
924 }
925 }
926
927 private void outputInclude(String href) {
928 xw.startElement(xs("include"));
929 xw.attribute("schemaLocation", od.reference(sourceUri, href));
930 xw.endElement();
931 }
932
933 private void outputImport(String ns, String href) {
934 xw.startElement(xs("import"));
935 if (!ns.equals(""))
936 xw.attribute("namespace", ns);
937 xw.attribute("schemaLocation", od.reference(sourceUri, href));
938 xw.endElement();
939 }
940
941 private void outputComplexTypeComplexContent(ComplexTypeComplexContentExtension t, String name, Annotated parent) {
942 String base = t.getBase();
943 if (base != null) {
944 base = qualifyRef(schema.getGroup(base).getParentSchema().getUri(), base);
945 if (name == null
946 && t.getParticle() == null
947 && !t.isMixed()
948 && t.getAttributeUses().equals(AttributeGroup.EMPTY)) {
949 xw.attribute("type", base);
950 if (parent != null)
951 outputAnnotation(parent);
952 return;
953 }
954 }
955 if (name == null && parent != null)
956 outputAnnotation(parent);
957 xw.startElement(xs("complexType"));
958 if (name != null)
959 xw.attribute("name", name);
960 if (t.isMixed())
961 xw.attribute("mixed", "true");
962 if (name != null && parent != null)
963 outputAnnotation(parent);
964 if (base != null) {
965 xw.startElement(xs("complexContent"));
966 xw.startElement(xs("extension"));
967 xw.attribute("base", base);
968 }
969 if (t.getParticle() != null) {
970 particleOutput.context = COMPLEX_TYPE_CONTEXT;
971 t.getParticle().accept(particleOutput);
972 }
973 outputAttributeUse(t.getAttributeUses());
974 if (base != null) {
975 xw.endElement();
976 xw.endElement();
977 }
978 xw.endElement();
979 }
980
981 private void outputComplexTypeSimpleContent(ComplexTypeSimpleContentExtension t, String name, Annotated parent) {
982 String base = t.getBase();
983 AttributeUse attributeUses = t.getAttributeUses();
984 if (base != null) {
985 base = qualifyRef(schema.getSimpleType(base).getParentSchema().getUri(), base);
986 if (name == null && attributeUses.equals(AttributeGroup.EMPTY)) {
987 xw.attribute("type", base);
988 if (parent != null)
989 outputAnnotation(parent);
990 return;
991 }
992 }
993 else if (attributeUses.equals(AttributeGroup.EMPTY)) {
994 simpleTypeOutput.outputWrap(t.getSimpleType(), parent);
995 return;
996 }
997 if (name == null && parent != null)
998 outputAnnotation(parent);
999 xw.startElement(xs("complexType"));
1000 if (name != null)
1001 xw.attribute("name", name);
1002 if (name != null && parent != null)
1003 outputAnnotation(parent);
1004 xw.startElement(xs("simpleContent"));
1005 if (base == null)
1006 base = t.getSimpleType().accept(simpleTypeNamer);
1007 if (base != null) {
1008 xw.startElement(xs("extension"));
1009 xw.attribute("base", base);
1010 }
1011 else {
1012 xw.startElement(xs("restriction"));
1013 xw.attribute("base", xs("anyType"));
1014 simpleTypeOutput.outputWrap(t.getSimpleType(), null);
1015 }
1016 outputAttributeUse(attributeUses);
1017 xw.endElement();
1018 xw.endElement();
1019 xw.endElement();
1020 }
1021
1022 private void outputComplexType(Name elementName, ComplexType ct, Annotated parent) {
1023 Name substitutionGroup = nsm.getSubstitutionGroup(elementName);
1024 if (substitutionGroup != null) {
1025 xw.attribute("substitutionGroup", qualifyName(substitutionGroup));
1026 if (ct != null && ct.equals(abstractElementTypeSelector.getAbstractElementType(substitutionGroup)))
1027 ct = null;
1028 }
1029 if (ct != null) {
1030 if (ct instanceof ComplexTypeNotAllowedContent) {
1031 xw.attribute("abstract", "true");
1032 outputAnnotation(parent);
1033 }
1034 else {
1035 complexTypeOutput.parent = parent;
1036 ct.accept(complexTypeOutput);
1037 }
1038 }
1039 else
1040 outputAnnotation(parent);
1041 }
1042
1043 private void outputAnnotation(Annotated annotated) {
1044 Annotation annotation = annotated.getAnnotation();
1045 if (annotation == null)
1046 return;
1047 xw.startElement(xs("annotation"));
1048 String documentation = annotation.getDocumentation();
1049 if (documentation != null) {
1050 xw.startElement(xs("documentation"));
1051 xw.text(documentation);
1052 xw.endElement();
1053 }
1054 xw.endElement();
1055 }
1056
1057 private void outputCommentList(List<Comment> list) {
1058 for (Comment comment : list)
1059 xw.comment((comment).getContent());
1060 }
1061 }