1 package smile.stored; 2 3 import java.util.*; 4 import java.io.*; 5 6 import org.cofax.*; 7 8 15 public class utils { 16 17 20 public utils() { 21 } 22 23 30 public static HashMap HashMapToUpperCase(HashMap hm) { 31 HashMap v_hm = new HashMap(hm.size()); 32 Iterator v_iterator = hm.keySet().iterator(); 33 while (v_iterator.hasNext()) { 34 String tmpStr = (String ) v_iterator.next(); 35 v_hm.put(tmpStr.toUpperCase(), hm.get(tmpStr)); 36 } 37 return v_hm; 38 } 39 40 51 public static String getString(HashMap hash, String label, String defaultVal) { 52 String retVal = defaultVal; 53 try { 54 retVal = (String ) hash.get(label); 55 if (retVal != null) { 57 retVal = replaceString(retVal, "'", "''"); 58 retVal = replaceString(retVal, "\\", "\\\\"); 59 } 60 62 } catch (Exception e) { 63 log("Error while getting " + label + " from the HashMap : " + e.toString()); 65 } 66 67 if (retVal == null) { 68 retVal = defaultVal; 69 } 70 71 return retVal; 72 } 73 74 81 protected static void log(String input) { 82 input = "smile.stored.Utils - " + new Date().toString() + " - " + input; 83 if (DataStore.dataLog.equals("1")) { 84 if (!DataStore.dataLogLocation.equals("")) { 85 try { 86 if (DataStore.dataLogMaxSize > 0) { 87 File file = new File(DataStore.dataLogLocation); 88 long fileSize = file.length(); 89 if (fileSize > DataStore.dataLogMaxSize) { 90 file.delete(); 91 } 92 } 93 PrintWriter fileOutputStream = new PrintWriter(new FileWriter(DataStore.dataLogLocation, true)); 94 fileOutputStream.println(input); 95 fileOutputStream.close(); 96 } catch (IOException e) { 97 System.out.println(e.toString()); 98 } 99 } else { 100 System.out.println("ODS log: " + input); 101 } 102 } 103 } 104 105 117 public static String replaceString(String source, String match, String replace) { 118 119 if (match.equals("")) { 121 return source; 122 } 123 124 int index = -1; 125 int matchLength = match.length(); 126 int offset = matchLength - replace.length(); 127 128 StringBuffer result = new StringBuffer (source); 129 Vector indexes = new Vector(); 130 131 while ((index = source.indexOf(match, index)) != -1) { 133 indexes.addElement(new Integer (index)); 134 index += matchLength; 135 } 136 137 for (int i = 0; i < indexes.size(); i++) { 139 int indexStart = ((Integer ) indexes.elementAt(i)).intValue() - offset * i; 140 int indexEnd = indexStart + matchLength; 141 result.replace(indexStart, indexEnd, replace); 142 } 143 144 return result.toString(); 145 } 146 147 } 148 | Popular Tags |