Developer's Daily Java Education - Test Projects
  front page | java | perl | unix | DevDirectory
   
Front Page
Java
Education
   
 


package com.devdaily.opensource.common.util;

import java.util.Date;
import java.text.SimpleDateFormat;
import java.sql.*;
import java.lang.Thread;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/*
  rev. 1.1: 2000-08-05:
	- added removePipeCharacters()

*/

public class Utils
{

  // these variables define what to do with the html code that is given to the
  // method dealWithHTML.
  public static final int LEAVE_HTML = 1;                   // good for dd mondo
  public static final int REMOVE_HTML = 2;                  // good for adware
  public static final int CONVERT_HTML_TO_ISO_LATIN = 3;    // maybe good elsewhere

  // for adware
  public static final int HANDLE_HTML = LEAVE_HTML;

/*  public static void main (String[] poop)
  {
	Timestamp t = Timestamp.valueOf("1999-05-31 18:30:00.9");
	System.out.println("OUT: " + Utils.convertTimestampToString(t));
  }*/

  /**
   *  Remove characters from the given string that are not alpha characters.
   */
  public static String allowAlphaCharacters(String s)
  {
	StringBuffer sb = new StringBuffer();

	String mask= new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
	for (int i=0; i= 0 )
	  {
		sb.append(c);
	  }
	}
	return new String(sb);
  }
  /**
   *  Remove characters from the given string that are not alpha or numeric characters.
   */
  public static String allowAlphaNumericCharacters(String s)
  {
	StringBuffer sb = new StringBuffer();

	String mask= new String("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
	for (int i=0; i= 0 )
	  {
		sb.append(c);
	  }
	}
	return new String(sb);
  }
  /**
   *  Remove characters from the given string that are not in the given mask.
   *  This is an example mask that allows alpha-numeric characters and an underscore.
   *  mask = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
   */
  public static String allowMaskedCharacters(String s, String mask)
  {
	StringBuffer sb = new StringBuffer();

	for (int i=0; i= 0 )
	  {
		sb.append(c);
	  }
	}
	return new String(sb);
  }
  /**
   *  Remove characters from the given string that are not numeric characters.
   */
  public static String allowNumericCharacters(String s)
  {
	StringBuffer sb = new StringBuffer();

	String mask= new String("0123456789");
	for (int i=0; i= 0 )
	  {
		sb.append(c);
	  }
	}
	return new String(sb);
  }
  /**
   * Convert a blank or null String to " "
   */
  public static String convertBlankOrNullToNbsp(String s)
  {
	String newS;
	newS = convertNullToNbsp(s);
	newS = convertBlankToNbsp(newS);
	return newS;
  }
  /**
   * Convert a blank String to " "
   */
  public static String convertBlankToNbsp(String s)
  {
	if ( s.trim().equals("") )
	{
	  return " ";
	}
	return s;
  }
  public static String convertBracketsToIsoLatin(String input)
  {
	StringBuffer buffer = new StringBuffer(input);
	StringBuffer output = new StringBuffer();
	for (int i = 0; i < buffer.length(); i++)
	{
	  if (buffer.charAt(i) == '{')
		output.append("{");
	  else if (buffer.charAt(i) == '}')
		output.append("}");
	  else
		output.append(buffer.charAt(i));
	}
	return output.toString();
  }
  /**
   * Convert extended characters to ISO-Latin equivalents.
   * Can be improved (made faster) by using a HashMap to map the key/values.
   * However, I'm currently limited to a JDK1.1.x environment, not JDK2.
   */
  public static String convertExtendedCharactersToIsoLatin(String input)
  {
	StringBuffer buffer = new StringBuffer(input);
	StringBuffer output = new StringBuffer();
	for (int i = 0; i < buffer.length(); i++)
	{
	  if (buffer.charAt(i) == '"')
		output.append(""");
	  //else if (buffer.charAt(i) == '!')
	  //  output.append("!");
	  //else if (buffer.charAt(i) == '#')
	  //  output.append("#");
	  //else if (buffer.charAt(i) == '&')
	  //  output.append("&");
	  else if (buffer.charAt(i) == '\'')
		output.append("'");
	  else if (buffer.charAt(i) == '(')
		output.append("(");
	  else if (buffer.charAt(i) == ')')
		output.append(")");
	  //else if (buffer.charAt(i) == '*')
	  //  output.append("*");
	  //else if (buffer.charAt(i) == '+')
	  //  output.append("+");
	  //else if (buffer.charAt(i) == ',')
	  //  output.append(",");
	  //else if (buffer.charAt(i) == '-')
	  //  output.append("-");
	  //else if (buffer.charAt(i) == '.')
	  //  output.append(".");
	  //else if (buffer.charAt(i) == '/')
	  //  output.append("/");
	  //else if (buffer.charAt(i) == ':')
	  //  output.append(":");
	  //else if (buffer.charAt(i) == ';')
	  //  output.append(";");
	  else if (buffer.charAt(i) == '<')
		output.append("<");
	  //else if (buffer.charAt(i) == '=')
	  //  output.append("=");
	  else if (buffer.charAt(i) == '>')
		output.append(">");
	  //else if (buffer.charAt(i) == '?')
	  //  output.append("?");
	  //else if (buffer.charAt(i) == '@')
	  //  output.append("@");
	  //else if (buffer.charAt(i) == '[')
	  //  output.append("[");
	  else if (buffer.charAt(i) == '\\')
		output.append("\");
	  //else if (buffer.charAt(i) == ']')
	  //  output.append("]");
	  //else if (buffer.charAt(i) == '^')
	  //  output.append("^");
	  //else if (buffer.charAt(i) == '_')
	  //  output.append("_");
	  else if (buffer.charAt(i) == '`')
		output.append("`");
	  else if (buffer.charAt(i) == '{')
		output.append("{");
	  //else if (buffer.charAt(i) == '|')
	  //  output.append("|");
	  else if (buffer.charAt(i) == '}')
		output.append("}");
	  //else if (buffer.charAt(i) == '~')
	  //  output.append("~");
	  else
		output.append(buffer.charAt(i));
	}
	return output.toString();
  }
  /**
   * Convert a null String to " "
   */
  public static String convertNullToNbsp(String s)
  {
	if ( s == null )
	{
	  return " ";
	}
	return s;
  }
  /**
   * Convert quotes to ISO-Latin equivalents.
   */
  public static String convertQuotesToIsoLatin(String input)
  {
	if ( input == null )
		return "";

	StringBuffer buffer = new StringBuffer(input);
	StringBuffer output = new StringBuffer();
	for (int i = 0; i < buffer.length(); i++)
	{
	  if (buffer.charAt(i) == '"')
		output.append(""");
	  else if (buffer.charAt(i) == '\'')
	    output.append("'");
	  else
		output.append(buffer.charAt(i));
	}
	return output.toString();
  }
  /**
   *  Convert java.sql.Timestamp to the String that we want (mm-dd-yyyy hh:mm:ss).
   */
  public static String convertTimestampToString(Timestamp t)
  {
	// t.toString() should be: "yyyy-mm-dd hh:mm:ss.nanoseconds"
	String tOld = t.toString();

	if ( tOld.length() < 21 )
	  return "";

	String tNew = tOld.substring(5,7)   + "-"
				+ tOld.substring(8,10)   + "-"
				+ tOld.substring(0,4)   + " "
				+ tOld.substring(11,13) + ":"
				+ tOld.substring(14,16) + ":"
				+ tOld.substring(17,19);

	return tNew;
}
  /**
   Takes the given String and reformats it so it is suitable as a URL string.
   Specifically, it resolves URL problems with Netscape browsers.
   */
  public static String convertToURLFormat(String input)
  {
	StringBuffer buffer = new StringBuffer(input);
	StringBuffer output = new StringBuffer();
	for (int i = 0; i < buffer.length(); i++)
	{
	if (buffer.charAt(i) == ' ')
	  output.append("%20");
	else
	  output.append(buffer.charAt(i));
	}
	return output.toString();
  }
  // for devdaily
  //public static final int HANDLE_HTML = LEAVE_HTML;

  /**
   * Decides what to do with the HTML tags that might be in the input String.
   * Based on that decision, it will return a modified String. There are
   * three possible decisions at this time:
* (1) Leave the HTML as-is.
* (2) Remove all HTML tags in input.
* (3) Convert the HTML tags to ISO Latin.
* * The default is to remove all HTML tags in input. */ public static String dealWithHTML(String input) { String s = null; if ( Utils.HANDLE_HTML == Utils.LEAVE_HTML ) { s = convertQuotesToIsoLatin(s); s = convertBracketsToIsoLatin(input); // brackets freak out mysql during updates 10-8-2000 } else if ( Utils.HANDLE_HTML == Utils.CONVERT_HTML_TO_ISO_LATIN ) { s = convertQuotesToIsoLatin(s); s = convertExtendedCharactersToIsoLatin(input); } else { s = removePipeCharacters(input); s = removeHtmlTags(s); s = convertBracketsToIsoLatin(s); // brackets freak out mysql during updates 10-8-2000 s = convertQuotesToIsoLatin(s); } return s; } /** * Insert the method's description here. * Creation date: (11/22/2000 12:43:39 PM) * @return java.lang.String * @param password java.lang.String */ public static byte[] encryptPassword(byte [] password) { MessageDigest algorithm=null; try { algorithm = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { System.err.println(e); } algorithm.reset(); algorithm.update(password); byte[] digest1 = algorithm.digest(); return digest1; } /** * Replace double quotes with their HTML equivalent. */ public static String escapeDoubleQuotes(String input) { StringBuffer buffer = new StringBuffer(input); StringBuffer output = new StringBuffer(); for (int i = 0; i < buffer.length(); i++) { if (buffer.charAt(i) == '\"') output.append("""); else output.append(buffer.charAt(i)); } return output.toString(); } /** * Replace single quotes with a backslash-single-quote. */ public static String escapeSingleQuotes(String input) { StringBuffer buffer = new StringBuffer(input); StringBuffer output = new StringBuffer(); for (int i = 0; i < buffer.length(); i++) { if (buffer.charAt(i) == '\'') { output.append('\''); } output.append(buffer.charAt(i)); } return output.toString(); } /** * Return today's date as a String formatted properly for use with a SQL database. * The format is yyyy-mm-dd. */ public static String getTodayAsDBString () { Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(d); } /** * Print the given SQL Exception information to standard error. */ public static void printSQLExceptionToSystemError (SQLException se) { System.err.println("SQLException: " + se.getMessage()); System.err.println("SQLState: " + se.getSQLState()); System.err.println("VendorError: " + se.getErrorCode()); } /** * Remove backslash characters (\) from the given String. */ public static String removeBackslashCharacters(String input) { StringBuffer buffer = new StringBuffer(input); StringBuffer output = new StringBuffer(); for (int i = 0; i < buffer.length(); i++) { if (buffer.charAt(i) == '\\') ; // do nothing else output.append(buffer.charAt(i)); } return output.toString(); } /** * Remove ALL offensive characters (those that cause DB or HTML errors. */ public static String removeBadCharacters(String input) { String output = null; output = Utils.escapeSingleQuotes(input); output = Utils.escapeDoubleQuotes(output); // output = Utils.removeHtmlTags(output); output = Utils.dealWithHTML(output); return output; } /** * Remove HTML symbols. */ public static String removeHtmlSymbols(String input) { StringBuffer buffer = new StringBuffer(input); StringBuffer output = new StringBuffer(); for (int i = 0; i < buffer.length(); i++) { if (buffer.charAt(i) == '>') output.append(">"); else if (buffer.charAt(i) == '<') output.append("<"); else output.append(buffer.charAt(i)); } return output.toString(); } /** * Remove any HTML tags in string "ins". * Note: this only removes things enclosed between '<' and '>', and does not remove * other special HTML symbols, and does not discriminate about what is between the * '<' and the '>' symbols. */ public static String removeHtmlTags(String ins) { StringBuffer finalStr = new StringBuffer(); boolean intag = false; for (int i=0; i') { intag=false; } } return finalStr.toString(); } /** * Remove pipe characters (|) from the given String. */ public static String removePipeCharacters(String input) { StringBuffer buffer = new StringBuffer(input); StringBuffer output = new StringBuffer(); for (int i = 0; i < buffer.length(); i++) { if (buffer.charAt(i) == '|') ; // do nothing else output.append(buffer.charAt(i)); } return output.toString(); } /** * Replace the \r\n character sequence with an HTML paragraph tag. */ public static String replaceNewlineCharsWithParagraphTag(String s) { //StringBuffer sb = new StringBuffer("
"); StringBuffer sb = new StringBuffer("

"); int stringLength = s.length(); for ( int i=0; i"); i++; // already accounted for this next character, so skip it } else { sb.append("
"); } } } else { sb.append(c); } } sb.append("

"); return new String(sb); } /** * A method to simplify the call to Thread.sleep. * Remember that sleepTime is in milliseconds. */ public static void sleep(int sleepTime) { try { Thread.sleep(sleepTime); } catch (InterruptedException e) { // do nothing } } }

Copyright 1998-2003 DevDaily Interactive, Inc.
All Rights Reserved.