KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > XSLTDemo


1 /*
2  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
6  *
7  * $Id: XSLTDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
8  */

9
10 package org.dom4j.samples;
11
12 import javax.xml.transform.Source JavaDoc;
13 import javax.xml.transform.Transformer JavaDoc;
14 import javax.xml.transform.TransformerFactory JavaDoc;
15 import javax.xml.transform.stream.StreamSource JavaDoc;
16
17 import org.dom4j.Document;
18 import org.dom4j.io.DocumentResult;
19 import org.dom4j.io.DocumentSource;
20
21 /**
22  * A sample program to demonstrate using XSLT to transform a dom4j Document.
23  *
24  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
25  * @version $Revision: 1.4 $
26  */

27 public class XSLTDemo extends SAXDemo {
28
29     protected String JavaDoc xsl;
30
31     public static void main(String JavaDoc[] args) {
32         run(new XSLTDemo(), args);
33     }
34
35     public XSLTDemo() {
36     }
37
38     public void run(String JavaDoc[] args) throws Exception JavaDoc {
39         if (args.length < 2) {
40             printUsage();
41             return;
42         }
43
44         int idx = format.parseOptions(args, 0);
45         if (args.length - idx < 2) {
46             printUsage();
47             return;
48         } else {
49             writer = createXMLWriter();
50
51             Document document = parse(args[idx++]);
52
53             xsl = args[idx++];
54
55             process(document);
56         }
57     }
58
59     protected void printUsage() {
60         printUsage("<XML URL> <XSLT URL>");
61     }
62
63     /** Perform XSLT on the stylesheet */
64     protected void process(Document document) throws Exception JavaDoc {
65         // load the transformer
66
TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
67         Transformer JavaDoc transformer = factory.newTransformer(new StreamSource JavaDoc(xsl));
68
69         // now lets create the TrAX source and result
70
// objects and do the transformation
71
Source JavaDoc source = new DocumentSource(document);
72         DocumentResult result = new DocumentResult();
73         transformer.transform(source, result);
74
75         // output the transformed document
76
Document transformedDoc = result.getDocument();
77         writer.write(transformedDoc);
78     }
79
80 }
81
82 /*
83  * Redistribution and use of this software and associated documentation
84  * ("Software"), with or without modification, are permitted provided that the
85  * following conditions are met:
86  *
87  * 1. Redistributions of source code must retain copyright statements and
88  * notices. Redistributions must also contain a copy of this document.
89  *
90  * 2. Redistributions in binary form must reproduce the above copyright notice,
91  * this list of conditions and the following disclaimer in the documentation
92  * and/or other materials provided with the distribution.
93  *
94  * 3. The name "DOM4J" must not be used to endorse or promote products derived
95  * from this Software without prior written permission of MetaStuff, Ltd. For
96  * written permission, please contact dom4j-info@metastuff.com.
97  *
98  * 4. Products derived from this Software may not be called "DOM4J" nor may
99  * "DOM4J" appear in their names without prior written permission of MetaStuff,
100  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
101  *
102  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
103  *
104  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
105  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
107  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
108  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
109  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
110  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
111  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
112  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
113  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
114  * POSSIBILITY OF SUCH DAMAGE.
115  *
116  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
117  *
118  * $Id: XSLTDemo.java,v 1.4 2005/01/29 14:52:57 maartenc Exp $
119  */

120
Popular Tags