KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mvnforum > admin > importexport > XMLUtil


1 /*
2  * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/importexport/XMLUtil.java,v 1.6 2006/04/14 17:36:29 minhnn Exp $
3  * $Author: minhnn $
4  * $Revision: 1.6 $
5  * $Date: 2006/04/14 17:36:29 $
6  *
7  * ====================================================================
8  *
9  * Copyright (C) 2002-2006 by MyVietnam.net
10  *
11  * All copyright notices regarding mvnForum MUST remain
12  * intact in the scripts and in the outputted HTML.
13  * The "powered by" text/logo with a link back to
14  * http://www.mvnForum.com and http://www.MyVietnam.net in
15  * the footer of the pages MUST remain visible when the pages
16  * are viewed on the internet or intranet.
17  *
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 2 of the License, or
21  * any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31  *
32  * Support can be obtained from support forums at:
33  * http://www.mvnForum.com/mvnforum/index
34  *
35  * Correspondence and Marketing Questions can be sent to:
36  * info at MyVietnam net
37  *
38  * @author: Igor Manic
39  */

40 package com.mvnforum.admin.importexport;
41
42 import java.sql.Timestamp JavaDoc;
43 import java.text.*;
44 import java.util.Locale JavaDoc;
45
46 import net.myvietnam.mvncore.util.DateUtil;
47
48 /**
49  * @author Igor Manic
50  * @version $Revision: 1.6 $, $Date: 2006/04/14 17:36:29 $
51  * <br/>
52  * <code>XMLUtil</code> todo Igor: enter description
53  *
54  */

