KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > CofaxUtil


1 /*
2  * CofaxUtil is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/CofaxUtil.java,v 1.7.2.1 2006/12/11 16:28:44 fxrobin Exp $
21  */

22
23 package org.cofax;
24
25 import java.util.*;
26 import java.text.*;
27
28 /**
29  * The general utilities class of Cofax. Routines used across the entire
30  * framework are stored here.
31  *
32  * @author Rajiv Pant
33  * @author Karl Martino
34  * @author Sam Cohen
35  * @created April 19, 2002
36  */

37 public final class CofaxUtil {
38
39     /**
40      * Constant used for returning the number of milliseconds per minute
41      */

42     public final static int MILLISECONDS_PER_MINUTE = 60 * 1000;
43
44     /**
45      * Constant used for returning the number of milliseconds per second
46      */

47     public final static int MILLISECONDS_PER_SECOND = 1000;
48
49     /**
50      * Constant used to return a string representing a newline character.
51      */

52     public final static String JavaDoc NEW_LINE = "\r\n";
53
54     /**
55      * Constant used to return a string representing a html line break.
56      */

57     public final static String JavaDoc XHTML_NEW_LINE = "<br />\r\n";
58
59     // This program deals with certain pieces of data in 16k chunks.
60
// This is a good "common" size for efficiency in such an application.
61
/**
62      * Description of the Field
63      */

64     public final static int COMMON_DATA_SIZE = 16384;
65
66     /**
67      * Tests to see if a string is composed only of digits. Note: will return
68      * false for "-" and "+".
69      *
70      * @param stringToParse
71      * Description of the Parameter
72      * @return The digits value
73      */

74     public static boolean isDigits(String JavaDoc stringToParse) {
75
76         char[] charsToTest = stringToParse.toCharArray();
77         boolean isDigits = true;
78         for (int x = 0; x < charsToTest.length; x++) {
79
80             if (!Character.isDigit(charsToTest[x])) {
81                 isDigits = false;
82                 break;
83             }
84         }
85
86         return isDigits;
87     }
88
89     /**
90      * Splits a string and returns a Collection.
91      *
92      * @param separator
93      * A token to parse on.
94      * @param str
95      * The original string.
96      * @return The list returned.
97      */

98     public static List split(String JavaDoc separator, String JavaDoc str) {
99
100         ArrayList list = new ArrayList();
101         String JavaDoc substr;
102
103         int cursor = 0;
104         int sepratorPosition;
105
106         if (!str.substring(str.length() - separator.length()).equals(separator)) {
107             str = str + separator;
108         }
109
110         while ((sepratorPosition = str.indexOf(separator, cursor)) > -1) {
111             substr = str.substring(cursor, sepratorPosition);
112             cursor = sepratorPosition + 1;
113             list.add(substr);
114         }
115         return list;
116     }
117
118     /**
119      * Encodes a string into URL format.
120      *
121      * @param url
122      * The URL to be encoded
123      * @return The encoded URL.
124      */

125     public final static String JavaDoc encodeUrl(String JavaDoc url) {
126
127         return replace(url, "?", "%3F");
128     }
129
130     /**
131      * Replaces all occurances of the specified character with the specified
132      * sub-string to replace with. This function is written using a char[] array
133      * and StringBuffer for efficiency.
134      *
135      * @param originalText
136      * The original string.
137      * @param charToFind
138      * The character to replace.
139      * @param subStringToReplaceWith
140      * The string to replace the character with.
141      * @return The new string.
142      */

143     public static String JavaDoc replace(String JavaDoc originalText, char charToFind, String JavaDoc subStringToReplaceWith) {
144
145         char[] originalTextAsArray = originalText.toCharArray();
146         StringBuffer JavaDoc newText = new StringBuffer JavaDoc(originalTextAsArray.length);
147
148         for (int i = 0; i < originalTextAsArray.length; i++) {
149             if (originalTextAsArray[i] == charToFind) {
150                 newText.append(subStringToReplaceWith);
151             } else {
152                 newText.append(originalTextAsArray[i]);
153             }
154         }
155
156         return newText.toString();
157     }
158
159     // end replace(String, char, String)
160

161     /**
162      * Replaces all occurances of the specified sub-string to find with the
163      * specified sub-string to replace with.
164      *
165      * @param originalText
166      * The original string.
167      * @param subStringToFind
168      * The string to find and replace.
169      * @param subStringToReplaceWith
170      * The string to replace with.
171      * @return The modified original string.
172      */

173     public static String JavaDoc replace(String JavaDoc originalText, String JavaDoc subStringToFind, String JavaDoc subStringToReplaceWith) {
174         int s = 0;
175         int e = 0;
176
177         StringBuffer JavaDoc newText = new StringBuffer JavaDoc();
178
179         while ((e = originalText.indexOf(subStringToFind, s)) >= 0) {
180
181             newText.append(originalText.substring(s, e));
182             newText.append(subStringToReplaceWith);
183             s = e + subStringToFind.length();
184
185         }
186
187         newText.append(originalText.substring(s));
188         return newText.toString();
189     }
190
191     // end replace(String, String, String)
192

193     /**
194      * Escapes out (puts a backslash in front of) perl regular expression chars
195      * contained in the passed in string. This version uses the replace(String,
196      * char, String) function defined in this class. It does not use a Perl5Util
197      * regex object to do its own work. The replace(String, char, String)
198      * function uses a character array StringBuffer object.
199      *
200      * @param s
201      * The string to modify
202      * @return The modified string
203      */

204     public static String JavaDoc quotemetaLight(String JavaDoc s) {
205
206         s = replace(s, '\\', "\\\\");
207         s = replace(s, '/', "\\/");
208         s = replace(s, '$', "&#36;");
209
210         return s;
211     }
212
213     // end quotemetaLight(String)
214

215     /**
216      * Returns a date, as a string, in the desired format. Note that the string
217      * passed in must already be in "yyyy/mm/dd" format.
218      *
219      * @param style
220      * The format of the date desired.
221      * @param pubDate
222      * The date string being passed in.
223      * @return The formatted date.
224      */

225     public static String JavaDoc getDateFormat(String JavaDoc style, String JavaDoc pubDate) {
226
227         int year = 0;
228         int month = 0;
229         int day = 0;
230
231         Calendar cal;
232
233         // If pubDate is not blank, then we use it. If it is blank,
234
// we get the current date from the system.
235

236         if (!pubDate.equals("")) {
237
238             try {
239                 Iterator parts = split("/", pubDate).iterator();
240                 year = Integer.parseInt((String JavaDoc) parts.next());
241                 month = Integer.parseInt((String JavaDoc) parts.next());
242                 day = Integer.parseInt((String JavaDoc) parts.next());
243
244                 month--;
245
246                 cal = new GregorianCalendar(year, month, day);
247
248             } catch (Exception JavaDoc e) {
249                 cal = new GregorianCalendar();
250             }
251
252         } else {
253             cal = new GregorianCalendar();
254         }
255
256         Locale currentLocale = new Locale("en", "US");
257
258         SimpleDateFormat formatter = new SimpleDateFormat(style);
259         return formatter.format(cal.getTime());
260     }
261
262     /**
263      * Returns a date as a string, in the desired format.
264      *
265      * @param style
266      * The format desired.
267      * @param dateToFormat
268      * The date object to be formatted.
269      * @return The dateFormat value
270      */

271     public static String JavaDoc getDateFormat(String JavaDoc style, Date dateToFormat) {
272
273         Locale currentLocale = new Locale("en", "US");
274         SimpleDateFormat formatter = new SimpleDateFormat(style);
275         return formatter.format(dateToFormat);
276     }
277
278     /**
279      * Returns a member of a hashtable as a string. It the value does not exist,
280      * an empty ("") string will be returned.
281      *
282      * @param fields
283      * The HashMaps collection to search
284      * @param fieldName
285      * The key of the value desired.
286      * @return The string value returned. A blank ("") string is returned when
287      * the key is not located.
288      */

289     public static String JavaDoc getString(HashMap fields, String JavaDoc fieldName) {
290
291         String JavaDoc value = null;
292
293         try {
294             value = fields.get(fieldName).toString();
295         } catch (Exception JavaDoc e) {
296             value = (String JavaDoc) fields.get(fieldName);
297         }
298
299         if (value == null) {
300             return "";
301         } else {
302             return value;
303         }
304
305     }
306
307     /**
308      * Returns a member of a hashtable as a string. If a value does not exist,
309      * the value "0" will be reurned.
310      *
311      * @param fields
312      * Description of the Parameter
313      * @param fieldName
314      * Description of the Parameter
315      * @return The integerAsString value
316      */

317     public static String JavaDoc getIntegerAsString(HashMap fields, String JavaDoc fieldName) {
318
319         String JavaDoc value = "0";
320
321         if (fields.containsKey(fieldName)) {
322             try {
323                 value = fields.get(fieldName).toString();
324             } catch (Exception JavaDoc e) {
325                 value = (String JavaDoc) fields.get(fieldName);
326             }
327         }
328
329         return value;
330
331     }
332
333     /**
334      * Returns a date, as a string, in a format desirable for MySQL:
335      * "YYYY-MM-DD" Author: Smile.
336      *
337      * @param pstrValeur
338      * The passed in string
339      * @return The date, as string, in the desired format.
340      * @exception ParseException
341      * Description of the Exception
342      */

343     public static String JavaDoc dateToMySql(String JavaDoc pstrValeur) throws ParseException {
344
345         Date date1 = null;
346
347         SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.FRENCH);
348         String JavaDoc strDateToNum = pstrValeur;
349
350         try {
351
352             // on convertit la chaine en date
353
date1 = sdf.parse(strDateToNum);
354             // on change le format de la date
355
sdf.applyPattern("yyyy-MM-dd");
356             strDateToNum = sdf.format(date1);
357
358         } catch (Exception JavaDoc e) {
359             // do nothing...
360
} finally {
361             // System.err.println("SmileDate.dateToMySQL : " + pstrValeur + " ->
362
// " + strDateToNum);
363

364         }
365         return strDateToNum;
366
367     }
368
369 }
370
Popular Tags