KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ccil > cowan > tagsoup > PYXWriter


1 // This file is part of TagSoup.
2
//
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation; either version 2 of the License, or
6
// (at your option) any later version. You may also distribute
7
// and/or modify it under version 2.1 of the Academic Free License.
8
//
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
//
13
//
14
// PYX Writer
15
// FIXME: does not do escapes in attribute values
16
// FIXME: outputs entities as bare '&' character
17

18 package org.ccil.cowan.tagsoup;
19 import java.io.*;
20 import org.xml.sax.*;
21 import org.xml.sax.ext.LexicalHandler JavaDoc;
22
23 /**
24 A ContentHandler that generates PYX format instead of XML.
25 Primarily useful for debugging.
26 **/

27 public class PYXWriter
28     implements ScanHandler, ContentHandler, LexicalHandler JavaDoc {
29
30     private PrintWriter theWriter; // where we write to
31
private static char[] dummy = new char[1];
32     private String JavaDoc attrName; // saved attribute name
33

34     // ScanHandler implementation
35

36     public void adup(char[] buff, int offset, int length) throws SAXException {
37         theWriter.println(attrName);
38         attrName = null;
39         }
40
41     public void aname(char[] buff, int offset, int length) throws SAXException {
42         theWriter.print('A');
43         theWriter.write(buff, offset, length);
44         theWriter.print(' ');
45         attrName = new String JavaDoc(buff, offset, length);
46         }
47
48     public void aval(char[] buff, int offset, int length) throws SAXException {
49         theWriter.write(buff, offset, length);
50         theWriter.println();
51         attrName = null;
52         }
53
54     public void cmnt(char [] buff, int offset, int length) throws SAXException {
55 // theWriter.print('!');
56
// theWriter.write(buff, offset, length);
57
// theWriter.println();
58
}
59
60     public void entity(char[] buff, int offset, int length) throws SAXException { }
61
62     public char getEntity() { return 0; }
63
64     public void eof(char[] buff, int offset, int length) throws SAXException {
65         theWriter.close();
66         }
67
68     public void etag(char[] buff, int offset, int length) throws SAXException {
69         theWriter.print(')');
70         theWriter.write(buff, offset, length);
71         theWriter.println();
72         }
73
74     public void decl(char[] buff, int offset, int length) throws SAXException {
75         }
76
77     public void gi(char[] buff, int offset, int length) throws SAXException {
78         theWriter.print('(');
79         theWriter.write(buff, offset, length);
80         theWriter.println();
81         }
82
83     public void pcdata(char[] buff, int offset, int length) throws SAXException {
84         if (length == 0) return; // nothing to do
85
boolean inProgress = false;
86         length += offset;
87         for (int i = offset; i < length; i++) {
88             if (buff[i] == '\n') {
89                 if (inProgress) {
90                     theWriter.println();
91                     }
92                 theWriter.println("-\\n");
93                 inProgress = false;
94                 }
95             else {
96                 if (!inProgress) {
97                     theWriter.print('-');
98                     }
99                 switch(buff[i]) {
100                 case '\t':
101                     theWriter.print("\\t");
102                     break;
103                 case '\\':
104                     theWriter.print("\\\\");
105                     break;
106                 default:
107                     theWriter.print(buff[i]);
108                     }
109                 inProgress = true;
110                 }
111             }
112         if (inProgress) {
113             theWriter.println();
114             }
115         }
116
117     public void pitarget(char[] buff, int offset, int length) throws SAXException {
118         theWriter.print('?');
119         theWriter.write(buff, offset, length);
120         theWriter.write(' ');
121         }
122
123     public void pi(char[] buff, int offset, int length) throws SAXException {
124         theWriter.write(buff, offset, length);
125         theWriter.println();
126         }
127
128     public void stagc(char[] buff, int offset, int length) throws SAXException {
129         theWriter.println("!"); // FIXME
130
}
131
132     public void stage(char[] buff, int offset, int length) throws SAXException {
133         theWriter.println("!"); // FIXME
134
}
135
136     // SAX ContentHandler implementation
137

138     public void characters(char[] buff, int offset, int length) throws SAXException {
139         pcdata(buff, offset, length);
140         }
141
142     public void endDocument() throws SAXException {
143         theWriter.close();
144         }
145
146     public void endElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname) throws SAXException {
147         if (qname.length() == 0) qname = localname;
148         theWriter.print(')');
149         theWriter.println(qname);
150         }
151
152     public void endPrefixMapping(String JavaDoc prefix) throws SAXException { }
153
154     public void ignorableWhitespace(char[] buff, int offset, int length) throws SAXException {
155         characters(buff, offset, length);
156         }
157
158     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException {
159         theWriter.print('?');
160         theWriter.print(target);
161         theWriter.print(' ');
162         theWriter.println(data);
163         }
164
165     public void setDocumentLocator(Locator locator) { }
166
167     public void skippedEntity(String JavaDoc name) throws SAXException { }
168
169     public void startDocument() throws SAXException { }
170
171     public void startElement(String JavaDoc uri, String JavaDoc localname, String JavaDoc qname,
172             Attributes atts) throws SAXException {
173         if (qname.length() == 0) qname=localname;
174         theWriter.print('(');
175         theWriter.println(qname);
176         int length = atts.getLength();
177         for (int i = 0; i < length; i++) {
178             qname = atts.getQName(i);
179             if (qname.length() == 0) qname = atts.getLocalName(i);
180             theWriter.print('A');
181 // theWriter.print(atts.getType(i)); // DEBUG
182
theWriter.print(qname);
183             theWriter.print(' ');
184             theWriter.println(atts.getValue(i));
185             }
186         }
187
188     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException { }
189
190     // Default LexicalHandler implementation
191

192     public void comment(char[] ch, int start, int length) throws SAXException {
193         cmnt(ch, start, length);
194         }
195     public void endCDATA() throws SAXException { }
196     public void endDTD() throws SAXException { }
197     public void endEntity(String JavaDoc name) throws SAXException { }
198     public void startCDATA() throws SAXException { }
199     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException { }
200     public void startEntity(String JavaDoc name) throws SAXException { }
201
202     // Constructor
203

204     public PYXWriter(Writer w) {
205         if (w instanceof PrintWriter) {
206             theWriter = (PrintWriter)w;
207             }
208         else {
209             theWriter = new PrintWriter(w);
210             }
211         }
212     }
213
Popular Tags