You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ca...@apache.org on 2001/05/21 22:57:33 UTC

cvs commit: jakarta-taglibs/random/src/org/apache/taglibs/random RandomStrgTag.java

catlett     01/05/21 13:57:33

  Added:       random/src/org/apache/taglibs/random RandomStrgTag.java
  Log:
  first import of random taglib
  
  Revision  Changes    Path
  1.1                  jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrgTag.java
  
  Index: RandomStrgTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrgTag.java,v 1.1 2001/05/21 20:57:32 catlett Exp $
   * $Revision: 1.1 $
   * $Date: 2001/05/21 20:57:32 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 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.taglibs.random;
  
  import java.util.*;
  import javax.servlet.jsp.*;
  import javax.servlet.jsp.tagext.*;
  import org.apache.taglibs.random.RandomStrg;
  
  /**
   * RandomStrg tag will create a random string generator accessiable by the 
   * <b>jsp:getProperty</b> tag..
   *
   * &lt;tag&gt;
   *	&lt;name&gt;random&lt;/name&gt;
   *	&lt;tagclass&gt;org.apache.taglibs.random.RandomTag&lt;/tagclass&gt;
   *	&lt;bodycontent&gt;empty&lt;/bodycontent&gt;
   *	&lt;info&gt;Creates an variable length random string generator&lt;/info&gt;
   *
   *	&lt;attribute&gt;
   *		&lt;name&gt;id&lt;/name&gt;
   *		&lt;required&gt;true&lt;/required&gt;
   *		&lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
   *	&lt;/attribute&gt;
   *	&lt;attribute&gt;
   *		&lt;name&gt;length&lt;/name&gt;
   *		&lt;required&gt;false&lt;/required&gt;
   *		&lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
   *	&lt;/attribute&gt;
   *	&lt;attribute&gt;
   *		&lt;name&gt;map&lt;/name&gt;
   *		&lt;required&gt;false&lt;/required&gt;
   *		&lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
   *	&lt;/attribute&gt;
   *	&lt;attribute&gt;
   *		&lt;name&gt;charset&lt;/name&gt;
   *		&lt;required&gt;false&lt;/required&gt;
   *		&lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
   *	&lt;/attribute&gt;
   * &lt;/tag&gt;
   *
   * @author Rich Catlett
   *
   * @version 1.0
   *
   */
  
  public class RandomStrgTag extends TagSupport {
  
      /**
       * generated password
       */
      private String randomstr = null;
      /**
       * flag determines if all chars ar to be used
       */
      private boolean allchars = false;
      /**
       * length of random string defaults to 8
       */
      private Integer length = new Integer(8);
      /**
       * list of all generated strings, list is stored at the application level
       */
      private HashMap hmap;
      /**
       * Hashmap to check keys against to see if they have already been used
       */
      private String map = null;
      /**
       * ArrayList for the lowerbound of the char sets
       */
      private ArrayList lower = null;
      /**
       * ArrayList for the upperbound of the char sets
       */
      private ArrayList upper = null;
      /**
       * ArrayL for the set of chars that aren't part of a range
       */
      private char[] single = null;
      /**
       * counter for position in the array single
       */
      private int singlecount = 0;
  
      /**
       * implementation of method from the Tag interface that tells the JSP what
       * to do upon encountering the start tag for this tag set
       *
       * @return SKIP_BODY - integer value telling the JSP engine to not evaluate
       *                      the body of this tag
       *
       * @throw JspException  thrown when error occurs in processing the body of
       *                       this method
       *
       */
      public final int doStartTag() throws JspException {
  
  	// create the class that will become the script variable available through
  	// the jsp:getProperty tag
  	RandomStrg random = new RandomStrg();
  
  	// set the default for the charset to a-ZA-Z0-9
  	if ((lower == null) && (single == null))
  	    setCharset("a-zA-Z0-9");
  
  	if (map != null) {
  	    try {
  
  		// check if the list is to be used if so get list
  		if ((hmap = (HashMap) pageContext.findAttribute(map)) == null)
  		    // named hashmap does not exist throw error out to the author
  		    throw new JspException("A hashmap does not exist in any " +
  					   "scope under the name " + map);
  	    } catch (ClassCastException cce) {
  		throw new JspException("The named attribute exists but it is not" +
  				       " a hashmap.");
  	    }
  
  	    random.setHmap(hmap);  // set the Hashmap in the script variable
  	}
  
  	// if ranges exist set them in the script variable
  	if (lower != null)
  	    random.setRanges(lower, upper);
  
  	// if a set of single chars exists set that in the script variable
  	if (single != null)
  	    random.setSingle(single, singlecount);
  
  	random.setLength(length);  // set the length of the random string
  
  	random.setAllchars(allchars); // set the allchars flag
  
  	// place the script variable in the page scope
  	pageContext.setAttribute(id, random, PageContext.PAGE_SCOPE);
  
  	return SKIP_BODY;
      }
  
      /**
       * set the name of the map
       *
       * @param value  name of the hashmap to search for on the server that contains
       *               the keys to compare the random strings to
       */
      public final void setMap(String value) {
  	map = value;
      }
  
      /**
       * set the length of the password
       *
       * @param value  length of the random string to be generated
       */
      public final void setLength(String value) {
  	try {
  	    length = new Integer(value);
  	} catch (NumberFormatException ne) {
  	    pageContext.getServletContext().log("length attribute could not be" +
  			       " turned into an Integer default value was used");
  	}
      }
  
      /**
       * set the range of characters to use
       *
       * @param value  the range of characters to use could be any char from a-z, A-Z
       *               0-9 or ! @ # $ % ^ & * ( ) _ \- + = [ ] { } \ | ; : ' " , . / 
       *               < > ?
       *
       */
      public final void setCharset(String value) {
  	// values tells the method whether or not to check for single chars
  	boolean more = true;
  
  	// create the arraylists to hold the upper and lower bounds for the char
  	// ranges
  	lower = new ArrayList(3);
  	upper = new ArrayList(3);
  
  	// user has chosen to use all possible characters in the random string
  	if (value.compareTo("all") == 0) {
  	    allchars = true;  // set allchars flag
  	    // all chars are to be used so there are no single chars to sort 
  	    // through
  	    more = false;
  	} else if ((value.charAt(1) == '-') && (value.charAt(0) != '\\')) {
  	    // run through the ranges at most 3 
  	    while (more && (value.charAt(1) == '-')){
  
  		// check to make sure that the dash is not the single char
  		if (value.charAt(0) == '\\')
  		    break;
  		else {
  		    // add upper and lower ranges to there list
  		    lower.add(new Character(value.charAt(0)));
  		    upper.add(new Character(value.charAt(2)));
  		}
  
  		// check to see if there is more to the charset
  		if (value.length() <= 3)
  		    more = false;
  		else
  		    // create a new string so that the next range if there is one
  		    // starts it
  		    value = value.substring(3);
  	    }
  	}
  
  	// if more = false there are no single chars in the charset
  	if (more) {
  
  	    single = new char[30];  // create single
  
  	    // create a set of tokens from the string of single chars
  	    StringTokenizer tokens = new StringTokenizer(value);
  
  	    while (tokens.hasMoreTokens()) {
  		// get the next token from the string
  		String token = tokens.nextToken();
  
  		if (token.length() > 1)
  		    // char is a - add it to the list
  		    single[singlecount++] = '-';
  
  		// add the current char to the list
  		single[singlecount++] = token.charAt(0);
  	    }
  	}
      }
  }