taylor 2003/02/11 21:57:48
Added: src/java/org/apache/jetspeed/util TestValidation.java
TestValidationParameterParser.java
ValidationHelper.java
ValidationParameterParser.java
Log:
- ValidationParameter parser added, but was not made default parameter parser
- ValidationHelper functions added for basic data types
- unit tests added for validation
Revision Changes Path
1.1
jakarta-jetspeed/src/java/org/apache/jetspeed/util/TestValidation.java
Index: TestValidation.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" 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" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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.jetspeed.util;
// Junit imports
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.framework.TestCase;
import org.apache.turbine.util.TurbineConfig;
import org.apache.turbine.util.StringUtils;
/**
* Command Line Test Validation routines.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Ben Woodward</a>
* @version $Id: TestValidation.java,v 1.1 2003/02/12 05:57:48 taylor Exp $
*/
public class TestValidation extends TestCase
{
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public TestValidation( String name ) {
super( name );
}
/**
* Start the tests.
*
* @param args the arguments. Not used
*/
public static void main(String args[]) {
junit.awtui.TestRunner.main( new String[] { TestValidation.class.getName() }
);
}
public void setup() {
System.out.println("Setup: Testing Validation");
}
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite( TestValidation.class );
}
///////////////////////////////////////////////////////////////////////////
public void testAlphaNumeric() throws Exception
{
String goodString = "The quick brown fox jumped over the lazy dog
0123456789";
String badString = "><�$$!&.*";
assertTrue(ValidationHelper.isAlphaNumeric(goodString));
assertTrue(!ValidationHelper.isAlphaNumeric(badString));
}
public void testLooseAlphaNumeric() throws Exception
{
//String goodString = "The quick brown fox jumped over the lazy dog
0123456789 .";
//String goodString = "www.www.www-/()+";
//String badString = "><�$$!&*.";
String goodString[] = {"a","a.a","aaa.aaa","a-a","aaa-aaa", "(aaa) aaa",
"+aa-aaa.aa", "555-4545", "555,4545"};
String badString[] = {"><�$$!&*."};
for (int ia=0; ia < goodString.length; ia++)
{
assertTrue(ValidationHelper.isLooseAlphaNumeric(goodString[ia]));
System.out.println(goodString[ia]+" is Good:
"+ValidationHelper.isLooseAlphaNumeric(goodString[ia]));
}
for (int ib=0; ib < badString.length; ib++)
{
assertTrue(!ValidationHelper.isLooseAlphaNumeric(badString[ib]));
System.out.println(badString[ib]+" is Bad:
"+!ValidationHelper.isLooseAlphaNumeric(badString[ib]));
}
}
public void testDecimal() throws Exception
{
String goodString[] = {"1","1.1","11.1","1.11","11.11"};
String badString[] = {"a","1.a","1-a","1..1","1.1.1"};
for (int ia=0; ia < goodString.length; ia++)
{
assertTrue(ValidationHelper.isDecimal(goodString[ia]));
}
for (int ib=0; ib < badString.length; ib++)
{
assertTrue(!ValidationHelper.isDecimal(badString[ib]));
}
}
public void testInteger() throws Exception
{
String goodString[] = {"1","11","111"};
String badString[] = {"a","1.1","1.a","1-a","1..1","1.1.1"};
for (int ia=0; ia < goodString.length; ia++)
{
assertTrue(ValidationHelper.isInteger(goodString[ia]));
}
for (int ib=0; ib < badString.length; ib++)
{
assertTrue(!ValidationHelper.isInteger(badString[ib]));
}
}
public void testEmailAddress() throws Exception
{
String goodString[] = {"[EMAIL PROTECTED]","[EMAIL PROTECTED]","[EMAIL PROTECTED]","[EMAIL PROTECTED]"};
String badString[] =
{"*@b.c","a","a@","@a",".@a","a@b.","a@b","a@@b.c","a@[EMAIL PROTECTED]","aaa@b^.c"};
for (int ia=0; ia < goodString.length; ia++)
{
assertTrue(ValidationHelper.isEmailAddress(goodString[ia]));
}
for (int ib=0; ib < badString.length; ib++)
{
assertTrue(!ValidationHelper.isEmailAddress(badString[ib]));
}
}
public void testURL() throws Exception
{
String goodString = "http://www.apache.org";
String badString = "me.";
assertTrue(ValidationHelper.isURL(goodString));
assertTrue(!ValidationHelper.isURL(badString));
}
/*
Configuration object to run Turbine outside a servlet container
( uses turbine.properties )
private static TurbineConfig config = null;
static
{
try
{
config = new TurbineConfig( "./bin",
"/WEB-INF/conf/TurbineResources.properties");
config.init();
}
catch (Exception e)
{
//fail(StringUtils.stackTrace(e));
System.out.println(StringUtils.stackTrace(e));
}
}
*/
}
1.1
jakarta-jetspeed/src/java/org/apache/jetspeed/util/TestValidationParameterParser.java
Index: TestValidationParameterParser.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" 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" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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.jetspeed.util;
import java.util.List;
// Junit imports
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.framework.TestCase;
import org.apache.turbine.util.TurbineConfig;
import org.apache.turbine.util.StringUtils;
import org.apache.jetspeed.test.HeadlessBaseTest;
/**
* Command Line Test ValidationParameterParser routines.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Ben Woodward</a>
* @author <a href="mailto:[EMAIL PROTECTED]">David Sean Taylor</a>
* @version $Id: TestValidationParameterParser.java,v 1.1 2003/02/12 05:57:48 taylor
Exp $
*/
public class TestValidationParameterParser extends HeadlessBaseTest
{
/**
* Defines the testcase name for JUnit.
*
* @param name the testcase's name.
*/
public TestValidationParameterParser( String name ) {
super( name );
}
/**
* Start the tests.
*
* @param args the arguments. Not used
*/
public static void main(String args[]) {
junit.awtui.TestRunner.main( new String[] {
TestValidationParameterParser.class.getName() } );
}
public void setup() {
System.out.println("Setup: Testing ValidationParameterParser");
}
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite( TestValidationParameterParser.class );
}
///////////////////////////////////////////////////////////////////////////
public void testGood() throws Exception
{
try
{
Dummy dummy = new Dummy();
ValidationParameterParser parser = new ValidationParameterParser();
parser.add("Name", "David");
parser.add("Age", "41");
parser.add("Email", "[EMAIL PROTECTED]");
parser.setProperties(dummy);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
public void testFail() throws Exception
{
boolean failed = false;
try
{
Dummy dummy = new Dummy();
ValidationParameterParser parser = new ValidationParameterParser();
parser.add("Name", "David");
parser.add("Age", "1041");
parser.add("Email", "[EMAIL PROTECTED]");
parser.setProperties(dummy);
}
catch (Throwable t)
{
failed = true;
}
assertTrue(failed == true);
}
public class Dummy
{
private String name;
private String email;
private int age;
public String getName()
{
return name;
}
public String getEmail()
{
return email;
}
public int getAge()
{
return age;
}
public void setName(String name)
{
this.name = name;
}
public void setEmail(String email)
{
this.email = email;
}
public void setAge(int age)
{
this.age = age;
}
public boolean validateAge(int age)
{
if (age > 0 && age < 120)
{
return true;
}
return false;
}
public boolean validateEmail(String email)
{
return ValidationHelper.isEmailAddress(email);
}
public boolean validateName(String name)
{
return ValidationHelper.isLooseAlphaNumeric(name);
}
}
}
1.1
jakarta-jetspeed/src/java/org/apache/jetspeed/util/ValidationHelper.java
Index: ValidationHelper.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" 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" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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.jetspeed.util;
import org.apache.oro.text.perl.*;
//import java.util.regex.*;
import javax.mail.internet.*;
import java.net.*;
import java.util.Properties;
/**
* <P>Data Validation functions using Regex</P>
*
* @author <a href="mailto:[EMAIL PROTECTED]">Ben Woodward</a>
* @author <a href="mailto:[EMAIL PROTECTED]">David Sean Taylor</a>
* @version $Id: ValidationHelper.java,v 1.1 2003/02/12 05:57:48 taylor Exp $
*/
public class ValidationHelper
{
//static string bigEmailRegex = new
String("[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\"[^\\\\\\x80-\\xff\\n\\015\"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015\"]*)*\")[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:\\.[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\"[^\\\\\\x80-\\xff\\n\\015\"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015\"]*)*\")[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*)*@[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:\\.[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*)*|(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\"[^\\\\\\x80-\\xff\\n\\015\"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015\"]*)*\")[^()<>@,;:\".\\\\\\[\\]\\x80-\\xff\\000-\\010\\012-\\037]*(?:(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)|\"[^\\\\\\x80-\\xff\\n\\015\"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015\"]*)*\")[^()<>@,;:\".\\\\\\[\\]\\x80-\\xff\\000-\\010\\012-\\037]*)*<[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:@[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:\\.[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*)*(?:,[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*@[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:\\.[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*)*)*:[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*)?(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\"[^\\\\\\x80-\\xff\\n\\015\"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015\"]*)*\")[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:\\.[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\"[^\\\\\\x80-\\xff\\n\\015\"]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015\"]*)*\")[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*)*@[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:\\.[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\\)[\\040\\t]*)*(?:[^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff]+(?![^(\\040)<>@,;:\".\\\\\\[\\]\\000-\\037\\x80-\\xff])|\\[(?:[^\\\\\\x80-\\xff\\n\\015\\[\\]]|\\\\[^\\x80-\\xff])*\\])[\\040\\t]*(?:\\([^\\\\\\x80-\\xff\\n\\015()]*(?:(?:\\\\[^\\x80-\\xff]|\\([^\\\\\\x80-\\xff\\n\\015()]*(?:\\\\[^\\x80-\\xff][^\\\\\\x80-\\xff\\n\\015()]*)*\\))[^\\\\\\x80-\\xff\\n\\015()]*)*\)[\040\t]*)*)*>)");
//static string emailRegex = new
String("/^(([^\\-][\\w\\-]*?\\.?){1,})[^-](\\@{1})([^\\-][\\w+\\.\\-])[^\\2]{1,}[^\\-]$/");
/**
* Tests that the input string contains only alpha numeric or white spaces
* <P>
* @param evalString The string that is to be evaluated
* @return True if the input is alpha numeric, false otherwise.
**/
public static boolean isAlphaNumeric(String evalString)
{
if ((evalString == null) || evalString.equals(""))
{
return true;
}
Perl5Util util = new Perl5Util();
return util.match("/^[\\w\\s]+$/", evalString);
}
public static boolean isLooseAlphaNumeric(String evalString)
{
if ((evalString == null) || evalString.equals(""))
{
return true;
}
Perl5Util util = new Perl5Util();
return util.match("/^[\\w\\s\\.\\,\\/\\-\\(\\)\\+]+$/", evalString);
}
/**
* Tests that the input string contains only numeric
* <P>
* @param evalString The string that is to be evaluated
* @return True if the input is numeric, false otherwise.
**/
public static boolean isDecimal(String evalString)
{
if ((evalString == null) || evalString.equals(""))
{
return true;
}
Perl5Util util = new Perl5Util();
return util.match("/^(\\d+\\.)?\\d+$/", evalString);
}
/**
* Tests that the input string contains only an integer
* <P>
* @param evalString The string that is to be evaluated
* @return True if the input is numeric, false otherwise.
**/
public static boolean isInteger (String evalString)
{
if ((evalString == null) || evalString.equals(""))
{
return true;
}
Perl5Util util = new Perl5Util();
return util.match("/^\\d+$/", evalString);
}
/**
* Tests that the input string contains a valid email addess
* <P>
* @param evalString The string that is to be evaluated
* @return True if the input is a valid email address.
**/
public static boolean isEmailAddress(String evalString)
{
if ((evalString == null) || evalString.equals(""))
{
return true;
}
Perl5Util util = new Perl5Util();
return
util.match("/^(?:\\w[\\w-]*\\.)*\\w[\\w-]*@(?:\\w[\\w-]*\\.)+\\w[\\w-]*$/",
evalString);
}
/**
* Tests that the input string contains a valid URL
* <P>
* @param evalString The string that is to be evaluated
* @return True if the input is a valid URL.
**/
public static boolean isURL(String evalString)
{
try
{
if ((evalString == null) || evalString.equals(""))
{
return true;
}
URL url = new URL(evalString);
/*
Perl5Util util = new Perl5Util();
System.out.println("looking at " +evalString);
return util.match("/^[\\w%?-_~]$/", evalString);
*/
//Object content = url.getContent();
//System.out.println("url contains :["+content+"]");
return true;
}
catch (Exception e)
{
System.err.println(evalString+" is not a valid URL: "+ e);
return false;
}
}
public static boolean isValidIdentifier(String folderName)
{
boolean valid = true;
char[] chars = folderName.toCharArray();
for (int ix = 0; ix < chars.length; ix++)
{
if (!Character.isJavaIdentifierPart(chars[ix]))
{
valid = false;
break;
}
}
return valid;
}
}
1.1
jakarta-jetspeed/src/java/org/apache/jetspeed/util/ValidationParameterParser.java
Index: ValidationParameterParser.java
===================================================================
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2000-2003 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 acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Jetspeed" 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" or
* "Apache Jetspeed", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* 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.jetspeed.util;
import java.io.BufferedReader;
import java.io.Reader;
import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyDescriptor;
import java.beans.IndexedPropertyDescriptor;
import java.beans.Introspector;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.StringKey;
import java.util.Date;
import org.apache.turbine.util.Log;
import org.apache.turbine.util.parser.DefaultParameterParser;
import org.apache.turbine.util.ParameterParser;
import org.apache.turbine.util.TurbineException;
import org.apache.turbine.util.pool.Recyclable;
import org.apache.turbine.util.upload.FileItem;
import org.apache.turbine.services.upload.TurbineUpload;
import org.apache.jetspeed.util.ValidationException;
/**
* A Turbine parameter parser with Validation built in.
* Validates any bean with methods that begin with validate[AttributeName].
* Works with Torque-generated beans.
* To use this class, override the TurbineResources.properties:
*
*
services.RunDataService.default.parameter.parser=org.apache.turbine.util.parser.DefaultParameterParser
*
* @author <a href="mailto:[EMAIL PROTECTED]">David Sean Taylor</a>
* @version $Id: ValidationParameterParser.java,v 1.1 2003/02/12 05:57:48 taylor Exp
$
*/
public class ValidationParameterParser
extends DefaultParameterParser
implements ParameterParser,
Recyclable
{
public ValidationParameterParser()
{
super();
}
public ValidationParameterParser(String characterEncoding)
{
super (characterEncoding);
}
/**
* Uses bean introspection to set writable properties of bean from
* the parameters, where a (case-insensitive) name match between
* the bean property and the parameter is looked for.
*
* @param bean An Object.
* @exception Exception, a generic exception.
*/
public void setProperties(Object bean)
throws Exception
{
Class beanClass = bean.getClass();
PropertyDescriptor[] props
= Introspector.getBeanInfo(beanClass).getPropertyDescriptors();
StringBuffer invalidFieldMessages = new StringBuffer("");
boolean valid = true;
for (int i = 0; i < props.length; i++)
{
String propname = props[i].getName();
Method setter = props[i].getWriteMethod();
if (setter != null &&
(containsKey(propname) ||
containsDateSelectorKeys(propname) ||
containsTimeSelectorKeys(propname)))
{
/* VALIDATION */
if (!validateProperty(bean, props[i]))
{
invalidFieldMessages.append("'");
invalidFieldMessages.append(propname);
invalidFieldMessages.append("' is not a valid field, ");
valid = false;
}
setMyProperty(bean, props[i]);
}
}
//System.out.println("invalidFieldMessages ["+invalidFieldMessages+"]");
if (!valid)
{
//int lastComma = invalidFieldMessages.lastIndexOf(", ");
int lastComma = new String(invalidFieldMessages).lastIndexOf(", ");
String result = invalidFieldMessages.substring(0,lastComma);
// System.err.println("Had a validation problem and am throwing:
"+invalidFieldMessages);
throw new ValidationException(result);
}
}
/**
* Set the property 'prop' in the bean to the value of the
* corresponding parameters. Supports all types supported by
* getXXX methods plus a few more that come for free because
* primitives have to be wrapped before being passed to invoke
* anyway.
*
* @param bean An Object.
* @param prop A PropertyDescriptor.
* @exception Exception, a generic exception.
*/
protected void setMyProperty(Object bean,
PropertyDescriptor prop)
throws Exception
{
if (prop instanceof IndexedPropertyDescriptor)
{
throw new Exception(prop.getName() +
" is an indexed property (not supported)");
}
Method setter = prop.getWriteMethod();
if (setter == null)
{
throw new Exception(prop.getName() +
" is a read only property");
}
Object[] args = getArguments(prop);
setter.invoke(bean, args);
}
protected Object[] getArguments(PropertyDescriptor prop)
throws Exception
{
Class propclass = prop.getPropertyType();
Object[] args = { null };
if (propclass == String.class)
{
args[0] = getString(prop.getName());
}
else if (propclass == Integer.class || propclass == Integer.TYPE)
{
args[0] = getInteger(prop.getName());
}
else if (propclass == Long.class || propclass == Long.TYPE)
{
args[0] = new Long(getLong(prop.getName()));
}
else if (propclass == Boolean.class || propclass == Boolean.TYPE)
{
args[0] = getBool(prop.getName());
}
else if (propclass == Double.class || propclass == Double.TYPE)
{
args[0] = new Double(getDouble(prop.getName()));
}
else if (propclass == BigDecimal.class)
{
args[0] = getBigDecimal(prop.getName());
}
else if (propclass == String[].class)
{
args[0] = getStrings(prop.getName());
}
else if (propclass == Object.class)
{
args[0] = getObject(prop.getName());
}
else if (propclass == int[].class)
{
args[0] = getInts(prop.getName());
}
else if (propclass == Integer[].class)
{
args[0] = getIntegers(prop.getName());
}
else if (propclass == Date.class)
{
args[0] = getDate(prop.getName());
}
else if (propclass == NumberKey.class)
{
args[0] = getNumberKey(prop.getName());
}
else if (propclass == StringKey.class)
{
args[0] = getStringKey(prop.getName());
}
else
{
//throw new Exception("property " + prop.getName() + " is of unsupported
type " + propclass.toString());
}
return args;
}
/**
* Validate a bean's property based on definition in the Torque Object Model
*
* @param bean The bean to be validated.
* @param prop The bean's property descriptor
* @return true if validation was successful, false if validation failed
**/
protected boolean validateProperty(Object bean,
PropertyDescriptor prop)
throws Exception
{
String propertyName = prop.getName();
String methodName = "validate" +
Character.toUpperCase(propertyName.charAt(0)) +
propertyName.substring(1);
Class[] signatureParams = { prop.getPropertyType() }; //{
java.lang.String.class };
Object[] methodParams = getArguments(prop);
try
{
Method method = bean.getClass().getMethod(methodName, signatureParams);
boolean isValidBool = ((Boolean)method.invoke(bean,
methodParams)).booleanValue();
return isValidBool;
}
catch (NoSuchMethodException nsm_e)
{
try{
return validateObject(prop);
}
catch (Exception e)
{
// System.err.println("EXCEPTION With the auto validation "+ e);
return false;
}
}
catch (Exception e)
{
// System.err.println("EXCEPTION INVOKING METHOD " + methodName + " : "
+ e);
}
return true;
}
protected boolean validateObject(PropertyDescriptor prop)
throws Exception
{
Class propclass = prop.getPropertyType();
Object[] args = { null };
if (propclass == String.class)
{
return ValidationHelper.isAlphaNumeric(getString(prop.getName()));
}
else if (propclass == Integer.class || propclass == Integer.TYPE)
{
return ValidationHelper.isInteger(getString(prop.getName()));
}
else if (propclass == Long.class || propclass == Long.TYPE)
{
return ValidationHelper.isDecimal(getString(prop.getName()));
}
else if (propclass == Boolean.class || propclass == Boolean.TYPE)
{
System.err.println("Auto validate: Boolean -- NOT IMPLEMENTED");
return true;// WORK NEEDED
}
else if (propclass == Double.class || propclass == Double.TYPE)
{
return ValidationHelper.isDecimal(getString(prop.getName()));
}
else if (propclass == BigDecimal.class)
{
return ValidationHelper.isDecimal(getString(prop.getName()));
}
else if (propclass == String[].class)
{
return ValidationHelper.isAlphaNumeric(getString(prop.getName()));
}
else if (propclass == Object.class)
{
System.err.println("Auto validate: Object-- NOT IMPLEMENTED");
return true;//work needed
}
else if (propclass == int[].class)
{
return ValidationHelper.isInteger(getString(prop.getName()));
}
else if (propclass == Integer[].class)
{
return ValidationHelper.isInteger(getString(prop.getName()));
}
else if (propclass == Date.class)
{
System.err.println("Auto validate: Date -- NOT IMPLEMENTED");
return true;//work needed
}
else if (propclass == NumberKey.class)
{
return ValidationHelper.isInteger(getString(prop.getName()));
}
else if (propclass == StringKey.class)
{
return ValidationHelper.isInteger(getString(prop.getName()));
}
else
{
/*
throw new Exception("property "
+ prop.getName()
+ " is of unsupported type "
+ propclass.toString());
*/
}
return false;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]