01: package net.sf.saxon.jdom;
02:
03: import net.sf.saxon.Transform;
04: import net.sf.saxon.trans.DynamicError;
05: import net.sf.saxon.trans.XPathException;
06: import org.jdom.JDOMException;
07: import org.jdom.input.SAXBuilder;
08: import org.xml.sax.InputSource;
09:
10: import javax.xml.transform.Source;
11: import javax.xml.transform.sax.SAXSource;
12: import javax.xml.transform.stream.StreamSource;
13: import java.io.IOException;
14: import java.util.ArrayList;
15: import java.util.List;
16:
17: /**
18: * Variant of command line net.sf.saxon.Transform do build the source document
19: * in JDOM and then proceed with the transformation. This class is provided largely for
20: * testing purposes.
21: */
22:
23: public class JDOMTransform extends Transform {
24:
25: public List preprocess(List sources) throws XPathException {
26: try {
27: ArrayList jdomSources = new ArrayList(sources.size());
28: for (int i = 0; i < sources.size(); i++) {
29: Source src = (Source) sources.get(i);
30: InputSource is;
31: if (src instanceof SAXSource) {
32: SAXSource ss = (SAXSource) sources.get(i);
33: is = ss.getInputSource();
34: } else if (src instanceof StreamSource) {
35: StreamSource ss = (StreamSource) src;
36: if (ss.getInputStream() != null) {
37: is = new InputSource(ss.getInputStream());
38: } else if (ss.getReader() != null) {
39: is = new InputSource(ss.getReader());
40: } else {
41: is = new InputSource(ss.getSystemId());
42: }
43: } else {
44: throw new IllegalArgumentException(
45: "Unknown kind of source");
46: }
47: is.setSystemId(src.getSystemId());
48: SAXBuilder builder = new SAXBuilder();
49: org.jdom.Document doc = builder.build(is);
50: DocumentWrapper jdom = new DocumentWrapper(doc, is
51: .getSystemId(), config);
52: jdomSources.add(jdom);
53: }
54: return jdomSources;
55: } catch (JDOMException e) {
56: throw new DynamicError(e);
57: } catch (IOException e) {
58: throw new DynamicError(e);
59: }
60: }
61:
62: public static void main(String[] args) {
63: new JDOMTransform().doMain(args, "JDOMTransform");
64: }
65: }
66:
67: //
68: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
69: // you may not use this file except in compliance with the License. You may obtain a copy of the
70: // License at http://www.mozilla.org/MPL/
71: //
72: // Software distributed under the License is distributed on an "AS IS" basis,
73: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
74: // See the License for the specific language governing rights and limitations under the License.
75: //
76: // The Original Code is: all this file.
77: //
78: // The Initial Developer of the Original Code is Michael H. Kay
79: //
80: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
81: //
82: // Contributor(s): none
83: //
|