skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, September 17, 2012

Validate Bean - A Java Program

Posted by Admin at 12:44 PM – 0 comments
 

ValidateBean - this bean will usefull for checking the validations. for example charReplace,properString,displayString,encodeDate and highlightStrings


package qc.beans;
import java.lang.*;
import java.util.*;
import java.sql.*;
import java.io.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class ValidateBean extends java.lang.Object implements java.io.Serializable
{
 /**
  *  proper string variable declarations
  */
 private String strstringrepbtxt[]={"\",""","'"};
 private String strstringrepatxt[]={"\\","\"","\'"};
 /**
  *  proper quote variable declarations
  */
 private String strquoterepbtxt[]={""","'","\",">","<","r","n"};
 private String strquoterepatxt[]={"&quot;","&#39;","&#92;","&gt;","&lt;","","<BR>"};

 /**
  *   Reads the willis properties.
  *   @param      Nothing
  *   @return     Nothing
  *   @exception  Exception   if any error in reading text file
  */

 public String[] readDBProperties()
 {
  String strDBProperties[]=new String[5];
  try
  {
   Properties properties = new Properties();

   properties.load( new FileInputStream( "G:/tomcat/webapps/Propertyfile.props" ) );
   strDBProperties[0] = properties.getProperty( "Driver" );
   strDBProperties[1] = properties.getProperty( "DB" );
   strDBProperties[2] = properties.getProperty( "URL" );
   strDBProperties[3] = properties.getProperty( "DBName" );
   strDBProperties[4] = properties.getProperty( "DBpwd" );
   properties.clear();
  }
  catch ( Exception ex )
  {
   System.out.println("Error :During willis Message properties file,  n"+ex);
   strDBProperties[4]+="Error :During willis Message properties file,  n"+ex;
   ex.printStackTrace();
  }
  return strDBProperties;
 }
 /**
  *   Replace character the  existing string  array with given string array
  *   @param      strtxt        Normal values get from text or combo  or textarea boxs
  *   @param      strrepbtxt    String array values if exist int given string
  *   @param      strrepatxt    String array values replace into above values accordingly
  *   @return     string        Formated string
  *   @exception  StringIndexOutOfBoundsException   if any error in substring
  */
 public String charReplace(String strtxt,String[] strrepbtxt,String[] strrepatxt) throws StringIndexOutOfBoundsException
 {
  if(strtxt==null){return "";}
  for(int inti=0;inti<strrepbtxt.length;inti++)
  {
   strtxt=charReplace(strtxt,strrepbtxt[inti],strrepatxt[inti]);
  }
  return strtxt.trim();
 }
 /**
  *   Replace character the  existing string  array with given string array
  *   @param      strtxt        Normal values get from text or combo  or textarea boxs
  *   @param      strrepbtxt    String  value if exist int given string
  *   @param      strrepatxt    String  value replace into above values accordingly
  *   @return     string        Formated string
  *   @exception  StringIndexOutOfBoundsException   if any error in substring
  */
 public String charReplace(String strtxt,String strrepbtxt,String strrepatxt) throws StringIndexOutOfBoundsException
 {
  int intindx=-1 ;
  int intfindx=0;
  if(strtxt==null){return "";}
  while(strtxt.indexOf(strrepbtxt,intfindx)!=-1)
  {
   intindx=strtxt.indexOf(strrepbtxt,intfindx);
   if(intindx!=-1)
   {
    strtxt = strtxt.substring(0,intindx)+strrepatxt+strtxt.substring(intindx+strrepbtxt.length());
    intfindx= intindx+strrepatxt.length();
   }
  }
  return strtxt.trim();
 }
 /**
  *   Replace following character {"\",""","'"} into {"\\","\"","\'"}
  *   @param      strvalue                         Normal values get from text or combo  or textarea boxs
  *   @return     string                           Formated string
  *   @exception  StringIndexOutOfBoundsException  If any error in substring
  */
 public String properString(String strvalue)
 {
  if(strvalue==null){return "";}
  try{return charReplace(strvalue,strstringrepbtxt,strstringrepatxt);}
  catch(StringIndexOutOfBoundsException  siobe)
  {throw new StringIndexOutOfBoundsException(""+siobe);}
 }
 /**
  *   Replace following character {""","'","\",">","<"}  into {"&quot;","&#39","&#92","&gt;","&lt;"}
  *   @param      strvalue                         Normal values get from text or combo  or textarea boxs
  *   @return     string                           Formated string
  *   @exception  StringIndexOutOfBoundsException  If any error in substring
  */
 public String displayString(String strvalue)
 {
  if(strvalue==null){return "";}
  try{return charReplace(strvalue,strquoterepbtxt,strquoterepatxt);}
  catch(StringIndexOutOfBoundsException  siobe)
  {throw new StringIndexOutOfBoundsException(""+siobe); }
 }
 /**
  *   Convert given date into yyyy/mm/dd or yyyy/mm/dd hh:mm:ss
  *   find date in given array string using binary search
  *   @param      strvalue                         Normal values get from text or combo  or textarea boxs
  *   @param      strarydt                         Date fields are contains string array
  *   @param      strparm                          Field name as search key above string array
  *   @return     string                           Formated date yyyy/mm/dd or yyyy/mm/dd hh:mm:ss
  *   @exception  StringIndexOutOfBoundsException  If any error in substring
  */
 public String encodeDate(String strvalue,String[] strarydt,String strparm) throws StringIndexOutOfBoundsException
 {
  if((! strparm.equals("") ) && Arrays.binarySearch(strarydt,strparm)>=0)
  {strvalue=encodeDate(strvalue);}
  return strvalue.trim();
 }
 /**
  *   Convert given date into yyyy/mm/dd or yyyy/mm/dd hh:mm:ss
  *   @param      strvalue                         Normal values get from text or combo  or textarea boxs
  *   @return     string                           Formated date yyyy/mm/dd or yyyy/mm/dd hh:mm:ss
  *   @exception  StringIndexOutOfBoundsException  If any error in substring
  */
 public String encodeDate(String strvalue) throws StringIndexOutOfBoundsException
 {
  if(strvalue.indexOf("/")!=-1)
   if(strvalue.length()==10){
    strvalue = strvalue.substring(6,10)+"/"+strvalue.substring(0,2)+"/"+strvalue.substring(3,5);
   }else if (strvalue.length()>=19){
    strvalue = strvalue.substring(6,10)+"/"+strvalue.substring(0,2)+"/"+strvalue.substring(3,5)+strvalue.substring(10,19);
   }
  return strvalue.trim();
 }
 /**
  *   read session object from
  *   @param       strvalue    Normal values get from text or combo  or textarea boxs
  *   @return     string       String values from session object or empty
  */
 public String getSesValue(HttpSession sesval,String strfield)
 {
  String strvalue = (sesval.getAttribute(strfield)!=null)?(String)sesval.getAttribute(strfield):"";
  return strvalue.trim();
 }
 /**
  *   read Request object from
  *   @param       strvalue  Normal values get from text or combo  or textarea boxs
  *   @return     string     String values from request object or empty
  */
 public String getReqValue(HttpServletRequest req,String strfield){
  String strvalue = (req.getParameter(strfield)!=null)?req.getParameter(strfield):"";
  return strvalue.trim();
 }
 private String highlightStrings(String strreturn,String strhighlight[])  throws Exception
 {
  String strdisval="";
  Hashtable hashhighligtindx=new Hashtable();
  int intindx=0;
  for(int inti=0;inti<strhighlight.length;inti++)
  {
   strdisval= strreturn;
   intindx=0;
   loop:while(true)
   {
    intindx=strdisval.toLowerCase().indexOf(strhighlight[inti].toLowerCase(),intindx);
    if (intindx!=-1)
    {
     if(! hashhighligtindx.containsKey(""+intindx))
     { hashhighligtindx.put(""+intindx,strdisval.substring(intindx,intindx+strhighlight[inti].length()));  }
     intindx=intindx+strhighlight[inti].length() ;
    }
    else{break loop;}
   }// while end
  }
  HashSet hashset=new HashSet(hashhighligtindx.keySet());
  String strkeys[]=(String[]) hashset.toArray(new String[0]);
  int intkey[]=new int[strkeys.length];
  for(int inti=0;inti<strkeys.length;inti++){intkey[inti]=Integer.parseInt(strkeys[inti]);}
  Arrays.sort(intkey);
  strdisval= strreturn;
  strreturn="";
  intindx=0;
  for(int inti=0;inti<intkey.length;inti++)
  {
   if(intindx<=intkey[inti])
   {
    strreturn+=strdisval.substring(intindx,intkey[inti])+"<span class=searchhighlight>"+(String)hashhighligtindx.get(""+intkey[inti])+"</span>";
    intindx=intkey[inti]+(((String)hashhighligtindx.get(""+intkey[inti])).length());
   }
  }
  if(intindx<strdisval.length()){ strreturn+=strdisval.substring(intindx);}
  return strreturn ;
 }

}// class file end


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger