Debian Codegraph @

net/sf/saxon/dom/DOMTransform.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.dom;
9
10 import net.sf.saxon.Transform;
11 import net.sf.saxon.trans.XPathException;
12 import org.w3c.dom.Document;
13 import org.xml.sax.InputSource;
14 import org.xml.sax.SAXException;
15
16 import javax.xml.parsers.DocumentBuilder;
17 import javax.xml.parsers.DocumentBuilderFactory;
18 import javax.xml.parsers.ParserConfigurationException;
19 import javax.xml.transform.Source;
20 import javax.xml.transform.stream.StreamSource;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.List;
24
25
26 /**
27 * Variant of command line net.sf.saxon.Transform do build the source document
28 * in DOM and then proceed with the transformation. This class is provided largely for
29 * testing purposes.
30 */
31
32 public class DOMTransform extends Transform {
33
34 public List<Source> preprocess(List<Source> sources) throws XPathException {
35 try {
36 ArrayList<Source> domSources = new ArrayList<>(sources.size());
37 for (Object source : sources) {
38 StreamSource src = (StreamSource) source;
39 InputSource ins = new InputSource(src.getSystemId());
40
41 // The following statement, if uncommented, forces use of the Xerces DOM.
42 // This system property can also be set from the command line using the -D option
43
44 // System.setProperty("javax.xml.parser.DocumentBuilderFactory",
45 // "org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
46
47 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
48 factory.setNamespaceAware(true);
49 DocumentBuilder builder = factory.newDocumentBuilder();
50 Document doc = builder.parse(ins);
51 DocumentWrapper dom = new DocumentWrapper(doc, src.getSystemId(), getConfiguration());
52 domSources.add(dom.getRootNode());
53 }
54 return domSources;
55 } catch (ParserConfigurationException | IOException | SAXException e) {
56 throw new XPathException(e);
57 }
58 }
59
60 public static void main(String[] args) {
61 new DOMTransform().doTransform(args, "DOMTransform");
62 }
63 }
64