1
2
3
4 package net.sf.tomp.xml.include;
5
6 import net.sf.tomp.xml.type.TypeDatabaseImpl;
7 import net.sf.tomp.xtcl.Context;
8
9 import org.apache.commons.cli.CommandLine;
10 import org.apache.commons.cli.HelpFormatter;
11 import org.apache.commons.cli.Option;
12 import org.apache.commons.cli.OptionBuilder;
13 import org.apache.commons.cli.Options;
14 import org.apache.commons.cli.PosixParser;
15 import org.xml.sax.SAXException;
16 import org.xml.sax.XMLReader;
17 import org.xml.sax.helpers.XMLReaderFactory;
18
19 import javax.xml.parsers.ParserConfigurationException;
20 import javax.xml.transform.Result;
21 import javax.xml.transform.TransformerConfigurationException;
22 import javax.xml.transform.TransformerException;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.sax.SAXSource;
25 import javax.xml.transform.sax.SAXTransformerFactory;
26 import javax.xml.transform.sax.TransformerHandler;
27 import javax.xml.transform.stream.StreamResult;
28
29 import java.io.IOException;
30
31 /***
32 * @author tomp
33 */
34 public class AdaptiveXIncluder {
35 private boolean verbose;
36
37 private String tdFileName;
38
39 private String inputFileName;
40
41 private String outputFileName;
42
43 private TypeDatabaseImpl tdb;
44
45 private Context context;
46
47 private AdaptiveXIncludeFilter axif;
48
49 private SAXTransformerFactory stf;
50
51 /***
52 * Creates a new instance of AdaptiveXIncluder
53 *
54 * @throws TransformerConfigurationException
55 * @throws ParserConfigurationException
56 * @throws IOException
57 * @throws ClassNotFoundException
58 * @throws IllegalAccessException
59 * @throws InstantiationException
60 */
61 public AdaptiveXIncluder() throws TransformerConfigurationException,
62 ParserConfigurationException, IOException, InstantiationException,
63 IllegalAccessException, ClassNotFoundException {
64 context = new Context();
65 }
66
67 /***
68 * @param args the command line arguments
69 */
70 public static void main(String[] args) throws Exception {
71 String tdFileName = "type-database.xml";
72
73 OptionBuilder.hasArgs(1);
74 OptionBuilder.withLongOpt("type-database");
75 OptionBuilder
76 .withDescription("use the specified file as Type Database");
77 OptionBuilder.withArgName("file");
78
79 Option oTdb = OptionBuilder.create('d');
80
81 OptionBuilder.withLongOpt("verbose");
82 OptionBuilder
83 .withDescription("provide more detailed info on processing");
84
85 Option oVerb = OptionBuilder.create('v');
86
87 Options opts = new Options();
88
89 opts.addOption(oTdb);
90 opts.addOption(oVerb);
91
92 CommandLine cl = new PosixParser().parse(opts, args);
93
94 AdaptiveXIncluder axi = new AdaptiveXIncluder();
95
96 try {
97 axi.setInputFileName(cl.getArgs()[0]);
98 } catch (ArrayIndexOutOfBoundsException aiob) {
99 new HelpFormatter()
100 .printHelp(
101 "Adaptive XML Includer, the axi:include references in infile are processes using the Type Database",
102 opts);
103
104 return;
105 }
106
107 if (cl.getArgs().length > 1) {
108 axi.setOutputFileName(cl.getArgs()[1]);
109 }
110
111 axi.setVerbose(cl.hasOption('v'));
112 axi.setTypeDatabaseFileName(cl.hasOption('d') ? cl.getOptionValue('d')
113 : tdFileName);
114 axi.include();
115 }
116
117 public void include() throws IOException, TransformerException,
118 TransformerConfigurationException, ParserConfigurationException,
119 SAXException {
120 tdb = new TypeDatabaseImpl();
121 tdb.setXTFilterFactory(context.getXTFilterFactory());
122
123
124 XMLReader reader = getXMLReader();
125
126 reader.setContentHandler(tdb);
127 reader.parse(tdFileName);
128
129 axif = new AdaptiveXIncludeFilter(getXMLReader());
130 axif.setTypeDatabase(tdb);
131
132 TransformerFactory tf = context.getTransformerFactory();
133
134 if (!tf.getFeature(SAXSource.FEATURE)) {
135 throw new TransformerException("Not a SAXTransformerFactory");
136 }
137
138 stf = (SAXTransformerFactory) tf;
139
140 Result result = (outputFileName == null) ? new StreamResult(System.out)
141 : new StreamResult(context.getFile(outputFileName));
142
143 TransformerHandler th = stf.newTransformerHandler();
144
145 axif.setContentHandler(th);
146 axif.setProperty("http://xml.org/sax/properties/lexical-handler", th);
147 th.setResult(result);
148
149 if (verbose) {
150 System.out.println("NOW PARSING source=" + inputFileName);
151 }
152
153 axif.parse(context.getFile(inputFileName).toString());
154 }
155
156 protected XMLReader getXMLReader() throws SAXException {
157 XMLReader parser = null;
158
159 try {
160 parser = XMLReaderFactory.createXMLReader();
161 } catch (SAXException e) {
162 parser = XMLReaderFactory.createXMLReader(
163
164
165 "org.apache.crimson.parser.XMLReaderImpl");
166 }
167
168 return parser;
169 }
170
171 public void setInputFileName(String ifn) {
172 inputFileName = ifn;
173 }
174
175 public void setOutputFileName(String ofn) {
176 outputFileName = ofn;
177 }
178
179 public void setVerbose(boolean v) {
180 verbose = v;
181 }
182
183 public void setTypeDatabaseFileName(String tdfn) {
184 tdFileName = tdfn;
185 }
186 }