| %line | %branch | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| org.apache.commons.jelly.tags.xml.ForEachTag |
|
|
| 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: 155420 $ |
|
| 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 | 40 | 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 | 40 | public ForEachTag() { |
| 53 | 40 | } |
| 54 | ||
| 55 | // Tag interface |
|
| 56 | //------------------------------------------------------------------------- |
|
| 57 | public void doTag(XMLOutput output) throws JellyTagException { |
|
| 58 | 35 | if (select != null) { |
| 59 | 35 | List nodes = null; |
| 60 | try { |
|
| 61 | 35 | nodes = select.selectNodes( getXPathContext() ); |
| 62 | } |
|
| 63 | 0 | catch (JaxenException e) { |
| 64 | 0 | throw new JellyTagException(e); |
| 65 | 35 | } |
| 66 | ||
| 67 | // sort the list if xpCmp is set. |
|
| 68 | 35 | if (xpCmp != null && (xpCmp.getXpath() != class="keyword">null)) { |
| 69 | 20 | Collections.sort(nodes, xpCmp); |
| 70 | } |
|
| 71 | ||
| 72 | 35 | Iterator iter = nodes.iterator(); |
| 73 | 180 | while (iter.hasNext()) { |
| 74 | 145 | iterationValue = iter.next(); |
| 75 | 145 | if (var != null) { |
| 76 | 125 | context.setVariable(var, iterationValue); |
| 77 | } |
|
| 78 | 145 | invokeBody(output); |
| 79 | } |
|
| 80 | } |
|
| 81 | 35 | } |
| 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 | 125 | return iterationValue; |
| 92 | } |
|
| 93 | ||
| 94 | // Properties |
|
| 95 | //------------------------------------------------------------------------- |
|
| 96 | /** Sets the XPath selection expression |
|
| 97 | */ |
|
| 98 | public void setSelect(XPath select) { |
|
| 99 | 35 | this.select = select; |
| 100 | 35 | } |
| 101 | ||
| 102 | /** Sets the variable name to export for the item being iterated over |
|
| 103 | */ |
|
| 104 | public void setVar(String var) { |
|
| 105 | 25 | this.var = class="keyword">var; |
| 106 | 25 | } |
| 107 | ||
| 108 | /** Sets the xpath expression to use to sort selected nodes. |
|
| 109 | */ |
|
| 110 | public void setSort(XPath sortXPath) throws JaxenException { |
|
| 111 | 20 | if (xpCmp == null) xpCmp = new XPathComparator(); |
| 112 | 20 | xpCmp.setXpath(sortXPath); |
| 113 | 20 | } |
| 114 | ||
| 115 | /** |
|
| 116 | * Set whether to sort ascending or descending. |
|
| 117 | */ |
|
| 118 | public void setDescending(boolean descending) { |
|
| 119 | 5 | if (xpCmp == null) xpCmp = new XPathComparator(); |
| 120 | 5 | xpCmp.setDescending(descending); |
| 121 | 5 | } |
| 122 | ||
| 123 | /* |
|
| 124 | * Override superclass so method can be access by IfTag |
|
| 125 | */ |
|
| 126 | protected Object getXPathContext() { |
|
| 127 | 35 | return super.getXPathContext(); |
| 128 | } |
|
| 129 | ||
| 130 | } |
| This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |