KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > gumby > tags > script > Expr


1 package org.sapia.gumby.tags.script;
2
3 import org.sapia.gumby.RenderContext;
4 import org.sapia.gumby.factory.ContextAware;
5 import org.sapia.gumby.script.EngineFactory;
6 import org.sapia.gumby.script.Script;
7 import org.sapia.util.xml.confix.ConfigurationException;
8 import org.sapia.util.xml.confix.ObjectCreationCallback;
9
10 /**
11  * @author Yanick Duchesne
12  *
13  * <dl>
14  * <dt><b>Copyright: </b>
15  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
16  * Source Software </a>. All Rights Reserved.</dd>
17  * </dt>
18  * <dt><b>License: </b>
19  * <dd>Read the license.txt file of the jar or visit the <a
20  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
21  * OSS web site</dd>
22  * </dt>
23  * </dl>
24  */

25 public class Expr implements ObjectCreationCallback, ContextAware {
26
27   private static final Object JavaDoc[] EMPTY_ARGS = new Object JavaDoc[0];
28
29   private String JavaDoc _text, _lang;
30   private RenderContext _context;
31
32   public void setText(String JavaDoc txt) {
33     _text = txt;
34   }
35
36   public void setLang(String JavaDoc lang) {
37     _lang = lang;
38   }
39
40   /**
41    * @see org.sapia.gumby.factory.ContextAware#handleContext(org.sapia.gumby.RenderContext)
42    */

43   public void handleContext(RenderContext context) {
44     _context = context;
45   }
46
47   /**
48    * @see org.sapia.util.xml.confix.ObjectCreationCallback#onCreate()
49    */

50   public Object JavaDoc onCreate() throws ConfigurationException {
51     if(_text == null)
52       throw new ConfigurationException("Script source not specified");
53
54     Script script;
55     try {
56       if(_lang == null) {
57         script = EngineFactory.getDefaultEngine().parseScript(_text);
58       } else {
59         script = EngineFactory.getEngineFor(_lang).parseScript(_text);
60       }
61     } catch(Exception JavaDoc e) {
62       throw new ConfigurationException("Could not parse script", e);
63     }
64     try {
65       return script.execute(_context, EMPTY_ARGS);
66     } catch(Exception JavaDoc e) {
67       throw new ConfigurationException("Could not evaluate expression", e);
68     }
69   }
70
71 }
72
Popular Tags