1package com.mot.dm.tool; 2 3import java.io.*; 4import com.mot.dm.io.Node; 5 6public class Util { 7 8 public static boolean VERBOSE = false; 9 10 // The following 4 functions are using in all classes to read/write files with UTF-8 encoding 11 12 public static Reader openUtf8FileReader( String filename ) throws java.io.FileNotFoundException 13 { 14 try { 15 return new InputStreamReader( new FileInputStream( filename ), "UTF-8" ); 16 } catch( java.io.UnsupportedEncodingException e ) { 17 throw new Error( "UTF-8 encoding is unsupported !!?", e ); 18 } 19 } 20 21 public static Writer openUtf8FileWriter( String filename ) throws java.io.FileNotFoundException 22 { 23 try { 24 return new OutputStreamWriter( new FileOutputStream( filename ), "UTF-8" ); 25 } catch( java.io.UnsupportedEncodingException e ) { 26 throw new Error( "UTF-8 encoding is unsupported !!?", e ); 27 } 28 } 29 30 public static Reader openUtf8FileReader( File filename ) throws java.io.FileNotFoundException 31 { 32 try { 33 return new InputStreamReader( new FileInputStream( filename ), "UTF-8" ); 34 } catch( java.io.UnsupportedEncodingException e ) { 35 throw new Error( "UTF-8 encoding is unsupported !!?", e ); 36 } 37 } 38 39 public static Writer openUtf8FileWriter( File filename ) throws java.io.FileNotFoundException 40 { 41 try { 42 return new OutputStreamWriter( new FileOutputStream( filename ), "UTF-8" ); 43 } catch( java.io.UnsupportedEncodingException e ) { 44 throw new Error( "UTF-8 encoding is unsupported !!?", e ); 45 } 46 } 47 48 public static String replaceStr(String str, String oldPat, String newPat) { 49 String result = str; 50 int from = 0; 51 while ( (from = str.indexOf(oldPat, from)) >= 0) { 52 result = str.substring(0, from); 53 result += newPat; 54 result += str.substring(from + oldPat.length(), str.length()); 55 str = result; 56 } 57 return result; 58 } 59 60 public static void writeFile(String path, String body) throws Exception { 61 PrintWriter fileWriter = new PrintWriter(new FileWriter(path)); 62 fileWriter.print(body); 63 fileWriter.flush(); 64 fileWriter.close(); 65 } 66 67 public static boolean deleteDir(File dir) { 68 if (dir.isDirectory()) { 69 String[] children = dir.list(); 70 for (int i = 0; i < children.length; i++) { 71 boolean success = deleteDir(new File(dir, children[i])); 72 if (!success) { 73 return false; 74 } 75 } 76 } 77 return dir.delete(); 78 } 79 80 public static void quickSort(Node children[]) { 81 q_sort(children, 0, children.length - 1); 82 } 83 84 private static void q_sort(Node a[], int low, int high) { 85 int lo = low; 86 int hi = high; 87 Node mid; 88 89 if (high > low) { 90 mid = a[(low + high) / 2]; 91 92 while (lo <= hi) { 93 94 while ((lo < high) 95 && (compareStringMultiNodeFirst(a[lo].getName(), mid 96 .getName()) < 0)) 97 ++lo; 98 99 while ((hi > low) 100 && (compareStringMultiNodeFirst(a[hi].getName(), mid 101 .getName()) > 0)) 102 --hi; 103 104 if (lo <= hi) { 105 Node n = a[hi]; 106 a[hi] = a[lo]; 107 a[lo] = n; 108 109 ++lo; 110 --hi; 111 } 112 } 113 114 if (low < hi) 115 q_sort(a, low, hi); 116 117 if (lo < high) 118 q_sort(a, lo, high); 119 120 } 121 } 122 123 private static int compareStringMultiNodeFirst(String str1, String str2) { 124 if (str1 == null) { 125 if (str2 == null) { 126 return 0; 127 } else { 128 return -1; 129 } 130 131 } else if (str2 == null) { 132 return 1; 133 } 134 // Check first for multinode definition "[]". The file "[]" should be always first. 135 if (str1.equals("[]") && str2.equals("[]")) { 136 return 0; 137 } else if (str1.equals("[]")) { 138 return -1; 139 } else if (str2.equals("[]")) { 140 return 1; 141 } 142 143 return str1.compareToIgnoreCase(str2); 144 } 145 146 public static void verbose(String msg) { 147 if (VERBOSE) { 148 System.out.println(msg); 149 } 150 } 151 152} 153