1 7 8 package test; 9 10 import org.cyberneko.html.filters.DefaultFilter; 11 12 import java.io.OutputStream ; 13 import java.io.OutputStreamWriter ; 14 import java.io.PrintWriter ; 15 import java.io.UnsupportedEncodingException ; 16 17 import org.apache.xerces.util.XMLStringBuffer; 18 import org.apache.xerces.xni.Augmentations; 19 import org.apache.xerces.xni.NamespaceContext; 20 import org.apache.xerces.xni.QName; 21 import org.apache.xerces.xni.XMLAttributes; 22 import org.apache.xerces.xni.XMLLocator; 23 import org.apache.xerces.xni.XMLString; 24 import org.apache.xerces.xni.XNIException; 25 26 32 public class Writer 33 extends DefaultFilter { 34 35 39 40 protected PrintWriter out = new PrintWriter (System.out); 41 42 44 45 private final XMLStringBuffer fStringBuffer = new XMLStringBuffer(); 46 47 51 55 public Writer() { 56 this(System.out); 57 } 59 63 public Writer(OutputStream stream) { 64 this(stream, "UTF8"); 65 } 67 68 public Writer(OutputStream stream, String encoding) { 69 try { 70 out = new PrintWriter (new OutputStreamWriter (stream, encoding), true); 71 } 72 catch (UnsupportedEncodingException e) { 73 throw new RuntimeException ("JVM must have "+encoding+" decoder"); 74 } 75 } 77 78 public Writer(java.io.Writer writer) { 79 out = new PrintWriter (writer); 80 } 82 86 88 89 public void startDocument(XMLLocator locator, String encoding, 90 NamespaceContext nscontext, Augmentations augs) throws XNIException { 91 fStringBuffer.clear(); 92 } 94 96 97 public void startDocument(XMLLocator locator, String encoding, Augmentations augs) throws XNIException { 98 startDocument(locator, encoding, null, augs); 99 } 101 102 public void xmlDecl(String version, String encoding, String standalone, 103 Augmentations augs) throws XNIException { 104 if (version!=null) { 105 out.print("xversion "); 106 out.println(version); 107 } 108 if (encoding!=null) { 109 out.print("xencoding "); 110 out.println(encoding); 111 } 112 if (standalone!=null) { 113 out.print("xstandalone "); 114 out.println(standalone); 115 } 116 out.flush(); 117 } 119 120 public void doctypeDecl(String root, String pubid, String sysid, Augmentations augs) throws XNIException { 121 chars(); 122 out.print('!'); 123 if (root != null) { 124 out.print(root); 125 } 126 out.println(); 127 if (pubid != null) { 128 out.print('p'); 129 out.print(pubid); 130 out.println(); 131 } 132 if (sysid != null) { 133 out.print('s'); 134 out.print(sysid); 135 out.println(); 136 } 137 out.flush(); 138 } 140 141 public void processingInstruction(String target, XMLString data, Augmentations augs) throws XNIException { 142 chars(); 143 out.print('?'); 144 out.print(target); 145 if (data != null && data.length > 0) { 146 out.print(' '); 147 print(data.toString()); 148 } 149 out.println(); 150 out.flush(); 151 } 153 154 public void comment(XMLString text, Augmentations augs) throws XNIException { 155 chars(); 156 out.print('#'); 157 print(text.toString()); 158 out.println(); 159 out.flush(); 160 } 162 163 public void startElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException { 164 chars(); 165 out.print('('); 166 out.print(element.rawname); 167 int acount = attrs != null ? attrs.getLength() : 0; 168 if (acount > 0) { 169 String [] anames = new String [acount]; 170 String [] auris = new String [acount]; 171 sortAttrNames(attrs, anames, auris); 172 for (int i = 0; i < acount; i++) { 173 String aname = anames[i]; 174 out.println(); 175 out.flush(); 176 out.print('A'); 177 if (auris[i] != null) { 178 out.print('{'); 179 out.print(auris[i]); 180 out.print('}'); 181 } 182 out.print(aname); 183 out.print(' '); 184 print(attrs.getValue(aname)); 185 } 186 } 187 out.println(); 188 out.flush(); 189 } 191 192 public void endElement(QName element, Augmentations augs) throws XNIException { 193 chars(); 194 out.print(')'); 195 out.print(element.rawname); 196 out.println(); 197 out.flush(); 198 } 200 201 public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs) throws XNIException { 202 startElement(element, attrs, augs); 203 endElement(element, augs); 204 } 206 207 public void characters(XMLString text, Augmentations augs) throws XNIException { 208 fStringBuffer.append(text); 209 } 211 212 public void ignorableWhitespace(XMLString text, Augmentations augs) throws XNIException { 213 characters(text, augs); 214 } 216 220 221 protected void chars() { 222 if (fStringBuffer.length == 0) { 223 return; 224 } 225 out.print('"'); 226 print(fStringBuffer.toString()); 227 out.println(); 228 out.flush(); 229 fStringBuffer.clear(); 230 } 232 233 protected void print(String s) { 234 int length = s != null ? s.length() : 0; 235 for (int i = 0; i < length; i++) { 236 char c = s.charAt(i); 237 switch (c) { 238 case '\n': { 239 out.print("\\n"); 240 break; 241 } 242 case '\r': { 243 out.print("\\r"); 244 break; 245 } 246 case '\t': { 247 out.print("\\t"); 248 break; 249 } 250 case '\\': { 251 out.print("\\\\"); 252 break; 253 } 254 default: { 255 out.print(c); 256 } 257 } 258 } 259 } 261 265 266 protected static void sortAttrNames(XMLAttributes attrs, 267 String [] anames, String [] auris) { 268 for (int i = 0; i < anames.length; i++) { 269 anames[i] = attrs.getQName(i); 270 auris[i] = attrs.getURI(i); 271 } 272 for (int i = 0; i < anames.length - 1; i++) { 274 int index = i; 275 for (int j = i + 1; j < anames.length; j++) { 276 if (anames[j].compareTo(anames[index]) < 0) { 277 index = j; 278 } 279 } 280 if (index != i) { 281 String tn = anames[i]; 282 anames[i] = anames[index]; 283 anames[index] = tn; 284 String tu = auris[i]; 285 auris[i] = auris[index]; 286 auris[index] = tu; 287 } 288 } 289 } 291 295 296 public static void main(String [] argv) throws Exception { 297 org.apache.xerces.xni.parser.XMLDocumentFilter[] filters = { 298 new Writer(), 299 }; 300 org.apache.xerces.xni.parser.XMLParserConfiguration parser = 301 new org.cyberneko.html.HTMLConfiguration(); 302 parser.setProperty("http://cyberneko.org/html/properties/filters", filters); 303 for (int i = 0; i < argv.length; i++) { 304 org.apache.xerces.xni.parser.XMLInputSource source = 305 new org.apache.xerces.xni.parser.XMLInputSource(null, argv[i], null); 306 parser.parse(source); 307 } 308 } 310 } | Popular Tags |