1 /*
2 * ====================================================================
3 *
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution, if
22 * any, must include the following acknowlegement:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowlegement may appear in the software itself,
26 * if and wherever such third-party acknowlegements normally appear.
27 *
28 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29 * Foundation" must not be used to endorse or promote products derived
30 * from this software without prior written permission. For written
31 * permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache"
34 * nor may "Apache" appear in their names without prior written
35 * permission of the Apache Group.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 *
56 */
57 package org.apache.commons.jelly.impl;
58
59 import java.io.ByteArrayInputStream;
60 import java.io.File;
61 import java.io.IOException;
62 import java.io.InputStream;
63 import java.io.OutputStream;
64 import java.io.OutputStreamWriter;
65 import java.net.MalformedURLException;
66 import java.net.URL;
67
68 import org.apache.commons.jelly.Jelly;
69 import org.apache.commons.jelly.JellyContext;
70 import org.apache.commons.jelly.Script;
71 import org.apache.commons.jelly.TagLibrary;
72 import org.apache.commons.jelly.XMLOutput;
73 import org.apache.commons.jelly.parser.XMLParser;
74 import org.apache.commons.logging.Log;
75 import org.apache.commons.logging.LogFactory;
76 import org.xml.sax.SAXException;
77
78 /***
79 *
80 *
81 * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
82 */
83 /*** <p><code>Embedded</code> provides easy means to embed JellyEngine <br/>
84 * and use Jelly scripts within an application</p>
85 * A typical usage:<br/>
86 * <code><br/>
87 * Embedded embedded = new Embedded();<br/>
88 * embedded.setOutputStream(new ByteArrayOutputStream());<br/>
89 * embedded.setVariable("some-var","some-object");<br/>
90 * .....<br/>
91 * embedded.setScript(scriptAsString);<br/>
92 * //or one can do.<br/>
93 * //embedded.setScript(scriptAsInputStream);<br/>
94 * <br/>
95 * boolean bStatus=embedded.execute();<br/>
96 * if(!bStatus) //if error<br/>
97 * {<br/>
98 * String errorMsg=embedded.getErrorMsg();<br/>
99 * }<br/>
100 * </code> <br/>
101 *
102 * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a>
103 */
104 public class Embedded {
105 /*** Jelly Engine */
106 Jelly m_jellyEngine = new Jelly();
107 /*** JellyContext*/
108 private JellyContext m_context = new JellyContext();
109 /*** Compiled Script Object*/
110 private Script m_script;
111 /*** Input script as stream*/
112 private InputStream m_inputStream;
113 /*** Output Stream */
114 private OutputStream m_outputStream;
115 /*** Output(default System.out) */
116 private XMLOutput m_output =
117 XMLOutput.createXMLOutput(new OutputStreamWriter(System.out));
118 /*** Exception thrown during compilation of script*/
119 Exception m_scriptCompilationException;
120 /*** boolean value indicating whether the script has been successfully compiled or NOT */
121 boolean m_scriptCompiled = false;
122 /*** ErrorMsg*/
123 private String m_errorMsg;
124 /*** The Log to which logging calls will be made. */
125 private static final Log log = LogFactory.getLog(Embedded.class);
126
127 /***
128 * Default Constructor
129 *
130 */
131 public Embedded() {
132 //m_context.setClassLoader(new TagLibraryClassLoader(m_context));
133 }
134
135 /***
136 * Method setContext.
137 * @param context
138 */
139 public void setContext(JellyContext context) {
140 m_context = context;
141 }
142
143 /***
144 * Method getContext.
145 * @return JellyContext
146 */
147 public JellyContext getContext() {
148 return m_context;
149 }
150
151 /***
152 * Set a new variable within the context for the script to use.
153 * @param name
154 * @param value
155 */
156 public void setVariable(String name, Object value) {
157 m_context.setVariable(name, value);
158 }
159
160 /***
161 * Set the input script
162 * @param scriptAsString
163 */
164 public void setScript(String scriptAsString) {
165
166 try {
167 URL url = resolveURL(scriptAsString);
168 m_inputStream = url.openStream();
169 }
170 catch (MalformedURLException e) {
171 //Encapsulate the string within
172 m_inputStream = new ByteArrayInputStream(scriptAsString.getBytes());
173 }
174 catch (IOException e) {
175 //Error reading from the URL
176 m_inputStream = null;
177 }
178
179 compileScriptAndKeep();
180
181 }
182
183 /***
184 * @return the URL for the relative file name or absolute URL
185 */
186 private URL resolveURL(String name) throws MalformedURLException {
187 File file = new File(name);
188 if (file.exists()) {
189 return file.toURL();
190 }
191 return new URL(name);
192 }
193
194 /***
195 * Set the input stream
196 * @param scriptAsInputStream
197 */
198 public void setScript(InputStream scriptAsInputStream) {
199 m_inputStream = scriptAsInputStream;
200 compileScriptAndKeep();
201 }
202
203 /***
204 * Compile the script
205 */
206 private void compileScriptAndKeep() {
207 XMLParser parser = new XMLParser();
208 parser.setContext(m_context);
209 try {
210 m_script = parser.parse(m_inputStream);
211 m_script = m_script.compile();
212 m_scriptCompiled = true;
213 }
214 catch (IOException e) {
215 m_scriptCompilationException = e;
216 m_scriptCompiled = false;
217 }
218 catch (SAXException e) {
219 m_scriptCompilationException = e;
220 m_scriptCompiled = false;
221 }
222 catch (Exception e) {
223 m_scriptCompilationException = e;
224 m_scriptCompiled = false;
225 }
226 }
227
228 /***
229 * Method setOutputStream.
230 * @param outputStream
231 */
232 public void setOutputStream(OutputStream outputStream) {
233 m_outputStream = outputStream;
234 m_output =
235 XMLOutput.createXMLOutput(new OutputStreamWriter(m_outputStream));
236 }
237
238 /***
239 * Registers the given tag library class name against the given namespace URI.
240 * The class will be loaded via the given ClassLoader
241 * This should be called before the parser is used.
242 */
243 public void registerTagLibrary(String namespaceURI, String className) {
244 if (m_context != null)
245 m_context.registerTagLibrary(namespaceURI, className);
246 }
247
248 /***
249 * Registers the given tag library against the given namespace URI.
250 * This should be called before the parser is used.
251 */
252 public void registerTagLibrary(String namespaceURI, TagLibrary taglib) {
253 if (m_context != null)
254 m_context.registerTagLibrary(namespaceURI, taglib);
255 }
256
257 /***
258 * Returns the errorMsg.
259 * @return String
260 */
261 public String getErrorMsg() {
262 return m_errorMsg;
263 }
264
265 /***
266 * Sets the errorMsg.
267 * @param errorMsg The errorMsg to set
268 */
269 private void setErrorMsg(String errorMsg) {
270 m_errorMsg = errorMsg;
271 }
272
273 /***
274 * Execute the jelly script and capture the errors (ifany)within.
275 *
276 * @throws JellyException
277 */
278 public boolean execute() {
279 if (log.isDebugEnabled())
280 log.debug("Starting Execution");
281 //If script has not been compiled then return the errorMsg that occured during compilation
282 if (!m_scriptCompiled) {
283 if (log.isErrorEnabled())
284 log.error(m_scriptCompilationException.getMessage());
285 setErrorMsg(m_scriptCompilationException.getMessage());
286 return false;
287 }
288 if (m_inputStream == null) {
289 if (log.isErrorEnabled())
290 log.error("[Error] Input script-resource NOT accessible");
291 setErrorMsg("[Error] Input script-resource NOT accessible");
292 return false;
293 }
294 try {
295 m_script.run(m_context, m_output);
296 m_outputStream.close();
297 m_output.flush();
298 }
299 catch (Exception e) {
300 if (log.isErrorEnabled())
301 log.error(e.getMessage());
302 setErrorMsg(e.getMessage());
303 return false;
304 }
305 if (log.isDebugEnabled())
306 log.debug("Done Executing");
307 return true;
308 }
309
310 }
This page was automatically generated by Maven