KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > smile > stored > utils


1 package smile.stored;
2
3 import java.util.*;
4 import java.io.*;
5
6 import org.cofax.*;
7
8 /**
9  * utils is a utilities class. Copyright 2002 Smile Les motoristes Internet
10  * http://www.smile.fr/ Contact cofax@smile.fr for further information
11  *
12  * @author Smile Les motoristes Internet
13  * @created March 22, 2002
14  */

15 public class utils {
16
17     /**
18      * Constructor for the utils object
19      */

20     public utils() {
21     }
22
23     /**
24      * Description of the Method
25      *
26      * @param hm
27      * Description of the Parameter
28      * @return Description of the Return Value
29      */

30     public static HashMap HashMapToUpperCase(HashMap hm) {
31         HashMap v_hm = new HashMap(hm.size());
32         Iterator v_iterator = hm.keySet().iterator();
33         while (v_iterator.hasNext()) {
34             String JavaDoc tmpStr = (String JavaDoc) v_iterator.next();
35             v_hm.put(tmpStr.toUpperCase(), hm.get(tmpStr));
36         }
37         return v_hm;
38     }
39
40     /**
41      * Gets the string attribute of the utils class
42      *
43      * @param hash
44      * Description of the Parameter
45      * @param label
46      * Description of the Parameter
47      * @param defaultVal
48      * Description of the Parameter
49      * @return The string value
50      */

51     public static String JavaDoc getString(HashMap hash, String JavaDoc label, String JavaDoc defaultVal) {
52         String JavaDoc retVal = defaultVal;
53         try {
54             retVal = (String JavaDoc) hash.get(label);
55             // FX : test if retval is null before
56
if (retVal != null) {
57                 retVal = replaceString(retVal, "'", "''");
58                 retVal = replaceString(retVal, "\\", "\\\\");
59             }
60             // end of update
61

62         } catch (Exception JavaDoc e) {
63             // FX : updating the log
64
log("Error while getting " + label + " from the HashMap : " + e.toString());
65         }
66
67         if (retVal == null) {
68             retVal = defaultVal;
69         }
70
71         return retVal;
72     }
73
74     /**
75      * Logs to a file or to System.out - basic function is to concentrate error
76      * checking.
77      *
78      * @param input
79      * Description of the Parameter
80      */

81     protected static void log(String JavaDoc input) {
82         input = "smile.stored.Utils - " + new Date().toString() + " - " + input;
83         if (DataStore.dataLog.equals("1")) {
84             if (!DataStore.dataLogLocation.equals("")) {
85                 try {
86                     if (DataStore.dataLogMaxSize > 0) {
87                         File file = new File(DataStore.dataLogLocation);
88                         long fileSize = file.length();
89                         if (fileSize > DataStore.dataLogMaxSize) {
90                             file.delete();
91                         }
92                     }
93                     PrintWriter fileOutputStream = new PrintWriter(new FileWriter(DataStore.dataLogLocation, true));
94                     fileOutputStream.println(input);
95                     fileOutputStream.close();
96                 } catch (IOException e) {
97                     System.out.println(e.toString());
98                 }
99             } else {
100                 System.out.println("ODS log: " + input);
101             }
102         }
103     }
104
105     // --------------------------------------------------------------------------79
106
/**
107      * Replace the occurences of match in source by replace.
108      *
109      * @param source ;
110      * the String source
111      * @param match :
112      * the subString to be replaced
113      * @param replace :
114      * the new subString which replace match
115      * @return Description of the Return Value
116      */

117     public static String JavaDoc replaceString(String JavaDoc source, String JavaDoc match, String JavaDoc replace) {
118
119         // Si match est "", boucle infinie..
120
if (match.equals("")) {
121             return source;
122         }
123
124         int index = -1;
125         int matchLength = match.length();
126         int offset = matchLength - replace.length();
127
128         StringBuffer JavaDoc result = new StringBuffer JavaDoc(source);
129         Vector indexes = new Vector();
130
131         // On recupere les indices de toutes les occurences :
132
while ((index = source.indexOf(match, index)) != -1) {
133             indexes.addElement(new Integer JavaDoc(index));
134             index += matchLength;
135         }
136
137         // On remplace toutes les occurences :
138
for (int i = 0; i < indexes.size(); i++) {
139             int indexStart = ((Integer JavaDoc) indexes.elementAt(i)).intValue() - offset * i;
140             int indexEnd = indexStart + matchLength;
141             result.replace(indexStart, indexEnd, replace);
142         }
143
144         return result.toString();
145     }
146
147 }
148
Popular Tags