1 import net.sf.saxon.Configuration;
2 import net.sf.saxon.TransformerFactoryImpl;
3 import net.sf.saxon.jdom.DocumentWrapper;
4 import net.sf.saxon.xpath.XPathEvaluator;
5 import org.jdom.Document;
6 import org.jdom.Element;
7 import org.jdom.JDOMException;
8 import org.jdom.input.SAXBuilder;
9 
10 import javax.xml.transform.Templates;
11 import javax.xml.transform.Transformer;
12 import javax.xml.transform.TransformerException;
13 import javax.xml.transform.TransformerFactory;
14 import javax.xml.transform.stream.StreamResult;
15 import javax.xml.transform.stream.StreamSource;
16 import javax.xml.xpath.XPathConstants;
17 import javax.xml.xpath.XPathExpression;
18 import javax.xml.xpath.XPathExpressionException;
19 import java.io.File;
20 import java.io.IOException;
21 import java.util.Iterator;
22 import java.util.List;
23 
24 /**
25  * A simple example to show how SAXON can be used with a JDOM tree.
26  * It is designed to be used with the source document books.xml and the
27  * stylesheet total.xsl
28  * @author Michael H. Kay
29  */
30 public class JDOMExample {
31 
JDOMExample()32     private JDOMExample() {}
33 
34     /**
35      * Method main
36      */
main(String argv[])37     public static void main(String argv[])
38             throws TransformerException, XPathExpressionException,
39                    JDOMException, IOException {
40 
41         if (argv.length == 2) {
42             transform(argv[0], argv[1]);
43         } else {
44             System.err.println("Usage: JDOMExample source.xml style.xsl >out.xml");
45         }
46 
47     }
48 
49     /**
50      * Show (a) use of freestanding XPath expressions against the JDOM document, and
51      * (b) the simplest possible transformation from system id to output stream.
52      */
53 
transform(String sourceID, String xslID)54     public static void transform(String sourceID, String xslID)
55             throws TransformerException, XPathExpressionException, JDOMException, IOException {
56 
57         // Get a TransformerFactory
58         System.setProperty("javax.xml.transform.TransformerFactory",
59                            "net.sf.saxon.TransformerFactoryImpl");
60         TransformerFactory tfactory = TransformerFactory.newInstance();
61         Configuration config = ((TransformerFactoryImpl)tfactory).getConfiguration();
62 
63         // Build the JDOM document
64         SAXBuilder builder = new SAXBuilder();
65         Document doc = builder.build(new File(sourceID));
66 
67         // Give it a Saxon wrapper
68         DocumentWrapper docw =
69                 new DocumentWrapper(doc, sourceID, config);
70 
71         // Retrieve all the ITEM elements
72         XPathEvaluator xpath = new XPathEvaluator(config);
73         XPathExpression exp = xpath.compile("//ITEM");
74         List list = (List)exp.evaluate(docw, XPathConstants.NODESET);
75 
76         // For each of these, compute an additional attribute
77 
78         for (Iterator iter=list.iterator(); iter.hasNext();) {
79             Element item = (Element)iter.next();
80             String price = item.getChildText("PRICE");
81             String quantity = item.getChildText("QUANTITY");
82             try {
83                 double priceval = Double.parseDouble(price);
84                 double quantityval = Double.parseDouble(quantity);
85                 double value = priceval * quantityval;
86                 item.setAttribute("VALUE", ""+value);
87             } catch (NumberFormatException err) {
88                 item.setAttribute("VALUE", "?");
89             }
90         }
91 
92         // Un-comment the following lines to get trace output from the transformation
93         // tfactory.setAttribute(net.sf.saxon.FeatureKeys.TRACE_LISTENER,
94         //                       new net.sf.saxon.trace.XSLTTraceListener());
95 
96         // Compile the stylesheet
97 
98         Templates templates = tfactory.newTemplates(new StreamSource(xslID));
99         Transformer transformer = templates.newTransformer();
100 
101         // Now do a transformation
102 
103         transformer.transform(docw, new StreamResult(System.out));
104 
105     }
106 
107     /**
108      * Here is an extension function you can try calling from the XSLT stylesheet
109      */
110 
elementContentSize(Element element)111     public static int elementContentSize(Element element) {
112         return element.getContentSize();
113     }
114 
115 
116 }
117