Blame | Last modification | View Log | RSS feed
/** Created on 31/03/2005*/package DMS;import java.text.SimpleDateFormat;import java.util.Vector;import java.net.URLEncoder;import java.io.InputStream;import java.io.File;import java.io.FileWriter;import java.sql.Timestamp;/*** @author mtayler*/public class DMSUtils {//log errors for debuggingpublic static void log(String s) {File log=null;FileWriter fw=null;if (log==null) {log = new File ("C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\logs\\log.txt");try {if (!log.exists()) log.createNewFile();fw = new FileWriter(log, true);} catch (Exception e){e.printStackTrace();}}try {fw.write(s);fw.write(13);fw.write(10);fw.flush();fw.close();} catch (Exception e){e.printStackTrace();}}public static Object NVL(Object obj, Object defValue) {if (obj==null) return defValue;else return obj;}public static String NVL(String obj, String defValue) {return (String)NVL((Object)obj,(Object)defValue);}public static String NVL(String obj) {return (String)NVL((Object)obj,(Object)new String(""));}public static comboDataItem[] genComboDataItemArray(String[] values, String[] descrips) {Vector v = new Vector();for (int i=0;i<values.length;i++) {comboDataItem di = new comboDataItem(values[i],descrips[i]);v.add(di);}return (comboDataItem[])v.toArray(new comboDataItem[0]);}//generate a properly encoded URL stringpublic static String cleanURLStr(String value) {String ret = null;try {ret = URLEncoder.encode(value, "UTF8");} catch (Exception e) {e.printStackTrace();}return ret;}//generate a properly encoded HTML stringpublic static String cleanHTMLStr(String value, boolean showlinebreak) {StringBuffer sbClean = new StringBuffer("");if (value == null || value.trim().equals("")) sbClean.append(" ");if (value != null) {for (int i=0; i<value.length(); i++) {switch (value.charAt(i)) {case '<': sbClean.append("<"); break;case '>': sbClean.append(">"); break;case '\"': sbClean.append("""); break;case '\'': sbClean.append("'"); break;case '&': sbClean.append("&"); break;case '\r': break;case '\n':if (showlinebreak)sbClean.append("<br>\n");elsesbClean.append('\r');default:int c = (int)value.charAt(i);if (c<32 && c>126) sbClean.append("&#" + String.valueOf(c) + ";");else sbClean.append(value.charAt(i));}}}return sbClean.toString();}//generate a properly encoded HTML stringpublic static String cleanHTMLStr(String value){return cleanHTMLStr(value, false);}//generate a properly encoded Javascript stringpublic static String cleanJStr(String value) {String ret = value;try {ret = DMSUtils.StrReplace(ret, "\\\\", "\\", true);ret = DMSUtils.StrReplace(ret, "\\\"", "\"", true);ret = DMSUtils.StrReplace(ret, "\\'", "'", true);} catch (Exception e) {e.printStackTrace();}return ret;}public static InputStream getResourceAsStream(String aFileName) {//return ClassLoader.getSystemResourceAsStream(aFileName);DMSUtils c = new DMSUtils();return c.getClass().getClassLoader().getResourceAsStream(aFileName);}public static String StrReplace(String aStr, String aNew, String aOld,boolean aRepAll){int iStart = aStr.indexOf(aOld);if (iStart==-1){return aStr;}if (aRepAll){return aStr.substring(0,iStart)+aNew+StrReplace(aStr.substring(iStart+aOld.length()),aNew,aOld,aRepAll);}else {return aStr.substring(0,iStart)+aNew+aStr.substring(iStart+aOld.length());}}public static String replaceParams(String URL, dbTemplateData templateData) {int startIndex;int endIndex;String fieldName, data, tempURL;tempURL = URL;startIndex = 0;while (startIndex != -1) {startIndex = tempURL.indexOf("{");endIndex = tempURL.indexOf("}");if (startIndex != -1) {fieldName = tempURL.substring(startIndex+1,endIndex).trim();data=DMSUtils.NVL(templateData.getFieldValue(fieldName));tempURL=tempURL.substring(0,startIndex) + DMSUtils.cleanURLStr(data) + tempURL.substring(endIndex+1,tempURL.length());}}return tempURL;}public static int compareToValue(String value1, String value2, boolean isAscending) {return compareToValue(value1, value2, "string", "", isAscending);}public static int compareToValue(String value1, String value2, String datatype, String format, boolean isAscending) {int sign = 0;if (isAscending) sign = 1;else sign = -1;if (datatype.equals("date")||datatype.equals("datetime")) {Timestamp tvalue1 = StringToTimestamp(value1, format);Timestamp tvalue2 = StringToTimestamp(value2, format);if (tvalue1==null && tvalue2!=null) return -1*sign;else if (tvalue1==null && tvalue2==null) return 0;else if (tvalue1!=null && tvalue2==null) return 1*sign;else return tvalue1.compareTo(tvalue2)*sign;} else if (datatype.equals("number")||datatype.equals("integer")||datatype.equals("int")||datatype.equals("bigint")) {Integer ivalue1 = DMSUtils.StringToInteger(value1);Integer ivalue2 = DMSUtils.StringToInteger(value2);if (ivalue1==null && ivalue2!=null) return -1*sign;else if (ivalue1==null && ivalue2==null) return 0;else if (ivalue1!=null && ivalue2==null) return 1*sign;else return ivalue1.compareTo(ivalue2)*sign;} else {return DMSUtils.NVL(value1).compareToIgnoreCase(DMSUtils.NVL(value2))*sign;}}private static void addSortedStringArray(Vector array, boolean isAscending, String data, int startIndex, int endIndex) {String firstRecordValue = DMSUtils.NVL((String)array.get(startIndex));String lastRecordValue = DMSUtils.NVL((String)array.get(endIndex));String newValue = DMSUtils.NVL(data);if (compareToValue(firstRecordValue, newValue, isAscending)>0) { //if newValue is less than firstValuearray.insertElementAt(data,startIndex); //insert before the first record} else if (compareToValue(lastRecordValue, newValue, isAscending)<=0) { //if newValue is greater than or equal to the lastValuearray.insertElementAt(data,endIndex+1); //insert after the last record} else if (startIndex == endIndex-1) { //if there are no more records in betweenarray.insertElementAt(data,startIndex+1); //insert after the first record} else {//recursive sortint midIndex = (startIndex + endIndex) / 2;String midRecordValue = DMSUtils.NVL((String)array.get(midIndex));if (compareToValue(midRecordValue, newValue, isAscending)>0) { //if newValue is less than midRecordValueaddSortedStringArray(array, isAscending, data, startIndex+1, midIndex);} else {addSortedStringArray(array, isAscending, data, midIndex, endIndex-1);}}}public static String[] SortArray(String [] data) {Vector v = new Vector();for (int i=0; i< data.length; i++) {if (i==0) v.add(data[i]);else addSortedStringArray(v, true, data[i], 0, i-1);}return (String[])v.toArray(new String[0]);}//generate a properly encoded CSV cell stringpublic static String cleanCSV(String value) {String ret = DMSUtils.NVL(value);try {ret = DMSUtils.StrReplace(ret, "\"\"", "\"", true);ret = DMSUtils.StrReplace(ret, "\n","\r\n", true);ret = "\"" + ret + "\"";} catch (Exception e) {e.printStackTrace();}return ret;}public static String TimestampToString(Timestamp value, String format) {SimpleDateFormat df = new SimpleDateFormat(format);return df.format(value);}public static int StringToInt(String value) {return Integer.valueOf(value).intValue();}public static Integer StringToInteger(String value) {Integer ret = null;try {ret = new Integer(value);} catch (Exception e){e.printStackTrace();}return ret;}public static Timestamp StringToTimestampEndOfDay(String year, String month, String day) {return StringToTimestamp(year, month, day, "23", "59", "59");}public static Timestamp StringToTimestamp(String year, String month, String day, String hour, String minute, String second) {Timestamp data = null;try {int iYear = StringToInt(year);if (iYear>1900) iYear = iYear-1900;int iMonth = StringToInt(month);int iDay = StringToInt(day);int iHour = StringToInt(hour);int iMinute = StringToInt(minute);int iSecond = StringToInt(second);data = new Timestamp(iYear, iMonth, iDay, iHour, iMinute, iSecond,0);} catch (Exception e) {e.printStackTrace();}return data;}public static Timestamp StringToTimestamp(String year, String month, String day) {return StringToTimestamp(year, month, day, "0", "0", "0");}public static Timestamp StringToTimestamp(String value, String format) {Timestamp data = null;SimpleDateFormat df = new SimpleDateFormat(format);try {java.util.Date d = df.parse(value);data = new Timestamp(d.getYear(), d.getMonth(), d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds(),0);} catch (Exception e) {e.printStackTrace();}return data;}}