View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2   // Copyright (c) 2018-2020 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.option.jdom2;
9   
10  import net.sf.saxon.Query;
11  import net.sf.saxon.trans.XPathException;
12  import org.jdom2.JDOMException;
13  import org.jdom2.input.SAXBuilder;
14  
15  import javax.xml.transform.sax.SAXSource;
16  import java.io.IOException;
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  /**
21   * Variant of command line net.sf.saxon.Transform do build the source document
22   * in JDOM2 and then proceed with the transformation. This class is provided largely for
23   * testing purposes.
24   */
25  
26  public class JDOM2Query extends Query {
27  
28      public List preprocess(List sources) throws XPathException {
29          try {
30              ArrayList jdomSources = new ArrayList(sources.size());
31              for (int i = 0; i < sources.size(); i++) {
32                  SAXSource ss = (SAXSource) sources.get(i);
33                  SAXBuilder builder = new SAXBuilder();
34                  org.jdom2.Document doc = builder.build(ss.getInputSource());
35                  doc.setBaseURI(ss.getSystemId());
36                  JDOM2DocumentWrapperntWrapper.html#JDOM2DocumentWrapper">JDOM2DocumentWrapper jdom = new JDOM2DocumentWrapper(doc, getConfiguration());
37                  jdomSources.add(jdom);
38              }
39              return jdomSources;
40          } catch (JDOMException e) {
41              throw new XPathException(e);
42          } catch (IOException e) {
43              throw new XPathException(e);
44          }
45      }
46  
47      public static void main(String[] args) {
48          new JDOM2Query().doQuery(args, "JDOM2Query");
49      }
50  }
51  
52  
53  // Original Code is Copyright (c) 2009-2020 Saxonica Limited