KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > dom > DOMTransform


1 package net.sf.saxon.dom;
2
3 import net.sf.saxon.Transform;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.XPathException;
6 import org.xml.sax.InputSource JavaDoc;
7 import org.xml.sax.SAXException JavaDoc;
8
9 import javax.xml.parsers.DocumentBuilder JavaDoc;
10 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
11 import javax.xml.parsers.ParserConfigurationException JavaDoc;
12 import javax.xml.transform.stream.StreamSource JavaDoc;
13 import java.io.IOException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17
18 /**
19  * Variant of command line net.sf.saxon.Transform do build the source document
20  * in DOM and then proceed with the transformation. This class is provided largely for
21  * testing purposes.
22  */

23
24 public class DOMTransform extends Transform {
25
26     public List JavaDoc preprocess(List JavaDoc sources) throws XPathException {
27         try {
28             ArrayList JavaDoc domSources = new ArrayList JavaDoc(sources.size());
29             for (int i=0; i<sources.size(); i++) {
30                 StreamSource JavaDoc src = (StreamSource JavaDoc)sources.get(i);
31                 InputSource JavaDoc ins = new InputSource JavaDoc(src.getSystemId());
32
33                 // The following statement, if uncommented, forces use of the Xerces DOM.
34
// This system property can also be set from the command line using the -D option
35

36                 System.setProperty("javax.xml.parser.DocumentBuilderFactory",
37                                    "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
38
39                 DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
40                 factory.setNamespaceAware(true);
41                 DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
42                 org.w3c.dom.Document JavaDoc doc = builder.parse(ins);
43                 DocumentWrapper dom = new DocumentWrapper(doc, src.getSystemId(), getConfiguration());
44                 domSources.add(dom);
45             }
46             return domSources;
47         } catch (ParserConfigurationException JavaDoc e) {
48             throw new DynamicError(e);
49         } catch (SAXException JavaDoc e) {
50             throw new DynamicError(e);
51         } catch (IOException JavaDoc e) {
52             throw new DynamicError(e);
53     }
54     }
55
56     public static void main(String JavaDoc[] args) {
57         new DOMTransform().doMain(args, "DOMTransform");
58     }
59 }
60 //
61
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
62
// you may not use this file except in compliance with the License. You may obtain a copy of the
63
// License at http://www.mozilla.org/MPL/
64
//
65
// Software distributed under the License is distributed on an "AS IS" basis,
66
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
67
// See the License for the specific language governing rights and limitations under the License.
68
//
69
// The Original Code is: all this file.
70
//
71
// The Initial Developer of the Original Code is Michael H. Kay.
72
//
73
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
74
//
75
// Contributor(s): none.
76
//
Popular Tags