<commit_before>/**
 * <p>Title: Validator Class>
 * <p>Description:  This Class contains the methods used for validation of the fields in the userform.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author <NAME>
 * @version 1.00
 * Created on Apr 1, 2005
 */

package edu.wustl.catissuecore.util.global;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

import edu.wustl.common.util.logger.Logger;


/**
 *  This Class contains the methods used for validation of the fields in the userform.
 *  @author gautam_shetty
 */

public class Validator
{

    /**
     * Checks that the input String is a valid email address.
     * @param aEmailAddress String containing the email address to be checked.
     * @return Returns true if its a valid email address, else returns false.  
     * */
    public boolean isValidEmailAddress(String aEmailAddress)
    {
        boolean result = true;
        try
        {
            if (isEmpty(aEmailAddress))
            {
                result = false;
            }
            else
            {
            	result = isValidEmailId(aEmailAddress);
            }
        }
        catch (Exception ex)
        {
            result = false;
        }
        return result;
    }
/*
    private boolean hasNameAndDomain(String aEmailAddress)
    {
        StringTokenizer token = new StringTokenizer(aEmailAddress, "@");

        if (token.countTokens() == 2)
        {
            return true;
        }
        return false;
    }
    
  */  
    /**
     * Checks whether a string is empty and adds an ActionError object in the ActionErrors object.
     * @param componentName Component which is to be checked.
     * @param labelName Label of the component on the jsp page which is checked. 
     * @param errors ActionErrors Object.
     * @return Returns true if the componentName is empty else returns false.
     */
    public boolean isEmpty(String str)
    {
        if (str == null || str.trim().length() == 0)
        {
            return true;
        }
        return false;
    }

    /**
     * Checks that the input String contains only alphabetic characters.
     * @param alphaString The string whose characters are to be checked.
     * @return Returns false if the String contains any digit else returns true. 
     * */
    public boolean isAlpha(String alphaString)
    {
        int i = 0;
        while (i < alphaString.length())
        {
            if (!Character.isLetter(alphaString.charAt(i)))
            {
                return false;
            }
            i++;
        }
        return true;
    }

    /**
     * Checks that the input String contains only numeric digits.
     * @param numString The string whose characters are to be checked.
     * @return Returns false if the String contains any alphabet else returns true. 
     * */
    public boolean isNumeric(String numString)
    {
        try
        {
            long longValue = Long.parseLong(numString);
            if (longValue <= 0)
            {
                return false;
            }
            
            return true;
        }
        catch(NumberFormatException exp)
        {
            return false;
        }
    }

    /**
     * 
     * Checks that the input String contains only numeric digits.
     * @param numString The string whose characters are to be checked.
     * @param positiveCheck Positive integer to check for positive number
     * @return Returns false if the String contains any alphabet else returns true.
     * Depending on the value of the positiveCheck will check for positive values
     *   
     */
    public boolean isNumeric(String numString, int positiveCheck)
    {
        try
        {
            long longValue = Long.parseLong(numString);
           if(positiveCheck >0 )
           {
	            if (longValue <= 0)
	            {
	                return false;
	            }
           }     
            return true;
        }
        catch(NumberFormatException exp)
        {
            return false;
        }
    }

    
    /**
     * Checks the given String for double value.
     * 
     * @param dblString
     * @return boolean True if the string contains double number or false if any non numeric character is present.
     */
    public boolean isDouble(String dblString)
    {
        try
        {
            double dblValue = Double.parseDouble(dblString);

            if (dblValue <= 0  || Double.isNaN(dblValue)) 
            {
                return false;
            }
            
            return true;
        }
        catch(NumberFormatException exp)
        {
        	System.out.println("Error : "+exp);
            return false;
        }
    	
    }

