KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > jdom > JDOMTransform


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