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.jsl;
17  
18  import org.apache.commons.jelly.JellyException;
19  import org.apache.commons.jelly.expression.Expression;
20  import org.apache.commons.jelly.expression.ExpressionFactory;
21  import org.apache.commons.jelly.impl.TagScript;
22  import org.apache.commons.jelly.tags.xml.ExprTag;
23  import org.apache.commons.jelly.tags.xml.XMLTagLibrary;
24  import org.apache.commons.jelly.expression.xpath.XPathExpression;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.dom4j.DocumentHelper;
28  import org.dom4j.rule.Pattern;
29  
30  /*** Describes the Taglib. This class could be generated by XDoclet
31    *
32    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33    * @version $Revision: 155420 $
34    */
35  public class JSLTagLibrary extends XMLTagLibrary {
36  
37      /*** The Log to which logging calls will be made. */
38      private Log log = LogFactory.getLog(JSLTagLibrary.class);
39  
40      public JSLTagLibrary() {
41          registerTag("stylesheet", StylesheetTag.class);
42          registerTag("style", StyleTag.class);
43          registerTag("template", TemplateTag.class);
44          registerTag("applyTemplates", ApplyTemplatesTag.class);
45          registerTag("valueOf", ExprTag.class);
46      }
47  
48      public Expression createExpression(
49          ExpressionFactory factory,
50          TagScript tagScript,
51          String attributeName,
52          String attributeValue) throws JellyException {
53  
54          // #### may need to include some namespace URI information in the XPath instance?
55  
56          if (attributeName.equals("select")) {
57              if ( log.isDebugEnabled() ) {
58                  log.debug( "Parsing XPath expression: " + attributeValue );
59              }
60  
61              Expression xpathExpr = createXPathTextExpression( attributeValue );
62  
63              return new XPathExpression(attributeValue, xpathExpr, tagScript);
64          }
65  
66          if (attributeName.equals("match")) {
67              if ( log.isDebugEnabled() ) {
68                  log.debug( "Parsing XPath pattern: " + attributeValue );
69              }
70  
71              try {
72                  Pattern pattern = DocumentHelper.createPattern( attributeValue );
73                  return new XPathPatternExpression(attributeValue, pattern);
74              }
75              catch (Exception e) {
76                  throw new JellyException( "Could not parse XPath expression: \"" + attributeValue + "\" reason: " + e, e );
77              }
78          }
79  
80          // will use the default expression instead
81          return super.createExpression(factory, tagScript, attributeName, attributeValue);
82      }
83  
84  
85  }