01: package com.jclark.xml.apps;
02:
03: import java.io.*;
04: import com.jclark.xml.parse.*;
05: import com.jclark.xml.parse.io.*;
06: import com.jclark.xml.output.*;
07:
08: /**
09: * @version $Revision: 1.7 $ $Date: 1998/05/08 06:42:17 $
10: */
11: public class Normalize extends ApplicationImpl {
12:
13: private final XMLWriter w;
14:
15: /**
16: * Writes a normalized version of an XML document to the standard
17: * output.
18: * If an argument is specified, then that is treated as the filename
19: * of the XML document, otherwise the XML document is read from the
20: * standard input.
21: */
22: public static void main(String args[]) throws IOException {
23: if (args.length > 1) {
24: System.err
25: .println("usage: jview com.jclark.xml.apps.Normalize [file]");
26: System.exit(1);
27: }
28: Parser parser = new ParserImpl();
29: parser.setApplication(new Normalize(new UTF8XMLWriter(
30: new FileOutputStream(FileDescriptor.out))));
31: try {
32: parser.parseDocument(args.length == 0 ? EntityManagerImpl
33: .openStandardInput() : EntityManagerImpl
34: .openFile(args[0]));
35: } catch (NotWellFormedException e) {
36: System.err.println(e.getMessage());
37: System.exit(1);
38: }
39: }
40:
41: public Normalize(XMLWriter w) {
42: this .w = w;
43: }
44:
45: public void startElement(StartElementEvent event)
46: throws IOException {
47: w.startElement(event.getName());
48: int nAtts = event.getAttributeCount();
49: for (int i = 0; i < nAtts; i++)
50: w.attribute(event.getAttributeName(i), event
51: .getAttributeValue(i));
52: }
53:
54: public void endElement(EndElementEvent event) throws IOException {
55: w.endElement(event.getName());
56: }
57:
58: public void endDocument() throws IOException {
59: w.write('\n');
60: w.flush();
61: }
62:
63: public void processingInstruction(ProcessingInstructionEvent event)
64: throws IOException {
65: w
66: .processingInstruction(event.getName(), event
67: .getInstruction());
68: }
69:
70: public void characterData(CharacterDataEvent event)
71: throws IOException {
72: event.writeChars(w);
73: }
74: }
|