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 ; 9 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 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 ("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 e) { 56 throw new DynamicError(e); 57 } 58 } 59 60 public static void main(String [] args) { 61 new JDOMTransform().doMain(args, "JDOMTransform"); 62 } 63 } 64 65 | Popular Tags |