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.relique.jdbc.csv;
21
22 import java.io.*;
23 import java.util.*;
24
25 /***
26 * Utility methods for csv jdbc.
27 *
28 * @author Zoran Milakovic
29 */
30
31 public class Utils {
32
33 public static final String NOT_NULL_STRING = "NOTNULLSTRING";
34
35 //keywords escape
36 public static final String[] keywords = { "AND", "WHERE", "FROM", "SET", "IS", "CREATE TABLE", "INT0", "INSERT", "VALUES" };
37 public static final String keywordEscape = "~#~KEYWORD~#~";
38 /***
39 * Replace all occurence of forReplace with replaceWith in input string.
40 * @param input represents input string
41 * @param forReplace represents substring for replace
42 * @param replaceWith represents replaced string value
43 * @return new string with replaced values
44 */
45 public static String replaceAll(
46 String input,
47 String forReplace,
48 String replaceWith) {
49 if( input == null )
50 return null;
51 StringBuffer result = new StringBuffer();
52 boolean hasMore = true;
53 while (hasMore) {
54 int start = input.indexOf(forReplace);
55 int end = start + forReplace.length();
56 if (start != -1) {
57 result.append(input.substring(0, start) + replaceWith);
58 input = input.substring(end);
59 }
60 else {
61 hasMore = false;
62 result.append(input);
63 }
64 }
65 if (result.toString().equals(""))
66 return input; //nothing is changed
67 else
68 return result.toString();
69 }
70
71 /***
72 * This method transform binary object to string object
73 * @param b is array of bytes which represents binary object
74 * @return string representation of binary object
75 */
76 public static String bytesToHexString(byte[] b) {
77 String hexString = null;
78 try {
79 if (b != null) {
80 ByteArrayInputStream is = new ByteArrayInputStream(b);
81 hexString = streamToHexString(is);
82 return hexString;
83 }
84 else {
85 return null;
86 }
87 }
88 catch (Exception e) {
89 }
90 return hexString;
91 }
92
93 public static String handleBinaryString(String binaryString, List binaryStreamObjectList) {
94 if( binaryString == null )
95 return null;
96 String retVal = binaryString;
97 if (retVal.startsWith(CsvSqlParser.BINARY_STREAM_OBJECT)) {
98 int index = Integer.valueOf(retVal.substring(CsvSqlParser.
99 BINARY_STREAM_OBJECT.length())).intValue();
100 //check for null values
101 if( binaryStreamObjectList.get(index - 1 ) != null )
102 retVal = binaryStreamObjectList.get(index - 1).toString();
103 else retVal = null;
104 }
105 return retVal;
106 }
107
108 public static String handleQuotedString(String quotedString) {
109 if( quotedString == null )
110 return null;
111 String retVal = quotedString;
112 if ( (retVal.startsWith("'") && retVal.endsWith("'"))) {
113 if (!retVal.equals("''")) {
114 retVal = retVal.substring(retVal.indexOf("'") + 1,
115 retVal.lastIndexOf("'"));
116 }
117 else {
118 retVal = "";
119 }
120 } else {
121 if( retVal.equals("null") )
122 retVal = null;
123 }
124 return retVal;
125 }
126
127 public static String[] replaceLineBrakesAndCarrReturn(
128 String[] toReplace,
129 String lineBreakEscape,
130 String carriageReturnEscape) {
131 String[] retVal = new String[toReplace.length];
132 for (int i = 0; i < toReplace.length; i++) {
133 if (toReplace[i] != null) {
134 retVal[i] = replaceAll(toReplace[i], "\n", lineBreakEscape);
135 retVal[i] = replaceAll(retVal[i], "\r", carriageReturnEscape);
136 }
137 }
138 return retVal;
139 }
140
141 /***
142 * Compare two values.
143 * @param valA first value
144 * @param valB second value
145 * @return true if values are equal, false otherwise
146 */
147 public static boolean compareValues(String valA, String valB) {
148 boolean retVal = false;
149
150 if( valA == null && valB == null )
151 retVal = true;
152 else if( valA == null && valB != null )
153 retVal = false;
154 else if( valA != null && valB == null )
155 retVal = false;
156 else if( valA.equals(valB) )
157 retVal = true;
158 else if( valA != null && valA.equals(Utils.NOT_NULL_STRING) && valB != null )
159 retVal = true;
160 else if( valB != null && valB.equals(Utils.NOT_NULL_STRING) && valA != null )
161 retVal = true;
162
163 return retVal;
164 }
165
166 /***
167 * This method transform string object to binary object (array of bytes)
168 * @param val is string representation of binary object
169 * @return binary object
170 */
171 public static byte[] hexStringToBytes(String val) {
172 byte[] buf = new byte[val.length() / 2];
173 final char[] hexBytes = {
174 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
175 'E', 'F'
176 };
177 byte[] hexMap = new byte[256];
178 for (int i = 0; i < hexBytes.length; i++) {
179 hexMap[hexBytes[i]] = (byte) i;
180 }
181 int pos = 0;
182 for (int i = 0; i < buf.length; i++) {
183 buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
184 buf[i] += hexMap[val.charAt(pos++)];
185 }
186 return buf;
187 }
188
189 /***
190 *
191 * @param is
192 * @return String that represent InputStream is.
193 * @throws IOException
194 */
195 public static String streamToHexString(InputStream is) throws IOException {
196 try {
197 char[] hexBytes = {
198 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
199 'E', 'F'};
200 int c;
201 StringBuffer hexString = new StringBuffer();
202 while ( (c = is.read()) >= 0) {
203 hexString.append(hexBytes[ (c >> 4) & 0xf]);
204 hexString.append(hexBytes[c & 0xf]);
205 }
206 return hexString.toString();
207 }
208 catch (Exception e) {
209 throw new IOException(e.getMessage());
210 }
211 }
212
213 /***
214 * Method replace all keywords in string passed as parameter.
215 * @param s String within replace should be done.
216 * @param oldValues HashMap with old values.
217 * @return String with special character array instead of keywords.
218 */
219 public static String replaceKeywords(String s, HashMap oldValues) {
220 String retVal = s;
221 int index = 1;
222 for (int i = 0; i < keywords.length; i++) {
223 StringBuffer result = new StringBuffer();
224 boolean hasMore = true;
225 while (hasMore) {
226 int start = retVal.toUpperCase().indexOf(keywords[i].toUpperCase());
227 int end = start + keywords[i].length();
228 if (start != -1) {
229 String newValue = keywordEscape + String.valueOf(index);
230 while( oldValues.containsKey(newValue) ) {
231 index++;
232 newValue = keywordEscape + String.valueOf(index);
233 }
234 result.append(retVal.substring(0, start) + newValue);
235 oldValues.put(newValue, retVal.substring(start, end));
236 retVal = retVal.substring(end);
237 }
238 else {
239 hasMore = false;
240 result.append(retVal);
241 }
242 }
243 if (!result.toString().equals(""))
244 retVal = result.toString();
245 }
246 return retVal;
247 }
248
249 /***
250 * Method replace all keywords in string passed as parameter.
251 * @param s String within replace should be done.
252 * @param oldValues HashMap with old values.
253 * @return String with special character array instead of keywords.
254 */
255 public static String replaceKeywordsBack(String s, HashMap oldValues) {
256 String retVal = s;
257 Set keys = oldValues.keySet();
258 Iterator it = keys.iterator();
259 Object key = "";
260 String value = "";
261 while( it.hasNext() ) {
262 key = it.next();
263 value = (String)oldValues.get(key.toString());
264 if( value != null )
265 retVal = replaceAll(retVal, key.toString(), value.toString());
266 }
267 return retVal;
268 }
269
270 public static boolean isUTF16(String charset) {
271 if(charset != null && (
272 charset.equalsIgnoreCase("UnicodeBig") ||
273 charset.equalsIgnoreCase("UnicodeLittle") ||
274 charset.equalsIgnoreCase("Unicode") ||
275 charset.equalsIgnoreCase("UTF16") ||
276 charset.equalsIgnoreCase("UTF-16")))
277 return true;
278 else
279 return false;
280 }
281
282 }
This page was automatically generated by Maven