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