dion 2003/01/06 08:15:49 Added: jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util PropertiesTag.java UtilTagLibrary.java TokenizeTag.java AvailableTag.java package.html ReplaceTag.java LoadTextTag.java SleepTag.java jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util foo.properties dummy.xml suite.jelly TestJelly.java Log: Move util out into it's own tag Revision Changes Path 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/PropertiesTag.java Index: PropertiesTag.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 [EMAIL PROTECTED] * * 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.io.FileInputStream; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; import org.apache.commons.jelly.JellyException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; /** * A tag which loads a properties file from a given file name or URI * which are loaded into the current context. * * @author Jim Birchfield * @version $Revision: 1.1 $ */ public class PropertiesTag extends TagSupport { private String file; private String uri; private String var; public PropertiesTag() { } // Tag interface //------------------------------------------------------------------------- public void doTag(final XMLOutput output) throws Exception { if (file == null && uri == null) { throw new JellyException("This tag must define a 'file' or 'uri' attribute"); } InputStream is = null; if (file != null) { File f = new File(file); if (!f.exists()) { throw new JellyException("file: " + file + " does not exist!"); } is = new FileInputStream(f); } else { is = context.getResourceAsStream(uri); if (is == null) { throw new JellyException( "Could not find: " + uri ); } } Properties props = new Properties(); props.load(is); if (var != null) { context.setVariable(var, props); } else { Enumeration enum = props.propertyNames(); while (enum.hasMoreElements()) { String key = (String) enum.nextElement(); String value = props.getProperty(key); // @todo we should parse the value in case its an Expression context.setVariable(key, value); } } } // Properties //------------------------------------------------------------------------- /** * Sets the file name to be used to load the properties file. */ public void setFile(String file) { this.file = file; } /** * Sets the URI of the properties file to use. This can be a full URL or a relative URI * or an absolute URI to the root context of this JellyContext. */ public void setUri(String uri) { this.uri = uri; } /** * If this is defined then a Properties object containing all the * properties will be created and exported, otherwise the current variable * scope will be set to the value of the properties. * * @param var The var to set */ public void setVar(String var) { this.var = var; } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java Index: UtilTagLibrary.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/UtilTagLibrary.java,v 1.1 2003/01/06 16:15:46 dion Exp $ * $Revision: 1.1 $ * $Date: 2003/01/06 16:15: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 [EMAIL PROTECTED] * * 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: UtilTagLibrary.java,v 1.1 2003/01/06 16:15:46 dion Exp $ */ package org.apache.commons.jelly.tags.util; import org.apache.commons.jelly.TagLibrary; /** Implements general utility tags. * * @author <a href="mailto:[EMAIL PROTECTED]">bob mcwhirter</a> * @version $Revision: 1.1 $ */ public class UtilTagLibrary extends TagLibrary { public UtilTagLibrary() { registerTag("available", AvailableTag.class); registerTag("loadText", LoadTextTag.class); registerTag("properties", PropertiesTag.class); registerTag("replace", ReplaceTag.class); registerTag("tokenize", TokenizeTag.class); registerTag("sleep", SleepTag.class); } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/TokenizeTag.java Index: TokenizeTag.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 [EMAIL PROTECTED] * * 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.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import org.apache.commons.jelly.MissingAttributeException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; public class TokenizeTag extends TagSupport { private String var; private String delim; public TokenizeTag() { } // Tag interface //------------------------------------------------------------------------- public void doTag(final XMLOutput output) throws Exception { if ( this.var == null ) { throw new MissingAttributeException( "var" ); } if ( this.delim == null ) { throw new MissingAttributeException( "delim" ); } StringTokenizer tokenizer = new StringTokenizer( getBodyText(), this.delim ); List tokens = new ArrayList(); while ( tokenizer.hasMoreTokens() ) { tokens.add( tokenizer.nextToken() ); } getContext().setVariable( this.var, tokens ); } /** The variable name to hold the list of tokens */ public void setVar(String var) { this.var = var; } /** the delimiter that separates the tokens */ public void setDelim(String delim) { this.delim = delim; } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/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 [EMAIL PROTECTED] * * 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.io.IOException; import java.io.InputStream; import java.net.URL; 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:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ 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(); try { InputStream is = url.openStream(); available = (is != null); is.close(); } catch (IOException ioe) { available = false; } } 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.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/package.html Index: package.html =================================================================== <html> <head> </head> <body> <p>A number of utility tags such as for tokenizing Strings. </p> </body> </html> 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/ReplaceTag.java Index: ReplaceTag.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 [EMAIL PROTECTED] * * 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 org.apache.commons.jelly.expression.Expression; import org.apache.commons.jelly.MissingAttributeException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A tag that replaces occurrences of strings in its body (or value) * and places the result into the context * * @author dion */ public class ReplaceTag extends TagSupport { /** The Log to which logging calls will be made. */ private static final Log log = LogFactory.getLog(ReplaceTag.class); /** The variable name to export. */ private String var; /** The expression to evaluate. */ private Expression value; /** the old character to be replaced */ private String oldChar; /** the new character that will replace the old */ private String newChar; // Tag interface //------------------------------------------------------------------------- public void doTag(XMLOutput output) throws Exception { // check required properties if (oldChar == null) { throw new MissingAttributeException("oldChar must be provided"); } if (newChar == null) { throw new MissingAttributeException("newChar must be provided"); } // get either the value or the body of the tag Object answer = null; if ( value != null ) { answer = value.evaluateAsString(context); } else { answer = getBodyText(false); } // set the result in the context, or output it if (answer != null) { String stringAnswer = answer.toString().replace(oldChar.charAt(0), newChar.charAt(0)); if ( var != null ) { context.setVariable(var, stringAnswer); } else { output.write(stringAnswer); } } } /** * Returns the newChar used in replacing. Should only be a single * character. * @return String */ public String getNewChar() { return newChar; } /** * Returns the oldChar that will be replaced. Should only be a single * character. * @return String */ public String getOldChar() { return oldChar; } /** * Returns the value. * @return Expression */ public Expression getValue() { return value; } /** * Returns the var. * @return String */ public String getVar() { return var; } /** * Sets the newChar. * @param newChar The newChar to set */ public void setNewChar(String newChar) { this.newChar = newChar; } /** * Sets the oldChar. * @param oldChar The oldChar to set */ public void setOldChar(String oldChar) { this.oldChar = oldChar; } /** * Sets the value. * @param value The value to set */ public void setValue(Expression value) { this.value = value; } /** * Sets the var. * @param var The var to set */ public void setVar(String var) { this.var = var; } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/LoadTextTag.java Index: LoadTextTag.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/LoadTextTag.java,v 1.1 2003/01/06 16:15:46 dion Exp $ * $Revision: 1.1 $ * $Date: 2003/01/06 16:15: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 [EMAIL PROTECTED] * * 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: LoadTextTag.java,v 1.1 2003/01/06 16:15:46 dion Exp $ */ package org.apache.commons.jelly.tags.util; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import org.apache.commons.jelly.JellyException; import org.apache.commons.jelly.MissingAttributeException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A tag which loads text from a file or URI into a Jelly variable. * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ public class LoadTextTag extends TagSupport { /** The Log to which logging calls will be made. */ private static final Log log = LogFactory.getLog(LoadTextTag.class); private String var; private File file; private String uri; public LoadTextTag() { } // Tag interface //------------------------------------------------------------------------- public void doTag(XMLOutput output) throws Exception { if (var == null) { throw new MissingAttributeException("var"); } if (file == null && uri == null) { throw new JellyException( "This tag must have a 'file' or 'uri' specified" ); } Reader reader = null; if (file != null) { if (! file.exists()) { throw new JellyException( "The file: " + file + " does not exist" ); } reader = new FileReader(file); } else { InputStream in = context.getResourceAsStream(uri); if (in == null) { throw new JellyException( "Could not find uri: " + uri ); } // @todo should we allow an encoding to be specified? reader = new InputStreamReader(in); } String text = loadText(reader); context.setVariable(var, text); } // Properties //------------------------------------------------------------------------- /** * Sets the name of the variable which will be exported with the text value of the * given file. */ public void setVar(String var) { this.var = var; } /** * Returns the file. * @return File */ public File getFile() { return file; } /** * Returns the uri. * @return String */ public String getUri() { return uri; } /** * Returns the var. * @return String */ public String getVar() { return var; } /** * Sets the file to be parsed as text */ public void setFile(File file) { this.file = file; } /** * Sets the uri to be parsed as text. * This can be an absolute URL or a relative or absolute URI * from this Jelly script or the root context. */ public void setUri(String uri) { this.uri = uri; } // Implementation methods //------------------------------------------------------------------------- /** * Loads all the text from the given Reader */ protected String loadText(Reader reader) throws Exception { StringBuffer buffer = new StringBuffer(); // @todo its probably more efficient to use a fixed char[] buffer instead try { BufferedReader bufferedReader = new BufferedReader(reader); while (true) { String line = bufferedReader.readLine(); if (line == null) { break; } else { buffer.append(line); buffer.append('\n'); } } return buffer.toString(); } finally { try { reader.close(); } catch (Exception e) { log.error( "Caught exception closing Reader: " + e, e); } } } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/SleepTag.java Index: SleepTag.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 [EMAIL PROTECTED] * * 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 org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; /** * A tag which sleeps for a given amount of time. * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ public class SleepTag extends TagSupport { private long millis; public SleepTag() { } // Tag interface //------------------------------------------------------------------------- public void doTag(final XMLOutput output) throws Exception { if (millis > 0) { Thread.sleep(millis); } } // Properties //------------------------------------------------------------------------- /** * Sets the amount of time that this thread should sleep for in milliseconds. */ public void setMillis(long millis) { this.millis = millis; } } 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/foo.properties Index: foo.properties =================================================================== foo=ABC bar=XYZ 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/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.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/suite.jelly Index: suite.jelly =================================================================== <?xml version="1.0"?> <test:suite xmlns:j="jelly:core" xmlns:util="jelly:util" xmlns:test="jelly:junit" xmlns:log="jelly:log"> <test:case name="testTokenize"> <util:tokenize var="tokens" delim=",">Test1,Test2,Test3,Test4</util:tokenize> <j:set var="s" value="${size(tokens)}"/> <test:assert test="${s == 4}"/> <test:assert test="${size(tokens) == 4}"/> <!-- <j:set var="s" value="${tokens.size()}"/> <test:assert test="${s == 4}"/> <test:assert test="${tokens.size() == 4}"/> --> <test:assertEquals expected="Test1" actual="${tokens[0]}"/> <test:assertEquals expected="Test2" actual="${tokens[1]}"/> <test:assertEquals expected="Test3" actual="${tokens[2]}"/> <test:assertEquals expected="Test4" actual="${tokens[3]}"/> <test:assertEquals expected="Test4" actual="${tokens[s - 1]}"/> <test:assertEquals expected="Test4" actual="${tokens[size(tokens) - 1]}"/> <!-- <test:assertEquals expected="Test4" actual="${tokens.get(size(tokens) - 1)}"/> <test:assertEquals expected="Test4" actual="${tokens[tokens.size() - 1]}"/> <test:assertEquals expected="Test4" actual="${tokens.get(tokens.size() - 1)}"/> --> </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:case name="testReplace"> <util:replace oldChar="\" newChar="/" var="testString">A\B</util:replace> <test:assertEquals expected="A/B" actual="${testString}"> Should have replaced a back slash with a forward one </test:assertEquals> <j:set var="testString2"><util:replace oldChar="\" newChar="/">A\B</util:replace></j:set> <test:assertEquals expected="A/B" actual="${testString2}"> Should have replaced a back slash with a forward one and placed the result into output </test:assertEquals> <util:replace oldChar="/" newChar="\" value="${testString}" var="testString3" /> <test:assertEquals expected="A\B" actual="${testString3}"> Should have replaced a slash with a back slash from a variable and placed the result into a variable </test:assertEquals> </test:case> <test:case name="testProperties"> <util:properties uri="foo.properties"/> <test:assertEquals expected="ABC" actual="${foo}"/> </test:case> <test:case name="testPropertiesVar"> <util:properties uri="foo.properties" var="props"/> <test:assertEquals expected="ABC" actual="${props.foo}"/> <log:info>Loaded properties value ${props}</log:info> </test:case> </test:suite> 1.1 jakarta-commons-sandbox/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/TestJelly.java Index: TestJelly.java =================================================================== /* * $Header: /home/cvs/jakarta-commons-sandbox/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/TestJelly.java,v 1.1 2003/01/06 16:15:48 dion Exp $ * $Revision: 1.1 $ * $Date: 2003/01/06 16:15: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 [EMAIL PROTECTED] * * 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: TestJelly.java,v 1.1 2003/01/06 16:15:48 dion Exp $ */ package org.apache.commons.jelly.tags.util; import junit.framework.TestSuite; import junit.textui.TestRunner; import org.apache.commons.jelly.tags.junit.JellyTestSuite; /** * A helper class to run jelly test cases as part of Ant's JUnit tests * * @author <a href="mailto:[EMAIL PROTECTED]">James Strachan</a> * @version $Revision: 1.1 $ */ public class TestJelly extends JellyTestSuite { public static void main( String[] args ) throws Exception { TestRunner.run( suite() ); } public static TestSuite suite() throws Exception { return createTestSuite(TestJelly.class, "suite.jelly"); } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>