KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TransformBean


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: TransformBean.java,v 1.7 2004/02/17 19:04:22 minchau Exp $
18  */

19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22
23 import javax.xml.transform.Transformer JavaDoc;
24 import javax.xml.transform.TransformerFactory JavaDoc;
25 import javax.xml.transform.ErrorListener JavaDoc;
26 import javax.xml.transform.stream.StreamResult JavaDoc;
27 import javax.xml.transform.stream.StreamSource JavaDoc;
28
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31
32 /**
33  * @author Morten Jorgensen
34  */

35 public class TransformBean implements SessionBean JavaDoc {
36
37     private SessionContext JavaDoc m_context = null;
38     
39     private final static String JavaDoc nullErrorMsg =
40     "<h1>XSL transformation error</h1>"+
41     "<p>'null' parameters sent to the XSL transformation bean's "+
42     "<tt>transform(String document, String translet)</tt> method.</p>";
43
44     private static final String JavaDoc NAMESPACE_FEATURE =
45     "http://xml.org/sax/features/namespaces";
46
47     /**
48      * Generates HTML from a basic error message and an exception
49      */

50     private void errorMsg(PrintWriter JavaDoc out, Exception JavaDoc e, String JavaDoc msg) {
51     out.println("<h1>Error</h1>");
52     out.println("<p>"+msg+"</p><br>");
53     out.println(e.toString());
54     }
55
56     /**
57      * Main bean entry point
58      */

59     public String JavaDoc transform(String JavaDoc document, String JavaDoc transletName) {
60
61     // Initialise the output stream
62
final StringWriter JavaDoc sout = new StringWriter JavaDoc();
63     final PrintWriter JavaDoc out = new PrintWriter JavaDoc(sout);
64
65     try {
66         if ((document == null) || (transletName == null)) {
67         out.println(nullErrorMsg);
68         }
69         else {
70                 TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
71                 try {
72                     tf.setAttribute("use-classpath", Boolean.TRUE);
73                 } catch (IllegalArgumentException JavaDoc iae) {
74                     System.err.println(
75                         "Could not set XSLTC-specific TransformerFactory "
76                       + "attributes. Transformation failed.");
77                 }
78
79                 Transformer JavaDoc t =
80                     tf.newTransformer(new StreamSource JavaDoc(transletName));
81
82                 // Do the actual transformation
83
final long start = System.currentTimeMillis();
84                 t.transform(new StreamSource JavaDoc(document),
85                             new StreamResult JavaDoc(out));
86                 final long done = System.currentTimeMillis() - start;
87                 out.println("<!-- transformed by XSLTC in "+done+"msecs -->");
88         }
89     }
90
91     catch (Exception JavaDoc e) {
92         errorMsg(out, e, "Impossible state reached.");
93     }
94
95     // Now close up the sink, and return the HTML output in the
96
// StringWrite object as a string.
97
out.close();
98     return sout.toString();
99     }
100
101     /**
102      *
103      */

104     public void setSessionContext(SessionContext JavaDoc context) {
105     m_context = context;
106     }
107
108     // General EJB entry points
109
public void ejbCreate() { }
110     public void ejbRemove() { }
111     public void ejbActivate() { }
112     public void ejbPassivate() { }
113     public void ejbLoad() { }
114     public void ejbStore() { }
115 }
116
Popular Tags