KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > Writer


1 /*
2  * (C) Copyright 2002-2005, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

7  
8 package test;
9
10 import org.cyberneko.html.filters.DefaultFilter;
11
12 import java.io.OutputStream JavaDoc;
13 import java.io.OutputStreamWriter JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.UnsupportedEncodingException JavaDoc;
16
17 import org.apache.xerces.util.XMLStringBuffer;
18 import org.apache.xerces.xni.Augmentations;
19 import org.apache.xerces.xni.NamespaceContext;
20 import org.apache.xerces.xni.QName;
21 import org.apache.xerces.xni.XMLAttributes;
22 import org.apache.xerces.xni.XMLLocator;
23 import org.apache.xerces.xni.XMLString;
24 import org.apache.xerces.xni.XNIException;
25
26 /**
27  * This class implements an filter to output "canonical" files for
28  * regression testing.
29  *
30  * @author Andy Clark
31  */

32 public class Writer
33     extends DefaultFilter {
34
35     //
36
// Data
37
//
38

39     /** Writer. */
40     protected PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out);
41
42     // temp vars
43

44     /** String buffer for collecting text content. */
45     private final XMLStringBuffer fStringBuffer = new XMLStringBuffer();
46
47     //
48
// Constructors
49
//
50

51     /**
52      * Creates a writer to the standard output stream using UTF-8
53      * encoding.
54      */

55     public Writer() {
56         this(System.out);
57     } // <init>()
58

59     /**
60      * Creates a writer with the specified output stream using UTF-8
61      * encoding.
62      */

63     public Writer(OutputStream JavaDoc stream) {
64         this(stream, "UTF8");
65     } // <init>(OutputStream)
66

67     /** Creates a writer with the specified output stream and encoding. */
68     public Writer(OutputStream JavaDoc stream, String JavaDoc encoding) {
69         try {
70             out = new PrintWriter JavaDoc(new OutputStreamWriter JavaDoc(stream, encoding), true);
71         }
72         catch (UnsupportedEncodingException JavaDoc e) {
73             throw new RuntimeException JavaDoc("JVM must have "+encoding+" decoder");
74         }
75     } // <init>(OutputStream,String)
76

77     /** Creates a writer with the specified Java Writer. */
78     public Writer(java.io.Writer JavaDoc writer) {
79         out = new PrintWriter JavaDoc(writer);
80     } // <init>(java.io.Writer)
81

82     //
83
// XMLDocumentHandler methods
84
//
85

86     // since Xerces-J 2.2.0
87

88     /** Start document. */
89     public void startDocument(XMLLocator locator, String JavaDoc encoding,
90                               NamespaceContext nscontext, Augmentations augs) throws XNIException {
91         fStringBuffer.clear();
92     } // startDocument(XMLLocator,String,NamespaceContext,Augmentations)
93

94     // old methods
95

96     /** Start document. */
97     public void startDocument(XMLLocator locator, String JavaDoc encoding, Augmentations augs) throws XNIException {
98         startDocument(locator, encoding, null, augs);
99     } // startDocument(XMLLocator,String,Augmentations)
100

101     /** XML declaration. */
102     public void xmlDecl(String JavaDoc version, String JavaDoc encoding, String JavaDoc standalone,
103                         Augmentations augs) throws XNIException {
104         if (version!=null) {
105             out.print("xversion ");
106             out.println(version);
107         }
108         if (encoding!=null) {
109             out.print("xencoding ");
110             out.println(encoding);
111         }
112         if (standalone!=null) {
113             out.print("xstandalone ");
114             out.println(standalone);
115         }
116         out.flush();
117     } // xmlDecl(String,String,String,Augmentations)
118

119     /** Doctype declaration. */
120     public void doctypeDecl(String JavaDoc root, String JavaDoc pubid, String JavaDoc sysid, Augmentations augs) throws XNIException {
121         chars();
122         out.print('!');
123         if (root != null) {
124             out.print(root);
125         }
126         out.println();
127         if (pubid != null) {
128             out.print('p');
129             out.print(pubid);
130             out.println();
131         }
132         if (sysid != null) {
133             out.print('s');
134             out.print(sysid);
135             out.println();
136         }
137         out.flush();
138     } // doctypeDecl(String,String,String,Augmentations)
139

140     /** Processing instruction. */
141     public void processingInstruction(String JavaDoc target, XMLString data, Augmentations augs) throws XNIException {
142         chars();
143         out.print('?');
144         out.print(target);
145         if (data != null && data.length > 0) {
146             out.print(' ');
147             print(data.toString());
148         }
149         out.println();
150         out.flush();
151     } // processingInstruction(String,XMLString,Augmentations)
152

