KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > test > tools > WBXMLConverter


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19
20 package sync4j.test.tools;
21
22 import sync4j.framework.tools.IOTools;
23
24 import java.io.*;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import sync4j.framework.core.Sync4jException;
30 import sync4j.framework.tools.WBXMLTools;
31
32 /**
33  * <p>An utility to convert XML files to WBXML and vice versa.</p>
34  *
35  * @author Stefano Fornari
36  *
37  * @version $Id: WBXMLConverter.java,v 1.4 2005/05/18 13:00:47 luigiafassina Exp $
38  */

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 JavaDoc args[])
48     throws Exception JavaDoc {
49         try {
50             checkArguments(args);
51         } catch (IllegalArgumentException JavaDoc 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 JavaDoc[] args)
61     throws IllegalArgumentException JavaDoc {
62         switch (args.length) {
63             case 0:
64                 throw new IllegalArgumentException JavaDoc();
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 JavaDoc();
77                 }
78                 break;
79
80             default:
81                 throw new IllegalArgumentException JavaDoc();
82         }
83     }
84
85     private static void convert(String JavaDoc xmlPath)
86     throws IOException, Sync4jException {
87         System.out.println("converting xmlPath:" + xmlPath);
88
89         File xmlDir = new File(xmlPath);
90         List JavaDoc filesToConvert = getFilesInDir(xmlDir);
91         for (Iterator JavaDoc 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 JavaDoc ex){
109                 ex.printStackTrace();
110             }
111         }
112     }
113
114     private static void xmlToWbxml(File file)
115     throws IOException, Sync4jException {
116         String JavaDoc xml = IOTools.readFileString(file);
117
118         byte[] wbxml = WBXMLTools.toWBXML(xml);
119
120         String JavaDoc xmlFileName = file.getAbsolutePath();
121         String JavaDoc wbxmlFileName = xmlFileName.substring(0, xmlFileName.lastIndexOf("."));
122         wbxmlFileName = wbxmlFileName + ".wbxml";
123
124         //now write the wbxml to a file
125
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 JavaDoc xml = WBXMLTools.wbxmlToXml(wbxml);
134
135         String JavaDoc wbxmlFileName = file.getAbsolutePath();
136         String JavaDoc xmlFileName = wbxmlFileName.substring(0, wbxmlFileName.lastIndexOf("."));
137         xmlFileName = xmlFileName + ".xml";
138
139         //now write the xml to a file
140
System.out.println("Writing to: " + xmlFileName);
141         IOTools.writeFile(xml, xmlFileName);
142     }
143
144     private static List JavaDoc getFilesInDir(File dir){
145         List JavaDoc files = new ArrayList JavaDoc();
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 JavaDoc 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