KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > util > xsl_transform


1 /* $Id: xsl_transform.java,v 1.4 2001/01/17 22:40:03 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.util;
16
17 import java.io.*;
18 import org.w3c.dom.*;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.apache.xalan.xslt.XSLTProcessorFactory;
21 import org.apache.xalan.xslt.XSLTInputSource;
22 import org.apache.xalan.xslt.XSLTResultTarget;
23 import org.apache.xalan.xslt.XSLTProcessor;
24
25
26 /**
27  * Direct access to the Xalan XSL formating capabilities
28  *
29  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
30  */

31 public class xsl_transform
32 {
33    public xsl_transform ()
34    {
35    }
36    
37    /**
38     * Transformation method; We expect to improve this method soon.
39     * Given a xml stream and an XSL template, it returns a string with
40     * the document transformation;
41     * @param input Content of the XML file
42     * @param templateName Name of the template
43     * @return String Output of the transformation
44     */

45    public String JavaDoc process (String JavaDoc input, String JavaDoc templateName)
46    throws java.io.IOException JavaDoc,
47           java.net.MalformedURLException JavaDoc
48
49    {
50     StringReader readerInput = new StringReader (input);
51     XSLTInputSource XSLinput = new XSLTInputSource (readerInput);
52
53     StringWriter outBuffer = new StringWriter();
54     XSLTResultTarget XSLoutput = new XSLTResultTarget(outBuffer);
55 try {
56     XSLTProcessor processor = XSLTProcessorFactory.getProcessor();
57     processor.process(XSLinput, new XSLTInputSource(templateName),
58                           XSLoutput);
59 } catch (Exception JavaDoc e) {
60     e.printStackTrace();
61 }
62     String JavaDoc result = XSLoutput.getCharacterStream().toString();
63     //System.out.println (result);
64
return (result);
65    }
66
67     /**
68     * Transformation method; We expect to improve this method soon
69     * In this case, the input source is a DOM tree.
70     * @param input DOM tree of the document
71     * @param templateName Name of the template
72     * @return String XSL output
73     */

74    public String JavaDoc processDOM (Document input, String JavaDoc templateName)
75    throws java.io.IOException JavaDoc,
76           java.net.MalformedURLException JavaDoc
77
78    {
79     XSLTInputSource XSLinput = new XSLTInputSource (input);
80
81     StringWriter outBuffer = new StringWriter();
82     XSLTResultTarget XSLoutput = new XSLTResultTarget(outBuffer);
83 try {
84     // Set up the XSLTProcessor to use XercesLiaison.
85
XSLTProcessor processor = XSLTProcessorFactory.getProcessor
86                                   (new org.apache.xalan.xpath.xdom.XercesLiaison());
87
88     processor.process(XSLinput, new XSLTInputSource(templateName),
89                           XSLoutput);
90 } catch (Exception JavaDoc e) {
91     e.printStackTrace();
92 }
93     String JavaDoc result = XSLoutput.getCharacterStream().toString();
94     return (result);
95    }
96
97 }
98
Popular Tags