File: JDOMExample.java

package info (click to toggle)
libsaxon-java 1%3A6.5.5-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 16,556 kB
  • sloc: java: 36,724; xml: 7,470; makefile: 22; sh: 1
file content (101 lines) | stat: -rw-r--r-- 3,448 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import com.icl.saxon.Context;
import com.icl.saxon.expr.Expression;
import com.icl.saxon.expr.StandaloneContext;
import com.icl.saxon.jdom.DocumentWrapper;
import com.icl.saxon.jdom.NodeWrapper;
import com.icl.saxon.om.NamePool;
import com.icl.saxon.om.NodeEnumeration;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.IOException;

/**
 * A simple example to show how SAXON can be used with a JDOM tree.
 * It is designed to be used with the source document books.xml and the
 * stylesheet total.xsl
 * @author Michael H. Kay
 */
public class JDOMExample {

    /**
     * Method main
     */
    public static void main(String argv[])
            throws TransformerException, TransformerConfigurationException,
                   JDOMException, IOException {

        if (argv.length != 2) {
            System.err.println("Usage: JDOMExample source.xml style.xsl >out.xml");
        } else {
            transform(argv[0], argv[1]);
        }

    }

    /**
     * Show the simplest possible transformation from system id
     * to output stream.
     */

    public static void transform(String sourceID, String xslID)
            throws TransformerException, TransformerConfigurationException,
                   JDOMException, IOException {


        // Build the JDOM document
        SAXBuilder builder = new SAXBuilder("com.icl.saxon.aelfred.SAXDriver");
        Document doc = builder.build(new File(sourceID));

        // Give it a Saxon wrapper
        DocumentWrapper docw = new DocumentWrapper(doc, sourceID);
        NamePool pool = NamePool.getDefaultNamePool();
        docw.setNamePool(pool);

        // Retrieve all the ITEM elements
        Expression exp = Expression.make("//ITEM", new StandaloneContext(pool));
        Context context = new Context();
        context.setContextNode(docw);
        context.setPosition(1);
        context.setLast(1);

        NodeEnumeration enm = exp.enumerate(context, false);

        // For each of these, compute an additional attribute

        while (enm.hasMoreElements()) {
            NodeWrapper node = (NodeWrapper)enm.nextElement();
            Element item = (Element)node.getNode();
            String price = item.getChildText("PRICE");
            String quantity = item.getChildText("QUANTITY");
            try {
                double priceval = Double.parseDouble(price);
                double quantityval = Double.parseDouble(quantity);
                double value = priceval * quantityval;
                item.setAttribute("VALUE", ""+value);
            } catch (NumberFormatException err) {
                item.setAttribute("VALUE", "?");
            }
        }

        // Now do a transformation

        System.setProperty("javax.xml.transform.TransformerFactory",
                           "com.icl.saxon.TransformerFactoryImpl");
        TransformerFactory tfactory = TransformerFactory.newInstance();

        Templates templates = tfactory.newTemplates(new StreamSource(xslID));
        Transformer transformer = templates.newTransformer();

        transformer.transform(docw, new StreamResult(System.out));

    }


}