1 19 20 package org.webdocwf.util.i18njdbc; 21 22 import java.io.*; 23 import java.util.*; 24 25 31 32 public class Utils { 33 34 public static final String [] keywords = { "AND", "WHERE", "FROM", "SET", "IS", "CREATE TABLE", "INT0", "INSERT", "VALUES" }; 36 public static final String keywordEscape = "~#~KEYWORD~#~"; 37 44 public static String replaceAll( 45 String input, 46 String forReplace, 47 String replaceWith) { 48 if( input == null ) 49 return null; 50 StringBuffer result = new StringBuffer (); 51 boolean hasMore = true; 52 while (hasMore) { 53 int start = input.indexOf(forReplace); 54 int end = start + forReplace.length(); 55 if (start != -1) { 56 result.append(input.substring(0, start) + replaceWith); 57 input = input.substring(end); 58 } 59 else { 60 hasMore = false; 61 result.append(input); 62 } 63 } 64 if (result.toString().equals("")) 65 return input; else 67 return result.toString(); 68 } 69 70 75 public static String bytesToHexString(byte[] b) { 76 String hexString = null; 77 try { 78 if (b != null) { 79 ByteArrayInputStream is = new ByteArrayInputStream(b); 80 hexString = streamToHexString(is); 81 return hexString; 82 } 83 else { 84 return null; 85 } 86 } 87 catch (Exception e) { 88 } 89 return hexString; 90 } 91 92 public static String handleBinaryString(String binaryString, List binaryStreamObjectList) { 93 if( binaryString == null ) 94 return null; 95 String retVal = binaryString; 96 if (retVal.startsWith(I18nSqlParser.BINARY_STREAM_OBJECT)) { 97 int index = Integer.valueOf(retVal.substring(I18nSqlParser. 98 BINARY_STREAM_OBJECT.length())).intValue(); 99 if( binaryStreamObjectList.get(index - 1 ) != null ) 101 retVal = binaryStreamObjectList.get(index - 1).toString(); 102 else retVal = null; 103 } 104 return retVal; 105 } 106 107 public static String handleQuotedString(String quotedString) { 108 if( quotedString == null ) 109 return null; 110 String retVal = quotedString; 111 if ( (retVal.startsWith("'") && retVal.endsWith("'"))) { 112 if (!retVal.equals("''")) { 113 retVal = retVal.substring(retVal.indexOf("'") + 1, 114 retVal.lastIndexOf("'")); 115 } 116 else { 117 retVal = ""; 118 } 119 } else { 120 if( retVal.equals("null") ) 121 retVal = null; 122 } 123 return retVal; 124 } 125 126 public static String [] replaceLineBrakesAndCarrReturn( 127 String [] toReplace, 128 String lineBreakEscape, 129 String carriageReturnEscape) { 130 String [] retVal = new String [toReplace.length]; 131 for (int i = 0; i < toReplace.length; i++) { 132 if (toReplace[i] != null) { 133 retVal[i] = replaceAll(toReplace[i], "\n", lineBreakEscape); 134 retVal[i] = replaceAll(retVal[i], "\r", carriageReturnEscape); 135 } 136 } 137 return retVal; 138 } 139 140 146 public static boolean compareValues(String valA, String valB) { 147 boolean retVal = false; 148 149 if( valA == null && valB == null ) 150 retVal = true; 151 else if( valA == null && valB != null ) 152 retVal = false; 153 else if( valA != null && valB == null ) 154 retVal = false; 155 else if( valA.equals(valB) ) 156 retVal = true; 157 158 return retVal; 159 } 160 161 166 public static byte[] hexStringToBytes(String val) { 167 byte[] buf = new byte[val.length() / 2]; 168 final char[] hexBytes = { 169 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 170 'E', 'F' 171 }; 172 byte[] hexMap = new byte[256]; 173 for (int i = 0; i < hexBytes.length; i++) { 174 hexMap[hexBytes[i]] = (byte) i; 175 } 176 int pos = 0; 177 for (int i = 0; i < buf.length; i++) { 178 buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4); 179 buf[i] += hexMap[val.charAt(pos++)]; 180 } 181 return buf; 182 } 183 184 190 public static String streamToHexString(InputStream is) throws IOException { 191 try { 192 char[] hexBytes = { 193 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 194 'E', 'F'}; 195 int c; 196 StringBuffer hexString = new StringBuffer (); 197 while ( (c = is.read()) >= 0) { 198 hexString.append(hexBytes[ (c >> 4) & 0xf]); 199 hexString.append(hexBytes[c & 0xf]); 200 } 201 return hexString.toString(); 202 } 203 catch (Exception e) { 204 throw new IOException(e.getMessage()); 205 } 206 } 207 208 214 public static String replaceKeywords(String s, HashMap oldValues) { 215 String retVal = s; 216 int index = 1; 217 for (int i = 0; i < keywords.length; i++) { 218 StringBuffer result = new StringBuffer (); 219 boolean hasMore = true; 220 while (hasMore) { 221 int start = retVal.toUpperCase().indexOf(keywords[i].toUpperCase()); 222 int end = start + keywords[i].length(); 223 if (start != -1) { 224 String newValue = keywordEscape + String.valueOf(index); 225 while( oldValues.containsKey(newValue) ) { 226 index++; 227 newValue = keywordEscape + String.valueOf(index); 228 } 229 result.append(retVal.substring(0, start) + newValue); 230 oldValues.put(newValue, retVal.substring(start, end)); 231 retVal = retVal.substring(end); 232 } 233 else { 234 hasMore = false; 235 result.append(retVal); 236 } 237 } 238 if (!result.toString().equals("")) 239 retVal = result.toString(); 240 } 241 return retVal; 242 } 243 244 250 public static String replaceKeywordsBack(String s, HashMap oldValues) { 251 String retVal = s; 252 Set keys = oldValues.keySet(); 253 Iterator it = keys.iterator(); 254 Object key = ""; 255 String value = ""; 256 while( it.hasNext() ) { 257 key = it.next(); 258 value = (String )oldValues.get(key.toString()); 259 if( value != null ) 260 retVal = replaceAll(retVal, key.toString(), value.toString()); 261 } 262 return retVal; 263 } 264 265 } | Popular Tags |