001: package com.quadcap.text.sax;
002:
003: /* Copyright 2000 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.BufferedReader;
042: import java.io.CharArrayWriter;
043: import java.io.FileReader;
044: import java.io.IOException;
045: import java.io.Reader;
046:
047: import org.xml.sax.AttributeList;
048: import org.xml.sax.DocumentHandler;
049: import org.xml.sax.DTDHandler;
050: import org.xml.sax.EntityResolver;
051: import org.xml.sax.ErrorHandler;
052: import org.xml.sax.InputSource;
053: import org.xml.sax.Locator;
054: import org.xml.sax.SAXException;
055:
056: import org.xml.sax.helpers.AttributeListImpl;
057: import org.xml.sax.helpers.ParserFactory;
058:
059: /**
060: * Package level tests.
061: *
062: * @author Stan Bailes
063: */
064: public class Test extends com.quadcap.util.Test implements
065: DocumentHandler, EntityResolver {
066: public Test() {
067: }
068:
069: final void msg(String s) {
070: writer.println(s);
071: }
072:
073: public void characters(char[] ch, int off, int len) {
074: msg("characters(" + new String(ch, off, len) + ")");
075: }
076:
077: public void endDocument() {
078: msg("endDocument");
079: }
080:
081: public void endElement(String name) {
082: msg("endElement(" + name + ")");
083: }
084:
085: public void ignorableWhitespace(char[] ch, int off, int len) {
086: msg("ignorableWhitespace(" + new String(ch, off, len) + ")");
087: }
088:
089: public void processingInstruction(String target, String data) {
090: msg("processingInstruction(" + target + ", " + data + ")");
091: }
092:
093: public void setDocumentLocator(Locator loc) {
094: msg("setDocumentLocator(" + loc + ")");
095: msg(" publicId = " + loc.getPublicId());
096: msg(" systemId = " + loc.getSystemId());
097: msg(" line/col = " + loc.getLineNumber() + "/"
098: + loc.getColumnNumber());
099: }
100:
101: public void startDocument() {
102: msg("startDocument");
103: }
104:
105: public void startElement(String name, AttributeList attrs) {
106: msg("startElement(" + name + ")");
107: for (int i = 0; i < attrs.getLength(); i++) {
108: msg(" attr[" + i + "]: " + attrs.getName(i) + " = "
109: + attrs.getValue(i) + ": " + attrs.getType(i));
110: }
111: }
112:
113: public InputSource resolveEntity(String publicId, String systemId)
114: throws IOException, SAXException {
115: msg("resolveEntity(" + publicId + ", " + systemId + ")");
116: return null;
117: }
118:
119: public void test1(String[] args) throws Exception {
120: org.xml.sax.Parser p = ParserFactory.makeParser();
121: p.setDocumentHandler(this );
122: FileReader f = new FileReader(args[0]);
123: BufferedReader r = new BufferedReader(f);
124: p.parse(new InputSource(r));
125: }
126:
127: public void test2(String[] args) throws Exception {
128: FileReader f = new FileReader(args[0]);
129: BufferedReader r = new BufferedReader(f);
130: int c;
131: int cnt = 0;
132: CharArrayWriter w = new CharArrayWriter();
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 args[]) {
145: Test t = new Test();
146: t.test(args);
147: }
148: }
|