You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by we...@apache.org on 2002/12/24 05:50:19 UTC

cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml XMLTagLibrary.java XPathExpression.java

werken      2002/12/23 20:50:19

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/jsl
                        JSLTagLibrary.java
               jelly/src/java/org/apache/commons/jelly/tags/junit
                        JUnitTagLibrary.java
               jelly/src/java/org/apache/commons/jelly/tags/xml
                        XMLTagLibrary.java XPathExpression.java
  Log:
  Experimental support in XPathExpression to allow actual xpath expression
  text to be resolved from a jexl expression.
  
  ie:
  
    <j:set var="foo" value="/foo/bar/baz"/>
    <x:expr select="${foo}"/>
  
  should work.
  
  Previously, the contents of the @select had to be static text, without
  any ${jexl}-style textual evaluation.  The text itself is now a CompositeExpression.
  
  Revision  Changes    Path
  1.8       +3 -7      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jsl/JSLTagLibrary.java
  
  Index: JSLTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/jsl/JSLTagLibrary.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- JSLTagLibrary.java	11 Dec 2002 12:40:55 -0000	1.7
  +++ JSLTagLibrary.java	24 Dec 2002 04:50:19 -0000	1.8
  @@ -107,13 +107,9 @@
                   log.debug( "Parsing XPath expression: " + attributeValue );
               }
               
  -            try {
  -                XPath xpath = new Dom4jXPath(attributeValue);
  -                return new XPathExpression(attributeValue, xpath, tagScript);
  -            }
  -            catch (JaxenException e) {
  -                throw new JellyException( "Could not parse XPath expression: \"" + attributeValue + "\" reason: " + e, e );            
  -            }            
  +            Expression xpathExpr = createXPathTextExpression( attributeValue );
  +            
  +            return new XPathExpression(attributeValue, xpathExpr, tagScript);
           }
           
           if (attributeName.equals("match")) {
  
  
  
  1.9       +7 -2      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/JUnitTagLibrary.java
  
  Index: JUnitTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/JUnitTagLibrary.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- JUnitTagLibrary.java	4 Dec 2002 16:09:12 -0000	1.8
  +++ JUnitTagLibrary.java	24 Dec 2002 04:50:19 -0000	1.9
  @@ -111,8 +111,13 @@
               }
               
               try {
  -                XPath xpath = new Dom4jXPath(attributeValue);
  -                return new XPathExpression(attributeValue, xpath, tagScript);
  +                // XPath xpath = new Dom4jXPath(attributeValue);
  +                Expression xpathExpr = super.createExpression( factory,
  +                                                               tagScript,
  +                                                               attributeName,
  +                                                               attributeValue );
  +
  +                return new XPathExpression(attributeValue, xpathExpr, tagScript);
               }
               catch (JaxenException e) {
                   throw new JellyException( "Could not parse XPath expression: \"" + attributeValue + "\" reason: " + e, e );            
  
  
  
  1.19      +21 -12    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/XMLTagLibrary.java
  
  Index: XMLTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/XMLTagLibrary.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- XMLTagLibrary.java	13 Dec 2002 18:08:31 -0000	1.18
  +++ XMLTagLibrary.java	24 Dec 2002 04:50:19 -0000	1.19
  @@ -65,6 +65,8 @@
   import org.apache.commons.jelly.TagLibrary;
   import org.apache.commons.jelly.expression.Expression;
   import org.apache.commons.jelly.expression.ExpressionFactory;
  +import org.apache.commons.jelly.expression.CompositeExpression;
  +import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
   import org.apache.commons.jelly.impl.TagScript;
   
   import org.apache.commons.logging.Log;
  @@ -84,6 +86,8 @@
       /** The Log to which logging calls will be made. */
       private Log log = LogFactory.getLog(XMLTagLibrary.class);
   
  +    private JexlExpressionFactory jexlFactory;
  +
       public XMLTagLibrary() {
           registerTag("out", ExprTag.class);
           registerTag("if", IfTag.class);
  @@ -102,6 +106,8 @@
           registerTag("comment", CommentTag.class);
           registerTag("doctype", DoctypeTag.class);
           registerTag("sort", SortTag.class);
  +
  +        this.jexlFactory = new JexlExpressionFactory();
       }
   
       public Expression createExpression(
  @@ -117,16 +123,19 @@
                   log.debug( "Parsing XPath expression: " + attributeValue );
               }
   
  -            try {
  -                XPath xpath = new Dom4jXPath(attributeValue);
  -                return new XPathExpression(attributeValue, xpath, tagScript);
  -            }
  -            catch (JaxenException e) {
  -                throw new JellyException( "Could not parse XPath expression: \"" + attributeValue + "\" reason: " + e, e );
  -            }
  +            Expression xpathExpr = createXPathTextExpression( attributeValue );
  +            
  +            return new XPathExpression(attributeValue,
  +                                       xpathExpr,
  +                                       tagScript);
           }
   
           // will use the default expression instead
           return super.createExpression(factory, tagScript, attributeName, attributeValue);
  +    }
  +
  +    protected Expression createXPathTextExpression(String exprText) throws Exception {
  +        return CompositeExpression.parse( exprText,
  +                                          this.jexlFactory );
       }
   }
  
  
  
  1.12      +40 -20    jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/XPathExpression.java
  
  Index: XPathExpression.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/XPathExpression.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- XPathExpression.java	11 Dec 2002 12:40:56 -0000	1.11
  +++ XPathExpression.java	24 Dec 2002 04:50:19 -0000	1.12
  @@ -66,6 +66,7 @@
   import java.util.Map;
   
   import org.apache.commons.jelly.JellyContext;
  +import org.apache.commons.jelly.expression.Expression;
   import org.apache.commons.jelly.expression.ExpressionSupport;
   import org.apache.commons.jelly.impl.TagScript;
   import org.apache.commons.logging.Log;
  @@ -73,6 +74,8 @@
   import org.jaxen.SimpleNamespaceContext;
   import org.jaxen.VariableContext;
   import org.jaxen.XPath;
  +import org.jaxen.JaxenException;
  +import org.jaxen.dom4j.Dom4jXPath;
   
   /** An expression which returns an XPath object.
     *
  @@ -85,41 +88,58 @@
       private Log log = LogFactory.getLog(XPathExpression.class);
   
       private String text;
  -    private XPath xpath;
  +    private Expression xpathExpr;
       private JellyContext context;
  +    private Map uris;
       
       public XPathExpression() {
       }
   
  -    public XPathExpression(String text, XPath xpath, TagScript tagScript) {
  +    public XPathExpression(String text,
  +                           Expression xpathExpr,
  +                           TagScript tagScript) {
           this.text = text;
  -        this.xpath = xpath;
  +        this.xpathExpr = xpathExpr;
           
           Map namespaceContext = tagScript.getNamespaceContext();
           
  -        Map uris = createUriMap(namespaceContext);
  -        
  -        if (log.isDebugEnabled()) {
  -        	log.debug( "Setting the namespace context to be: " + uris );
  -        }
  -        
  -        xpath.setNamespaceContext( new SimpleNamespaceContext( uris ) );
  +        this.uris = createUriMap(namespaceContext);
       }
  -    
  +
       public String toString() {
  -        return xpath.toString();
  +        return getExpressionText();
       }
               
       // Expression interface
       //------------------------------------------------------------------------- 
       public String getExpressionText() {
  -        return text;
  +        return this.text;
       }
       
       public Object evaluate(JellyContext context) {
           this.context = context;
  -        xpath.setVariableContext(this);
  -        return xpath;
  +
  +        try
  +        {
  +            XPath xpath = new Dom4jXPath( this.xpathExpr.evaluateAsString( context ) );
  +            
  +            xpath.setVariableContext(this);
  +            
  +            if (log.isDebugEnabled()) {
  +                log.debug( "Setting the namespace context to be: " + uris );
  +            }
  +            
  +            xpath.setNamespaceContext( new SimpleNamespaceContext( this.uris ) );
  +            
  +            return xpath;
  +        }
  +        catch (JaxenException e)
  +        {
  +            log.error( "Error constructing xpath",
  +                       e );
  +        }
  +
  +        return null;
       }
       
       // VariableContext interface
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>