55 public class XMLUtil {
56
57     private XMLUtil() {
58     }
59
60     /**
61      * Parses integer value out of a string.
62      * If the string is invalid, it throws <code>NumberFormatException</code>,
63      * so a calling method knows of that issue.<br/>
64      *
65      * @param value <code>String</code> to be parsed
66      * @param defaultValue Default <code>int</code> value if <code>value==null</code>
67      *
68      * @return Parsed <code>int</code> value.
69      * @throws NumberFormatException If <code>value</code> is not valid number.
70      *
71      */

72     public static int stringToIntDef(String JavaDoc value, int defaultValue)
73     throws NumberFormatException JavaDoc {
74         if (value==null) return defaultValue;
75         else return Integer.parseInt(value);
76     }
77
78     //1=male, 0=female
79
public static int stringToGender(String JavaDoc s) {
80         if (s.equalsIgnoreCase("male") || s.equals("1")) {
81             return 1;
82         } else if (s.equalsIgnoreCase("female") || s.equals("0")) {
83             return 0;
84         } else throw new IllegalArgumentException JavaDoc("Illegal gender string format.");
85     }
86
87     public static int stringToGenderDef(String JavaDoc value, int defaultValue) {
88         if (value==null) return defaultValue;
89         else return stringToGender(value);
90     }
91
92     public static String JavaDoc genderToString(int gender) {
93         if (gender==0) return "0"; //or, is it better to return "female" ?
94
else if (gender==1) return "1"; //"male"
95
else throw new IllegalArgumentException JavaDoc("Illegal gender value.");
96     }
97
98 //todo Igor: add utility methods for IPs and permissions
99

100     public static boolean stringToBoolean(String JavaDoc s) {
101         if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes") || s.equals("1")) {
102             return true;
103         } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("no") || s.equals("0")) {
104             return false;
105         } else throw new IllegalArgumentException JavaDoc("Illegal boolean format.");
106     }
107
108     public static boolean stringToBooleanDef(String JavaDoc value, boolean defaultValue) {
109         if (value==null) return defaultValue;
110         else return stringToBoolean(value);
111     }
112
113     public static String JavaDoc booleanToString(boolean value) {
114         if (value) return "true";
115         else return "false";
116     }
117
118     public static java.sql.Date JavaDoc stringToSqlDate(String JavaDoc s) {
119         /* I have to accept following formats:
120          * yyyy/MM/dd
121          * yyyy-MM-dd
122          * yyyyMMdd
123          * EEE MMM dd yyyy (e.g.: "Fri Jan 16 2002")
124          */

125         if (s==null) throw new java.lang.IllegalArgumentException JavaDoc("null string");
126         s=s.trim();
127
128         //SimpleDateFormat f1=new SimpleDateFormat("yyyy/MM/dd", Locale.US);
129
//SimpleDateFormat f2=new SimpleDateFormat("yyyy-MM-dd", Locale.US);
130
//SimpleDateFormat f3=new SimpleDateFormat("yyyyMMdd", Locale.US);
131
//SimpleDateFormat f4=new SimpleDateFormat("EEE MMM dd yyyy", Locale.US); //example: "Fri Jan 16 2002"
132
try {
133             //discover the format pattern to use for parsing
134
SimpleDateFormat f=/*f3*/new SimpleDateFormat("yyyyMMdd", Locale.US);
135             if (s.indexOf('/')>0) f=/*f1*/new SimpleDateFormat("yyyy/MM/dd", Locale.US);
136             else if (s.indexOf('-')>0) f=/*f2*/new SimpleDateFormat("yyyy-MM-dd", Locale.US);
137             else if (s.indexOf(' ')>0) f=/*f4*/new SimpleDateFormat("EEE MMM dd yyyy", Locale.US);
138             java.util.Date JavaDoc d=f.parse(s);
139             return new java.sql.Date JavaDoc(d.getTime());
140         } catch (ParseException e) {
141             throw new java.lang.IllegalArgumentException JavaDoc("Invalid date format: \""+s+"\"");
142         }
143     }
144
145     public static java.sql.Date JavaDoc stringToSqlDateDef(String JavaDoc value, java.sql.Date JavaDoc defaultValue) {
146         if (value==null) return defaultValue;
147         else return stringToSqlDate(value);
148     }
149
150     public static java.sql.Date JavaDoc stringToSqlDateDefNow(String JavaDoc value) {
151         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
152         return stringToSqlDateDef(value, new java.sql.Date JavaDoc(now.getTime()));
153     }
154
155     public static java.sql.Date JavaDoc stringToSqlDateDefNull(String JavaDoc value) {
156         /* todo Igor: important: must change this so it doesn't return now()
157          * but null, as it should. For now, I must not return null, because
158          * db.*WebHelper classes don't handle null dates correctly.
159          * They should check if aDate is null and if it is, don't send null
160          * to SQL engine (since database schema states it must be non-null),
161          * but send empty string in the query, so SQL engine will do the rest.
162         if (value==null) return null;
163         else return stringToSqlDate(value);
164          */

165         return stringToSqlDateDefNow(value);
166     }
167
168     public static String JavaDoc sqlDateToString(java.sql.Date JavaDoc value) {
169         DateFormat frm = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
170         return frm.format(value);
171     }
172
173     public static String JavaDoc sqlDateToStringDefNow(java.sql.Date JavaDoc value) {
174         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
175         if (value==null) return sqlDateToString(new java.sql.Date JavaDoc(now.getTime()));
176         else return sqlDateToString(value);
177     }
178
179     public static String JavaDoc sqlDateToStringDefEmpty(java.sql.Date JavaDoc value) {
180         //todo Igor: should I return "0000-00-00 00:00:00" instead of ""? same for Timestamp
181
if (value==null) return "";
182         else return sqlDateToString(value);
183     }
184
185     public static java.sql.Timestamp JavaDoc stringToSqlTimestamp(String JavaDoc s) {
186         /* I have to accept following formats:
187          * yyyy/MM/dd HH:mm:ss.nn
188          * yyyy-MM-dd HH:mm:ss.nn
189          * yyyyMMddHHmmssnn
190          * EEE MMM dd HH:mm:ss z yyyy (e.g.: "Fri Jan 16 18:48:25 CEST 2002")
191          * In first three formats, last nn are hundreths and are optional
192          */

193         if (s==null) throw new java.lang.IllegalArgumentException JavaDoc("null string");
194         s=s.trim();
195
196         //SimpleDateFormat f1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US); //may have extra ".nn" on the end (hundreths)
197
//SimpleDateFormat f2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); //may have extra ".nn" on the end (hundreths)
198
//SimpleDateFormat f3=new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); //may have extra "nn" on the end (hundreths)
199
//SimpleDateFormat f4=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); //example: "Fri Jan 16 18:48:25 CEST 2002"
200
try {
201             //discover the format pattern to use for parsing
202
SimpleDateFormat f=/*f3*/new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
203             if (s.indexOf('/')>0) {
204                 s=s.substring(0, 19); //cut hundreths if they exist
205
f=/*f1*/new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
206             } else if (s.indexOf('-')>0) {
207                 s=s.substring(0, 19); //cut hundreths if they exist
208
f=/*f2*/new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
209             } else if (s.indexOf(' ')>0) {
210                 f=/*f4*/new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US);
211             } else {
212                 s=s.substring(0, 14); //cut hundreths if they exist
213
f=/*f3*/new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);
214             }
215             java.util.Date JavaDoc d=f.parse(s);
216             return new Timestamp JavaDoc(d.getTime());
217         } catch (StringIndexOutOfBoundsException JavaDoc e) {
218             throw new java.lang.IllegalArgumentException JavaDoc("Invalid timestamp format: \""+s+"\"");
219         } catch (ParseException e) {
220             throw new java.lang.IllegalArgumentException JavaDoc("Invalid timestamp format: \""+s+"\"");
221         }
222     }
223
224     public static java.sql.Timestamp JavaDoc stringToSqlTimestampDef(String JavaDoc value, java.sql.Timestamp JavaDoc defaultValue) {
225         if (value==null) return defaultValue;
226         else return stringToSqlTimestamp(value);
227     }
228
229     public static java.sql.Timestamp JavaDoc stringToSqlTimestampDefNow(String JavaDoc value) {
230         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
231         return stringToSqlTimestampDef(value, now);
232     }
233
234     public static java.sql.Timestamp JavaDoc stringToSqlTimestampDefNull(String JavaDoc value) {
235         /* todo Igor: important: must change this so it doesn't return now()
236          * but null, as it should. For now, I must not return null, because
237          * db.*WebHelper classes don't handle null dates correctly.
238          * They should check if aTimestamp is null and if it is, don't send null
239          * to SQL engine (since database schema states it must be non-null),
240          * but send empty string in the query, so SQL engine will do the rest.
241         if (value==null) return null;
242         else return stringToSqlTimestamp(value);
243          */

244         return stringToSqlTimestampDefNow(value);
245     }
246
247     public static String JavaDoc sqlTimestampToString(java.sql.Timestamp JavaDoc value) {
248         DateFormat frm = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US);
249         return frm.format(value);
250     }
251
252     public static String JavaDoc sqlTimestampToStringDefNow(java.sql.Timestamp JavaDoc value) {
253         Timestamp JavaDoc now = DateUtil.getCurrentGMTTimestamp();
254         if (value==null) return sqlTimestampToString(now);
255         else return sqlTimestampToString(value);
256     }
257
258     public static String JavaDoc sqlTimestampToStringDefEmpty(java.sql.Timestamp JavaDoc value) {
259         if (value==null) return "";
260         else return sqlTimestampToString(value);
261     }
262
263
264 }
265
Popular Tags