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.Transform;
11  import net.sf.saxon.trans.XPathException;
12  import org.jdom2.JDOMException;
13  import org.jdom2.input.SAXBuilder;
14  import org.xml.sax.InputSource;
15  
16  import javax.xml.transform.Source;
17  import javax.xml.transform.sax.SAXSource;
18  import javax.xml.transform.stream.StreamSource;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /**
24   * Variant of command line net.sf.saxon.Transform do build the source document
25   * in JDOM2 and then proceed with the transformation. This class is provided largely for
26   * testing purposes.
27   */
28  
29  public class JDOM2Transform extends Transform {
30  
31      @Override
32      public List preprocess(List sources) throws XPathException {
33          try {
34              ArrayList jdomSources = new ArrayList(sources.size());
35              for (int i = 0; i < sources.size(); i++) {
36                  Source src = (Source) sources.get(i);
37                  InputSource is;
38                  if (src instanceof SAXSource) {
39                      SAXSource ss = (SAXSource) sources.get(i);
40                      is = ss.getInputSource();
41                  } else if (src instanceof StreamSource) {
42                      StreamSource ss = (StreamSource) src;
43                      if (ss.getInputStream() != null) {
44                          is = new InputSource(ss.getInputStream());
45                      } else if (ss.getReader() != null) {
46                          is = new InputSource(ss.getReader());
47                      } else {
48                          is = new InputSource(ss.getSystemId());
49                      }
50                  } else {
51                      throw new IllegalArgumentException("Unknown kind of source");
52                  }
53                  is.setSystemId(src.getSystemId());
54                  SAXBuilder builder = new SAXBuilder();
55                  org.jdom2.Document doc = builder.build(is);
56                  doc.setBaseURI(is.getSystemId());
57                  net.sf.saxon.option.jdom2.JDOM2DocumentWrapper jdom = new JDOM2DocumentWrapper(doc, getConfiguration());
58                  jdomSources.add(jdom);
59              }
60              return jdomSources;
61          } catch (JDOMException e) {
62              throw new XPathException(e);
63          } catch (IOException e) {
64              throw new XPathException(e);
65          }
66      }
67  
68      public static void main(String[] args) {
69          new JDOM2Transform().doTransform(args, "JDOM2Transform");
70      }
71  }
72  
73  // Original Code is Copyright (c) 2009-2020 Saxonica Limited