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.JellyException;
19  import org.apache.commons.jelly.TagLibrary;
20  import org.apache.commons.jelly.expression.Expression;
21  import org.apache.commons.jelly.expression.ExpressionFactory;
22  import org.apache.commons.jelly.expression.CompositeExpression;
23  import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
24  import org.apache.commons.jelly.expression.xpath.XPathExpression;
25  import org.apache.commons.jelly.impl.TagScript;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  
31  /*** Describes the Taglib. This class could be generated by XDoclet
32    *
33    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
34    * @version $Revision: 1.5 $
35    */
36  public class XMLTagLibrary extends TagLibrary {
37  
38      /*** The Log to which logging calls will be made. */
39      private Log log = LogFactory.getLog(XMLTagLibrary.class);
40  
41      private JexlExpressionFactory jexlFactory;
42  
43      public XMLTagLibrary() {
44          registerTag("out", ExprTag.class);
45          registerTag("if", IfTag.class);
46          registerTag("forEach", ForEachTag.class);
47          registerTag("parse", ParseTag.class);
48          registerTag("set", SetTag.class);
49          registerTag("transform", TransformTag.class);
50          registerTag("param", ParamTag.class);
51  
52          // extensions to JSTL
53          registerTag("expr", ExprTag.class);
54          registerTag("element", ElementTag.class);
55          registerTag("attribute", AttributeTag.class);
56          registerTag("copy", CopyTag.class);
57          registerTag("copyOf", CopyOfTag.class);
58          registerTag("comment", CommentTag.class);
59          registerTag("doctype", DoctypeTag.class);
60          registerTag("sort", SortTag.class);
61  
62          this.jexlFactory = new JexlExpressionFactory();
63      }
64  
65      public Expression createExpression(
66          ExpressionFactory factory,
67          TagScript tagScript,
68          String attributeName,
69          String attributeValue) throws JellyException {
70  
71          // #### may need to include some namespace URI information in the XPath instance?
72  
73          if (attributeName.equals("select") || attributeName.equals("sort")) {
74              if ( log.isDebugEnabled() ) {
75                  log.debug( "Parsing XPath expression: " + attributeValue );
76              }
77  
78              Expression xpathExpr = createXPathTextExpression( attributeValue );
79  
80              return new XPathExpression(attributeValue,
81                                         xpathExpr,
82                                         tagScript);
83          }
84  
85          // will use the default expression instead
86          return super.createExpression(factory, tagScript, attributeName, attributeValue);
87      }
88  
89      protected Expression createXPathTextExpression(String exprText) throws JellyException {
90          return CompositeExpression.parse( exprText,
91                                            this.jexlFactory );
92      }
93  }