View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.rat.tools.xsd;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  import org.apache.rat.report.xml.writer.XmlWriter;
25  
26  /**
27   * A writer that writes XSD nodes.
28   */
29  public class XsdWriter {
30      /** The XML Writer that this writer uses */
31      private final XmlWriter writer;
32  
33      /** Types of elements in the XSD */
34      public enum Type {
35          /** An element */
36          ELEMENT("xs:element"),
37          /** An attribute */
38          ATTRIBUTE("xs:attribute"),
39          /** A complex type */
40          COMPLEX("xs:complexType"),
41          /** A sequence */
42          SEQUENCE("xs:sequence"),
43          /** A simple type */
44          SIMPLE("xs:simpleContent"),
45          /** An extension */
46          EXTENSION("xs:extension"),
47          /** A choice */
48          CHOICE("xs:choice"),
49          /** A complex type */
50          COMPLEX_CONTENT("xs:complexContent");
51          /** The element name associated with the type */
52          private final String elementName;
53  
54          /**
55           * Type constructor.
56           *
57           * @param name The element name associated with the type.
58           */
59          Type(final String name) {
60              elementName = name;
61          }
62      }
63  
64      /**
65       * Creates an XSD writer that wraps a standard Writer.
66       * @param writer the writer to wrap.
67       */
68      public XsdWriter(final Writer writer) {
69          this.writer = new XmlWriter(writer);
70      }
71  
72      /**
73       * Initializes the writer. Writes the initial "xs:schema tag" .
74       * @return the Writer.
75       * @throws IOException on error.
76       */
77      public XsdWriter init() throws IOException {
78          writer.startDocument()
79          .openElement("xs:schema")
80          .attribute("attributeFormDefault", "unqualified")
81                  .attribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
82          return this;
83      }
84  
85      /**
86       * Finishes the process. Closes the document.
87       * @throws IOException on error.
88       */
89      public void finish() throws IOException {
90          writer.closeDocument();
91      }
92  
93      /**
94       * Writes an attribute map, each pair of items in the string list are considered attribute name and value.
95       * @param attributeMap the array of attribute names and values.
96       * @throws IOException on error.
97       */
98      private void writeAttributes(final String[] attributeMap) throws IOException {
99          if (attributeMap != null) {
100             for (int i = 0; i < attributeMap.length; i += 2) {
101                 writer.attribute(attributeMap[i], attributeMap[i + 1]);
102             }
103         }
104     }
105 
106     /**
107      * Opens (Starts) an element of the specified type along with its attributes.
108      * @param type the Type to start.
109      * @param attributeMap the attributes for the element.
110      * @return this.
111      * @throws IOException on error.
112      */
113     public XsdWriter open(final Type type, final String... attributeMap) throws IOException {
114         writer.openElement(type.elementName);
115         writeAttributes(attributeMap);
116         return this;
117     }
118 
119     /**
120      * Writes the attributes
121      * @param name The name of the attribute.
122      * @param attributeMap the attributes of the attribute.
123      * @return this.
124      * @throws IOException on error.
125      */
126     public XsdWriter attribute(final String name, final String... attributeMap) throws IOException {
127         writer.openElement("xs:attribute").attribute("name", name);
128         writeAttributes(attributeMap);
129         writer.closeElement();
130         return this;
131     }
132 
133     /**
134      * Closes (Ends) the element for the type.
135      * @param type The type to close.
136      * @return this.
137      * @throws IOException on error
138      */
139     public XsdWriter close(final Type type) throws IOException {
140         writer.closeElement(type.elementName);
141         return this;
142     }
143 }