153     /** Comment. */
154     public void comment(XMLString text, Augmentations augs) throws XNIException {
155         chars();
156         out.print('#');
157         print(text.toString());
158         out.println();
159         out.flush();
160     } // comment(XMLString,Augmentations)
161

162     /** Start element. */
163     public void startElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
164         chars();
165         out.print('(');
166         out.print(element.rawname);
167         int acount = attrs != null ? attrs.getLength() : 0;
168         if (acount > 0) {
169             String JavaDoc[] anames = new String JavaDoc[acount];
170             String JavaDoc[] auris = new String JavaDoc[acount];
171             sortAttrNames(attrs, anames, auris);
172             for (int i = 0; i < acount; i++) {
173                 String JavaDoc aname = anames[i];
174                 out.println();
175                 out.flush();
176                 out.print('A');
177                 if (auris[i] != null) {
178                     out.print('{');
179                     out.print(auris[i]);
180                     out.print('}');
181                 }
182                 out.print(aname);
183                 out.print(' ');
184                 print(attrs.getValue(aname));
185             }
186         }
187         out.println();
188         out.flush();
189     } // startElement(QName,XMLAttributes,Augmentations)
190

191     /** End element. */
192     public void endElement(QName element, Augmentations augs) throws XNIException {
193         chars();
194         out.print(')');
195         out.print(element.rawname);
196         out.println();
197         out.flush();
198     } // endElement(QName,Augmentations)
199

200     /** Empty element. */
201     public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException {
202         startElement(element, attrs, augs);
203         endElement(element, augs);
204     } // emptyElement(QName,XMLAttributes,Augmentations)
205

206     /** Characters. */
207     public void characters(XMLString text, Augmentations augs) throws XNIException {
208         fStringBuffer.append(text);
209     } // characters(XMLString,Augmentations)
210

211     /** Ignorable whitespace. */
212     public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException {
213         characters(text, augs);
214     } // ignorableWhitespace(XMLString,Augmentations)
215

216     //
217
// Protected methods
218
//
219

220     /** Prints collected characters. */
221     protected void chars() {
222         if (fStringBuffer.length == 0) {
223             return;
224         }
225         out.print('"');
226         print(fStringBuffer.toString());
227         out.println();
228         out.flush();
229         fStringBuffer.clear();
230     } // chars()
231

232     /** Prints the specified string. */
233     protected void print(String JavaDoc s) {
234         int length = s != null ? s.length() : 0;
235         for (int i = 0; i < length; i++) {
236             char c = s.charAt(i);
237             switch (c) {
238                 case '\n': {
239                     out.print("\\n");
240                     break;
241                 }
242                 case '\r': {
243                     out.print("\\r");
244                     break;
245                 }
246                 case '\t': {
247                     out.print("\\t");
248                     break;
249                 }
250                 case '\\': {
251                     out.print("\\\\");
252                     break;
253                 }
254                 default: {
255                     out.print(c);
256                 }
257             }
258         }
259     } // print(String)
260

261     //
262
// Protected static methods
263
//
264

265     /** Sorts the attribute names. */
266     protected static void sortAttrNames(XMLAttributes attrs,
267                                         String JavaDoc[] anames, String JavaDoc[] auris) {
268         for (int i = 0; i < anames.length; i++) {
269             anames[i] = attrs.getQName(i);
270             auris[i] = attrs.getURI(i);
271         }
272         // NOTE: This is super inefficient but it doesn't really matter. -Ac
273
for (int i = 0; i < anames.length - 1; i++) {
274             int index = i;
275             for (int j = i + 1; j < anames.length; j++) {
276                 if (anames[j].compareTo(anames[index]) < 0) {
277                     index = j;
278                 }
279             }
280             if (index != i) {
281                 String JavaDoc tn = anames[i];
282                 anames[i] = anames[index];
283                 anames[index] = tn;
284                 String JavaDoc tu = auris[i];
285                 auris[i] = auris[index];
286                 auris[index] = tu;
287             }
288         }
289     } // sortAttrNames(XMLAttributes,String[])
290

291     //
292
// MAIN
293
//
294

295     /** Main program. */
296     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
297         org.apache.xerces.xni.parser.XMLDocumentFilter[] filters = {
298             new Writer(),
299         };
300         org.apache.xerces.xni.parser.XMLParserConfiguration parser =
301             new org.cyberneko.html.HTMLConfiguration();
302         parser.setProperty("http://cyberneko.org/html/properties/filters", filters);
303         for (int i = 0; i < argv.length; i++) {
304             org.apache.xerces.xni.parser.XMLInputSource source =
305                 new org.apache.xerces.xni.parser.XMLInputSource(null, argv[i], null);
306             parser.parse(source);
307         }
308     } // main(String[])
309

310 } // class Writer
311
Popular Tags