commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly project.xml
Date Wed, 13 Nov 2002 12:59:35 GMT
jstrachan    2002/11/13 04:59:35

  Modified:    jelly/src/java/org/apache/commons/jelly/tags/util
                        UtilTagLibrary.java
               jelly/src/test/org/apache/commons/jelly/util suite.jelly
               jelly    project.xml
  Added:       jelly/src/java/org/apache/commons/jelly/tags/util
                        AvailableTag.java
               jelly/src/test/org/apache/commons/jelly/util dummy.xml
  Log:
  Added a new helper tag, <util:available> which can be used to test for files or URIs
being available and conditionally executing its body.
  
  so its like a simplified version of the <available> Ant task without requiring the
use of a seperate property to define whether the file is available or not.
  
  Revision  Changes    Path
  1.6       +6 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java
  
  Index: UtilTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- UtilTagLibrary.java	30 Oct 2002 19:16:20 -0000	1.5
  +++ UtilTagLibrary.java	13 Nov 2002 12:59:35 -0000	1.6
  @@ -71,6 +71,7 @@
   public class UtilTagLibrary extends TagLibrary {
       
       public UtilTagLibrary() {
  +        registerTag("available", AvailableTag.class);
           registerTag("loadText", LoadTextTag.class);
           registerTag("properties", PropertiesTag.class);
           registerTag("tokenize", TokenizeTag.class);
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/AvailableTag.java
  
  Index: AvailableTag.java
  ===================================================================
  /*
   *
   * ====================================================================
   *
   * 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", "Tomcat", 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/>.
   *
   */
  
  package org.apache.commons.jelly.tags.util;
  
  import java.io.File;
  import java.net.URL;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /**
   * A tag which evaluates its body if the given file is available.
   * The file can be specified via a File object or via a relative or absolute
   * URI from the current Jelly script.
   * 
   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
   * @version $Revision: 1.6 $
   */
  public class AvailableTag extends TagSupport {
  	
  	private File file;
  	private String uri;
  
      public AvailableTag() {
      }
  
      // Tag interface
      //------------------------------------------------------------------------- 
      public void doTag(final XMLOutput output) throws Exception {
      	boolean available = false;
      	
      	if (file != null) {
      		available = file.exists();
      	}
      	else if (uri != null) {
      		URL url = context.getResource(uri);
      		String fileName = url.getFile();
      		File file = new File(fileName);
      		available = file.exists();
      	}
      	
      	if (available) {
      		invokeBody(output);
      	}
      }
  
      // Properties
      //------------------------------------------------------------------------- 
  
  
  	/**
  	 * Returns the file.
  	 * @return File
  	 */
  	public File getFile() {
  		return file;
  	}
  
  	/**
  	 * Returns the uri.
  	 * @return String
  	 */
  	public String getUri() {
  		return uri;
  	}
  
  	/**
  	 * Sets the file to use to test whether it exists or not.
  	 * @param file the file to test for
  	 */
  	public void setFile(File file) {
  		this.file = file;
  	}
  
  	/**
  	 * Sets the URI to use to test for availability. 
  	 * The URI can be a full file based URL or a relative URI 
  	 * or an absolute URI from the root context.
  	 * 
  	 * @param uri the URI of the file to test
  	 */
  	public void setUri(String uri) {
  		this.uri = uri;
  	}
  
  }
  
  
  
  1.3       +44 -0     jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/util/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/util/suite.jelly,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- suite.jelly	10 Oct 2002 11:11:23 -0000	1.2
  +++ suite.jelly	13 Nov 2002 12:59:35 -0000	1.3
  @@ -37,4 +37,48 @@
   -->		
   		
     </test:case>
  +
  +  <test:case name="testFileExists">
  +
  +		<!-- ensure that ${base.dir} is defined -->
  +		<j:if test="${empty base.dir}">
  +			<j:set var="base.dir" value="."/>
  +		</j:if>
  +		
  +  	<j:set var="flag" value="not found"/>
  +
  +  	<util:available file="${base.dir}/project.xml">
  +  		<j:set var="flag" value="found"/>
  +		</util:available>
  +		
  +		<test:assertEquals expected="found" actual="${flag}">
  +			Should have found the file via the file $${base.dir}/project.xml with base.dir=${base.dir}
  +		</test:assertEquals>
  +		
  +  	<util:available file="${base.dir}/doesNotExist.xml">
  +  		<test:fail>The file ${base.dir}/doesNotExist.xml should not exist</test:fail>
  +		</util:available>
  +		
  +  </test:case>
  +	
  +  <test:case name="testURIExists">
  +
  +  	<j:set var="flag" value="not found"/>
  +
  +		<!-- use relative URIs-->
  +		
  +  	<util:available uri="dummy.xml">
  +  		<j:set var="flag" value="found"/>
  +		</util:available>
  +		
  +		<test:assertEquals expected="found" actual="${flag}">
  +			Should have found the file via the URI dummy.xml
  +		</test:assertEquals>
  +		
  +  	<util:available uri="doesNotExist.xml">
  +  		<test:fail>The URI doesNotExist.xml should not exist!</test:fail>
  +		</util:available>
  +		
  +  </test:case>
  +	
   </test:suite>
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/util/dummy.xml
  
  Index: dummy.xml
  ===================================================================
  <?xml version="1.0"?>
  <dummy>
    <entry id="1">This is some sample XML</entry>
    <entry id="2">And some more XML</entry>
    <empty/>
  </dummy>  
  
  
  1.89      +1 -1      jakarta-commons-sandbox/jelly/project.xml
  
  Index: project.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/project.xml,v
  retrieving revision 1.88
  retrieving revision 1.89
  diff -u -r1.88 -r1.89
  --- project.xml	12 Nov 2002 08:39:50 -0000	1.88
  +++ project.xml	13 Nov 2002 12:59:35 -0000	1.89
  @@ -260,7 +260,7 @@
   
       <dependency>
         <id>dom4j</id>
  -      <version>1.4-dev-7</version>
  +      <version>1.4-dev-8</version>
       </dependency>
   
   	<!-- for the jelly startup scripts -->
  
  
  

--
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