KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > i18njdbc > Utils


1 /**
2     Copyright (C) 2002-2003 Together
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 */

19
20 package org.webdocwf.util.i18njdbc;
21
22 import java.io.*;
23 import java.util.*;
24
25 /**
26  * Utility methods for i18n jdbc.
27  *
28  * @author Zoran Milakovic
29  * @author Zeljko Kovacevic
30  */

31
32 public class Utils {
33
34   //keywords escape
35
public static final String JavaDoc[] keywords = { "AND", "WHERE", "FROM", "SET", "IS", "CREATE TABLE", "INT0", "INSERT", "VALUES" };
36   public static final String JavaDoc keywordEscape = "~#~KEYWORD~#~";
37   /**
38    * Replace all occurence of forReplace with replaceWith in input string.
39    * @param input represents input string
40    * @param forReplace represents substring for replace
41    * @param replaceWith represents replaced string value
42    * @return new string with replaced values
43    */

44   public static String JavaDoc replaceAll(
45     String JavaDoc input,
46     String JavaDoc forReplace,
47     String JavaDoc replaceWith) {
48     if( input == null )
49       return null;
50     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
51     boolean hasMore = true;
52     while (hasMore) {
53       int start = input.indexOf(forReplace);
54       int end = start + forReplace.length();
55       if (start != -1) {
56         result.append(input.substring(0, start) + replaceWith);
57         input = input.substring(end);
58       }
59       else {
60         hasMore = false;
61         result.append(input);
62       }
63     }
64     if (result.toString().equals(""))
65       return input; //nothing is changed
66
else
67       return result.toString();
68   }
69
70   /**
71    * This method transform binary object to string object
72    * @param b is array of bytes which represents binary object
73    * @return string representation of binary object
74    */

75   public static String JavaDoc bytesToHexString(byte[] b) {
76     String JavaDoc hexString = null;
77     try {
78       if (b != null) {
79         ByteArrayInputStream is = new ByteArrayInputStream(b);
80         hexString = streamToHexString(is);
81         return hexString;
82       }
83       else {
84         return null;
85       }
86     }
87     catch (Exception JavaDoc e) {
88     }
89     return hexString;
90   }
91
92   public static String JavaDoc handleBinaryString(String JavaDoc binaryString, List binaryStreamObjectList) {
93     if( binaryString == null )
94       return null;
95     String JavaDoc retVal = binaryString;
96     if (retVal.startsWith(I18nSqlParser.BINARY_STREAM_OBJECT)) {
97       int index = Integer.valueOf(retVal.substring(I18nSqlParser.
98           BINARY_STREAM_OBJECT.length())).intValue();
99       //check for null values
100
if( binaryStreamObjectList.get(index - 1 ) != null )
101         retVal = binaryStreamObjectList.get(index - 1).toString();
102       else retVal = null;
103     }
104     return retVal;
105   }
106
107   public static String JavaDoc handleQuotedString(String JavaDoc quotedString) {
108     if( quotedString == null )
109       return null;
110     String JavaDoc retVal = quotedString;
111     if ( (retVal.startsWith("'") && retVal.endsWith("'"))) {
112       if (!retVal.equals("''")) {
113         retVal = retVal.substring(retVal.indexOf("'") + 1,
114                                   retVal.lastIndexOf("'"));
115       }
116       else {
117         retVal = "";
118       }
119     } else {
120       if( retVal.equals("null") )
121         retVal = null;
122     }
123     return retVal;
124   }
125
126   public static String JavaDoc[] replaceLineBrakesAndCarrReturn(
127       String JavaDoc[] toReplace,
128       String JavaDoc lineBreakEscape,
129       String JavaDoc carriageReturnEscape) {
130     String JavaDoc[] retVal = new String JavaDoc[toReplace.length];
131     for (int i = 0; i < toReplace.length; i++) {
132       if (toReplace[i] != null) {
133         retVal[i] = replaceAll(toReplace[i], "\n", lineBreakEscape);
134         retVal[i] = replaceAll(retVal[i], "\r", carriageReturnEscape);
135       }
136     }
137     return retVal;
138   }
139
140   /**
141    * Compare two values.
142    * @param valA first value
143    * @param valB second value
144    * @return true if values are equal, false otherwise
145    */

146   public static boolean compareValues(String JavaDoc valA, String JavaDoc valB) {
147     boolean retVal = false;
148
149     if( valA == null && valB == null )
150       retVal = true;
151     else if( valA == null && valB != null )
152       retVal = false;
153     else if( valA != null && valB == null )
154       retVal = false;
155     else if( valA.equals(valB) )
156       retVal = true;
157
158     return retVal;
159   }
160
161   /**
162    * This method transform string object to binary object (array of bytes)
163    * @param val is string representation of binary object
164    * @return binary object
165    */

166   public static byte[] hexStringToBytes(String JavaDoc val) {
167     byte[] buf = new byte[val.length() / 2];
168     final char[] hexBytes = {
169         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
170         'E', 'F'
171     };
172     byte[] hexMap = new byte[256];
173     for (int i = 0; i < hexBytes.length; i++) {
174       hexMap[hexBytes[i]] = (byte) i;
175     }
176     int pos = 0;
177     for (int i = 0; i < buf.length; i++) {
178       buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
179       buf[i] += hexMap[val.charAt(pos++)];
180     }
181     return buf;
182   }
183
184   /**
185    *
186    * @param is
187    * @return String that represent InputStream is.
188    * @throws IOException
189    */

190   public static String JavaDoc streamToHexString(InputStream is) throws IOException {
191     try {
192       char[] hexBytes = {
193           '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
194           'E', 'F'};
195       int c;
196       StringBuffer JavaDoc hexString = new StringBuffer JavaDoc();
197       while ( (c = is.read()) >= 0) {
198         hexString.append(hexBytes[ (c >> 4) & 0xf]);
199         hexString.append(hexBytes[c & 0xf]);
200       }
201       return hexString.toString();
202     }
203     catch (Exception JavaDoc e) {
204       throw new IOException(e.getMessage());
205     }
206   }
207
208   /**
209    * Method replace all keywords in string passed as parameter.
210    * @param s String within replace should be done.
211    * @param oldValues HashMap with old values.
212    * @return String with special character array instead of keywords.
213    */

214   public static String JavaDoc replaceKeywords(String JavaDoc s, HashMap oldValues) {
215     String JavaDoc retVal = s;
216     int index = 1;
217     for (int i = 0; i < keywords.length; i++) {
218       StringBuffer JavaDoc result = new StringBuffer JavaDoc();
219       boolean hasMore = true;
220       while (hasMore) {
221         int start = retVal.toUpperCase().indexOf(keywords[i].toUpperCase());
222         int end = start + keywords[i].length();
223         if (start != -1) {
224           String JavaDoc newValue = keywordEscape + String.valueOf(index);
225           while( oldValues.containsKey(newValue) ) {
226             index++;
227             newValue = keywordEscape + String.valueOf(index);
228           }
229           result.append(retVal.substring(0, start) + newValue);
230           oldValues.put(newValue, retVal.substring(start, end));
231           retVal = retVal.substring(end);
232         }
233         else {
234           hasMore = false;
235           result.append(retVal);
236         }
237       }
238       if (!result.toString().equals(""))
239         retVal = result.toString();
240     }
241     return retVal;
242   }
243
244   /**
245    * Method replace all keywords in string passed as parameter.
246    * @param s String within replace should be done.
247    * @param oldValues HashMap with old values.
248    * @return String with special character array instead of keywords.
249    */

250   public static String JavaDoc replaceKeywordsBack(String JavaDoc s, HashMap oldValues) {
251     String JavaDoc retVal = s;
252     Set keys = oldValues.keySet();
253     Iterator it = keys.iterator();
254     Object JavaDoc key = "";
255     String JavaDoc value = "";
256     while( it.hasNext() ) {
257       key = it.next();
258       value = (String JavaDoc)oldValues.get(key.toString());
259       if( value != null )
260       retVal = replaceAll(retVal, key.toString(), value.toString());
261     }
262     return retVal;
263   }
264
265 }
Popular Tags