    public boolean isDouble(String dblString,int positiveCheck)
    {
        try
        {
            double dblValue = Double.parseDouble(dblString);

            if (Double.isNaN(dblValue)) 
            {
                return false;
            }
            if(positiveCheck >0 )
            {
 	            if (dblValue <= 0)
 	            {
 	                return false;
 	            }
            }     
      
            return true;
        }
        catch(NumberFormatException exp)
        {
        	System.out.println("Error : "+exp);
            return false;
        }
    	
    }
    
    
    public boolean isValidOption(String option)
    {
        Logger.out.debug("option value: "+option);
    	if(option != null)
    	{
    		if(option.trim().equals("-1") || option.equals(Constants.SELECT_OPTION))
    			return false;
    		else
    			return true;
    	}
    	
    	return false;
    }

    
    private boolean isValidEmailId(String emailAddress)
    {
    	boolean result = true;
    	try
		{
    		
    		Pattern re = Pattern.compile("^\\w(\\.?[\\w-])*@\\w(\\.?[-\\w])*\\.([a-z]{3}(\\.[a-z]{2})?|[a-z]{2}(\\.[a-z]{2})?)$", Pattern.CASE_INSENSITIVE);
    		Matcher  mat =re.matcher(emailAddress); 
    		result = mat.matches();
		}
    	catch(Exception exp)
		{
			System.out.println("5");
    		return false;
		}
    	return result;
    }
    
    public boolean containsSpecialCharacters( String mainString, String delimiter)
    {
    	try
		{
    		StringTokenizer st = new StringTokenizer(mainString, delimiter);
    		int count = st.countTokens();
    		if(count>1)
    			return true;
    		else
    			return false;
		}
    	catch(Exception exp)
		{
			System.out.println("error : " + exp);
    		return true;
		}

    }
    
    public String delimiterExcludingGiven(String ignoreList)
    {
    	String spChars = "!@#$%^&*()=+\\|{[ ]}\'\";:/?.>,<`~ -_";
//    	System.out.println("Original : " + spChars);
    	StringBuffer sb = new StringBuffer(spChars);
//    	System.out.println("SB : " +sb);
    	StringBuffer retStr = new StringBuffer();

    	try
		{
    		char spIgnoreChars[] = ignoreList.toCharArray();
    		for(int spCharCount =0; spCharCount<spIgnoreChars.length ; spCharCount++)
    		{
    			char chkChar = spIgnoreChars[spCharCount];
//    			System.out.println(chkChar);
    			int chInd = sb.indexOf(""+chkChar );
    			while(chInd !=-1)
    			{
    				sb = sb.deleteCharAt(chInd );
    				chInd = sb.indexOf(""+chkChar );
//    				System.out.println("\t"+sb);
    			}
    			
    		}
    		retStr = sb;
//    		System.out.println("\n\nRetStr : " + retStr);
		}
    	catch(Exception exp)
		{
			System.out.println("error : " + exp);
		}
    	
    	return retStr.toString(); 
    }
    
    public static void main(String[] args)
    {
        Validator validator = new Validator();
        String str = "mandar; deshmukh";
        String delim=";,";
        System.out.println("\nstr: "+str);
        System.out.println("\ndelim: "+delim);
        System.out.println("\nContains : " + validator.containsSpecialCharacters(str,delim )); 
        
        String s= new String("- _");
        String delimitedString = validator.delimiterExcludingGiven(s );
        System.out.println("\n\n" + delimitedString );
        System.out.println(delimitedString.indexOf(s ));
        
        
//        String str = new String("<EMAIL>");
//        boolean boo = validator.isNumeric(str);
//        System.out.println(boo);
//        boo = validator.isValidEmailAddress(str);
//        System.out.println(boo);
//        System.out.println("\n************************************\n\n\n\n");
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="@<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="@@<EMAIL>.co.in";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str=".<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="@.<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        try
//		{
//            BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
//            System.out.println("\nEnter N/n to Quit \n\n");
//            System.out.print("\n\nDo you want to check the Email Address : ");
//            String ch = br.readLine();
//            while(!ch.equalsIgnoreCase("N") )
//            {
//            	System.out.println("\nEnter the Email Adress to Check: ");
//            	String email = br.readLine();
//            	boolean b = validator.isValidEmailAddress(email );
//            	System.out.println("\n Is Valid : " + b);
//            	System.out.println("---------------------");
//            	System.out.print("Do you want to Continue : ");
//            	ch = br.readLine();
//            }
//            System.out.print("\n\n**************** D O N E *************\n"); 
//		}
//        catch(Exception exp)
//		{
//        	System.out.println("Error : " + exp);
//		}

        
        
    }

}<commit_msg>getObjectValue() method added to check for null and return the value.<commit_after>/**
 * <p>Title: Validator Class>
 * <p>Description:  This Class contains the methods used for validation of the fields in the userform.</p>
 * Copyright:    Copyright (c) year
 * Company: Washington University, School of Medicine, St. Louis.
 * @author <NAME>
 * @version 1.00
 * Created on Apr 1, 2005
 */

