KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > text > sax > Test


1 package com.quadcap.text.sax;
2
3 /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.BufferedReader JavaDoc;
42 import java.io.CharArrayWriter JavaDoc;
43 import java.io.FileReader JavaDoc;
44 import java.io.IOException JavaDoc;
45 import java.io.Reader JavaDoc;
46
47 import org.xml.sax.AttributeList JavaDoc;
48 import org.xml.sax.DocumentHandler JavaDoc;
49 import org.xml.sax.DTDHandler JavaDoc;
50 import org.xml.sax.EntityResolver JavaDoc;
51 import org.xml.sax.ErrorHandler JavaDoc;
52 import org.xml.sax.InputSource JavaDoc;
53 import org.xml.sax.Locator JavaDoc;
54 import org.xml.sax.SAXException JavaDoc;
55
56 import org.xml.sax.helpers.AttributeListImpl JavaDoc;
57 import org.xml.sax.helpers.ParserFactory JavaDoc;
58
59 /**
60  * Package level tests.
61  *
62  * @author Stan Bailes
63  */

64 public class Test extends com.quadcap.util.Test
65 implements DocumentHandler JavaDoc, EntityResolver JavaDoc {
66     public Test() {}
67
68     final void msg(String JavaDoc s) {
69         writer.println(s);
70     }
71     
72     public void characters(char[] ch, int off, int len) {
73         msg("characters(" + new String JavaDoc(ch,off,len) + ")");
74     }
75
76     public void endDocument() {
77         msg("endDocument");
78     }
79
80     public void endElement(String JavaDoc name) {
81         msg("endElement(" + name + ")");
82     }
83
84     public void ignorableWhitespace(char[] ch, int off, int len) {
85         msg("ignorableWhitespace(" + new String JavaDoc(ch,off,len) + ")");
86     }
87
88     public void processingInstruction(String JavaDoc target, String JavaDoc data) {
89         msg("processingInstruction(" + target + ", " + data + ")");
90     }
91
92     public void setDocumentLocator(Locator loc) {
93         msg("setDocumentLocator(" + loc + ")");
94         msg(" publicId = " + loc.getPublicId());
95         msg(" systemId = " + loc.getSystemId());
96         msg(" line/col = " + loc.getLineNumber() + "/" +
97             loc.getColumnNumber());
98     }
99
100     public void startDocument() {
101         msg("startDocument");
102     }
103
104     public void startElement(String JavaDoc name, AttributeList attrs) {
105         msg("startElement(" + name + ")");
106         for (int i = 0; i < attrs.getLength(); i++) {
107             msg(" attr[" + i + "]: " + attrs.getName(i) + " = " +
108                            attrs.getValue(i) + ": " + attrs.getType(i));
109         }
110     }
111
112     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
113         throws IOException JavaDoc, SAXException JavaDoc
114     {
115         msg("resolveEntity(" + publicId + ", " + systemId + ")");
116         return null;
117     }
118
119     public void test1(String JavaDoc[] args) throws Exception JavaDoc {
120         org.xml.sax.Parser JavaDoc p = ParserFactory.makeParser();
121         p.setDocumentHandler(this);
122         FileReader JavaDoc f = new FileReader JavaDoc(args[0]);
123         BufferedReader JavaDoc r = new BufferedReader JavaDoc(f);
124         p.parse(new InputSource JavaDoc(r));
125     }
126
127     public void test2(String JavaDoc[] args) throws Exception JavaDoc {
128         FileReader JavaDoc f = new FileReader JavaDoc(args[0]);
129         BufferedReader JavaDoc r = new BufferedReader JavaDoc(f);
130         int c;
131         int cnt = 0;
132         CharArrayWriter JavaDoc w = new CharArrayWriter JavaDoc();
133         while ((c = r.read()) >= 0) {
134             w.write(c);
135             if (cnt++ > 10) {
136                 w.toString();
137                 w.reset();
138                 cnt = 0;
139             }
140             continue;
141         }
142     }
143
144     public static void main(String JavaDoc args[]) {
145     Test t = new Test();
146     t.test(args);
147     }
148 }
149
Popular Tags