Coverage report

  %line %branch
org.apache.commons.jelly.tags.xml.ForEachTag
97% 
100% 

 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 java.util.Iterator;
 19  
 import java.util.List;
 20  
 import java.util.Collections;
 21  
 
 22  
 import org.apache.commons.jelly.JellyTagException;
 23  
 import org.apache.commons.jelly.XMLOutput;
 24  
 import org.apache.commons.jelly.xpath.XPathComparator;
 25  
 import org.apache.commons.jelly.xpath.XPathSource;
 26  
 import org.apache.commons.jelly.xpath.XPathTagSupport;
 27  
 
 28  
 import org.jaxen.XPath;
 29  
 import org.jaxen.JaxenException;
 30  
 
 31  
 /** A tag which performs an iteration over the results of an XPath expression
 32  
   *
 33  
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
 34  
   * @version $Revision: 1.5 $
 35  
   */
 36  
 public class ForEachTag extends XPathTagSupport implements XPathSource {
 37  
 
 38  
     /** Holds the XPath selector. */
 39  
     private XPath select;
 40  
 
 41  
     /** Xpath comparator for sorting */
 42  7
     private XPathComparator xpCmp = null;
 43  
 
 44  
     /** If specified then the current item iterated through will be defined
 45  
       * as the given variable name. */
 46  
     private String var;
 47  
 
 48  
     /** The current iteration value */
 49  
     private Object iterationValue;
 50  
 
 51  
 
 52  7
     public ForEachTag() {
 53  7
     }
 54  
 
 55  
     // Tag interface
 56  
     //-------------------------------------------------------------------------
 57  
     public void doTag(XMLOutput output) throws JellyTagException {
 58  7
         if (select != null) {
 59  7
             List nodes = null;
 60  
             try {
 61  7
                 nodes = select.selectNodes( getXPathContext() );
 62  7
             }
 63  
             catch (JaxenException e) {
 64  0
                 throw new JellyTagException(e);
 65  
             }
 66  
 
 67  
             // sort the list if xpCmp is set.
 68  7
             if (xpCmp != null && (xpCmp.getXpath() != class="keyword">null)) {
 69  4
                 Collections.sort(nodes, xpCmp);
 70  
             }
 71  
 
 72  7
             Iterator iter = nodes.iterator();
 73  43
             while (iter.hasNext()) {
 74  29
                 iterationValue = iter.next();
 75  29
                 if (var != null) {
 76  25
                     context.setVariable(var, iterationValue);
 77  
                 }
 78  29
                 invokeBody(output);
 79  
             }
 80  
         }
 81  7
     }
 82  
 
 83  
     // XPathSource interface
 84  
     //-------------------------------------------------------------------------
 85  
 
 86  
     /**
 87  
      * @return the current XPath iteration value
 88  
      *  so that any other XPath aware child tags to use
 89  
      */
 90  
     public Object getXPathSource() {
 91  25
         return iterationValue;
 92  
     }
 93  
 
 94  
     // Properties
 95  
     //-------------------------------------------------------------------------
 96  
     /** Sets the XPath selection expression
 97  
       */
 98  
     public void setSelect(XPath select) {
 99  7
         this.select = select;
 100  7
     }
 101  
 
 102  
     /** Sets the variable name to export for the item being iterated over
 103  
      */
 104  
     public void setVar(String var) {
 105  5
         this.var = class="keyword">var;
 106  5
     }
 107  
 
 108  
     /** Sets the xpath expression to use to sort selected nodes.
 109  
      */
 110  
     public void setSort(XPath sortXPath) throws JaxenException {
 111  4
         if (xpCmp == null) xpCmp = new XPathComparator();
 112  4
         xpCmp.setXpath(sortXPath);
 113  4
     }
 114  
 
 115  
     /**
 116  
      * Set whether to sort ascending or descending.
 117  
      */
 118  
     public void setDescending(boolean descending) {
 119  1
         if (xpCmp == null) xpCmp = new XPathComparator();
 120  1
         xpCmp.setDescending(descending);
 121  1
     }
 122  
 
 123  
     /*
 124  
      * Override superclass so method can be access by IfTag
 125  
      */
 126  
     protected Object getXPathContext() {
 127  7
         return super.getXPathContext();
 128  
     }
 129  
 
 130  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.