001 /* 002 * Copyright (c) 2007 Henri Sivonen 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a 005 * copy of this software and associated documentation files (the "Software"), 006 * to deal in the Software without restriction, including without limitation 007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 008 * and/or sell copies of the Software, and to permit persons to whom the 009 * Software is furnished to do so, subject to the following conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in 012 * all copies or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 017 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 019 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 020 * DEALINGS IN THE SOFTWARE. 021 */ 022 023 package nu.validator.saxtree.test; 024 025 import java.io.File; 026 import java.io.FileInputStream; 027 import java.io.IOException; 028 import java.util.Properties; 029 030 import javax.xml.parsers.ParserConfigurationException; 031 import javax.xml.parsers.SAXParserFactory; 032 033 import nu.validator.saxtree.Node; 034 import nu.validator.saxtree.TreeBuilder; 035 import nu.validator.saxtree.TreeParser; 036 037 import org.apache.xml.serializer.Method; 038 import org.apache.xml.serializer.OutputPropertiesFactory; 039 import org.apache.xml.serializer.Serializer; 040 import org.apache.xml.serializer.SerializerFactory; 041 import org.xml.sax.ContentHandler; 042 import org.xml.sax.InputSource; 043 import org.xml.sax.SAXException; 044 import org.xml.sax.XMLReader; 045 import org.xml.sax.ext.LexicalHandler; 046 047 public class PassThruPrinter { 048 public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException { 049 SAXParserFactory factory = SAXParserFactory.newInstance(); 050 factory.setNamespaceAware(true); 051 factory.setValidating(false); 052 XMLReader reader = factory.newSAXParser().getXMLReader(); 053 054 TreeBuilder treeBuilder = new TreeBuilder(); 055 reader.setContentHandler(treeBuilder); 056 reader.setProperty("http://xml.org/sax/properties/lexical-handler", treeBuilder); 057 058 File file = new File(args[0]); 059 InputSource is = new InputSource(new FileInputStream(file)); 060 is.setSystemId(file.toURI().toASCIIString()); 061 reader.parse(is); 062 063 Node doc = treeBuilder.getRoot(); 064 065 Properties props = OutputPropertiesFactory.getDefaultMethodProperties(Method.XML); 066 Serializer ser = SerializerFactory.getSerializer(props); 067 ser.setOutputStream(System.out); 068 ContentHandler xmlSerializer = ser.asContentHandler(); 069 070 TreeParser treeParser = new TreeParser(xmlSerializer, (LexicalHandler) xmlSerializer); 071 treeParser.parse(doc); 072 } 073 074 }