| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jelly.impl.Embedded |
|
|
| 1 | /* |
|
| 2 | * Copyright 2002,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 | package org.apache.commons.jelly.impl; |
|
| 17 | ||
| 18 | import java.io.ByteArrayInputStream; |
|
| 19 | import java.io.File; |
|
| 20 | import java.io.IOException; |
|
| 21 | import java.io.InputStream; |
|
| 22 | import java.io.OutputStream; |
|
| 23 | import java.io.OutputStreamWriter; |
|
| 24 | import java.net.MalformedURLException; |
|
| 25 | import java.net.URL; |
|
| 26 | ||
| 27 | import org.apache.commons.jelly.Jelly; |
|
| 28 | import org.apache.commons.jelly.JellyContext; |
|
| 29 | import org.apache.commons.jelly.Script; |
|
| 30 | import org.apache.commons.jelly.TagLibrary; |
|
| 31 | import org.apache.commons.jelly.XMLOutput; |
|
| 32 | import org.apache.commons.jelly.parser.XMLParser; |
|
| 33 | import org.apache.commons.logging.Log; |
|
| 34 | import org.apache.commons.logging.LogFactory; |
|
| 35 | import org.xml.sax.SAXException; |
|
| 36 | ||
| 37 | /** |
|
| 38 | * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a> |
|
| 39 | * |
|
| 40 | * <p><code>Embedded</code> provides easy means to embed JellyEngine <br/> |
|
| 41 | * and use Jelly scripts within an application</p> |
|
| 42 | * A typical usage:<br/> |
|
| 43 | * <code><br/> |
|
| 44 | * Embedded embedded = new Embedded();<br/> |
|
| 45 | * embedded.setOutputStream(new ByteArrayOutputStream());<br/> |
|
| 46 | * embedded.setVariable("some-var","some-object");<br/> |
|
| 47 | * .....<br/> |
|
| 48 | * embedded.setScript(scriptAsString);<br/> |
|
| 49 | * //or one can do.<br/> |
|
| 50 | * //embedded.setScript(scriptAsInputStream);<br/> |
|
| 51 | * <br/> |
|
| 52 | * boolean bStatus=embedded.execute();<br/> |
|
| 53 | * if(!bStatus) //if error<br/> |
|
| 54 | * {<br/> |
|
| 55 | * String errorMsg=embedded.getErrorMsg();<br/> |
|
| 56 | * }<br/> |
|
| 57 | * </code> <br/> |
|
| 58 | * |
|
| 59 | * @author <a href="mailto:vinayc@apache.org">Vinay Chandran</a> |
|
| 60 | */ |
|
| 61 | public class Embedded { |
|
| 62 | /** Jelly Engine */ |
|
| 63 | 26 | Jelly m_jellyEngine = new Jelly(); |
| 64 | /** JellyContext*/ |
|
| 65 | 26 | private JellyContext m_context = new JellyContext(); |
| 66 | /** Compiled Script Object*/ |
|
| 67 | private Script m_script; |
|
| 68 | /** Input script as stream*/ |
|
| 69 | private InputStream m_inputStream; |
|
| 70 | /** Output Stream */ |
|
| 71 | private OutputStream m_outputStream; |
|
| 72 | /** Output(default System.out) */ |
|
| 73 | 26 | private XMLOutput m_output = |
| 74 | XMLOutput.createXMLOutput(new OutputStreamWriter(System.out)); |
|
| 75 | /** Exception thrown during compilation of script*/ |
|
| 76 | Exception m_scriptCompilationException; |
|
| 77 | /** boolean value indicating whether the script has been successfully compiled or NOT */ |
|
| 78 | 26 | boolean m_scriptCompiled = false; |
| 79 | /** ErrorMsg*/ |
|
| 80 | private String m_errorMsg; |
|
| 81 | /** The Log to which logging calls will be made. */ |
|
| 82 | 26 | private static final Log log = LogFactory.getLog(Embedded.class); |
| 83 | ||
| 84 | /** |
|
| 85 | * Default Constructor |
|
| 86 | * |
|
| 87 | */ |
|
| 88 | 26 | public Embedded() { |
| 89 | //m_context.setClassLoader(new TagLibraryClassLoader(m_context)); |
|
| 90 | 26 | } |
| 91 | ||
| 92 | /** |
|
| 93 | * Method setContext. |
|
| 94 | * @param context |
|
| 95 | */ |
|
| 96 | public void setContext(JellyContext context) { |
|
| 97 | 0 | m_context = context; |
| 98 | 0 | } |
| 99 | ||
| 100 | /** |
|
| 101 | * Method getContext. |
|
| 102 | * @return JellyContext |
|
| 103 | */ |
|
| 104 | public JellyContext getContext() { |
|
| 105 | 0 | return m_context; |
| 106 | } |
|
| 107 | ||
| 108 | /** |
|
| 109 | * Set a new variable within the context for the script to use. |
|
| 110 | * @param name |
|
| 111 | * @param value |
|
| 112 | */ |
|
| 113 | public void setVariable(String name, Object value) { |
|
| 114 | 0 | m_context.setVariable(name, value); |
| 115 | 0 | } |
| 116 | ||
| 117 | /** |
|
| 118 | * Set the input script |
|
| 119 | * @param scriptAsString |
|
| 120 | */ |
|
| 121 | public void setScript(String scriptAsString) { |
|
| 122 | ||
| 123 | try { |
|
| 124 | 26 | URL url = resolveURL(scriptAsString); |
| 125 | 0 | m_inputStream = url.openStream(); |
| 126 | } |
|
| 127 | 26 | catch (MalformedURLException e) { |
| 128 | //Encapsulate the string within |
|
| 129 | 26 | m_inputStream = new ByteArrayInputStream(scriptAsString.getBytes()); |
| 130 | } |
|
| 131 | 0 | catch (IOException e) { |
| 132 | //Error reading from the URL |
|
| 133 | 0 | m_inputStream = null; |
| 134 | 26 | } |
| 135 | ||
| 136 | 26 | compileScriptAndKeep(); |
| 137 | ||
| 138 | 26 | } |
| 139 | ||
| 140 | /** |
|
| 141 | * @return the URL for the relative file name or absolute URL |
|
| 142 | */ |
|
| 143 | private URL resolveURL(String name) throws MalformedURLException { |
|
| 144 | 26 | File file = new File(name); |
| 145 | 26 | if (file.exists()) { |
| 146 | 0 | return file.toURL(); |
| 147 | } |
|
| 148 | 26 | return new URL(name); |
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Set the input stream |
|
| 153 | * @param scriptAsInputStream |
|
| 154 | */ |
|
| 155 | public void setScript(InputStream scriptAsInputStream) { |
|
| 156 | 13 | m_inputStream = scriptAsInputStream; |
| 157 | 13 | compileScriptAndKeep(); |
| 158 | 13 | } |
| 159 | ||
| 160 | /** |
|
| 161 | * Compile the script |
|
| 162 | */ |
|
| 163 | private void compileScriptAndKeep() { |
|
| 164 | 39 | XMLParser parser = new XMLParser(); |
| 165 | 39 | parser.setContext(m_context); |
| 166 | 39 | m_scriptCompiled = false; |
| 167 | try { |
|
| 168 | 39 | m_script = parser.parse(m_inputStream); |
| 169 | 26 | m_script = m_script.compile(); |
| 170 | 26 | m_scriptCompiled = true; |
| 171 | } |
|
| 172 | 0 | catch (IOException e) { |
| 173 | 0 | m_scriptCompilationException = e; |
| 174 | } |
|
| 175 | 13 | catch (SAXException e) { |
| 176 | 13 | m_scriptCompilationException = e; |
| 177 | } |
|
| 178 | 0 | catch (Exception e) { |
| 179 | 0 | m_scriptCompilationException = e; |
| 180 | 39 | } |
| 181 | 39 | } |
| 182 | ||
| 183 | /** |
|
| 184 | * Method setOutputStream. |
|
| 185 | * @param outputStream |
|
| 186 | */ |
|
| 187 | public void setOutputStream(OutputStream outputStream) { |
|
| 188 | 26 | m_outputStream = outputStream; |
| 189 | 26 | m_output = |
| 190 | XMLOutput.createXMLOutput(new OutputStreamWriter(m_outputStream)); |
|
| 191 | 26 | } |
| 192 | ||
| 193 | /** |
|
| 194 | * Registers the given tag library class name against the given namespace URI. |
|
| 195 | * The class will be loaded via the given ClassLoader |
|
| 196 | * This should be called before the parser is used. |
|
| 197 | */ |
|
| 198 | public void registerTagLibrary(String namespaceURI, String className) { |
|
| 199 | 0 | if (m_context != null) |
| 200 | 0 | m_context.registerTagLibrary(namespaceURI, className); |
| 201 | 0 | } |
| 202 | ||
| 203 | /** |
|
| 204 | * Registers the given tag library against the given namespace URI. |
|
| 205 | * This should be called before the parser is used. |
|
| 206 | */ |
|
| 207 | public void registerTagLibrary(String namespaceURI, TagLibrary taglib) { |
|
| 208 | 0 | if (m_context != null) |
| 209 | 0 | m_context.registerTagLibrary(namespaceURI, taglib); |
| 210 | 0 | } |
| 211 | ||
| 212 | /** |
|
| 213 | * Returns the errorMsg. |
|
| 214 | * @return String |
|
| 215 | */ |
|
| 216 | public String getErrorMsg() { |
|
| 217 | 13 | return m_errorMsg; |
| 218 | } |
|
| 219 | ||
| 220 | /** |
|
| 221 | * Sets the errorMsg. |
|
| 222 | * @param errorMsg The errorMsg to set |
|
| 223 | */ |
|
| 224 | private void setErrorMsg(String errorMsg) { |
|
| 225 | 13 | m_errorMsg = errorMsg; |
| 226 | 13 | } |
| 227 | ||
| 228 | /** |
|
| 229 | * Execute the jelly script and capture the errors (ifany)within. |
|
| 230 | * |
|
| 231 | * @throws JellyException |
|
| 232 | */ |
|
| 233 | public boolean execute() { |
|
| 234 | 39 | if (log.isDebugEnabled()) |
| 235 | 0 | log.debug("Starting Execution"); |
| 236 | //If script has not been compiled then return the errorMsg that occured during compilation |
|
| 237 | 39 | if (!m_scriptCompiled) { |
| 238 | 13 | if (log.isErrorEnabled()) |
| 239 | 13 | log.error(m_scriptCompilationException.getMessage()); |
| 240 | 13 | setErrorMsg(m_scriptCompilationException.getMessage()); |
| 241 | 13 | return false; |
| 242 | } |
|
| 243 | 26 | if (m_inputStream == null) { |
| 244 | 0 | if (log.isErrorEnabled()) |
| 245 | 0 | log.error("[Error] Input script-resource NOT accessible"); |
| 246 | 0 | setErrorMsg("[Error] Input script-resource NOT accessible"); |
| 247 | 0 | return false; |
| 248 | } |
|
| 249 | try { |
|
| 250 | 26 | m_script.run(m_context, m_output); |
| 251 | 26 | m_outputStream.close(); |
| 252 | 26 | m_output.flush(); |
| 253 | } |
|
| 254 | 0 | catch (Exception e) { |
| 255 | 0 | if (log.isErrorEnabled()) |
| 256 | 0 | log.error(e.getMessage()); |
| 257 | 0 | setErrorMsg(e.getMessage()); |
| 258 | 0 | return false; |
| 259 | 26 | } |
| 260 | 26 | if (log.isDebugEnabled()) |
| 261 | 0 | log.debug("Done Executing"); |
| 262 | 26 | return true; |
| 263 | } |
|
| 264 | ||
| 265 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |