View Javadoc

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 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      // Script interface
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  }