KickJava   Java API By Example, From Geeks To Geeks.

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


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
// This file is part of TagSoup.
15
//
16
// This program is free software; you can redistribute it and/or modify
17
// it under the terms of the GNU General Public License as published by
18
// the Free Software Foundation; either version 2 of the License, or
19
// (at your option) any later version. You may also distribute
20
// and/or modify it under version 2.1 of the Academic Free License.
21
//
22
// This program is distributed in the hope that it will be useful,
23
// but WITHOUT ANY WARRANTY; without even the implied warranty of
24
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25
//
26
//
27
// PYX Scanner
28

29 package org.ccil.cowan.tagsoup;
30 import java.io.*;
31 import org.xml.sax.SAXException JavaDoc;
32
33 /**
34 A Scanner that accepts PYX format instead of HTML.
35 Useful primarily for debugging.
36 **/

37 public class PYXScanner implements Scanner {
38
39         public void resetDocumentLocator(String JavaDoc publicid, String JavaDoc systemid) {
40     // Need this method for interface compatibility, but note
41
// that PyxScanner does not implement Locator.
42
}
43
44     public void scan(Reader r, ScanHandler h) throws IOException, SAXException JavaDoc {
45         BufferedReader br = new BufferedReader(r);
46         String JavaDoc s;
47         char[] buff = null;
48         boolean instag = false;
49         while ((s = br.readLine()) != null) {
50             int size = s.length();
51             if (buff == null || buff.length < size) {
52                 buff = new char[size];
53                 }
54             s.getChars(0, size, buff, 0);
55             switch (buff[0]) {
56             case '(':
57                 if (instag) {
58                     h.stagc(buff, 0, 0);
59                     instag = false;
60                     }
61                 h.gi(buff, 1, size - 1);
62                 instag = true;
63                 break;
64             case ')':
65                 if (instag) {
66                     h.stagc(buff, 0, 0);
67                     instag = false;
68                     }
69                 h.etag(buff, 1, size - 1);
70                 break;
71             case '?':
72                 if (instag) {
73                     h.stagc(buff, 0, 0);
74                     instag = false;
75                     }
76                 h.pi(buff, 1, size - 1);
77                 break;
78             case 'A':
79                 int sp = s.indexOf(' ');
80                 h.aname(buff, 1, sp - 1);
81                 h.aval(buff, sp + 1, size - sp - 1);
82                 break;
83             case '-':
84                 if (instag) {
85                     h.stagc(buff, 0, 0);
86                     instag = false;
87                     }
88                 if (s.equals("-\\n")) {
89                     buff[0] = '\n';
90                     h.pcdata(buff, 0, 1);
91                     }
92                 else {
93                     // FIXME:
94
// Does not decode \t and \\ in input
95
h.pcdata(buff, 1, size - 1);
96                     }
97                 break;
98             case 'E':
99                 if (instag) {
100                     h.stagc(buff, 0, 0);
101                     instag = false;
102                     }
103                 h.entity(buff, 1, size - 1);
104                 break;
105             default:
106 // System.err.print("Gotcha ");
107
// System.err.print(s);
108
// System.err.print('\n');
109
break;
110                 }
111             }
112         h.eof(buff, 0, 0);
113         }
114
115     public void startCDATA() { }
116
117     public static void main(String JavaDoc[] argv) throws IOException, SAXException JavaDoc {
118         Scanner s = new PYXScanner();
119         Reader r = new InputStreamReader(System.in, "UTF-8");
120         Writer w = new BufferedWriter(new OutputStreamWriter(System.out, "UTF-8"));
121         s.scan(r, new PYXWriter(w));
122         }
123     }
124
Popular Tags