1
2
3
4
5
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
25
26
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