001: /**
002: Copyright (C) 2002-2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: */package org.relique.jdbc.csv;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: /**
024: * Utility methods for csv jdbc.
025: *
026: * @author Zoran Milakovic
027: */
028:
029: public class Utils {
030:
031: public static final String NOT_NULL_STRING = "NOTNULLSTRING";
032:
033: //keywords escape
034: public static final String[] keywords = { "AND", "WHERE", "FROM",
035: "SET", "IS", "CREATE TABLE", "INT0", "INSERT", "VALUES" };
036: public static final String keywordEscape = "~#~KEYWORD~#~";
037:
038: /**
039: * Replace all occurence of forReplace with replaceWith in input string.
040: * @param input represents input string
041: * @param forReplace represents substring for replace
042: * @param replaceWith represents replaced string value
043: * @return new string with replaced values
044: */
045: public static String replaceAll(String input, String forReplace,
046: String replaceWith) {
047: if (input == null)
048: return null;
049: StringBuffer result = new StringBuffer();
050: boolean hasMore = true;
051: while (hasMore) {
052: int start = input.indexOf(forReplace);
053: int end = start + forReplace.length();
054: if (start != -1) {
055: result.append(input.substring(0, start) + replaceWith);
056: input = input.substring(end);
057: } else {
058: hasMore = false;
059: result.append(input);
060: }
061: }
062: if (result.toString().equals(""))
063: return input; //nothing is changed
064: else
065: return result.toString();
066: }
067:
068: /**
069: * This method transform binary object to string object
070: * @param b is array of bytes which represents binary object
071: * @return string representation of binary object
072: */
073: public static String bytesToHexString(byte[] b) {
074: String hexString = null;
075: try {
076: if (b != null) {
077: ByteArrayInputStream is = new ByteArrayInputStream(b);
078: hexString = streamToHexString(is);
079: return hexString;
080: } else {
081: return null;
082: }
083: } catch (Exception e) {
084: }
085: return hexString;
086: }
087:
088: public static String handleBinaryString(String binaryString,
089: List binaryStreamObjectList) {
090: if (binaryString == null)
091: return null;
092: String retVal = binaryString;
093: if (retVal.startsWith(CsvSqlParser.BINARY_STREAM_OBJECT)) {
094: int index = Integer.valueOf(
095: retVal.substring(CsvSqlParser.BINARY_STREAM_OBJECT
096: .length())).intValue();
097: //check for null values
098: if (binaryStreamObjectList.get(index - 1) != null)
099: retVal = binaryStreamObjectList.get(index - 1)
100: .toString();
101: else
102: retVal = null;
103: }
104: return retVal;
105: }
106:
107: public static String handleQuotedString(String quotedString) {
108: if (quotedString == null)
109: return null;
110: String retVal = quotedString;
111: if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
112: if (!retVal.equals("''")) {
113: retVal = retVal.substring(retVal.indexOf("'") + 1,
114: retVal.lastIndexOf("'"));
115: } else {
116: retVal = "";
117: }
118: } else {
119: if (retVal.equals("null"))
120: retVal = null;
121: }
122: return retVal;
123: }
124:
125: public static String[] replaceLineBrakesAndCarrReturn(
126: String[] toReplace, String lineBreakEscape,
127: String carriageReturnEscape) {
128: String[] retVal = new String[toReplace.length];
129: for (int i = 0; i < toReplace.length; i++) {
130: if (toReplace[i] != null) {
131: retVal[i] = replaceAll(toReplace[i], "\n",
132: lineBreakEscape);
133: retVal[i] = replaceAll(retVal[i], "\r",
134: 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 valA, String 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: else if (valA != null && valA.equals(Utils.NOT_NULL_STRING)
158: && valB != null)
159: retVal = true;
160: else if (valB != null && valB.equals(Utils.NOT_NULL_STRING)
161: && valA != null)
162: retVal = true;
163:
164: return retVal;
165: }
166:
167: /**
168: * This method transform string object to binary object (array of bytes)
169: * @param val is string representation of binary object
170: * @return binary object
171: */
172: public static byte[] hexStringToBytes(String val) {
173: byte[] buf = new byte[val.length() / 2];
174: final char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6',
175: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
176: byte[] hexMap = new byte[256];
177: for (int i = 0; i < hexBytes.length; i++) {
178: hexMap[hexBytes[i]] = (byte) i;
179: }
180: int pos = 0;
181: for (int i = 0; i < buf.length; i++) {
182: buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
183: buf[i] += hexMap[val.charAt(pos++)];
184: }
185: return buf;
186: }
187:
188: /**
189: *
190: * @param is
191: * @return String that represent InputStream is.
192: * @throws IOException
193: */
194: public static String streamToHexString(InputStream is)
195: throws IOException {
196: try {
197: char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6', '7',
198: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
199: int c;
200: StringBuffer hexString = new StringBuffer();
201: while ((c = is.read()) >= 0) {
202: hexString.append(hexBytes[(c >> 4) & 0xf]);
203: hexString.append(hexBytes[c & 0xf]);
204: }
205: return hexString.toString();
206: } catch (Exception e) {
207: throw new IOException(e.getMessage());
208: }
209: }
210:
211: /**
212: * Method replace all keywords in string passed as parameter.
213: * @param s String within replace should be done.
214: * @param oldValues HashMap with old values.
215: * @return String with special character array instead of keywords.
216: */
217: public static String replaceKeywords(String s, HashMap oldValues) {
218: String retVal = s;
219: int index = 1;
220: for (int i = 0; i < keywords.length; i++) {
221: StringBuffer result = new StringBuffer();
222: boolean hasMore = true;
223: while (hasMore) {
224: int start = retVal.toUpperCase().indexOf(
225: keywords[i].toUpperCase());
226: int end = start + keywords[i].length();
227: if (start != -1) {
228: String newValue = keywordEscape
229: + String.valueOf(index);
230: while (oldValues.containsKey(newValue)) {
231: index++;
232: newValue = keywordEscape
233: + String.valueOf(index);
234: }
235: result
236: .append(retVal.substring(0, start)
237: + newValue);
238: oldValues.put(newValue, retVal
239: .substring(start, end));
240: retVal = retVal.substring(end);
241: } else {
242: hasMore = false;
243: result.append(retVal);
244: }
245: }
246: if (!result.toString().equals(""))
247: retVal = result.toString();
248: }
249: return retVal;
250: }
251:
252: /**
253: * Method replace all keywords in string passed as parameter.
254: * @param s String within replace should be done.
255: * @param oldValues HashMap with old values.
256: * @return String with special character array instead of keywords.
257: */
258: public static String replaceKeywordsBack(String s, HashMap oldValues) {
259: String retVal = s;
260: Set keys = oldValues.keySet();
261: Iterator it = keys.iterator();
262: Object key = "";
263: String value = "";
264: while (it.hasNext()) {
265: key = it.next();
266: value = (String) oldValues.get(key.toString());
267: if (value != null)
268: retVal = replaceAll(retVal, key.toString(), value
269: .toString());
270: }
271: return retVal;
272: }
273:
274: public static boolean isUTF16(String charset) {
275: if (charset != null
276: && (charset.equalsIgnoreCase("UnicodeBig")
277: || charset.equalsIgnoreCase("UnicodeLittle")
278: || charset.equalsIgnoreCase("Unicode")
279: || charset.equalsIgnoreCase("UTF16") || charset
280: .equalsIgnoreCase("UTF-16")))
281: return true;
282: else
283: return false;
284: }
285:
286: }
|