KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > bsf > engines > xslt > XSLTEngine


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2002 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "Apache BSF", "Apache", and "Apache Software Foundation"
27  * must not be used to endorse or promote products derived from
28  * this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many individuals
50  * on behalf of the Apache Software Foundation and was originally created by
51  * Sanjiva Weerawarana and others at International Business Machines
52  * Corporation. For more information on the Apache Software Foundation,
53  * please see <http://www.apache.org/>.
54  */

55
56 package org.apache.bsf.engines.xslt;
57
58 import java.util.*;
59 import java.io.*;
60 import java.net.URL JavaDoc;
61
62 import javax.xml.transform.Transformer JavaDoc;
63 import javax.xml.transform.TransformerFactory JavaDoc;
64 import javax.xml.transform.dom.DOMResult JavaDoc;
65 import javax.xml.transform.dom.DOMSource JavaDoc;
66 import javax.xml.transform.stream.StreamSource JavaDoc;
67
68 import org.w3c.dom.*;
69
70 import org.apache.xpath.objects.XObject;
71
72 import org.apache.bsf.*;
73 import org.apache.bsf.util.BSFEngineImpl;
74 import org.apache.bsf.util.BSFFunctions;
75 import org.apache.bsf.debug.util.DebugLog;
76
77 /**
78  * Xerces XSLT interface to BSF. Requires Xalan and Xerces from Apache.
79  *
80  * This integration uses the BSF registry to pass in any src document
81  * and stylesheet base URI that the user may wish to set.
82  *
83  * @author Sanjiva Weerawarana
84  * @author Sam Ruby
85  *
86  * Re-implemented for the Xalan 2 codebase
87  *
88  * @author Victor J. Orlikowski
89  */

90 public class XSLTEngine extends BSFEngineImpl {
91     TransformerFactory JavaDoc tFactory;
92     Transformer JavaDoc transformer;
93
94     /**
95      * call the named method of the given object.
96      */

97     public Object JavaDoc call (Object JavaDoc object, String JavaDoc method, Object JavaDoc[] args)
98         throws BSFException {
99     throw new BSFException (BSFException.REASON_UNSUPPORTED_FEATURE,
100                                 "BSF:XSLTEngine can't call methods");
101     }
102
103     /**
104      * Declare a bean by setting it as a parameter
105      */

106     public void declareBean (BSFDeclaredBean bean) throws BSFException {
107         transformer.setParameter (bean.name, new XObject (bean.bean));
108     }
109
110     /**
111      * Evaluate an expression. In this case, an expression is assumed
112      * to be a stylesheet of the template style (see the XSLT spec).
113      */

114     public Object JavaDoc eval (String JavaDoc source, int lineNo, int columnNo,
115                         Object JavaDoc oscript) throws BSFException {
116     // get the style base URI (the place from where Xerces XSLT will
117
// look for imported/included files and referenced docs): if a
118
// bean named "xslt:styleBaseURI" is registered, then cvt it
119
// to a string and use that. Otherwise use ".", which means the
120
// base is the directory where the process is running from
121
Object JavaDoc sbObj = mgr.lookupBean ("xslt:styleBaseURI");
122     String JavaDoc styleBaseURI = (sbObj == null) ? "." : sbObj.toString ();
123
124     // Locate the stylesheet.
125
StreamSource JavaDoc styleSource;
126
127         styleSource =
128             new StreamSource JavaDoc(new StringReader(oscript.toString ()));
129         styleSource.setSystemId(styleBaseURI);
130
131         try {
132             transformer = tFactory.newTransformer(styleSource);
133         } catch (Exception JavaDoc e) {
134             e.printStackTrace (DebugLog.getDebugStream());
135             throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
136                                     "Exception from Xerces XSLT: " + e, e);
137         }
138
139     // get the src to work on: if a bean named "xslt:src" is registered
140
// and its a Node, then use it as the source. If its not a Node, then
141
// if its a URL parse it, if not treat it as a file and make a URL and
142
// parse it and go. If no xslt:src is found, use an empty document
143
// (stylesheet is treated as a literal result element stylesheet)
144
Object JavaDoc srcObj = mgr.lookupBean ("xslt:src");
145     Object JavaDoc xis = null;
146     if (srcObj != null) {
147             if (srcObj instanceof Node) {
148         xis = new DOMSource JavaDoc((Node)srcObj);
149             } else {
150         try {
151                     String JavaDoc mesg = "as anything";
152                     if (srcObj instanceof Reader) {
153             xis = new StreamSource JavaDoc ((Reader) srcObj);
154             mesg = "as a Reader";
155                     } else if (srcObj instanceof File) {
156                         xis = new StreamSource JavaDoc ((File) srcObj);
157                         mesg = "as a file";
158                     } else {
159                         String JavaDoc srcObjstr=srcObj.toString();
160                         xis = new StreamSource JavaDoc (new StringReader(srcObjstr));
161                         if (srcObj instanceof URL JavaDoc) {
162                             mesg = "as a URL";
163                         } else {
164                             ((StreamSource JavaDoc) xis).setPublicId (srcObjstr);
165                             mesg = "as an XML string";
166                         }
167                     }
168
169                     if (xis == null) {
170             throw new Exception JavaDoc ("Unable to get input from '" +
171                                              srcObj + "' " + mesg);
172                     }
173         } catch (Exception JavaDoc e) {
174                     throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
175                                             "BSF:XSLTEngine: unable to get " +
176                                             "input from '" + srcObj + "' as XML", e);
177         }
178             }
179     } else {
180             // create an empty document - real src must come into the
181
// stylesheet using "doc(...)" [see XSLT spec] or the stylesheet
182
// must be of literal result element type
183
xis = new StreamSource JavaDoc();
184     }
185     
186     // set all declared beans as parameters.
187
for (int i = 0; i < declaredBeans.size (); i++) {
188             BSFDeclaredBean b = (BSFDeclaredBean) declaredBeans.elementAt (i);
189             transformer.setParameter (b.name, new XObject (b.bean));
190     }
191
192     // declare a "bsf" parameter which is the BSF handle so that
193
// the script can do BSF stuff if it wants to
194
transformer.setParameter ("bsf",
195                                   new XObject (new BSFFunctions (mgr, this)));
196
197     // do it
198
try {
199             DOMResult JavaDoc result = new DOMResult JavaDoc();
200             transformer.transform ((StreamSource JavaDoc) xis, result);
201             return new XSLTResultNode (result.getNode());
202     } catch (Exception JavaDoc e) {
203             throw new BSFException (BSFException.REASON_EXECUTION_ERROR,
204                                     "exception while eval'ing XSLT script" + e, e);
205     }
206     }
207
208     /**
209      * Initialize the engine.
210      */

211     public void initialize (BSFManager mgr, String JavaDoc lang,
212                             Vector declaredBeans) throws BSFException {
213     super.initialize (mgr, lang, declaredBeans);
214
215         tFactory = TransformerFactory.newInstance();
216     }
217
218     /**
219      * Undeclare a bean by setting he parameter represeting it to null
220      */

221     public void undeclareBean (BSFDeclaredBean bean) throws BSFException {
222         // Cannot clear only one parameter in Xalan 2, so we set it to null
223
if ((transformer.getParameter (bean.name)) != null) {
224             transformer.setParameter (bean.name, null);
225         }
226     }
227 }
228
Popular Tags