1 package com.quadcap.text.sax; 2 3 40 41 import java.io.BufferedReader ; 42 import java.io.CharArrayWriter ; 43 import java.io.FileReader ; 44 import java.io.IOException ; 45 import java.io.Reader ; 46 47 import org.xml.sax.AttributeList ; 48 import org.xml.sax.DocumentHandler ; 49 import org.xml.sax.DTDHandler ; 50 import org.xml.sax.EntityResolver ; 51 import org.xml.sax.ErrorHandler ; 52 import org.xml.sax.InputSource ; 53 import org.xml.sax.Locator ; 54 import org.xml.sax.SAXException ; 55 56 import org.xml.sax.helpers.AttributeListImpl ; 57 import org.xml.sax.helpers.ParserFactory ; 58 59 64 public class Test extends com.quadcap.util.Test 65 implements DocumentHandler , EntityResolver { 66 public Test() {} 67 68 final void msg(String s) { 69 writer.println(s); 70 } 71 72 public void characters(char[] ch, int off, int len) { 73 msg("characters(" + new String (ch,off,len) + ")"); 74 } 75 76 public void endDocument() { 77 msg("endDocument"); 78 } 79 80 public void endElement(String name) { 81 msg("endElement(" + name + ")"); 82 } 83 84 public void ignorableWhitespace(char[] ch, int off, int len) { 85 msg("ignorableWhitespace(" + new String (ch,off,len) + ")"); 86 } 87 88 public void processingInstruction(String target, String 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 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 resolveEntity(String publicId, String systemId) 113 throws IOException , SAXException 114 { 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 } 149 | Popular Tags |