01: package net.sf.saxon.dom;
02:
03: import net.sf.saxon.Transform;
04: import net.sf.saxon.trans.DynamicError;
05: import net.sf.saxon.trans.XPathException;
06: import org.xml.sax.InputSource;
07: import org.xml.sax.SAXException;
08:
09: import javax.xml.parsers.DocumentBuilder;
10: import javax.xml.parsers.DocumentBuilderFactory;
11: import javax.xml.parsers.ParserConfigurationException;
12: import javax.xml.transform.stream.StreamSource;
13: import java.io.IOException;
14: import java.util.ArrayList;
15: import java.util.List;
16:
17: /**
18: * Variant of command line net.sf.saxon.Transform do build the source document
19: * in DOM and then proceed with the transformation. This class is provided largely for
20: * testing purposes.
21: */
22:
23: public class DOMTransform extends Transform {
24:
25: public List preprocess(List sources) throws XPathException {
26: try {
27: ArrayList domSources = new ArrayList(sources.size());
28: for (int i = 0; i < sources.size(); i++) {
29: StreamSource src = (StreamSource) sources.get(i);
30: InputSource ins = new InputSource(src.getSystemId());
31:
32: // The following statement, if uncommented, forces use of the Xerces DOM.
33: // This system property can also be set from the command line using the -D option
34:
35: System
36: .setProperty(
37: "javax.xml.parser.DocumentBuilderFactory",
38: "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
39:
40: DocumentBuilderFactory factory = DocumentBuilderFactory
41: .newInstance();
42: factory.setNamespaceAware(true);
43: DocumentBuilder builder = factory.newDocumentBuilder();
44: org.w3c.dom.Document doc = builder.parse(ins);
45: DocumentWrapper dom = new DocumentWrapper(doc, src
46: .getSystemId(), getConfiguration());
47: domSources.add(dom);
48: }
49: return domSources;
50: } catch (ParserConfigurationException e) {
51: throw new DynamicError(e);
52: } catch (SAXException e) {
53: throw new DynamicError(e);
54: } catch (IOException e) {
55: throw new DynamicError(e);
56: }
57: }
58:
59: public static void main(String[] args) {
60: new DOMTransform().doMain(args, "DOMTransform");
61: }
62: }
63: //
64: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
65: // you may not use this file except in compliance with the License. You may obtain a copy of the
66: // License at http://www.mozilla.org/MPL/
67: //
68: // Software distributed under the License is distributed on an "AS IS" basis,
69: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
70: // See the License for the specific language governing rights and limitations under the License.
71: //
72: // The Original Code is: all this file.
73: //
74: // The Initial Developer of the Original Code is Michael H. Kay.
75: //
76: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
77: //
78: // Contributor(s): none.
79: //
|