package edu.wustl.catissuecore.util.global;

import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;

import edu.wustl.common.util.logger.Logger;


/**
 *  This Class contains the methods used for validation of the fields in the userform.
 *  @author gautam_shetty
 */

public class Validator
{

    /**
     * Checks that the input String is a valid email address.
     * @param aEmailAddress String containing the email address to be checked.
     * @return Returns true if its a valid email address, else returns false.  
     * */
    public boolean isValidEmailAddress(String aEmailAddress)
    {
        boolean result = true;
        try
        {
            if (isEmpty(aEmailAddress))
            {
                result = false;
            }
            else
            {
            	result = isValidEmailId(aEmailAddress);
            }
        }
        catch (Exception ex)
        {
            result = false;
        }
        return result;
    }
/*
    private boolean hasNameAndDomain(String aEmailAddress)
    {
        StringTokenizer token = new StringTokenizer(aEmailAddress, "@");

        if (token.countTokens() == 2)
        {
            return true;
        }
        return false;
    }
    
  */
    
    public String getObjectValue(String obj)
    {
    	if(isEmpty(obj))
    		return null;
    	else
    		return obj.toString(); 
    }
    
    /**
     * Checks whether a string is empty and adds an ActionError object in the ActionErrors object.
     * @param componentName Component which is to be checked.
     * @param labelName Label of the component on the jsp page which is checked. 
     * @param errors ActionErrors Object.
     * @return Returns true if the componentName is empty else returns false.
     */
    public boolean isEmpty(String str)
    {
        if (str == null || str.trim().length() == 0)
        {
            return true;
        }
        return false;
    }

    /**
     * Checks that the input String contains only alphabetic characters.
     * @param alphaString The string whose characters are to be checked.
     * @return Returns false if the String contains any digit else returns true. 
     * */
    public boolean isAlpha(String alphaString)
    {
        int i = 0;
        while (i < alphaString.length())
        {
            if (!Character.isLetter(alphaString.charAt(i)))
            {
                return false;
            }
            i++;
        }
        return true;
    }

    /**
     * Checks that the input String contains only numeric digits.
     * @param numString The string whose characters are to be checked.
     * @return Returns false if the String contains any alphabet else returns true. 
     * */
    public boolean isNumeric(String numString)
    {
        try
        {
            long longValue = Long.parseLong(numString);
            if (longValue <= 0)
            {
                return false;
            }
            
            return true;
        }
        catch(NumberFormatException exp)
        {
            return false;
        }
    }

    /**
     * 
     * Checks that the input String contains only numeric digits.
     * @param numString The string whose characters are to be checked.
     * @param positiveCheck Positive integer to check for positive number
     * @return Returns false if the String contains any alphabet else returns true.
     * Depending on the value of the positiveCheck will check for positive values
     *   
     */
    public boolean isNumeric(String numString, int positiveCheck)
    {
        try
        {
            long longValue = Long.parseLong(numString);
           if(positiveCheck >0 )
           {
	            if (longValue <= 0)
	            {
	                return false;
	            }
           }     
            return true;
        }
        catch(NumberFormatException exp)
        {
            return false;
        }
    }

    
    /**
     * Checks the given String for double value.
     * 
     * @param dblString
     * @return boolean True if the string contains double number or false if any non numeric character is present.
     */
    public boolean isDouble(String dblString)
    {
        try
        {
            double dblValue = Double.parseDouble(dblString);

            if (dblValue <= 0  || Double.isNaN(dblValue)) 
            {
                return false;
            }
            
            return true;
        }
        catch(NumberFormatException exp)
        {
        	System.out.println("Error : "+exp);
            return false;
        }
    	
    }

