001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/admin/importexport/XMLUtil.java,v 1.7 2007/01/15 10:27:01 dungbtm Exp $
003: * $Author: dungbtm $
004: * $Revision: 1.7 $
005: * $Date: 2007/01/15 10:27:01 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Igor Manic
039: */
040: package com.mvnforum.admin.importexport;
041:
042: import java.sql.Timestamp;
043: import java.text.*;
044: import java.util.Locale;
045:
046: import net.myvietnam.mvncore.util.DateUtil;
047:
048: /**
049: * @author Igor Manic
050: * @version $Revision: 1.7 $, $Date: 2007/01/15 10:27:01 $
051: * <br/>
052: * <code>XMLUtil</code> todo Igor: enter description
053: *
054: */
055: public class XMLUtil {
056:
057: private XMLUtil() {
058: }
059:
060: /**
061: * Parses integer value out of a string.
062: * If the string is invalid, it throws <code>NumberFormatException</code>,
063: * so a calling method knows of that issue.<br/>
064: *
065: * @param value <code>String</code> to be parsed
066: * @param defaultValue Default <code>int</code> value if <code>value==null</code>
067: *
068: * @return Parsed <code>int</code> value.
069: * @throws NumberFormatException If <code>value</code> is not valid number.
070: *
071: */
072: public static int stringToIntDef(String value, int defaultValue)
073: throws NumberFormatException {
074: if (value == null)
075: return defaultValue;
076: else
077: return Integer.parseInt(value);
078: }
079:
080: //1=male, 0=female
081: public static int stringToGender(String s) {
082: if (s.equalsIgnoreCase("male") || s.equals("1")) {
083: return 1;
084: } else if (s.equalsIgnoreCase("female") || s.equals("0")) {
085: return 0;
086: } else
087: throw new IllegalArgumentException(
088: "Illegal gender string format.");
089: }
090:
091: public static int stringToGenderDef(String value, int defaultValue) {
092: if (value == null)
093: return defaultValue;
094: else
095: return stringToGender(value);
096: }
097:
098: public static String genderToString(int gender) {
099: if (gender == 0)
100: return "0"; //or, is it better to return "female" ?
101: else if (gender == 1)
102: return "1"; //"male"
103: else
104: throw new IllegalArgumentException("Illegal gender value.");
105: }
106:
107: //todo Igor: add utility methods for IPs and permissions
108:
109: public static boolean stringToBoolean(String s) {
110: if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes")
111: || s.equals("1")) {
112: return true;
113: } else if (s.equalsIgnoreCase("false")
114: || s.equalsIgnoreCase("no") || s.equals("0")) {
115: return false;
116: } else
117: throw new IllegalArgumentException(
118: "Illegal boolean format.");
119: }
120:
121: public static boolean stringToBooleanDef(String value,
122: boolean defaultValue) {
123: if (value == null)
124: return defaultValue;
125: else
126: return stringToBoolean(value);
127: }
128:
129: public static String booleanToString(boolean value) {
130: if (value)
131: return "true";
132: else
133: return "false";
134: }
135:
136: public static java.sql.Date stringToSqlDate(String s) {
137: /* I have to accept following formats:
138: * yyyy/MM/dd
139: * yyyy-MM-dd
140: * yyyyMMdd
141: * EEE MMM dd yyyy (e.g.: "Fri Jan 16 2002")
142: */
143: if (s == null)
144: throw new java.lang.IllegalArgumentException("null string");
145: s = s.trim();
146:
147: //SimpleDateFormat f1=new SimpleDateFormat("yyyy/MM/dd", Locale.US);
148: //SimpleDateFormat f2=new SimpleDateFormat("yyyy-MM-dd", Locale.US);
149: //SimpleDateFormat f3=new SimpleDateFormat("yyyyMMdd", Locale.US);
150: //SimpleDateFormat f4=new SimpleDateFormat("EEE MMM dd yyyy", Locale.US); //example: "Fri Jan 16 2002"
151: try {
152: //discover the format pattern to use for parsing
153: SimpleDateFormat f = /*f3*/new SimpleDateFormat(
154: "yyyyMMdd", Locale.US);
155: if (s.indexOf('/') > 0)
156: f = /*f1*/new SimpleDateFormat("yyyy/MM/dd", Locale.US);
157: else if (s.indexOf('-') > 0)
158: f = /*f2*/new SimpleDateFormat("yyyy-MM-dd", Locale.US);
159: else if (s.indexOf(' ') > 0)
160: f = /*f4*/new SimpleDateFormat("EEE MMM dd yyyy",
161: Locale.US);
162: java.util.Date d = f.parse(s);
163: return new java.sql.Date(d.getTime());
164: } catch (ParseException e) {
165: throw new java.lang.IllegalArgumentException(
166: "Invalid date format: \"" + s + "\"");
167: }
168: }
169:
170: public static java.sql.Date stringToSqlDateDef(String value,
171: java.sql.Date defaultValue) {
172: if (value == null)
173: return defaultValue;
174: else
175: return stringToSqlDate(value);
176: }
177:
178: public static java.sql.Date stringToSqlDateDefNow(String value) {
179: Timestamp now = DateUtil.getCurrentGMTTimestamp();
180: return stringToSqlDateDef(value, new java.sql.Date(now
181: .getTime()));
182: }
183:
184: public static java.sql.Date stringToSqlDateDefNull(String value) {
185: /* todo Igor: important: must change this so it doesn't return now()
186: * but null, as it should. For now, I must not return null, because
187: * db.*WebHelper classes don't handle null dates correctly.
188: * They should check if aDate is null and if it is, don't send null
189: * to SQL engine (since database schema states it must be non-null),
190: * but send empty string in the query, so SQL engine will do the rest.
191: if (value==null) return null;
192: else return stringToSqlDate(value);
193: */
194: return stringToSqlDateDefNow(value);
195: }
196:
197: public static String sqlDateToString(java.sql.Date value) {
198: DateFormat frm = new SimpleDateFormat("yyyy/MM/dd", Locale.US);
199: return frm.format(value);
200: }
201:
202: public static String sqlDateToStringDefNow(java.sql.Date value) {
203: Timestamp now = DateUtil.getCurrentGMTTimestamp();
204: if (value == null)
205: return sqlDateToString(new java.sql.Date(now.getTime()));
206: else
207: return sqlDateToString(value);
208: }
209:
210: public static String sqlDateToStringDefEmpty(java.sql.Date value) {
211: //todo Igor: should I return "0000-00-00 00:00:00" instead of ""? same for Timestamp
212: if (value == null)
213: return "";
214: else
215: return sqlDateToString(value);
216: }
217:
218: public static java.sql.Timestamp stringToSqlTimestamp(String s) {
219: /* I have to accept following formats:
220: * yyyy/MM/dd HH:mm:ss.nn
221: * yyyy-MM-dd HH:mm:ss.nn
222: * yyyyMMddHHmmssnn
223: * EEE MMM dd HH:mm:ss z yyyy (e.g.: "Fri Jan 16 18:48:25 CEST 2002")
224: * In first three formats, last nn are hundreths and are optional
225: */
226: if (s == null)
227: throw new java.lang.IllegalArgumentException("null string");
228: s = s.trim();
229:
230: //SimpleDateFormat f1=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US); //may have extra ".nn" on the end (hundreths)
231: //SimpleDateFormat f2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); //may have extra ".nn" on the end (hundreths)
232: //SimpleDateFormat f3=new SimpleDateFormat("yyyyMMddHHmmss", Locale.US); //may have extra "nn" on the end (hundreths)
233: //SimpleDateFormat f4=new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.US); //example: "Fri Jan 16 18:48:25 CEST 2002"
234: try {
235: //discover the format pattern to use for parsing
236: SimpleDateFormat f = /*f3*/new SimpleDateFormat(
237: "yyyyMMddHHmmss", Locale.US);
238: if (s.indexOf('/') > 0) {
239: s = s.substring(0, 19); //cut hundreths if they exist
240: f = /*f1*/new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",
241: Locale.US);
242: } else if (s.indexOf('-') > 0) {
243: s = s.substring(0, 19); //cut hundreths if they exist
244: f = /*f2*/new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
245: Locale.US);
246: } else if (s.indexOf(' ') > 0) {
247: f = /*f4*/new SimpleDateFormat(
248: "EEE MMM dd HH:mm:ss z yyyy", Locale.US);
249: } else {
250: s = s.substring(0, 14); //cut hundreths if they exist
251: f = /*f3*/new SimpleDateFormat("yyyyMMddHHmmss",
252: Locale.US);
253: }
254: java.util.Date d = f.parse(s);
255: return new Timestamp(d.getTime());
256: } catch (StringIndexOutOfBoundsException e) {
257: throw new java.lang.IllegalArgumentException(
258: "Invalid timestamp format: \"" + s + "\"");
259: } catch (ParseException e) {
260: throw new java.lang.IllegalArgumentException(
261: "Invalid timestamp format: \"" + s + "\"");
262: }
263: }
264:
265: public static java.sql.Timestamp stringToSqlTimestampDef(
266: String value, java.sql.Timestamp defaultValue) {
267: if (value == null)
268: return defaultValue;
269: else
270: return stringToSqlTimestamp(value);
271: }
272:
273: public static java.sql.Timestamp stringToSqlTimestampDefNow(
274: String value) {
275: Timestamp now = DateUtil.getCurrentGMTTimestamp();
276: return stringToSqlTimestampDef(value, now);
277: }
278:
279: public static java.sql.Timestamp stringToSqlTimestampDefNull(
280: String value) {
281: /* todo Igor: important: must change this so it doesn't return now()
282: * but null, as it should. For now, I must not return null, because
283: * db.*WebHelper classes don't handle null dates correctly.
284: * They should check if aTimestamp is null and if it is, don't send null
285: * to SQL engine (since database schema states it must be non-null),
286: * but send empty string in the query, so SQL engine will do the rest.
287: if (value==null) return null;
288: else return stringToSqlTimestamp(value);
289: */
290: return stringToSqlTimestampDefNow(value);
291: }
292:
293: public static String sqlTimestampToString(java.sql.Timestamp value) {
294: DateFormat frm = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss",
295: Locale.US);
296: return frm.format(value);
297: }
298:
299: public static String sqlTimestampToStringDefNow(
300: java.sql.Timestamp value) {
301: Timestamp now = DateUtil.getCurrentGMTTimestamp();
302: if (value == null)
303: return sqlTimestampToString(now);
304: else
305: return sqlTimestampToString(value);
306: }
307:
308: public static String sqlTimestampToStringDefEmpty(
309: java.sql.Timestamp value) {
310: if (value == null)
311: return "";
312: else
313: return sqlTimestampToString(value);
314: }
315:
316: }
|