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.tags.xml;
17  
18  import org.apache.commons.jelly.JellyTagException;
19  import org.apache.commons.jelly.MissingAttributeException;
20  import org.apache.commons.jelly.XMLOutput;
21  import org.apache.commons.jelly.xpath.XPathTagSupport;
22  import org.jaxen.JaxenException;
23  import org.jaxen.XPath;
24  import org.xml.sax.SAXException;
25  
26  /*** A tag which performs a string XPath expression; similar to <xsl:value-of>
27    * in XSLT
28    *
29    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
30    * @version $Revision: 155420 $
31    */
32  public class ExprTag extends XPathTagSupport {
33  
34      /*** The XPath expression to evaluate. */
35      private XPath select;
36  
37      public ExprTag() {
38      }
39  
40      // Tag interface
41      //-------------------------------------------------------------------------
42      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
43          Object xpathContext = getXPathContext();
44  
45          if (select == null) {
46              throw new MissingAttributeException( "select" );
47          }
48  
49          try {
50              String text = select.stringValueOf(xpathContext);
51              if ( text != null ) {
52                  output.write(text);
53              }
54          }
55          catch (SAXException e) {
56              throw new JellyTagException(e);
57          }
58          catch (JaxenException e) {
59              throw new JellyTagException(e);
60          }
61      }
62  
63      // Properties
64      //-------------------------------------------------------------------------
65      /*** Sets the XPath expression to evaluate. */
66      public void setSelect(XPath select) {
67          this.select = select;
68      }
69  }