1 18 19 20 package sync4j.test.tools; 21 22 import sync4j.framework.tools.IOTools; 23 24 import java.io.*; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import sync4j.framework.core.Sync4jException; 30 import sync4j.framework.tools.WBXMLTools; 31 32 39 40 public class WBXMLConverter { 41 private static boolean xmlToWbxml = false; 42 private static boolean wbxmlToXml = false; 43 44 public WBXMLConverter() { 45 } 46 47 public static void main(String args[]) 48 throws Exception { 49 try { 50 checkArguments(args); 51 } catch (IllegalArgumentException e) { 52 System.out.println("usage:" ); 53 System.out.println(WBXMLConverter.class.getName() + " [-xml|-wbxml] <xml dir>"); 54 return; 55 } 56 57 convert((args.length == 1) ? args[0] : args[1]); 58 } 59 60 private static void checkArguments(String [] args) 61 throws IllegalArgumentException { 62 switch (args.length) { 63 case 0: 64 throw new IllegalArgumentException (); 65 66 case 1: 67 wbxmlToXml = false; 68 xmlToWbxml = true ; 69 break; 70 71 case 2: 72 wbxmlToXml = "-wbxml".equalsIgnoreCase(args[0]); 73 xmlToWbxml = "-xml".equalsIgnoreCase(args[0]); 74 75 if (!wbxmlToXml && !xmlToWbxml) { 76 throw new IllegalArgumentException (); 77 } 78 break; 79 80 default: 81 throw new IllegalArgumentException (); 82 } 83 } 84 85 private static void convert(String xmlPath) 86 throws IOException, Sync4jException { 87 System.out.println("converting xmlPath:" + xmlPath); 88 89 File xmlDir = new File(xmlPath); 90 List filesToConvert = getFilesInDir(xmlDir); 91 for (Iterator it=filesToConvert.iterator();it.hasNext();){ 92 try { 93 File file = (File)it.next(); 94 System.out.println("file:" + file.getAbsolutePath()); 95 96 if (file.getName().equalsIgnoreCase("build.xml") || 97 file.getName().equalsIgnoreCase("DBOfficer.xml")) { 98 continue; 99 } 100 101 if (xmlToWbxml) { 102 xmlToWbxml(file); 103 } 104 if (wbxmlToXml) { 105 wbxmlToXml(file); 106 } 107 108 } catch (Exception ex){ 109 ex.printStackTrace(); 110 } 111 } 112 } 113 114 private static void xmlToWbxml(File file) 115 throws IOException, Sync4jException { 116 String xml = IOTools.readFileString(file); 117 118 byte[] wbxml = WBXMLTools.toWBXML(xml); 119 120 String xmlFileName = file.getAbsolutePath(); 121 String wbxmlFileName = xmlFileName.substring(0, xmlFileName.lastIndexOf(".")); 122 wbxmlFileName = wbxmlFileName + ".wbxml"; 123 124 System.out.println("Writing to: " + wbxmlFileName); 126 IOTools.writeFile(wbxml, wbxmlFileName); 127 } 128 129 private static void wbxmlToXml(File file) 130 throws IOException, Sync4jException { 131 byte[] wbxml = IOTools.readFileBytes(file); 132 133 String xml = WBXMLTools.wbxmlToXml(wbxml); 134 135 String wbxmlFileName = file.getAbsolutePath(); 136 String xmlFileName = wbxmlFileName.substring(0, wbxmlFileName.lastIndexOf(".")); 137 xmlFileName = xmlFileName + ".xml"; 138 139 System.out.println("Writing to: " + xmlFileName); 141 IOTools.writeFile(xml, xmlFileName); 142 } 143 144 private static List getFilesInDir(File dir){ 145 List files = new ArrayList (); 146 File[] dirFiles = dir.listFiles(); 147 for (int i=0; ((dirFiles != null) && (i<dirFiles.length)); i++){ 148 File file = dirFiles[i]; 149 if (file.isFile()){ 150 if (xmlToWbxml) { 151 if (file.getName().toUpperCase().endsWith(".XML")){ 152 files.add(file); 153 } 154 } else { 155 if (file.getName().toUpperCase().endsWith(".WBXML")){ 156 files.add(file); 157 } 158 } 159 } else { 160 files.addAll(getFilesInDir(file)); 161 } 162 } 163 return files; 164 } 165 166 167 public static String readWbxmlAsXml(File wbxmlFile) 168 throws IOException, Sync4jException { 169 FileInputStream fis = new FileInputStream(wbxmlFile); 170 byte[] byteArray = new byte[(int)wbxmlFile.length()]; 171 int count = fis.read(byteArray); 172 fis.close(); 173 174 return WBXMLTools.wbxmlToXml(byteArray); 175 } 176 } | Popular Tags |