KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > demo > FieldsHelper


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.demo;
20
21 import java.util.Date JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.text.ParseException JavaDoc;
25 import java.text.SimpleDateFormat JavaDoc;
26
27 import sync4j.syncclient.spdm.DMException;
28 import sync4j.syncclient.spdm.SimpleDeviceManager;
29 import sync4j.syncclient.spdm.ManagementNode;
30
31 import sync4j.foundation.pdi.utils.TimeUtils;
32
33
34 /**
35  * Fields helper methods.
36  *
37  * @author Fabio Maggi @ Funambol
38  * @author Marco Magistrali @ Funambol
39  * @version $Id: FieldsHelper.java,v 1.9 2005/07/14 16:03:24 nichele Exp $
40  */

41
42 public abstract class FieldsHelper
43 implements ConfigurationParameters {
44
45     //--------------------------------------------------------------- Constants
46

47     public static final String JavaDoc DATE_FORMAT = "dd/MM/yyyy" ;
48     public static final String JavaDoc DATE_FORMAT_AND_TIME = "dd/MM/yyyy HH:mm:ss" ;
49     public static final String JavaDoc UTC_DATE_FORMAT = "yyyyMMdd'T'HHmmss'Z'" ;
50     public static final String JavaDoc NO_UTC_DATE_FORMAT = "yyyyMMdd'T'HHmmss" ;
51     public static final String JavaDoc BIRTHDAY_FORMAT = "yyyy-MM-dd" ;
52
53     //--------------------------------------------------------------- Private data
54

55     private static Language ln = new Language() ;
56
57     //--------------------------------------------------------------- Public methods
58

59     //--------------------------------------------------------------- Protected methods
60

61     protected static boolean checkDate (String JavaDoc date, String JavaDoc dateFormat) {
62
63         StringTokenizer JavaDoc st = null ;
64         SimpleDateFormat JavaDoc formatter = null ;
65         Date JavaDoc dTemp = new Date JavaDoc();
66         int k = 0;
67
68         if (date != null && date.length() > 0) {
69
70             try {
71                 if (dateFormat.equals(DATE_FORMAT)) {
72                     formatter = new SimpleDateFormat JavaDoc (DATE_FORMAT);
73                 }
74                 else if (dateFormat.equals(DATE_FORMAT_AND_TIME)) {
75                     formatter = new SimpleDateFormat JavaDoc (DATE_FORMAT_AND_TIME);
76                 }
77                 formatter.setLenient (false );
78                 dTemp = formatter.parse (date );
79
80                 if (dateFormat.equals(DATE_FORMAT)) {
81                     st = new StringTokenizer JavaDoc(date, "/");
82                     while (st.hasMoreTokens()) {
83
84                         k = Integer.parseInt(st.nextToken());
85
86                         if (k >9999) {
87                             throw new NumberFormatException JavaDoc(ln.
88                                     getString ("error_number_format"));
89                         }
90
91                     }
92                 } else if (dateFormat.equals(DATE_FORMAT_AND_TIME)) {
93                     String JavaDoc sTemp = date.substring(0, 10);
94                     st = new StringTokenizer JavaDoc(sTemp, "/");
95                     while (st.hasMoreTokens()) {
96
97                         k = Integer.parseInt(st.nextToken());
98
99                         if (k >9999) {
100                             throw new NumberFormatException JavaDoc(ln.
101                                     getString ("error_number_format"));
102                         }
103
104                     }
105                     sTemp = date.substring(11);
106
107                     st = new StringTokenizer JavaDoc(sTemp, ":");
108                     int i = 0;
109                     while (st.hasMoreTokens()) {
110                         String JavaDoc s = st.nextToken();
111
112                         if (s.length() > 2) {
113                             throw new NumberFormatException JavaDoc(ln.
114                                     getString ("error_number_format"));
115                         }
116
117                         k = Integer.parseInt(s);
118
119                         // HH
120
if (i == 0 && (k < 0 || k > 24)) {
121                             throw new NumberFormatException JavaDoc(ln.
122                                     getString ("error_number_format"));
123                         }
124                         // MM
125
if (i == 1 && (k < 0 || k > 60)) {
126                             throw new NumberFormatException JavaDoc(ln.
127                                     getString ("error_number_format"));
128                         }
129                         // ss
130
if (i == 2 && (k < 0 || k > 60)) {
131                             throw new NumberFormatException JavaDoc(ln.
132                                     getString ("error_number_format"));
133                         }
134
135                         i++;
136                     }
137                 }
138             } catch (NullPointerException JavaDoc e ) {
139                return false;
140             } catch (ParseException JavaDoc e ) {
141                return false;
142             } catch (NumberFormatException JavaDoc e ) {
143                return false;
144             } catch (Exception JavaDoc e ) {
145                return false;
146             }
147
148         }
149
150         return true;
151
152     }
153
154     protected static String JavaDoc convertDateToUTC(String JavaDoc stringDate) {
155
156         ManagementNode rootNode = null ;
157         Hashtable JavaDoc parameters = null;
158
159         if (stringDate == null || stringDate.length() == 0) {
160             return "";
161         }
162
163         rootNode = SimpleDeviceManager.getDeviceManager().getManagementTree();
164
165         try {
166             parameters = rootNode.getNodeValues(MainWindow.DM_VALUE_PATH);
167         } catch (DMException e) {
168             e.printStackTrace();
169         }
170
171         return TimeUtils.localTimeToUTC (stringDate) ;
172
173     }
174
175     protected static String JavaDoc convertDateFromUTC (String JavaDoc stringDate) {
176
177         ManagementNode rootNode = null ;
178         Hashtable JavaDoc parameters = null ;
179
180         String JavaDoc adjustDate = null ;
181
182         if (stringDate == null || stringDate.length() == 0) {
183             return "";
184         }
185
186         return TimeUtils.UTCToLocalTime (stringDate ) ;
187
188     }
189
190     /**
191      * Convert birthday field value from yyyy-MM-dd format
192      * into display format dd/MM/yyyy
193      *
194      * @param stringDate the birthday value to convert
195      * @return String the birthday into correct format
196      */

197     protected static String JavaDoc convertBirthdayFrom(String JavaDoc stringDate) {
198         if (stringDate == null || stringDate.length() == 0) {
199             return "";
200         }
201
202         try {
203             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(BIRTHDAY_FORMAT);
204             formatter.setLenient(false);
205             Date JavaDoc date = formatter.parse(stringDate);
206
207             formatter.applyPattern(DATE_FORMAT);
208             return formatter.format(date);
209
210         } catch (Exception JavaDoc e) {
211             e.printStackTrace();
212         }
213         return "";
214     }
215
216     /**
217      * Convert birthday field value from dd/MM/yyyy format
218      * into yyyy-MM-dd format
219      *
220      * @param stringDate the birthday value to convert
221      * @return String the birthday into correct format
222      */

223     protected static String JavaDoc convertBirthdayTo(String JavaDoc stringDate)
224     throws Exception JavaDoc {
225
226         if (stringDate == null || stringDate.length() == 0) {
227             return "";
228         }
229
230         try {
231             SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(DATE_FORMAT);
232             formatter.setLenient(false);
233             Date JavaDoc date = formatter.parse(stringDate);
234
235             formatter.applyPattern(BIRTHDAY_FORMAT);
236             stringDate = formatter.format(date);
237
238         } catch (Exception JavaDoc e) {
239             e.printStackTrace();
240         }
241         return TimeUtils.normalizeToISO8601(stringDate, null);
242     }
243 }
244
Popular Tags