/* * AdaptiveXIncluder.java Created on 25. duben 2004, 0:10 */ package net.sf.tomp.xml.include; import net.sf.tomp.xml.type.TypeDatabaseImpl; import net.sf.tomp.xtcl.Context; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.PosixParser; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.XMLReaderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.sax.SAXSource; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import java.io.IOException; /** * @author tomp */ public class AdaptiveXIncluder { private boolean verbose; private String xIncludeNamespace; private String tdFileName; private String inputFileName; private String outputFileName; private String targetType; private TypeDatabaseImpl tdb; private Context context; private AdaptiveXIncludeFilter axif; private SAXTransformerFactory stf; /** * Creates a new instance of AdaptiveXIncluder * * @throws TransformerConfigurationException * @throws ParserConfigurationException * @throws IOException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public AdaptiveXIncluder() throws TransformerConfigurationException, ParserConfigurationException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException { context = new Context(); } /** * @param args * the command line arguments */ public static void main(String[] args) throws Exception { String tdFileName = "type-database.xml"; OptionBuilder.hasArgs(1); OptionBuilder.withLongOpt("xinclude-namespace-uri"); OptionBuilder .withDescription("use the specified uri for xi:include elements (default 'http://tomp.sf.net/ns/axi/1.0')"); OptionBuilder.withArgName("xinclude-namespace-uri"); Option oAXINS = OptionBuilder.create('n'); OptionBuilder.hasArgs(1); OptionBuilder.withLongOpt("type-database"); OptionBuilder .withDescription("use the specified file as Type Database"); OptionBuilder.withArgName("file"); Option oTdb = OptionBuilder.create('d'); OptionBuilder.hasArgs(1); OptionBuilder.withLongOpt("target"); OptionBuilder .withDescription("specify target schema and type"); OptionBuilder.withArgName("schema:type"); Option oTarget = OptionBuilder.create('t'); OptionBuilder.withLongOpt("verbose"); OptionBuilder .withDescription("provide more detailed info on processing"); Option oVerb = OptionBuilder.create('v'); Options opts = new Options(); opts.addOption(oAXINS); opts.addOption(oTdb); opts.addOption(oTarget); opts.addOption(oVerb); CommandLine cl = new PosixParser().parse(opts, args); AdaptiveXIncluder axi = new AdaptiveXIncluder(); try { axi.setInputFileName(cl.getArgs()[0]); } catch (ArrayIndexOutOfBoundsException aiob) { new HelpFormatter() .printHelp( "java net.sf.tomp.xml.include.AdaptiveXIncluder [options] infile [outfile]\n" +"Adaptive XML Includer, the axi:include references in infile are processes " +"using the Type Database", opts); return; } if (cl.getArgs().length > 1) { axi.setOutputFileName(cl.getArgs()[1]); } axi.setVerbose(cl.hasOption('v')); axi.setTypeDatabaseFileName(cl.hasOption('d') ? cl.getOptionValue('d') : tdFileName); if(cl.hasOption('t')) { axi.setTargetType(cl.getOptionValue('t')); } axi.include(); } public void include() throws IOException, TransformerException, TransformerConfigurationException, ParserConfigurationException, SAXException { tdb = new TypeDatabaseImpl(); tdb.setXTFilterFactory(context.getXTFilterFactory()); // load Type Database XMLReader reader = getXMLReader(); reader.setContentHandler(tdb); reader.parse(tdFileName); axif = new AdaptiveXIncludeFilter(getXMLReader()); axif.setTypeDatabase(tdb); if(targetType != null) { axif.setTargetType(targetType); } // by TP Oct 5 2004 // axif.getTypeDetectorFilter().setEntityResolver(new XTResolver()); TransformerFactory tf = context.getTransformerFactory(); if (!tf.getFeature(SAXSource.FEATURE)) { throw new TransformerException("Not a SAXTransformerFactory"); } stf = (SAXTransformerFactory) tf; Result result = getResult(); TransformerHandler th = stf.newTransformerHandler(); axif.setContentHandler(th); axif.setProperty("http://xml.org/sax/properties/lexical-handler", th); th.setResult(result); if (verbose) { System.out.print("processing input file '" + inputFileName+"' "); if(outputFileName == null) { System.out.println("to standard output"); } else { System.out.println("to '" + outputFileName+"'"); } if(axif.getTargetType() != null) { System.out.println("target type="+axif.getTargetType()); } } axif.parse(context.getFile(inputFileName).toString()); } /** * @return */ protected Result getResult() { Result result = (outputFileName == null) ? new StreamResult(System.out) : new StreamResult(context.getFile(outputFileName)); return result; } protected XMLReader getXMLReader() throws SAXException { XMLReader parser = null; try { parser = XMLReaderFactory.createXMLReader(); } catch (SAXException e) { parser = XMLReaderFactory.createXMLReader( //"org.apache.xerces.parsers.SAXParser" "org.apache.crimson.parser.XMLReaderImpl"); } return parser; } public void setInputFileName(String ifn) { inputFileName = ifn; } public void setOutputFileName(String ofn) { outputFileName = ofn; } public void setVerbose(boolean v) { verbose = v; } public void setTypeDatabaseFileName(String tdfn) { tdFileName = tdfn; } /** * @param includeNamespace * The xIncludeNamespace to set. */ public void setXIncludeNamespace(String includeNamespace) { axif.setXIncludeNamespace(includeNamespace); } /** * @return Returns the context. */ public Context getContext() { return context; } /** * @param context * The context to set. */ public void setContext(Context context) { this.context = context; } /** * @param targetType The targetType to set. */ public void setTargetType(String targetType) { this.targetType = targetType; } }