KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > samples > dom > XSLTNativeDOMDemo


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: XSLTNativeDOMDemo.java,v 1.4 2005/01/29 14:52:58 maartenc Exp $
8  */

9
10 package org.dom4j.samples.dom;
11
12 import org.dom4j.samples.XSLTDemo;
13
14 import javax.xml.transform.Source JavaDoc;
15 import javax.xml.transform.Transformer JavaDoc;
16 import javax.xml.transform.TransformerFactory JavaDoc;
17 import javax.xml.transform.dom.DOMSource JavaDoc;
18 import javax.xml.transform.stream.StreamResult JavaDoc;
19 import javax.xml.transform.stream.StreamSource JavaDoc;
20
21 import org.dom4j.Document;
22 import org.dom4j.dom.DOMDocumentFactory;
23 import org.dom4j.io.DOMWriter;
24 import org.dom4j.io.SAXReader;
25
26 /**
27  * This sample program peforms XSLT on the native DOM implementation of dom4j.
28  *
29  * @author <a HREF="mailto:jstrachan@apache.org">James Strachan </a>
30  * @version $Revision: 1.4 $
31  */

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

111
Popular Tags