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:56:02 UTC

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

catlett     01/05/21 13:56:02

  Added:       random/src/org/apache/taglibs/random RandomNumTag.java
  Log:
  first import of random taglib
  
  Revision  Changes    Path
  1.1                  jakarta-taglibs/random/src/org/apache/taglibs/random/RandomNumTag.java
  
  Index: RandomNumTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomNumTag.java,v 1.1 2001/05/21 20:56:01 catlett Exp $
   * $Revision: 1.1 $
   * $Date: 2001/05/21 20:56:01 $
   *
   * ====================================================================
   *
   * 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.RandomNum;
  
  /**
   * The RandomNumTag will set up a random number generator which can be accessed by
   * the <b>jsp:getProperty</b> tag.
   *
   * &lt;tag&gt;
   *	&lt;name&gt;randomnum&lt;/name&gt;
   *	&lt;tagclass&gt;org.apache.taglibs.random.RandomNumTag&lt;/tagclass&gt;
   *	&lt;bodycontent&gt;empty&lt;/bodycontent&gt;
   *	&lt;info&gt;Creates a variable length random number 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;range&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 RandomNumTag extends TagSupport {
  
      /**
       * upper bound in which to search for a number defaults to 9
       */
      private long upper = 100;
      /**
       * lower bound in which to search for a number defaults to 0
       */
      private long lower = 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 {
  
  	RandomNum random = new RandomNum();
  
  	// set the range in the randomnum class
  	random.setRange(lower, upper);
  
  	// place random into page scope so that it will be available as a 
  	// script variable
  	pageContext.setAttribute(id, random, PageContext.PAGE_SCOPE);
  
  	return SKIP_BODY;
      }
  
      /**
       * set the Range
       *
       * @param value  String value that determines range from which the random
       *               number will be chosen
       *
       * @throws JspException if the lowerbound is greater than the upperbound
       *
       */
      public final void setRange(String value) throws JspException {
  	try {
  	  upper = new Integer(value.substring(value.indexOf('-') + 1)).longValue();
  	} catch (NumberFormatException nfe) {
  	    pageContext.getServletContext().log("upper attribute could not be" +
  			       " turned into an Integer default value was used");
  	}
  	try {
  	  lower = new Integer(value.substring(0, value.indexOf('-'))).longValue();
  	} catch (NumberFormatException nfe) {
  	    pageContext.getServletContext().log("lower attribute could not be" +
  			       " turned into an Integer default value was used");
  	}
  
  	// check that the range is possible i.e. the upperbound is not lower than
  	// the lowerbound
  	if (upper < lower)
  	    throw new JspException("You can't have a range where the lowerbound" +
  				   " is higher than the upperbound.");
      }
  }