KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beansdev > gen > XMLWriter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beansdev.gen;
21
22 import java.io.*;
23 import java.util.*;
24
25 public class XMLWriter extends IndentingWriter {
26     protected String JavaDoc xmlVersion = "1.0";
27     protected String JavaDoc encoding = "UTF-8";
28     protected boolean header = true;
29     protected Stack tags;
30
31     public int HEADER_SECTION = 0;
32     public int BODY_SECTION = 1;
33     static final protected int defaultSectionCount = 2;
34
35     public XMLWriter() {
36         super(defaultSectionCount);
37         privateInit();
38     }
39
40     public XMLWriter(boolean header) {
41         super(defaultSectionCount);
42         this.header = header;
43         privateInit();
44     }
45     
46     public XMLWriter(String JavaDoc encoding) {
47         super(defaultSectionCount);
48         this.encoding = encoding;
49         privateInit();
50     }
51     
52     public XMLWriter(String JavaDoc encoding, String JavaDoc xmlVersion) {
53         super(defaultSectionCount);
54         this.encoding = encoding;
55         this.xmlVersion = xmlVersion;
56         privateInit();
57     }
58
59     /**
60      * Insert a custom section after another section.
61      * eg:
62      * JavaWriter jw = new JavaWriter();
63      * int SPECIAL_SECTION = jw.insertSectionAfter(jw.CONSTRUCTOR_SECTION);
64      */

65     public int insertSectionAfter(int sectionNum) {
66         insertAdditionalBuffers(sectionNum, 1);
67         if (sectionNum < HEADER_SECTION) ++HEADER_SECTION;
68         if (sectionNum < BODY_SECTION) ++BODY_SECTION;
69         return sectionNum + 1;
70     }
71
72     public void reset() {
73         super.reset();
74         privateInit();
75     }
76
77     private void privateInit() {
78         tags = new Stack();
79         try {
80             if (header) {
81                 select(HEADER_SECTION);
82                 write("<?xml version='");
83                 write(xmlVersion);
84                 write("' encoding='");
85                 write(encoding);
86                 write("' ?>\n");
87             }
88             select(BODY_SECTION);
89         } catch (IOException e) {
90             // This exception should not occur.
91
throw new RuntimeException JavaDoc(e);
92         }
93     }
94     
95     public void startTag(String JavaDoc tag) throws IOException {
96         startTag(null, tag, null, true);
97     }
98
99     public void startTag(String JavaDoc tag, boolean finish) throws IOException {
100         startTag(null, tag, null, finish);
101     }
102
103     public void startTag(String JavaDoc namespace, String JavaDoc tag)
104         throws IOException {
105         startTag(namespace, tag, null, true);
106     }
107
108     public void startTag(String JavaDoc namespace, String JavaDoc tag, boolean finish)
109         throws IOException {
110         startTag(namespace, tag, null, finish);
111     }
112
113     public void startTag(String JavaDoc namespace, String JavaDoc tag, String JavaDoc attributeString)
114         throws IOException {
115         startTag(namespace, tag, attributeString, true);
116     }
117
118     /**
119      * @param finish Whether or not to add the finishing ">", if not then
120      * setFirst(" ") is called anticipating the addition of
121      * attributes.
122      * @param attributeString The attributes. Make sure to XML escape.
123      */

124     public void startTag(String JavaDoc namespace, String JavaDoc tag, String JavaDoc attributeString,
125                          boolean finish) throws IOException {
126         String JavaDoc fullTag;
127         if (namespace != null)
128             fullTag = namespace+":"+tag;
129         else
130             fullTag = tag;
131         tags.push(fullTag);
132         write("<");
133         write(fullTag);
134         if (attributeString != null) {
135             if (!attributeString.startsWith(" "))
136                 write(" ");
137             write(attributeString);
138         }
139         if (finish)
140             finishStartTag();
141         else
142             setFirst(" ");
143         indentRight();
144     }
145
146     /**
147      * Finish the start tag, and we expect to have children
148      * (ie, that means the client will call endTag).
149      */

150     public void finishStartTag() throws IOException {
151         write(">");
152     }
153
154     /**
155      * @param children if false, then there are no children, and
156      * endTag is called automatically.
157      */

158     public void finishStartTag(boolean children, boolean useCr) throws IOException {
159         if (!children) {
160             write("/");
161             indentLeft();
162             tags.pop();
163         }
164         finishStartTag();
165         if (useCr)
166             cr();
167     }
168
169     public void endTag() throws IOException {
170         endTag(true);
171     }
172     
173     public void endTag(boolean useCr) throws IOException {
174         indentLeft();
175         String JavaDoc fullTag = (String JavaDoc) tags.pop();
176         write("</");
177         write(fullTag);
178         write(">");
179         if (useCr)
180             cr();
181     }
182 }
183
Popular Tags