1 18 package org.ccil.cowan.tagsoup; 19 import java.io.*; 20 import org.xml.sax.*; 21 import org.xml.sax.ext.LexicalHandler ; 22 23 27 public class PYXWriter 28 implements ScanHandler, ContentHandler, LexicalHandler { 29 30 private PrintWriter theWriter; private static char[] dummy = new char[1]; 32 private String attrName; 34 36 public void adup(char[] buff, int offset, int length) throws SAXException { 37 theWriter.println(attrName); 38 attrName = null; 39 } 40 41 public void aname(char[] buff, int offset, int length) throws SAXException { 42 theWriter.print('A'); 43 theWriter.write(buff, offset, length); 44 theWriter.print(' '); 45 attrName = new String (buff, offset, length); 46 } 47 48 public void aval(char[] buff, int offset, int length) throws SAXException { 49 theWriter.write(buff, offset, length); 50 theWriter.println(); 51 attrName = null; 52 } 53 54 public void cmnt(char [] buff, int offset, int length) throws SAXException { 55 } 59 60 public void entity(char[] buff, int offset, int length) throws SAXException { } 61 62 public char getEntity() { return 0; } 63 64 public void eof(char[] buff, int offset, int length) throws SAXException { 65 theWriter.close(); 66 } 67 68 public void etag(char[] buff, int offset, int length) throws SAXException { 69 theWriter.print(')'); 70 theWriter.write(buff, offset, length); 71 theWriter.println(); 72 } 73 74 public void decl(char[] buff, int offset, int length) throws SAXException { 75 } 76 77 public void gi(char[] buff, int offset, int length) throws SAXException { 78 theWriter.print('('); 79 theWriter.write(buff, offset, length); 80 theWriter.println(); 81 } 82 83 public void pcdata(char[] buff, int offset, int length) throws SAXException { 84 if (length == 0) return; boolean inProgress = false; 86 length += offset; 87 for (int i = offset; i < length; i++) { 88 if (buff[i] == '\n') { 89 if (inProgress) { 90 theWriter.println(); 91 } 92 theWriter.println("-\\n"); 93 inProgress = false; 94 } 95 else { 96 if (!inProgress) { 97 theWriter.print('-'); 98 } 99 switch(buff[i]) { 100 case '\t': 101 theWriter.print("\\t"); 102 break; 103 case '\\': 104 theWriter.print("\\\\"); 105 break; 106 default: 107 theWriter.print(buff[i]); 108 } 109 inProgress = true; 110 } 111 } 112 if (inProgress) { 113 theWriter.println(); 114 } 115 } 116 117 public void pitarget(char[] buff, int offset, int length) throws SAXException { 118 theWriter.print('?'); 119 theWriter.write(buff, offset, length); 120 theWriter.write(' '); 121 } 122 123 public void pi(char[] buff, int offset, int length) throws SAXException { 124 theWriter.write(buff, offset, length); 125 theWriter.println(); 126 } 127 128 public void stagc(char[] buff, int offset, int length) throws SAXException { 129 theWriter.println("!"); } 131 132 public void stage(char[] buff, int offset, int length) throws SAXException { 133 theWriter.println("!"); } 135 136 138 public void characters(char[] buff, int offset, int length) throws SAXException { 139 pcdata(buff, offset, length); 140 } 141 142 public void endDocument() throws SAXException { 143 theWriter.close(); 144 } 145 146 public void endElement(String uri, String localname, String qname) throws SAXException { 147 if (qname.length() == 0) qname = localname; 148 theWriter.print(')'); 149 theWriter.println(qname); 150 } 151 152 public void endPrefixMapping(String prefix) throws SAXException { } 153 154 public void ignorableWhitespace(char[] buff, int offset, int length) throws SAXException { 155 characters(buff, offset, length); 156 } 157 158 public void processingInstruction(String target, String data) throws SAXException { 159 theWriter.print('?'); 160 theWriter.print(target); 161 theWriter.print(' '); 162 theWriter.println(data); 163 } 164 165 public void setDocumentLocator(Locator locator) { } 166 167 public void skippedEntity(String name) throws SAXException { } 168 169 public void startDocument() throws SAXException { } 170 171 public void startElement(String uri, String localname, String qname, 172 Attributes atts) throws SAXException { 173 if (qname.length() == 0) qname=localname; 174 theWriter.print('('); 175 theWriter.println(qname); 176 int length = atts.getLength(); 177 for (int i = 0; i < length; i++) { 178 qname = atts.getQName(i); 179 if (qname.length() == 0) qname = atts.getLocalName(i); 180 theWriter.print('A'); 181 theWriter.print(qname); 183 theWriter.print(' '); 184 theWriter.println(atts.getValue(i)); 185 } 186 } 187 188 public void startPrefixMapping(String prefix, String uri) throws SAXException { } 189 190 192 public void comment(char[] ch, int start, int length) throws SAXException { 193 cmnt(ch, start, length); 194 } 195 public void endCDATA() throws SAXException { } 196 public void endDTD() throws SAXException { } 197 public void endEntity(String name) throws SAXException { } 198 public void startCDATA() throws SAXException { } 199 public void startDTD(String name, String publicId, String systemId) throws SAXException { } 200 public void startEntity(String name) throws SAXException { } 201 202 204 public PYXWriter(Writer w) { 205 if (w instanceof PrintWriter) { 206 theWriter = (PrintWriter)w; 207 } 208 else { 209 theWriter = new PrintWriter(w); 210 } 211 } 212 } 213 | Popular Tags |