KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > sps > pocketpc > util > CEDatabaseImport


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 package sync4j.syncclient.sps.pocketpc.util;
20
21 import java.io.*;
22 import java.util.StringTokenizer JavaDoc;
23
24 import sync4j.syncclient.sps.pocketpc.RapiDB;
25
26 /**
27  * This class imports a Windows CE database from a CSV file. Volume and database
28  * names are passed on the command line. Optionally, the database is deleted and
29  * recreated. The comma separated values are read from the standard input stream.
30  *
31  * @author Stefano Fornari
32  * @version $Id: CEDatabaseImport.java,v 1.2 2005/01/19 11:18:37 fabius Exp $
33  */

34 public class CEDatabaseImport {
35     
36         
37     /**
38      * Syntax:
39      * sync4j.syncclient.sps.common.util.CEDatabaseImport [-create] volume database key_field status_field
40      * @param args the command line arguments
41      */

42     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
43         boolean create = false;
44         int argc = 0 ;
45         int keyField = 1 ;
46         int statusField = 2 ;
47         
48         if (args.length < 4) {
49             syntax();
50             System.exit(-1);
51         }
52         
53         if ((args.length == 5) && ("-create".equals(args[0]))) {
54             create = true;
55             argc = 1;
56         }
57         
58         RapiDB db = new RapiDB(args[argc++], args[argc++]);
59         
60         keyField = Integer.parseInt(args[argc++]);
61         statusField = Integer.parseInt(args[argc++]);
62         
63         if (create) {
64             if (db.openDB(keyField, statusField, keyField) == true) {
65                 db.deleteDB();
66             }
67             
68             if (!db.createDB(keyField, statusField)) {
69                 System.out.println("Error creating the database!");
70                 System.exit(-1);
71             }
72         }
73         
74         if (!db.openDB(keyField, statusField, keyField)) {
75             System.out.println("Error creating the database!");
76             System.exit(-1);
77         }
78                 
79         BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
80         
81         int i=0;
82         Object JavaDoc[] data;
83         String JavaDoc key;
84         String JavaDoc line;
85         StringTokenizer JavaDoc stk;
86         while((line = reader.readLine()) != null) {
87             if (line.startsWith("#") || (line.trim().length() == 0)) {
88                 continue;
89             }
90             
91             System.out.println("processing.... " + line);
92             
93             stk = new StringTokenizer JavaDoc(line, "|");
94             
95             //
96
// First token is the id
97
//
98
key = stk.nextToken();
99             
100             data = new Object JavaDoc[stk.countTokens()];
101             i = 0;
102             while (stk.hasMoreTokens()) {
103                 data[i++] = stk.nextToken();
104             }
105             
106             db.storeRecord(key, data);
107         }
108         
109         db.closeDB();
110         reader.close();
111     }
112     
113     private static void syntax() {
114         System.out.println("Wrong syntax.");
115         System.out.println("Syntax:");
116         System.out.println("sync4j.syncclient.sps.common.util.CEDatabaseImport [-create] volume database");
117     }
118     
119 }
Popular Tags