1package com.mot.dm.dbtool; 2 3import java.util.*; 4import java.io.*; 5import java.util.zip.*; 6 7public class Util { 8 9 public static Value[] attStrToArrValue(String[] result_str) { 10 Value[] vals = new Value[result_str.length]; 11 for (int i = 0; i < vals.length; i++) { 12 Value val = new Value(result_str[i], i); 13 vals[i] = val; 14 } 15 return vals; 16 } 17 18 public static String getIdNameFromRecords(String id, String key) throws 19 Exception { 20 int count = 0; 21 String tmp; 22 String tmp_id = id; 23 boolean isUnique = false; 24 Record rec = (Record) Generator.hashXmlRecords.get(key); 25 if (rec == null) { 26 return id; 27 } 28 // add count to the end of id to make it unique. 29 while (!isUnique) { 30 isUnique = true; 31 for (int i = 0; i < rec.arrIDs.size(); i++) { 32 tmp = (String) (rec.arrIDs.get(i)); 33 if (tmp.equalsIgnoreCase(tmp_id)) { 34 isUnique = false; 35 tmp_id = id + ++count; 36 break; 37 } 38 } 39 } 40 return tmp_id; 41 } 42 43 public static void addXmlToRecord(String xml, String key, String id_name, 44 String source) throws Exception { 45 Record rec = (Record) Generator.hashXmlRecords.get(key); 46 if (rec == null) { 47 rec = new Record(); 48 rec.key = key; 49 parseKeyToRecord(key, rec); 50 // rec.genCompareStr(); 51 } 52 rec.arrIDs.add(id_name); 53 rec.arrXMLs.add(xml); 54 if ("BROWSER".equalsIgnoreCase(source)) { 55 rec.containsBrowser = true; 56 } 57 else if ("MMS".equalsIgnoreCase(source)) { 58 rec.containsMMS = true; 59 } 60 else if ("JAVA".equalsIgnoreCase(source)) { 61 rec.containsJavaApp = true; 62 } 63 else if ("IM".equalsIgnoreCase(source)) { 64 rec.containsIM = true; 65 } 66 else { 67 throw new Exception( 68 "Error! Unsupported application type during record creation."); 69 } 70 Generator.hashXmlRecords.put(key, rec); 71 } 72 73 //set mnc, mcc, provider_name and prepaid values for a record from combine key 74 public static void parseKeyToRecord(String key, Record record) throws 75 Exception { 76 StringTokenizer st = new StringTokenizer(key, ":"); 77 if (st.countTokens() != 4) { 78 throw new Exception("Error: The key '" + key + "' is wrong."); 79 } 80 String tmp; 81 tmp = st.nextToken().trim(); 82 record.mcc = Integer.parseInt(tmp); 83 tmp = st.nextToken().trim(); 84 record.mncLen = tmp.length(); 85 record.mnc = Integer.parseInt(tmp); 86 tmp = st.nextToken().trim(); 87 record.operator_name = tmp; 88 tmp = st.nextToken().trim(); 89 if (Const.PREPAID.equalsIgnoreCase(tmp)) { 90 record.account_type = Const.PREPAID_INT; 91 } 92 else if (Const.POSTPAID.equalsIgnoreCase(tmp)) { 93 record.account_type = Const.POSTPAID_INT; 94 } 95 else { 96 record.account_type = Const.BOTH_INT; 97 } 98 } 99 100 //creates xml, wbxml, zip files 101 public static void writeRecordsToFiles() throws Exception { 102 String omacp_xml; 103 byte[] omacp_wbxml; 104 Iterator keys = Generator.hashXmlRecords.keySet().iterator(); 105 while (keys.hasNext()) { 106 String key = (String) keys.next(); 107 // create file name 108 //String fileGenName = key.replaceAll(":", "_"); ///rem 109 //fileGenName = key.replaceAll(" ", "_"); ///rem 110 String fileGenName = generateFileNameFromKey(key); 111 112 String fileXml = fileGenName + ".xml"; 113 String fileWbxml = fileGenName + ".wbxml"; 114 String fileZip = fileGenName + ".zip"; 115 116 omacp_xml = recordToXml(key); 117 writeFile(fileXml, omacp_xml); 118 119 120 WbxmlEncoder encoder = new WbxmlEncoder(); 121 //omacp_wbxml = encoder.encode(omacp_xml); //old usage 122 omacp_wbxml = encoder.encode(new File(fileXml)); 123 writeFile(fileWbxml, omacp_wbxml); 124 125 copyFile(fileXml, Const.WORKING_DIR + Const.PATH_SEP + fileXml); 126 copyFile(fileWbxml, Const.WORKING_DIR + Const.PATH_SEP + fileWbxml); 127 128 if (Generator.USE_ZIP) { 129 writeZipFile(fileWbxml, fileZip); 130 copyFile(fileZip, Const.WORKING_DIR + Const.PATH_SEP + fileZip); 131 (new File(fileZip)).delete(); 132 } 133 134 (new File(fileXml)).delete(); 135 (new File(fileWbxml)).delete(); 136 } 137 } 138 139 public static String recordToXml(String key) throws Exception { 140 StringBuffer allXmlForKey = new StringBuffer(); 141 Record record = (Record) Generator.hashXmlRecords.get(key); 142 if (record != null) { 143 for (int i = 0; i < record.arrXMLs.size(); i++) { 144 allXmlForKey.append( (String) record.arrXMLs.get(i)); 145 } 146 } 147 String validXml = "<wap-provisioningdoc version=\"1.1\">\n" + 148 allXmlForKey.toString() + "</wap-provisioningdoc>\n"; 149 return validXml; 150 } 151 152///############################################################################ 153 public static void sortAllRecordsIntoArray() throws Exception { 154 Generator.arraySortedRecords = new Record[Generator.hashXmlRecords.size()]; 155 Iterator iterator = Generator.hashXmlRecords.keySet().iterator(); 156 Record record; 157 int count = 0; 158 while (iterator.hasNext()) { 159 record = (Record) Generator.hashXmlRecords.get(iterator.next()); 160 Generator.arraySortedRecords[count] = record; 161 count++; 162 } 163 Arrays.sort(Generator.arraySortedRecords); 164 } 165 166 public static void addBitsToTable(BlockTable table, int value, 167 int bitsSize) { 168 for (int i = bitsSize - 1; i >= 0; i--) { 169 table.data[table.byteCount] |= 170 (value & (1 << i)) != 0 ? 1 << (7 - table.bitCount) : 0; 171 172 table.bitCount++; 173 174 if (table.bitCount == 8) { 175 table.bitCount = 0; 176 table.byteCount++; 177 } 178 } 179 } 180 181 public static ArrayList readFile(String path) throws Exception { 182 ArrayList arr = new ArrayList(); 183 File f = new File(path); 184 if (!f.exists()) { 185 throw new Exception("Error: The file " + f.getAbsolutePath() + 186 " doesn't exist!"); 187 } 188 // txt files should be encoded with UTF-16; csv - with UTF-8 189 Reader fr = new InputStreamReader(new FileInputStream(f), 190 Generator.ENCODING); 191 192 BufferedReader br = new BufferedReader(fr); 193 String line; 194 int count = 0; 195 while ( (line = br.readLine()) != null) { 196 count++; 197 line = new String(line.getBytes("UTF-8")); 198 line = line.trim(); 199 if (line.length() > 0) { 200 if (Generator.IS_INPUT_TXT_FILE) { //validate and replace tabs with "," 201 if (line.indexOf(',') >= 0) { 202 throw new Exception("Error: The line#" + count + 203 " in the text file " + f.getAbsolutePath() + 204 " cannot contains comma ',' as a value."); 205 } 206 line = line.replaceAll("\t", ","); 207 } 208 else if (line.indexOf('\t') >= 0) { 209 throw new Exception("Error: The line#" + count + 210 " in the csv file " + f.getAbsolutePath() + 211 " contains tabulation as a value."); 212 } 213 arr.add(line); 214 } 215 } 216 br.close(); 217 fr.close(); 218 return arr; 219 } 220 221///WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW 222/* public static void writeFile(String path, String content) throws 223 Exception { 224 Writer fw = new OutputStreamWriter(new FileOutputStream(path), "UTF-8"); //Generator.ENCODING 225 fw.write(content); 226 fw.flush(); 227 fw.close(); 228 }*/ 229 230 //old function 231 public static void writeFile(String path, String content) throws Exception { 232 writeFile(path, content.getBytes()); 233 } 234 235 public static void writeFile(String path, byte[] content) throws Exception { 236 File f = new File(path); 237 FileOutputStream fos = new FileOutputStream(f); 238 fos.write(content); 239 fos.flush(); 240 fos.close(); 241 } 242 243 public static void writeDBFile() throws Exception { 244 File f = new File(Const.DB_FILE_NAME); 245 FileOutputStream fos = new FileOutputStream(f); 246 // write header 247 fos.write(Generator.headerTable.data); 248 // write operators names table 249 fos.write(Generator.operatorsNamesTable.data); 250 // write carrier index table 251 fos.write(Generator.carrierIndexTable.data); 252 // write applications settings table 253 fos.write(Generator.applicationsSettingsTable.data); 254 255 fos.flush(); 256 fos.close(); 257 } 258 259 public static void deleteFile(String path) { 260 File f = new File(path); 261 if (f.exists()) { 262 f.delete(); 263 } 264 } 265 266 public static void writeZipFile(String inputFileName, 267 String outputZipFileName) throws Exception { 268 269 FileInputStream in = new FileInputStream(inputFileName); 270 int wbxmlLen = in.available(); 271 byte[] wbxml_content = new byte[wbxmlLen]; 272 in.read(wbxml_content); 273 in.close(); 274 275 Deflater compressor = new Deflater(); 276 compressor.setLevel(Deflater.BEST_COMPRESSION); 277 compressor.setInput(wbxml_content); 278 compressor.finish(); 279 280 ByteArrayOutputStream bos = new ByteArrayOutputStream(wbxmlLen); 281 byte[] buf = new byte[1024]; 282 while (!compressor.finished()) { 283 int count = compressor.deflate(buf); 284 bos.write(buf, 0, count); 285 } 286 bos.close(); 287 byte[] wbxml_zip = bos.toByteArray(); 288 FileOutputStream fos = new FileOutputStream(outputZipFileName); 289 fos.write(wbxml_zip); 290 fos.flush(); 291 fos.close(); 292 //System.out.println("in: " + wbxmlLen + " out: " + wbxml_zip.length); 293 } 294 295 296 public static void copyFile(String fromFile, String toFile) throws Exception { 297 FileInputStream fis = new FileInputStream(fromFile); 298 FileOutputStream fos = new FileOutputStream(toFile); 299 byte[] buf = new byte[1024]; 300 int i = 0; 301 while ( (i = fis.read(buf)) != -1) { 302 fos.write(buf, 0, i); 303 } 304 if (fis != null) { 305 fis.close(); 306 } 307 if (fos != null) { 308 fos.close(); 309 } 310 } 311 312 public static void deleteDir(String path) throws Exception { 313 File f = new File(path); 314 if (f.exists() && f.isDirectory()) { 315 File[] files = f.listFiles(); 316 for (int i = 0; i < files.length; i++) { 317 files[i].delete(); 318 } 319 f.delete(); 320 } 321 } 322 323 public static String str(String s) { 324 return (s != null) ? s : ""; 325 } 326 327 public static boolean strToBool(String s) { 328 s = (str(s)).toUpperCase(); 329 if (s.equals("YES") || s.equals("TRUE") || s.equals("1")) { 330 return true; 331 } 332 return false; 333 } 334 335 public static boolean isFileExists(String path) { 336 return (path == null) ? false : ( (new File(path)).exists()); 337 } 338 339 // validate input files and set Encoding 340 public static String validateAndSetInputParms() { 341 String mms = Generator.pathMMSFile; 342 String browser = Generator.pathBrowserFile; 343 String java = Generator.pathJavaAppFile; 344 String im = Generator.pathIMFile; 345 String fileExtention = ""; 346 String tmp; 347 //check that at least one file is presenting 348 if (!isFileExists(mms) && !isFileExists(browser) && !isFileExists(java) && 349 !isFileExists(im)) { 350 return "Error: At least one correct file (mms or browser or java or IM) must be provided !!!"; 351 } 352 //set files type 353 if (mms != null) { 354 tmp = mms.substring(mms.length() - 4); 355 if (fileExtention.length() > 0 && !fileExtention.equals(tmp)) { 356 return 357 "Error: All input files should have the same type: txt or csv !!!"; 358 } 359 fileExtention = tmp; 360 } 361 if (browser != null) { 362 tmp = browser.substring(browser.length() - 4); 363 if (fileExtention.length() > 0 && !fileExtention.equals(tmp)) { 364 return 365 "Error: All input files should have the same type: txt or csv !!!"; 366 } 367 fileExtention = tmp; 368 } 369 if (java != null) { 370 tmp = java.substring(java.length() - 4); 371 if (fileExtention.length() > 0 && !fileExtention.equals(tmp)) { 372 return 373 "Error: All input files should have the same type: txt or csv !!!"; 374 } 375 fileExtention = tmp; 376 } 377 378 if (im != null) { 379 tmp = im.substring(im.length() - 4); 380 if (fileExtention.length() > 0 && !fileExtention.equals(tmp)) { 381 return 382 "Error: All input files should have the same type: txt or csv !!!"; 383 } 384 fileExtention = tmp; 385 } 386 if (fileExtention.equals(".txt")) { 387 Generator.IS_INPUT_TXT_FILE = true; 388 Generator.ENCODING = "UTF-16"; 389 } 390 else { 391 Generator.ENCODING = "UTF-8"; 392 Generator.IS_INPUT_TXT_FILE = false; 393 } 394 return null; 395 } 396 397 public static String generateFileNameFromKey(String key) throws Exception{ 398 //byte[] bb = key.getBytes("UTF-8"); 399 //for (int i = 0; i < bb.length; i++) { 400 // System.out.print(Integer.toHexString(bb[i]) + " "); 401 //} 402 StringTokenizer st = new StringTokenizer(key, ":"); 403 StringBuffer fileName = new StringBuffer(); 404 String tmp; 405 tmp = st.nextToken(); //mnc 406 fileName.append(tmp).append("_"); 407 408 tmp = st.nextToken(); //mcc 409 fileName.append(tmp).append("_"); 410 411 //carrier name... replace unicode to hex if required. 412 tmp = st.nextToken(); 413 byte b; 414 String hex = ""; 415 String tmpHex; 416 boolean hexRequired = false; 417 byte[] bytes = tmp.getBytes(); 418 for (int i = 0; i < bytes.length; i++) { 419 b = bytes[i]; 420 if (! ( (b >= 48 && b <= 57) || (b >= 65 && b <= 90) || 421 (b >= 97 && b <= 122))) { 422 hexRequired = true; 423 } 424 tmpHex = Integer.toHexString(((int)b) & 0xff ).toUpperCase(); 425 hex += tmpHex; 426 } 427 428 tmp = (hexRequired) ? hex.replaceAll(" ", "_") : tmp.replaceAll(" ", "_"); //carrier name... 429 fileName.append(tmp).append("_"); 430 tmp = st.nextToken(); //payment type 431 fileName.append(tmp); 432//System.out.println(">" + fileName.toString() + "<"); 433 return fileName.toString(); 434 } 435 436 ///// ================== for test only..... =========================== 437 public static void printRecords() { 438 for (int i = 0; i < Generator.arraySortedRecords.length; i++) { 439 System.out.println( 440 " mcc=" + Generator.arraySortedRecords[i].mcc + 441 " mnc=" + Generator.arraySortedRecords[i].mnc + 442 " mncLen=" + Generator.arraySortedRecords[i].mncLen + 443 " offset=" + 444 Generator.arraySortedRecords[i].operator_name_offset + 445 " payment=" + 446 Generator.arraySortedRecords[i].account_type); 447 } 448 449 } 450 451} 452