Okay, here ya go.  As usual, change the variables, methods and params to
suit your requirements.

----------
Usage example (see the TLD for full list - 
and/or add your own. Be sure to reflect any 
changes in the TLD in the class as well):

<partner:validation baseName="name" isNotNull="true"
    isWithinMaxLength="true" parameter="50"/>
<partner:validation baseName="areaCode" isNotNull="true"
    isWithinMaxLength="true" parameter="3"/>

----------
TLD:

<?xml version="1.0" ?>
<taglib>
  <tlibversion>0.0</tlibversion>
  <jspversion>1.0</jspversion>
  <shortname>Partner Tag Library</shortname>
  <tag>
    <name>validation</name>
    <tagclass>com.tessco.partner.web.taglib.ValidationTag</tagclass>
    <bodycontent>empty</bodycontent>
    <attribute>
      <name>baseName</name>
        <required>true</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>parameter</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isNotNull</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isBoolean</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isInteger</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <name>isLong</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isDouble</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isFloat</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <name>isGreaterThan</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isLessThan</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isDate</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <name>isFutureDate</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isPastDate</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isWithinMaxLength</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isPhone</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isEmail</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isZipCode</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
        <name>isCreditCardNumber</name>
        <required>false</required>
        <rtexprvalue>false</rtexprvalue>
    </attribute>
  </tag> 
</taglib>

----------
Tag Handler:

package com.tessco.partner.web.taglib;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.ServletRequest;

import com.tessco.partner.web.validation.DefaultValidator;

/**
ValidationTag.java

@description: generates an HTML element that is 
consistent with the server-side validation logic.

example:
<code>
The following jsp code :

<partner:validation baseName="logon" isNotNull="true"
isWithinMaxLength="true" parameter="50"/>

generates the following HTML:

<input type="hidden" name="validations" value="nn-ml50|logon" />
</code>
*/

public final class ValidationTag extends TagSupport {
  priave boolean isNotNull = false; 
  private boolean isBoolean = false;
  private boolean isInteger = false;
  private boolean isLong = false;
  private boolean isDouble = false;
  private boolean isFloat = false;
  private boolean isGreaterThan = false;
  private boolean isLessThan = false;
  private boolean isDate = false;
  private boolean isFutureDate = false;
  private boolean isPastDate = false;
  private boolean isWithinMaxLength = false;
  private boolean isPhone = false;
  private boolean isEmail = false;
  private boolean isCreditCardNumber = false;
  private boolean isZipCode = false;
  private String baseName = null;
  private String parameter = "";

  public void setBaseName( String baseName ) {
    this.baseName = baseName;
  }

  public String getBaseName(){
    return baseName;
  }

  public void setParameter( String parameter ) {
    this.parameter = parameter;
  }

  public String getParameter(){
    return baseName;
  }

  public void setIsNotNull( boolean isNotNull ){
    this.isNotNull = isNotNull;
  }

  public void setIsBoolean( boolean isBoolean ){
    this.isBoolean = isBoolean;
  }

  public void setIsInteger( boolean isInteger ){
    this.isInteger = isInteger;
  }

  public void setIsLong( boolean isLong ){
    this.isLong = isLong;
  }

  public void setIsDouble( boolean isDouble ){
    this.isDouble = isDouble;
  }

  public void setIsFloat( boolean isFloat ){
    this.isFloat = isFloat;
  }

  public void setIsGreaterThan( boolean isGreaterThan ){
    this.isGreaterThan = isGreaterThan;
  }

  public void setIsLessThan( boolean isLessThan ){
    this.isLessThan = isLessThan;
  }

  public void setIsDate( boolean isDate ){
    this.isDate = isDate;
  }

  public void setIsFutureDate( boolean isFutureDate ){
    this.isFutureDate = isFutureDate;
  }

  public void setIsPastDate( boolean isPastDate ){
    this.isPastDate = isPastDate;
  }

  public void setIsWithinMaxLength( boolean isWithinMaxLength ){
    this.isWithinMaxLength = isWithinMaxLength;
  }

  public void setIsPhone( boolean isPhone ){
    this.isPhone = isPhone;
  }

  public void setIsEmail( boolean isEmail ){
    this.isEmail = isEmail;
  }

  public void setIsCreditCardNumber( boolean isCreditCardNumber ){
    this.isCreditCardNumber = isCreditCardNumber;
  }

  public void setIsZipCode( boolean isZipCode ){
    this.isZipCode = isZipCode;
  }

  public final int doStartTag() throws JspException {
    String fullName = generateName();
    // Print the results to our output writer
    JspWriter writer = pageContext.getOut();

    try {
        writer.print( " <input type=\"hidden\" name=\"validations\"
value=\"" );
        writer.print( fullName );
        writer.print( "\" />" );
    } catch (IOException e) {
          //TO DO:  Log critical exception!
          throw new JspException(e.toString());
    } finally{//re-initialize parameters
        isNotNull = false; 
        isBoolean = false;
        isInteger = false;
        isLong = false;
        isDouble = false;
        isFloat = false;
        isGreaterThan = false;
        isLessThan = false;
        isDate = false;
        isFutureDate = false;
        isPastDate = false;
        isWithinMaxLength = false;
        isPhone = false;
        isEmail = false;
        baseName = null;
        parameter = "";
      
        return SKIP_BODY;
    }
  }

  private String generateName(){
    StringBuffer nameBuffer = new StringBuffer();
    List validationList = new ArrayList();

    if( isNotNull ){
      validationList.add( DefaultValidator.IS_NOT_NULL );
    }

    if( isBoolean ){
      validationList.add( DefaultValidator.IS_BOOLEAN );
    }

    if( isInteger )
      validationList.add( DefaultValidator.IS_INTEGER );
    }

    if( isLong ){
      validationList.add( DefaultValidator.IS_LONG );
    }

    if( isDouble ){
      validationList.add( DefaultValidator.IS_DOUBLE );
    }

    if( isFloat ){
      validationList.add( DefaultValidator.IS_FLOAT );
    }

    if( isGreaterThan ){
      validationList.add( DefaultValidator.IS_GREATER_THAN + 
         parameter );
    }

    if( isLessThan ){
      validationList.add( DefaultValidator.IS_LESS_THAN + parameter );
    }

    if( isDate ){
      validationList.add( DefaultValidator.IS_DATE );
    }

    if( isFutureDate ){
      validationList.add( DefaultValidator.IS_FUTURE_DATE );
    }

    if( isPastDate ){
      validationList.add( DefaultValidator.IS_PAST_DATE );
    }

    if( isWithinMaxLength ){
      validationList.add( DefaultValidator.IS_WITHIN_MAX_LENGTH +
        parameter );
    }

    if( isPhone ){
      validationList.add( DefaultValidator.IS_PHONE );
    }

    if( isEmail ){
      validationList.add( DefaultValidator.IS_EMAIL );
    }

    if( isCreditCardNumber ){
      validationList.add( DefaultValidator.IS_CREDIT_CARD_NUMBER );
    }

    if( isZipCode ){
      validationList.add( DefaultValidator.IS_ZIP_CODE );
    }

    for( int i = 0; i < validationList.size(); i++ ){
        nameBuffer.append( validationList.get( i ) );
                        
        if( i + 1 < validationList.size() ){//if more validations, add
delimiter
          nameBuffer.append("*");
        }
    }

    if( validationList.size() > 0 ){
      nameBuffer.append("|");
    }

  nameBuffer.append( baseName );
  return nameBuffer.toString();
  }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to