catlett     01/05/21 13:57:11

  Added:       random/src/org/apache/taglibs/random RandomStrg.java
  Log:
  first import of random taglib
  
  Revision  Changes    Path
  1.1                  
jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrg.java
  
  Index: RandomStrg.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrg.java,v 1.1 
2001/05/21 20:57:09 catlett Exp $
   * $Revision: 1.1 $
   * $Date: 2001/05/21 20:57:09 $
   *
   * ====================================================================
   *
   * 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 [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.taglibs.random;
  
  import java.util.*;
  
  /**
   * RandomStrg class will produce a variable set of random characters.
   *
   * @author Rich Catlett
   *
   * @version 1.0
   *
   */
  
  public class RandomStrg {
  
      /**
       * generated password
       */
      private String randomstr;
      /**
       * 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;
      /**
       * 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;
      /**
       * boolean flat that tells the random char generator if there is a list of
       * single chars
       */
      private boolean singles = false;
  
      /**
       * generate the random string
       *
       */
      private final void generaterandom() {
  
        // use all chars in the string
        if (allchars)
            for (int i = 0; i < length.intValue(); i++)
                randomstr = randomstr + new Character((char)((int) 34 + 
                                        ((int)(Math.random() * 93)))).toString();
        else if (singles) {
            // check if there are single chars to be included
  
            if (upper.size() == 3) {
                // check for the number of ranges max 3 uppercase lowercase digits
  
                // build the random string
                for (int i = 0; i < length.intValue(); i++) {
                    // you have four groups to choose a random number from, to make
                    // the choice a little more random select a number out of 100
  
                    // get a random number even or odd
                    if (((int) (Math.random() * 100)) % 2 == 0) {
  
                        // the number was even get another number even or odd
                        if (((int) (Math.random() * 100)) % 2 == 0)
                            // choose a random char from the single char group
                            randomstr = randomstr + randomSingle().toString();
                        else
                            // get a random char from the first range
                            randomstr = randomstr + randomChar((Character)lower.get(2),
                                                (Character)upper.get(2)).toString();
                    } else {
                        // the number was odd
  
                        if (((int) (Math.random() * 100)) % 2 == 0)
                            // choose a random char from the second range
                            randomstr = randomstr + randomChar((Character)lower.get(1),
                                               (Character)upper.get(1)).toString();
                        else
                            // choose a random char from the third range
                            randomstr = randomstr + 
randomChar((Character)lower.get(0),  
                                               (Character)upper.get(0)).toString();
                    }
                }
            } else if (upper.size() == 2) {
                // single chars are to be included choose a random char from
                // two different ranges
  
                // build the random char from single chars and two ranges
                for (int i = 0; i < length.intValue(); i++) {
                    // select the single chars or a range to get each random char
                    // from
  
                    if (((int)(Math.random() * 100)) % 2 == 0) {
  
                        // get random char from the single chars
                        randomstr = randomstr + randomSingle().toString();
                    } else if (((int) (Math.random() * 100)) % 2 == 0) {
  
                        // get the random char from the first range
                        randomstr = randomstr + randomChar((Character)lower.get(1),
                                               (Character)upper.get(1)).toString();
                    } else {
  
                        // get the random char from the second range
                        randomstr = randomstr + randomChar((Character)lower.get(0),
                                               (Character)upper.get(0)).toString();
                    }
                }
            } else if (upper.size() == 1) {
  
                // build the random string from single chars and one range
                for (int i = 0; i < length.intValue(); i++) {
                    if (((int) Math.random() * 100) % 2 == 0)
                        // get a random single char
                        randomstr = randomstr + randomSingle().toString();
                    else
                        // get a random char from the range
                        randomstr = randomstr + randomChar((Character)lower.get(0),
                                               (Character)upper.get(0)).toString();
                }
            } else {
                // build the rand string from single chars
                for (int i = 0; i < length.intValue(); i++)
                    randomstr = randomstr + randomSingle().toString();
            }
        } else {
  
            // no single chars are to be included in the random string
            if (upper.size() == 3) {
  
                // build random strng from three ranges
                for (int i = 0; i < length.intValue(); i++) {
  
                    if (((int) (Math.random() * 100)) % 2 == 0) {
  
                        // get random char from first range
                        randomstr = randomstr + randomChar((Character)lower.get(2),
                                               (Character)upper.get(2)).toString();
                    } else if (((int) (Math.random() * 100)) % 2 == 0) {
  
                        // get random char form second range
                        randomstr = randomstr + randomChar((Character)lower.get(1),
                                               (Character)upper.get(1)).toString();
                    } else {
  
                        // get random char from third range
                        randomstr = randomstr + randomChar((Character)lower.get(0),
                                               (Character)upper.get(0)).toString();
                    }
                }
            } else if (upper.size() == 2) {
  
                // build random string from two ranges
                for (int i = 0; i < length.intValue(); i++) {
                    if (((int) (Math.random() * 100)) % 2 == 0)
                        // get random char from first range
                        randomstr = randomstr + randomChar((Character)lower.get(1),
                                               (Character)upper.get(1)).toString();
                    else
                        // get random char from second range
                        randomstr = randomstr + randomChar((Character)lower.get(0),
                                             (Character)upper.get(0)).toString();
                }
            } else
  
                // build random string
                for (int i = 0; i < length.intValue(); i++)
                    // get random char from only range
                    randomstr = randomstr + randomChar((Character)lower.get(0), 
                                               (Character)upper.get(0)).toString();
        }
      }
  
      /**
       * generate a random char from the single char list
       *
       * @returns - a randomly selscted character from the single char list
       *
       */
      private final Character randomSingle() {
  
        return (new Character(single[(int)((Math.random() * singlecount) - 1)]));
      }
  
      /**
       * generate a random character
       *
       * @param lower  lower bound from which to get a random char
       * @param upper  upper bound from which to get a random char
       *
       * @returns - a randomly generated character
       *
       */
      private final Character randomChar(Character lower, Character upper) {
        int tempval;
        char low = lower.charValue();
        char up = upper.charValue();
  
        // get a random number in the range lowlow - lowup
        tempval = (int)((int)low + (Math.random() * ((int)(up - low))));
  
        // return the random char
        return (new Character((char) tempval));
      }
  
      /**
       * get the randomly created string for use with the 
       * &lt;jsp:getProperty name=<i>"id"</i> property="randomstr"/&gt;
       *
       * @return - randomly created string
       *
       */
      public final String getRandom() {
  
        randomstr = new String();
  
        generaterandom(); // generate the first random string
  
        if (hmap != null) {
  
            while (hmap.containsKey(randomstr)) {
                // random string has already been created generate a different one
                generaterandom();
            }
  
            hmap.put(randomstr, null);  // add the new random string
        }
  
        return randomstr;
      }
  
      /**
       * set the ranges from which to choose the characters for the random string
       *
       * @param low  set of lower ranges
       * @param up  set of upper ranges
       *
       */
      public final void setRanges(ArrayList low, ArrayList up) {
        lower = low;
        upper = up;
      }
  
  
      /**
       * set the hashmap that is used to check the uniqueness of random strings
       *
       * @param map  hashmap whose keys are used to insure uniqueness of random strgs
       *
       */
      public final void setHmap(HashMap map) {
        hmap = map;
      }
  
      /**
       * set the length of the random string
       *
       * @param value  length of the random string
       *
       */
      public final void setLength(Integer value) {
        length = value;
      }
  
      /**
       * set the allchars flag
       *
       * @param value  boolean value of the allchars flag
       *
       */
      public final void setAllchars(boolean value) {
        allchars = value;
      }
  
      /**
       * set the array of single chars to choose from for this random string and the
       * number of chars in the array
       *
       * @param chars  the array of single chars
       * @param value  the number of single chars
       *
       */
      public final void setSingle(char[] chars, int value) {
        single = chars;  // set the array of chars
        singlecount = value;  // set the number of chars in array single
        singles = true;  // set flag that single chars are in use
      }
  }
  
  
  

Reply via email to