You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by po...@apache.org on 2004/09/09 00:26:33 UTC

cvs commit: jakarta-commons/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml suite.jelly

polx        2004/09/08 15:26:33

  Modified:    jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml
                        SetTag.java
               jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml
                        suite.jelly
  Log:
  Added support and test for single="true" (guarantees null or node)
  as well as single="false" (guarantees a list, be it empty).
  paul
  
  Revision  Changes    Path
  1.8       +30 -13    jakarta-commons/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/SetTag.java
  
  Index: SetTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/xml/src/java/org/apache/commons/jelly/tags/xml/SetTag.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SetTag.java	8 Sep 2004 04:52:22 -0000	1.7
  +++ SetTag.java	8 Sep 2004 22:26:33 -0000	1.8
  @@ -53,7 +53,7 @@
       /** Xpath comparator for sorting */
       private XPathComparator xpCmp = null;
   
  -    private boolean single=false;
  +    private Boolean single=null;
   
       public SetTag() {
   
  @@ -72,7 +72,11 @@
           Object xpathContext = getXPathContext();
           Object value = null;
           try {
  -            value = select.evaluate(xpathContext);
  +			if(single!=null && single.booleanValue()==true) {
  +				value = select.selectSingleNode(xpathContext);
  +			} else {
  +				value = select.evaluate(xpathContext);
  +			}
           }
           catch (JaxenException e) {
               throw new JellyTagException(e);
  @@ -84,17 +88,28 @@
                   Collections.sort((List)value, xpCmp);
               }
           }
  -        if (single==true) {
  -            if(value instanceof List) {
  -                List l = (List) value;
  -                if (l.size()==0)
  -                    value=null;
  -                else
  -                    value=l.get(0);
  -            }
  +        if (single!=null) {
  +			if (single.booleanValue()==true) {
  +				if(value instanceof List) {
  +					List l = (List) value;
  +					if (l.size()==0)
  +						value=null;
  +					else
  +						value=l.get(0);
  +				}
  +			} else { // single == false
  +				if(! (value instanceof List) ) {
  +					if (value==null) {
  +						l = new java.util.ArrayList(0)
  +					} else {
  +						List l = new java.util.ArrayList(1);
  +						l.add(value);
  +					}
  +					value = l;
  +				}
  +			}
           }
   
  -
           //log.info( "Evaluated xpath: " + select + " as: " + value + " of type: " + value.getClass().getName() );
   
           context.setVariable(var, value);
  @@ -115,17 +130,19 @@
       }
   
       /** If set to true will only take the first element matching.
  +		If set to false, guarantees that a list is returned.
           It then guarantees that the result is of type
           {@link org.dom4j.Node} thereby making sure that, for example,
           when an element is selected, one can directly call such methods
           as setAttribute.
           */
       public void setSingle(boolean single) {
  -        this.single = single;
  +        this.single = new Boolean(single);
       }
   
   
       /** Sets the xpath expression to use to sort selected nodes.
  +	 *  Ignored if single is true.
        */
       public void setSort(XPath sortXPath) throws JaxenException {
           if (xpCmp == null) xpCmp = new XPathComparator();
  
  
  
  1.12      +7 -2      jakarta-commons/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/xml/src/test/org/apache/commons/jelly/tags/xml/suite.jelly,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- suite.jelly	27 Aug 2004 04:37:45 -0000	1.11
  +++ suite.jelly	8 Sep 2004 22:26:33 -0000	1.12
  @@ -287,14 +287,19 @@
           <x:parse var="blopElements">
               <root>
               <blop>blop1</blop>
  +						<blip/>
               <blop>blop0</blop></root>
           </x:parse>
  -        <x:set var="blopSingle" select="$blopElements/root/blop" sort="text()" single="true"/>
  +        <x:set var="blopSingle" select="$blopElements/root/blop" single="true"/>
           <!-- should return the second -->
           <j:invokeStatic var="eltClass" className="java.lang.Class" method="forName"><j:arg value="org.dom4j.Element"/></j:invokeStatic>
           <test:assert test="${eltClass.isAssignableFrom(blopSingle.getClass())}"/>
           <j:set var="blopSingleText"><x:expr select="$blopSingle/text()"/></j:set>
  -        <test:assertEquals actual="${blopSingleText}" expected="blop0"/>
  +        <test:assertEquals actual="${blopSingleText}" expected="blop1"/>
  +				<!-- check if selecting root/blip returns a list -->
  +				<x:set var="blip" select="$blopElements/root/blip" single="false"/>
  +        <j:invokeStatic var="listClass" className="java.lang.Class" method="forName"><j:arg value="java.util.List"/></j:invokeStatic>
  +				<test:assert test="${listClass.isAssignableFrom(blip.getClass())}"/>
     </test:case>
      
     <test:case name="testEntities">
  
  
  

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