1 38 39 package samples.sax; 40 import java.io.*; 41 42 import org.xml.sax.*; 43 import org.xml.sax.helpers.DefaultHandler ; 44 import com.sun.xml.fastinfoset.sax.SAXDocumentParser; 45 46 56 public class FastInfosetParser extends DefaultHandler { 57 static private Writer out; 58 59 StringBuffer textBuffer; 60 61 66 public static void main(String argv[]) 67 { 68 if (argv.length != 1) { 69 System.err.println("Usage: FastInfosetParser filename"); 70 System.exit(1); 71 } 72 73 DefaultHandler handler = new FastInfosetParser(); 75 76 try { 77 out = new OutputStreamWriter(System.out, "UTF8"); 79 80 InputStream in = new BufferedInputStream(new FileInputStream(argv[0])); 82 83 SAXDocumentParser parser = new SAXDocumentParser(); 85 parser.setContentHandler(handler); 86 parser.parse(in); 88 89 } catch (Throwable t) { 90 t.printStackTrace(); 91 } 92 System.exit(0); 93 } 94 95 private static void displayUsageAndExit() { 96 System.err.println("Usage: ant FISAXParser or samples.sax.FIParser FI_input_file>"); 97 System.exit(1); 98 } 99 100 101 public FastInfosetParser() { 102 } 103 104 110 public void startDocument() throws SAXException { 111 display("<?xml version='1.0' encoding='UTF-8'?>"); 112 displayNewLine(); 113 } 114 115 118 public void endDocument() throws SAXException { 119 try { 120 displayNewLine(); 121 out.flush(); 122 } catch (IOException e) { 123 throw new SAXException("I/O error", e); 124 } 125 } 126 127 136 public void startElement(String namespaceURI, String localName, 137 String qName, Attributes attributes) throws SAXException { 138 139 flushText(); 141 142 String name = null; 143 144 if (localName.equals("")) { 145 name = qName; 146 } else { 147 name = localName; 148 } 149 150 display("<" + name); 152 153 if (attributes != null) { 154 for (int i = 0; i < attributes.getLength(); i++) { 155 String aName = attributes.getLocalName(i); 156 157 if ("".equals(aName)) { 158 aName = attributes.getQName(i); 159 } 160 161 display(" " + aName + "=\"" + attributes.getValue(i) + "\""); 162 } 163 } 164 165 display(">"); 166 } 167 168 175 public void endElement(String namespaceURI, String localName, String qName ) 176 throws SAXException { 177 178 flushText(); 180 181 String name = localName; 182 183 if ("".equals(name)) { 184 name = qName; 185 } 186 187 display("</" + name + ">"); 189 } 190 191 197 public void characters(char[] ch, int start, int length) 198 throws SAXException { 199 String s = new String (ch, start, length); 200 201 if (textBuffer == null) { 202 textBuffer = new StringBuffer (s); 203 } else { 204 textBuffer.append(s); 205 } 206 } 207 208 private void flushText() throws SAXException { 213 if (textBuffer == null) { 214 return; 215 } 216 217 display(textBuffer.toString()); 218 textBuffer = null; 219 } 220 221 private void display(String s) throws SAXException { 224 try { 225 out.write(s); 226 out.flush(); 227 } catch (IOException e) { 228 throw new SAXException("I/O error", e); 229 } 230 } 231 232 private void displayNewLine() throws SAXException { 234 try { 235 out.write(System.getProperty("line.separator")); 236 } catch (IOException e) { 237 throw new SAXException("I/O error", e); 238 } 239 } 240 } 241 | Popular Tags |