1package com.mot.dm.tool; 2 3import java.io.*; 4import java.util.*; 5 6public class DMTS { 7 private StringBuffer sb = null; 8 private String topPath = null; 9 10 public void convert(String pathFrom, String pathTo) throws Exception { 11 if (! (pathFrom.toUpperCase().endsWith(".ZIP") || 12 pathFrom.toUpperCase().endsWith(".DMTS"))) { 13 throw new Exception("\nFormat not supported for parameter <pathFrom>..."); 14 } 15 else if (! (pathTo.toUpperCase().endsWith(".ZIP") || 16 pathTo.toUpperCase().endsWith(".DMTS"))) { 17 throw new Exception("\nFormat not supported for parameter <fileFrom>..."); 18 } 19 20 File f; 21 f = new File(pathFrom); 22 if (!f.exists()) { 23 throw new Exception("File doesn't exists: " + pathFrom); 24 } 25 f = new File(pathTo); 26 if (f.exists()) { 27 Util.verbose("Removing file: " + pathTo + "..."); 28 f.delete(); 29 } 30 if (pathFrom.toUpperCase().endsWith(".ZIP") && 31 pathTo.toUpperCase().endsWith(".DMTS")) { 32 zip2dmts(pathFrom, pathTo); 33 } 34 else if (pathFrom.toUpperCase().endsWith(".DMTS") && 35 pathTo.toUpperCase().endsWith(".ZIP")) { 36 dmts2zip(pathFrom, pathTo); 37 } 38 else { 39 throw new Exception("Unsupported format for conversion. \nFormat should be \".zip -> .dmts\" or \".dmts -> .zip\""); 40 } 41 System.out.println("Done"); 42 } 43 44 public void dmts2zip(String dmtsPath, String zipPath) throws Exception { 45 String tmpDirPath = "tmp_s2z_" + Calendar.getInstance().getTime().getTime(); 46 try { 47 Util.verbose("Converting dmts file to directory Dmt ..."); 48 dmts2dir(dmtsPath, tmpDirPath); 49 50 Util.verbose("Zipping directory Dmt ..."); 51 Zip zip = new Zip(); 52 zip.zip(tmpDirPath + "/Dmt", zipPath); 53 } 54 finally { 55 File f = new File(tmpDirPath); 56 if (f.exists()) { 57 Util.deleteDir(f); 58 } 59 } 60 } 61 62 public void zip2dmts(String zipPath, String dmtsPath) throws Exception { 63 String tmpDirPath = "tmp_z2s_" + Calendar.getInstance().getTime().getTime(); 64 try { 65 Util.verbose("Unzipping file to directory Dmt ..."); 66 Zip zip = new Zip(); 67 zip.unzip(zipPath, tmpDirPath); 68 69 Util.verbose("Converting directory Dmt to dmts file..."); 70 dir2dmts(tmpDirPath + "/Dmt", dmtsPath); 71 } 72 finally { 73 File f = new File(tmpDirPath); 74 if (f.exists()) { 75 Util.deleteDir(f); 76 } 77 } 78 } 79 80 public void dmts2dir(String dmtsPath, String outputDir) throws Exception { 81 82 File dmts = new File(dmtsPath); 83 if (!dmts.exists()) { 84 throw new Exception("DMTS file doesn't exist: " + dmtsPath); 85 } 86 else if (dmts.isDirectory()) { 87 throw new Exception("Dmts file cannot be directory : " + dmtsPath); 88 } 89 90 if (outputDir.length() == 0) { 91 outputDir = "."; 92 } 93 94 File output = new File(outputDir); 95 96 if (!output.exists() && !output.mkdirs()) { 97 throw new Exception("Cannot create directory : " + topPath); 98 } 99 100 topPath = output.getPath() + "/Dmt"; 101 FileReader fr = new FileReader(dmts); 102 BufferedReader br = new BufferedReader(fr); 103 String line; 104 String currPath = null; 105 String tmp; 106 StringBuffer sbParm = new StringBuffer(); 107 try { 108 while ( (line = br.readLine()) != null) { 109 if (line.startsWith("[")) { 110 line = line.trim(); 111 tmp = line.substring(1, line.length() - 1); 112 tmp = (tmp.equals(".")) ? "" : Util.replaceStr(tmp, "*", "[]"); 113 // if (currPath != null && sbParm.length() > 0) { 114 if (currPath != null) { 115 writeParm(currPath, sbParm.toString()); 116 } 117 currPath = topPath + tmp; 118 sbParm = new StringBuffer(); 119 } 120 else { 121 sbParm.append(line + "\n"); 122 } 123 } 124 } 125 finally { 126 br.close(); 127 fr.close(); 128 } 129 //if (currPath != null && sbParm.length() > 0) { 130 if (currPath != null) { 131 writeParm(currPath, sbParm.toString()); 132 } 133 } 134 135 public void dir2dmtsDir(String dmtDir, String outputDir) throws Exception { 136 137 if(outputDir.toUpperCase().endsWith(".DMTS")){ 138 dir2dmts(dmtDir, outputDir); 139 } 140 else{ 141 142 if (outputDir.length() > 0) { 143 outputDir += (outputDir.endsWith("/")) ? "" : "/"; 144 } 145 146 File f = new File(dmtDir); 147 String dmtsName = f.getName() + ".dmts"; 148 149 File dmts = new File(outputDir + dmtsName); 150 if (dmts.exists() && !dmts.canWrite()) { 151 throw new Exception("DMTS file in read only mode: " + outputDir + 152 dmtsName); 153 } 154 dir2dmts(dmtDir, outputDir + dmtsName); 155 } 156 } 157 158 public void dir2dmts(String dmtDir, String dmtsPath) throws Exception { 159 sb = new StringBuffer(); 160 topPath = ""; 161 162 File top = new File(dmtDir); 163 if (!top.exists()) { 164 throw new Exception("Dmt dir doesn't exist: " + dmtDir); 165 } 166 else if (!top.isDirectory()) { 167 throw new Exception("Dmt dir not directory: " + dmtDir); 168 } 169 170 File dmts = new File(dmtsPath); 171 if (dmts.exists() && !dmts.canWrite()) { 172 throw new Exception("DMTS file in read only mode: " + dmtsPath); 173 } 174 175 File dmtsParent = dmts.getParentFile(); 176 if (dmtsParent != null && !dmtsParent.exists()) { 177 dmtsParent.mkdirs(); 178 } 179 180 sb.append("[.]\n"); 181 topPath = top.getAbsolutePath(); 182 183 File[] files = top.listFiles(); 184 File parm = getParmFile(files); 185 // if (parm == null) { 186 // throw new Exception("The parm.txt doesn't exists: " + top.getAbsolutePath()); 187 // } 188 if (parm != null) { 189 readParm(parm); 190 } 191 192 for (int i = 0; i < files.length; i++) { 193 if (files[i].isDirectory()) { 194 readDir(files[i]); 195 } 196 } 197 Util.writeFile(dmts.getAbsolutePath(), sb.toString()); 198 } 199 200 private void readDir(File f) throws Exception { 201 String path = Util.replaceStr(f.getAbsolutePath(), this.topPath, ""); 202 path = Util.replaceStr(path, "\\", "/"); 203 path = Util.replaceStr(path, "[]", "*"); 204 sb.append("[" + path + "]\n"); 205 206 File[] files = f.listFiles(); 207 File parm = getParmFile(files); 208 // if (parm == null) { 209 // throw new Exception("The parm.txt doesn't exists: " + f.getAbsolutePath()); 210 // } 211 if (parm != null) { 212 readParm(parm); 213 } 214 215 for (int i = 0; i < files.length; i++) { 216 if (files[i].isDirectory()) { 217 readDir(files[i]); 218 } 219 } 220 } 221 222 private void readParm(File f) throws Exception { 223 FileInputStream in = new FileInputStream(f); 224 byte[] b = new byte[in.available()]; 225 in.read(b); 226 String s = new String(b); 227 /* while (s.endsWith("\n")) { 228 s = s.substring(0, s.length() - 1); 229 } 230 sb.append(s + "\n\n"); 231 */ 232 // add additional 233 sb.append(s + "\n"); 234 in.close(); 235 } 236 237 private void writeParm(String dirPath, String text) throws Exception { 238 File dir = new File(dirPath); 239 if (!dir.exists()) { 240 if (!dir.mkdirs()) { 241 throw new Exception("Cannot create directorie(s) " + dirPath); 242 } 243 } 244 if (text.length() > 0) { 245 if (text.endsWith("\n")) { 246 text = text.substring(0, text.length() - 1); 247 } 248 Util.writeFile(dirPath + "/parm.txt", text); 249 } 250 } 251 252 private File getParmFile(File[] files) { 253 for (int i = 0; i < files.length; i++) { 254 if (files[i].isFile() && files[i].getName().equals("parm.txt")) { 255 return files[i]; 256 } 257 } 258 return null; 259 } 260 261 public static void main(String[] args) { 262 try { 263 if (args.length != 3) { 264 System.out.println("Wrong arguments"); 265 DMTSTool.usage(true); 266 System.exit( -1); 267 } 268 269 Util.VERBOSE = true; 270 DMTS dmts = new DMTS(); 271 if (args[0].equals("-sd")) { 272 dmts.dmts2dir(args[1], args[2]); 273 } 274 else if (args[0].equals("-ds")) { 275 dmts.dir2dmtsDir(args[1], args[2]); 276 } 277 else { 278 System.out.println("Wrong arguments"); 279 DMTSTool.usage(true); 280 System.exit( -1); 281 } 282 } 283 catch (Exception e) { 284 e.printStackTrace(); 285 } 286 } 287 288} 289