    public boolean isDouble(String dblString,int positiveCheck)
    {
        try
        {
            double dblValue = Double.parseDouble(dblString);

            if (Double.isNaN(dblValue)) 
            {
                return false;
            }
            if(positiveCheck >0 )
            {
 	            if (dblValue <= 0)
 	            {
 	                return false;
 	            }
            }     
      
            return true;
        }
        catch(NumberFormatException exp)
        {
        	System.out.println("Error : "+exp);
            return false;
        }
    	
    }
    
    
    public boolean isValidOption(String option)
    {
        Logger.out.debug("option value: "+option);
    	if(option != null)
    	{
    		if(option.trim().equals("-1") || option.equals(Constants.SELECT_OPTION))
    			return false;
    		else
    			return true;
    	}
    	
    	return false;
    }

    
    private boolean isValidEmailId(String emailAddress)
    {
    	boolean result = true;
    	try
		{
    		
    		Pattern re = Pattern.compile("^\\w(\\.?[\\w-])*@\\w(\\.?[-\\w])*\\.([a-z]{3}(\\.[a-z]{2})?|[a-z]{2}(\\.[a-z]{2})?)$", Pattern.CASE_INSENSITIVE);
    		Matcher  mat =re.matcher(emailAddress); 
    		result = mat.matches();
		}
    	catch(Exception exp)
		{
			System.out.println("5");
    		return false;
		}
    	return result;
    }
    
    public boolean containsSpecialCharacters( String mainString, String delimiter)
    {
    	try
		{
    		StringTokenizer st = new StringTokenizer(mainString, delimiter);
    		int count = st.countTokens();
    		if(count>1)
    			return true;
    		else
    			return false;
		}
    	catch(Exception exp)
		{
			System.out.println("error : " + exp);
    		return true;
		}

    }
    
    public String delimiterExcludingGiven(String ignoreList)
    {
    	String spChars = "!@#$%^&*()=+\\|{[ ]}\'\";:/?.>,<`~ -_";
//    	System.out.println("Original : " + spChars);
    	StringBuffer sb = new StringBuffer(spChars);
//    	System.out.println("SB : " +sb);
    	StringBuffer retStr = new StringBuffer();

    	try
		{
    		char spIgnoreChars[] = ignoreList.toCharArray();
    		for(int spCharCount =0; spCharCount<spIgnoreChars.length ; spCharCount++)
    		{
    			char chkChar = spIgnoreChars[spCharCount];
//    			System.out.println(chkChar);
    			int chInd = sb.indexOf(""+chkChar );
    			while(chInd !=-1)
    			{
    				sb = sb.deleteCharAt(chInd );
    				chInd = sb.indexOf(""+chkChar );
//    				System.out.println("\t"+sb);
    			}
    			
    		}
    		retStr = sb;
//    		System.out.println("\n\nRetStr : " + retStr);
		}
    	catch(Exception exp)
		{
			System.out.println("error : " + exp);
		}
    	
    	return retStr.toString(); 
    }
    
    public static void main(String[] args)
    {
        Validator validator = new Validator();
        String str = "mandar; deshmukh";
        String delim=";,";
        System.out.println("\nstr: "+str);
        System.out.println("\ndelim: "+delim);
        System.out.println("\nContains : " + validator.containsSpecialCharacters(str,delim )); 
        
        String s= new String("- _");
        String delimitedString = validator.delimiterExcludingGiven(s );
        System.out.println("\n\n" + delimitedString );
        System.out.println(delimitedString.indexOf(s ));
        
        
//        String str = new String("<EMAIL>");
//        boolean boo = validator.isNumeric(str);
//        System.out.println(boo);
//        boo = validator.isValidEmailAddress(str);
//        System.out.println(boo);
//        System.out.println("\n************************************\n\n\n\n");
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str=".<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        
//        str="@.<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//
//        str="<EMAIL>";
//        boo = validator.isValidEmailId(str );
//        System.out.println("\n\nEmail : " + str + " : " + boo );
//        try
//		{
//            BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
//            System.out.println("\nEnter N/n to Quit \n\n");
//            System.out.print("\n\nDo you want to check the Email Address : ");
//            String ch = br.readLine();
//            while(!ch.equalsIgnoreCase("N") )
//            {
//            	System.out.println("\nEnter the Email Adress to Check: ");
//            	String email = br.readLine();
//            	boolean b = validator.isValidEmailAddress(email );
//            	System.out.println("\n Is Valid : " + b);
//            	System.out.println("---------------------");
//            	System.out.print("Do you want to Continue : ");
//            	ch = br.readLine();
//            }
//            System.out.print("\n\n**************** D O N E *************\n"); 
//		}
//        catch(Exception exp)
//		{
//        	System.out.println("Error : " + exp);
//		}

        
        
    }

}
{'fraction_non_alphanumeric': 0.12881062881062882, 'fraction_numerical': 0.002366002366002366, 'mean_word_length': 1.5150474882709692, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1562824', 'n_tokens_mistral': 6663, 'n_tokens_neox': 6399, 'n_words': 1456}