1 38 39 40 package com.sun.xml.fastinfoset.tools; 41 42 import com.sun.xml.fastinfoset.sax.SAXDocumentSerializer; 43 import java.io.InputStream ; 44 import java.io.OutputStream ; 45 import javax.xml.parsers.SAXParser ; 46 import javax.xml.parsers.SAXParserFactory ; 47 import javax.xml.parsers.ParserConfigurationException ; 48 import java.io.Reader ; 49 import org.xml.sax.InputSource ; 50 51 public class XML_SAX_FI extends TransformInputOutput { 52 53 public XML_SAX_FI() { 54 } 55 56 public void parse(InputStream xml, OutputStream finf) throws Exception { 57 SAXParser saxParser = getParser(); 58 SAXDocumentSerializer documentSerializer = getSerializer(finf); 59 60 saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", documentSerializer); 61 saxParser.parse(xml, documentSerializer); 62 } 63 64 public void convert(Reader reader, OutputStream finf) throws Exception { 65 InputSource is = new InputSource (reader); 66 67 SAXParser saxParser = getParser(); 68 SAXDocumentSerializer documentSerializer = getSerializer(finf); 69 70 saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", documentSerializer); 71 saxParser.parse(is, documentSerializer); 72 } 73 74 private SAXParser getParser() { 75 SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 76 saxParserFactory.setNamespaceAware(true); 77 try { 78 return saxParserFactory.newSAXParser(); 79 } catch (Exception e) 80 { 81 return null; 82 } 83 } 84 85 private SAXDocumentSerializer getSerializer(OutputStream finf) { 86 SAXDocumentSerializer documentSerializer = new SAXDocumentSerializer(); 87 documentSerializer.setOutputStream(finf); 88 return documentSerializer; 89 } 90 public static void main(String [] args) throws Exception { 91 XML_SAX_FI s = new XML_SAX_FI(); 92 s.parse(args); 93 } 94 } 95 | Popular Tags |