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: 219726 $
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("replaceNamespace", ReplaceNamespaceTag.class);
57          registerTag("copy", CopyTag.class);
58          registerTag("copyOf", CopyOfTag.class);
59          registerTag("comment", CommentTag.class);
60          registerTag("doctype", DoctypeTag.class);
61          registerTag("sort", SortTag.class);
62  
63          this.jexlFactory = new JexlExpressionFactory();
64      }
65  
66      public Expression createExpression(
67          ExpressionFactory factory,
68          TagScript tagScript,
69          String attributeName,
70          String attributeValue) throws JellyException {
71  
72          // #### may need to include some namespace URI information in the XPath instance?
73  
74          if (attributeName.equals("select") || attributeName.equals("sort")) {
75              if ( log.isDebugEnabled() ) {
76                  log.debug( "Parsing XPath expression: " + attributeValue );
77              }
78  
79              Expression xpathExpr = createXPathTextExpression( attributeValue );
80  
81              return new XPathExpression(attributeValue,
82                                         xpathExpr,
83                                         tagScript);
84          }
85  
86          // will use the default expression instead
87          return super.createExpression(factory, tagScript, attributeName, attributeValue);
88      }
89  
90      protected Expression createXPathTextExpression(String exprText) throws JellyException {
91          return CompositeExpression.parse( exprText,
92                                            this.jexlFactory );
93      }
94  }