| 1 | |
package no.sesat.mojo.modes; |
| 2 | |
|
| 3 | |
import java.util.Iterator; |
| 4 | |
import java.util.Set; |
| 5 | |
import java.util.TreeSet; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
public class GenerateXSD extends GenerateSchemaFile { |
| 11 | 0 | private final Set<String> written = new TreeSet<String>(); |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
protected GenerateXSD(final ConfigElement element, final String name, final String idString) { |
| 22 | 0 | super(element, name, idString); |
| 23 | 0 | } |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
@Override |
| 29 | |
protected void runImpl() { |
| 30 | 0 | println("<?xml version='1.0'?>"); |
| 31 | 0 | println("<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' id='" + id + "'>"); |
| 32 | 0 | indent(); |
| 33 | 0 | println("<xsd:element name='" + root.name + "' type='" + root.name + "'/>"); |
| 34 | 0 | generate(root); |
| 35 | 0 | unindent(); |
| 36 | 0 | print("</xsd:schema>"); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
private void generate(final ConfigElement element) { |
| 40 | 0 | if (written.add(element.name)) { |
| 41 | 0 | printlnI("<xsd:complexType name='" + element.name + "'>"); |
| 42 | 0 | if (element.hasDoc()) { |
| 43 | 0 | printlnI("<xsd:annotation>"); |
| 44 | 0 | printlnI("<xsd:documentation>"); |
| 45 | 0 | println("<![CDATA[" + element.doc + "]]>)"); |
| 46 | 0 | printlnU("</xsd:documentation>"); |
| 47 | 0 | printlnU("</xsd:annotation>"); |
| 48 | |
} |
| 49 | |
|
| 50 | 0 | printlnI("<xsd:choice minOccurs='0' maxOccurs='unbounded'>"); |
| 51 | 0 | for (int i = 0; i < element.children.size(); i++) { |
| 52 | 0 | final ConfigElement child = element.children.get(i); |
| 53 | 0 | println("<xsd:element name='" + child.name + "' type='" + child.name + "'/>"); |
| 54 | |
} |
| 55 | 0 | printlnU("</xsd:choice>"); |
| 56 | |
|
| 57 | 0 | for (final Iterator<ConfigAttribute> iterator = element.attributes.iterator(); iterator.hasNext();) { |
| 58 | 0 | final ConfigAttribute attrib = iterator.next(); |
| 59 | 0 | if (attrib.hasDoc()) { |
| 60 | 0 | printlnI("<xsd:attribute name='" + attrib.name + "'>"); |
| 61 | 0 | printlnI("<xsd:annotation>"); |
| 62 | 0 | printlnI("<xsd:documentation>"); |
| 63 | 0 | println("<![CDATA[" + attrib.doc + "]]>)"); |
| 64 | 0 | printlnU("</xsd:documentation>"); |
| 65 | 0 | printlnU("</xsd:annotation>"); |
| 66 | 0 | printlnU("</xsd:attribute>"); |
| 67 | |
} else { |
| 68 | 0 | println("<xsd:attribute name='" + attrib.name + "'/>"); |
| 69 | |
} |
| 70 | 0 | } |
| 71 | 0 | printlnU("</xsd:complexType>"); |
| 72 | |
|
| 73 | |
} |
| 74 | 0 | for (ConfigElement child : element.children) { |
| 75 | 0 | if (!written.contains(child.name)) { |
| 76 | 0 | generate(child); |
| 77 | |
} |
| 78 | |
} |
| 79 | 0 | } |
| 80 | |
} |