1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.impl;
17
18 import org.apache.commons.jelly.JellyContext;
19 import org.apache.commons.jelly.JellyTagException;
20 import org.apache.commons.jelly.Script;
21 import org.apache.commons.jelly.XMLOutput;
22 import org.apache.commons.jelly.expression.Expression;
23
24 import org.xml.sax.SAXException;
25
26 /***
27 * <p><code>ExpressionScript</code> outputs the value of an expression as text.</p>
28 *
29 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30 * @version $Revision: 155420 $
31 */
32 public class ExpressionScript implements Script {
33
34 /*** the expression evaluated as a String and output by this script */
35 private Expression expression;
36
37 public ExpressionScript() {
38 }
39
40 public ExpressionScript(Expression expression) {
41 this.expression = expression;
42 }
43
44 public String toString() {
45 return super.toString() + "[expression=" + expression + "]";
46 }
47
48 /*** @return the expression evaluated as a String and output by this script */
49 public Expression getExpression() {
50 return expression;
51 }
52
53 /*** Sets the expression evaluated as a String and output by this script */
54 public void setExpression(Expression expression) {
55 this.expression = expression;
56 }
57
58
59
60 public Script compile() {
61 return this;
62 }
63
64 /*** Evaluates the body of a tag */
65 public void run(JellyContext context, XMLOutput output) throws JellyTagException {
66 Object result = expression.evaluate(context);
67 if ( result != null ) {
68
69 try {
70 output.objectData(result);
71 } catch (SAXException e) {
72 throw new JellyTagException("Could not write to XMLOutput",e);
73 }
74
75 }
76 }
77 }