aboutsummaryrefslogtreecommitdiff
path: root/src/org/ccil/cowan/tagsoup/PYXWriter.java
blob: 81917dd1964a9f412501248d45cb17b3d10953aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// This file is part of TagSoup and is Copyright 2002-2008 by John Cowan.
//
// TagSoup is licensed under the Apache License,
// Version 2.0.  You may obtain a copy of this license at
// http://www.apache.org/licenses/LICENSE-2.0 .  You may also have
// additional legal rights not granted by this license.
//
// TagSoup is distributed in the hope that it will be useful, but
// unless required by applicable law or agreed to in writing, TagSoup
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied; not even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// 
// 
// PYX Writer
// FIXME: does not do escapes in attribute values
// FIXME: outputs entities as bare '&' character

package org.ccil.cowan.tagsoup;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;

/**
A ContentHandler that generates PYX format instead of XML.
Primarily useful for debugging.
**/
public class PYXWriter
	implements ScanHandler, ContentHandler, LexicalHandler {

	private PrintWriter theWriter;		// where we write to
	private static char[] dummy = new char[1];
	private String attrName;		// saved attribute name

	// ScanHandler implementation

	public void adup(char[] buff, int offset, int length) throws SAXException {
		theWriter.println(attrName);
		attrName = null;
		}

	public void aname(char[] buff, int offset, int length) throws SAXException {
		theWriter.print('A');
		theWriter.write(buff, offset, length);
		theWriter.print(' ');
		attrName = new String(buff, offset, length);
		}

	public void aval(char[] buff, int offset, int length) throws SAXException {
		theWriter.write(buff, offset, length);
		theWriter.println();
		attrName = null;
		}

	public void cmnt(char [] buff, int offset, int length) throws SAXException {
//		theWriter.print('!');
//		theWriter.write(buff, offset, length);
//		theWriter.println();
		}

	public void entity(char[] buff, int offset, int length) throws SAXException { }

	public int getEntity() { return 0; }

	public void eof(char[] buff, int offset, int length) throws SAXException {
		theWriter.close();
		}

	public void etag(char[] buff, int offset, int length) throws SAXException {
		theWriter.print(')');
		theWriter.write(buff, offset, length);
		theWriter.println();
		}

	public void decl(char[] buff, int offset, int length) throws SAXException {
        }

	public void gi(char[] buff, int offset, int length) throws SAXException {
		theWriter.print('(');
		theWriter.write(buff, offset, length);
		theWriter.println();
		}

	public void cdsect(char[] buff, int offset, int length) throws SAXException {
		pcdata(buff, offset, length);
		}

	public void pcdata(char[] buff, int offset, int length) throws SAXException {
		if (length == 0) return;	// nothing to do
		boolean inProgress = false;
		length += offset;
		for (int i = offset; i < length; i++) {
			if (buff[i] == '\n') {
				if (inProgress) {
					theWriter.println();
					}
				theWriter.println("-\\n");
				inProgress = false;
				}
			else {
				if (!inProgress) {
					theWriter.print('-');
					}
				switch(buff[i]) {
				case '\t':
					theWriter.print("\\t");
					break;
				case '\\':
					theWriter.print("\\\\");
					break;
				default:
					theWriter.print(buff[i]);
					}
				inProgress = true;
				}
			}
		if (inProgress) {
			theWriter.println();
			}
		}

	public void pitarget(char[] buff, int offset, int length) throws SAXException {
		theWriter.print('?');
		theWriter.write(buff, offset, length);
		theWriter.write(' ');
		}

	public void pi(char[] buff, int offset, int length) throws SAXException {
		theWriter.write(buff, offset, length);
		theWriter.println();
		}

	public void stagc(char[] buff, int offset, int length) throws SAXException {
//		theWriter.println("!");			// FIXME
		}

	public void stage(char[] buff, int offset, int length) throws SAXException {
		theWriter.println("!");			// FIXME
		}

	// SAX ContentHandler implementation

	public void characters(char[] buff, int offset, int length) throws SAXException {
		pcdata(buff, offset, length);
		}

	public void endDocument() throws SAXException {
		theWriter.close();
		}

	public void endElement(String uri, String localname, String qname) throws SAXException {
		if (qname.length() == 0) qname = localname;
		theWriter.print(')');
		theWriter.println(qname);
		}

	public void endPrefixMapping(String prefix) throws SAXException { }

	public void ignorableWhitespace(char[] buff, int offset, int length) throws SAXException {
		characters(buff, offset, length);
		}

	public void processingInstruction(String target, String data) throws SAXException {
		theWriter.print('?');
		theWriter.print(target);
		theWriter.print(' ');
		theWriter.println(data);
		}

	public void setDocumentLocator(Locator locator) { }

	public void skippedEntity(String name) throws SAXException { }

	public void startDocument() throws SAXException { }

	public void startElement(String uri, String localname, String qname,
			Attributes atts) throws SAXException {
		if (qname.length() == 0) qname=localname;
		theWriter.print('(');
		theWriter.println(qname);
		int length = atts.getLength();
		for (int i = 0; i < length; i++) {
			qname = atts.getQName(i);
			if (qname.length() == 0) qname = atts.getLocalName(i);
			theWriter.print('A');
//			theWriter.print(atts.getType(i));	// DEBUG
			theWriter.print(qname);
			theWriter.print(' ');
			theWriter.println(atts.getValue(i));
			}
		}

	public void startPrefixMapping(String prefix, String uri) throws SAXException { }

	// Default LexicalHandler implementation

	public void comment(char[] ch, int start, int length) throws SAXException {
		cmnt(ch, start, length);
		}
	public void endCDATA() throws SAXException { }
	public void endDTD() throws SAXException { }
	public void endEntity(String name) throws SAXException { }
	public void startCDATA() throws SAXException { }
	public void startDTD(String name, String publicId, String systemId) throws SAXException { }
	public void startEntity(String name) throws SAXException { }

	// Constructor

	public PYXWriter(Writer w) {
		if (w instanceof PrintWriter) {
			theWriter = (PrintWriter)w;
			}
		else {
			theWriter = new PrintWriter(w);
			}
		}
	}