commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From morg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath XPathComparator.java XPathSource.java XPathTagSupport.java
Date Wed, 15 Jan 2003 06:42:49 GMT
morgand     2003/01/14 22:42:48

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/junit
                        AssertTagSupport.java JUnitTagLibrary.java
               jelly/src/java/org/apache/commons/jelly/tags/xml
                        CommentTag.java CopyOfTag.java CopyTag.java
                        DoctypeTag.java ExprTag.java ForEachTag.java
                        IfTag.java SetTag.java SortTag.java
                        XMLTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/expression/xpath
                        XPathExpression.java
               jelly/src/java/org/apache/commons/jelly/xpath
                        XPathComparator.java XPathSource.java
                        XPathTagSupport.java
  Removed:     jelly/src/java/org/apache/commons/jelly/tags/xml
                        XPathComparator.java XPathExpression.java
                        XPathSource.java XPathTagSupport.java
  Log:
  separated XPath support classes from XML taglib, since it is also utilized by
  junit tags and probably other taglibs in the future
  
  Revision  Changes    Path
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/xpath/XPathExpression.java
  
  Index: XPathExpression.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/expression/xpath/XPathExpression.java,v
1.1 2003/01/15 06:42:46 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/15 06:42:46 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: XPathExpression.java,v 1.1 2003/01/15 06:42:46 morgand Exp $
   */
  package org.apache.commons.jelly.expression.xpath;
  
  import java.util.Hashtable;
  import java.util.Iterator;
  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;
  import org.apache.commons.logging.LogFactory;
  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.
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public class XPathExpression extends ExpressionSupport implements VariableContext {
      
      /** The Log to which logging calls will be made. */
      private Log log = LogFactory.getLog(XPathExpression.class);
  
      private String text;
      private Expression xpathExpr;
      private JellyContext context;
      private Map uris;
      
      public XPathExpression() {
      }
  
      public XPathExpression(String text,
                             Expression xpathExpr,
                             TagScript tagScript) {
          this.text = text;
          this.xpathExpr = xpathExpr;
          
          Map namespaceContext = tagScript.getNamespaceContext();
          
          this.uris = createUriMap(namespaceContext);
      }
  
      public String toString() {
          return getExpressionText();
      }
              
      // Expression interface
      //------------------------------------------------------------------------- 
      public String getExpressionText() {
          return this.text;
      }
      
      public Object evaluate(JellyContext context) {
          this.context = context;
  
          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
      //------------------------------------------------------------------------- 
      public Object getVariableValue(
          String namespaceURI,
          String prefix,
          String localName) {
              
          Object value = context.getVariable(localName);
          
          //log.debug( "Looking up XPath variable of name: " + localName + " value is: " +
value );            
          
          return value;
      }
      
      // Implementation methods
      //-------------------------------------------------------------------------
      
      /**
       * Factory method to create a synchronized Map of non-null and non-blank
       * namespace prefixes to namespace URIs
       */ 
      protected Map createUriMap(Map namespaceContext) {
          // now lets clone the Map but ignoring default or null prefixes
          Map uris = new Hashtable(namespaceContext.size());
          for (Iterator iter = namespaceContext.entrySet().iterator(); iter.hasNext(); ) {
          	Map.Entry entry = (Map.Entry) iter.next();
          	String prefix = (String) entry.getKey();
          	if (prefix != null && prefix.length() != 0) {
          		uris.put(prefix, entry.getValue());
          	}
          }
          return uris;
      }
  }
  
  
  
  1.9       +1 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/AssertTagSupport.java
  
  Index: AssertTagSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/junit/AssertTagSupport.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- AssertTagSupport.java	11 Dec 2002 12:41:01 -0000	1.8
  +++ AssertTagSupport.java	15 Jan 2003 06:42:47 -0000	1.9
  @@ -61,7 +61,7 @@
    */
   package org.apache.commons.jelly.tags.junit;
   
  -import org.apache.commons.jelly.tags.xml.XPathTagSupport;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   
   /** 
    * The abstract base class of any assertion tag which is
  
  
  
  1.11      +1 -1      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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- JUnitTagLibrary.java	31 Dec 2002 03:12:23 -0000	1.10
  +++ JUnitTagLibrary.java	15 Jan 2003 06:42:47 -0000	1.11
  @@ -68,7 +68,7 @@
   import org.apache.commons.jelly.expression.Expression;
   import org.apache.commons.jelly.expression.ExpressionFactory;
   import org.apache.commons.jelly.impl.TagScript;
  -import org.apache.commons.jelly.tags.xml.XPathExpression;
  +import org.apache.commons.jelly.expression.xpath.XPathExpression;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  
  
  
  1.2       +1 -1      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/CommentTag.java
  
  Index: CommentTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/CommentTag.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- CommentTag.java	13 Dec 2002 18:08:31 -0000	1.1
  +++ CommentTag.java	15 Jan 2003 06:42:47 -0000	1.2
  @@ -62,7 +62,7 @@
   package org.apache.commons.jelly.tags.xml;
   
   import org.apache.commons.jelly.XMLOutput;
  -
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   
   /** 
    * A tag which outputs a comment to the underlying XMLOutput based on the
  
  
  
  1.4       +1 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/CopyOfTag.java
  
  Index: CopyOfTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/CopyOfTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CopyOfTag.java	11 Dec 2002 12:40:56 -0000	1.3
  +++ CopyOfTag.java	15 Jan 2003 06:42:47 -0000	1.4
  @@ -66,6 +66,7 @@
   
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   import org.dom4j.Node;
   import org.dom4j.io.SAXWriter;
   import org.jaxen.XPath;
  
  
  
  1.5       +1 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/CopyTag.java
  
  Index: CopyTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/CopyTag.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CopyTag.java	11 Dec 2002 12:40:56 -0000	1.4
  +++ CopyTag.java	15 Jan 2003 06:42:47 -0000	1.5
  @@ -62,6 +62,7 @@
   package org.apache.commons.jelly.tags.xml;
   
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   import org.dom4j.Element;
   import org.dom4j.io.SAXWriter;
   import org.jaxen.XPath;
  
  
  
  1.4       +1 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/DoctypeTag.java
  
  Index: DoctypeTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/DoctypeTag.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DoctypeTag.java	11 Dec 2002 12:40:56 -0000	1.3
  +++ DoctypeTag.java	15 Jan 2003 06:42:47 -0000	1.4
  @@ -63,6 +63,7 @@
   
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   
   
   /** 
  
  
  
  1.11      +6 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/ExprTag.java
  
  Index: ExprTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/ExprTag.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ExprTag.java	11 Dec 2002 12:40:56 -0000	1.10
  +++ ExprTag.java	15 Jan 2003 06:42:47 -0000	1.11
  @@ -63,6 +63,7 @@
   
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   import org.jaxen.XPath;
   
   /** A tag which performs a string XPath expression; similar to &lt;xsl:value-of&gt;
  
  
  
  1.13      +16 -5     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/ForEachTag.java
  
  Index: ForEachTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/ForEachTag.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- ForEachTag.java	27 Nov 2002 19:22:41 -0000	1.12
  +++ ForEachTag.java	15 Jan 2003 06:42:47 -0000	1.13
  @@ -66,6 +66,9 @@
   import java.util.Collections;
   
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathComparator;
  +import org.apache.commons.jelly.xpath.XPathSource;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   
   import org.jaxen.XPath;
   import org.jaxen.JaxenException;
  @@ -155,4 +158,12 @@
           if (xpCmp == null) xpCmp = new XPathComparator();
           xpCmp.setDescending(descending);
       }
  +    
  +    /*
  +     * Override superclass so method can be access by IfTag
  +     */
  +    protected Object getXPathContext() {
  +        return super.getXPathContext();
  +    }
  +    
   }
  
  
  
  1.5       +1 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/IfTag.java
  
  Index: IfTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/IfTag.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- IfTag.java	11 Dec 2002 12:40:56 -0000	1.4
  +++ IfTag.java	15 Jan 2003 06:42:47 -0000	1.5
  @@ -63,6 +63,7 @@
   
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   import org.jaxen.XPath;
   
   /** 
  
  
  
  1.12      +7 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/SetTag.java
  
  Index: SetTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/SetTag.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- SetTag.java	27 Nov 2002 19:22:41 -0000	1.11
  +++ SetTag.java	15 Jan 2003 06:42:47 -0000	1.12
  @@ -63,6 +63,8 @@
   
   import org.apache.commons.jelly.MissingAttributeException;
   import org.apache.commons.jelly.XMLOutput;
  +import org.apache.commons.jelly.xpath.XPathComparator;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
  
  
  
  1.3       +2 -0      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/SortTag.java
  
  Index: SortTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/xml/SortTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SortTag.java	27 Nov 2002 19:22:41 -0000	1.2
  +++ SortTag.java	15 Jan 2003 06:42:47 -0000	1.3
  @@ -64,6 +64,8 @@
   
   import org.apache.commons.jelly.XMLOutput;
   import org.apache.commons.jelly.MissingAttributeException;
  +import org.apache.commons.jelly.xpath.XPathComparator;
  +import org.apache.commons.jelly.xpath.XPathTagSupport;
   import org.jaxen.XPath;
   import org.jaxen.JaxenException;
   
  
  
  
  1.21      +6 -5      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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- XMLTagLibrary.java	31 Dec 2002 03:12:23 -0000	1.20
  +++ XMLTagLibrary.java	15 Jan 2003 06:42:47 -0000	1.21
  @@ -66,6 +66,7 @@
   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.expression.xpath.XPathExpression;
   import org.apache.commons.jelly.impl.TagScript;
   
   import org.apache.commons.logging.Log;
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath/XPathComparator.java
  
  Index: XPathComparator.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath/XPathComparator.java,v
1.1 2003/01/15 06:42:48 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/15 06:42:48 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * $Id: XPathComparator.java,v 1.1 2003/01/15 06:42:48 morgand Exp $
   */
  package org.apache.commons.jelly.xpath;
  
  import java.util.Comparator;
  import java.util.List;
  
  import org.apache.commons.jelly.util.NestedRuntimeException;
  import org.dom4j.Node;
  import org.jaxen.JaxenException;
  import org.jaxen.XPath;
  
  /**
   * Compares xml nodes by extracting the value at xpath and
   * comparing it.
   *
   * @author <a href="mailto:jason@jhorman.org">Jason Horman</a>
   * @version $Id: XPathComparator.java,v 1.1 2003/01/15 06:42:48 morgand Exp $
   */
  
  public class XPathComparator implements Comparator {
  
      /** The xpath to use to extract value from nodes to compare */
      private XPath xpath = null;
  
      /** Sort descending or ascending */
      private boolean descending = false;
  
      public XPathComparator() {
  
      }
  
      public XPathComparator(XPath xpath, boolean descending) {
          this.xpath = xpath;
          this.descending = descending;
      }
  
      public void setXpath(XPath xpath) {
          this.xpath = xpath;
      }
  
      public XPath getXpath() {
          return xpath;
      }
  
      public void setDescending(boolean descending) {
          this.descending = descending;
      }
  
      public int compare(Object o1, Object o2) {
          return compare((Node)o1, (Node)o2);
      }
  
      public int compare(Node n1, Node n2) {
          try {
  
              // apply the xpaths. not using stringValueOf since I don't
              // want all of the child nodes appended to the strings
              Object val1 = xpath.evaluate(n1);
              Object val2 = xpath.evaluate(n2);
  
              // return if null
              if (val1 == null || val2 == null) {
                  return val1 == null ? (val2 == null ? 1 : -1) : 1;
              }
  
              Comparable c1 = getComparableValue(val1);
              Comparable c2 = getComparableValue(val2);
  
              // compare descending or ascending
              if (!descending) {
                  return c1.compareTo(c2);
              } else {
                  return c2.compareTo(c1);
              }
  
          } catch (JaxenException e) {
  
              throw new XPathSortException("error sorting nodes", e);
  
          }
      }
  
      /**
       * Turns the XPath result value into a Comparable object.
       */
      protected Comparable getComparableValue(Object value) {
          if (value instanceof List) {
              List list = (List) value;
              if (list.isEmpty()) {
                  value = "";
              }
              value = list.get(0);
              if (value == null) {
                  value = "";
              }
          }
          if (value instanceof Comparable) {
              return (Comparable) value;
          }
          else if (value instanceof Node) {
              Node node = (Node) value;
              return node.getStringValue();
          }
          return value.toString();
      }
  
      /**
       * My own runtime exception in case something goes wrong with sort.
       */
      public static class XPathSortException extends NestedRuntimeException {
          public XPathSortException(String message, Throwable cause) {
              super(message, cause);
          }
      }
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath/XPathSource.java
  
  Index: XPathSource.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath/XPathSource.java,v
1.1 2003/01/15 06:42:48 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/15 06:42:48 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: XPathSource.java,v 1.1 2003/01/15 06:42:48 morgand Exp $
   */
  package org.apache.commons.jelly.xpath;
  
  
  /** 
   * A tag which is capable of producing a source of XPath context objects
   * such as &lt;x:forEach&gt;
   *
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.1 $
   */
  public interface XPathSource {
  
      public Object getXPathSource();
      
  }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath/XPathTagSupport.java
  
  Index: XPathTagSupport.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/xpath/XPathTagSupport.java,v
1.1 2003/01/15 06:42:48 morgand Exp $
   * $Revision: 1.1 $
   * $Date: 2003/01/15 06:42:48 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   * 
   * $Id: XPathTagSupport.java,v 1.1 2003/01/15 06:42:48 morgand Exp $
   */
  package org.apache.commons.jelly.xpath;
  
  import org.apache.commons.jelly.TagSupport;
  
  /** An abstract base class useful for implementation inheritence
    *
    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
    * @version $Revision: 1.1 $
    */
  public abstract class XPathTagSupport extends TagSupport {
  
      // Implementation methods
      //-------------------------------------------------------------------------        
       
      protected Object getXPathContext() {
          XPathSource tag = (XPathSource) findAncestorWithClass( XPathSource.class );    
          if ( tag != null ) {
              return tag.getXPathSource();
          }
          return null;
      }    
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message