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.webdocwf.util.i18njdbc;
019:
020: import java.io.*;
021: import java.util.*;
022:
023: /**
024: * Utility methods for i18n jdbc.
025: *
026: * @author Zoran Milakovic
027: * @author Zeljko Kovacevic
028: */
029:
030: public class Utils {
031:
032: //keywords escape
033: public static final String[] keywords = { "AND", "WHERE", "FROM",
034: "SET", "IS", "CREATE TABLE", "INT0", "INSERT", "VALUES" };
035: public static final String keywordEscape = "~#~KEYWORD~#~";
036:
037: /**
038: * Replace all occurence of forReplace with replaceWith in input string.
039: * @param input represents input string
040: * @param forReplace represents substring for replace
041: * @param replaceWith represents replaced string value
042: * @return new string with replaced values
043: */
044: public static String replaceAll(String input, String forReplace,
045: String replaceWith) {
046: if (input == null)
047: return null;
048: StringBuffer result = new StringBuffer();
049: boolean hasMore = true;
050: while (hasMore) {
051: int start = input.indexOf(forReplace);
052: int end = start + forReplace.length();
053: if (start != -1) {
054: result.append(input.substring(0, start) + replaceWith);
055: input = input.substring(end);
056: } else {
057: hasMore = false;
058: result.append(input);
059: }
060: }
061: if (result.toString().equals(""))
062: return input; //nothing is changed
063: else
064: return result.toString();
065: }
066:
067: /**
068: * This method transform binary object to string object
069: * @param b is array of bytes which represents binary object
070: * @return string representation of binary object
071: */
072: public static String bytesToHexString(byte[] b) {
073: String hexString = null;
074: try {
075: if (b != null) {
076: ByteArrayInputStream is = new ByteArrayInputStream(b);
077: hexString = streamToHexString(is);
078: return hexString;
079: } else {
080: return null;
081: }
082: } catch (Exception e) {
083: }
084: return hexString;
085: }
086:
087: public static String handleBinaryString(String binaryString,
088: List binaryStreamObjectList) {
089: if (binaryString == null)
090: return null;
091: String retVal = binaryString;
092: if (retVal.startsWith(I18nSqlParser.BINARY_STREAM_OBJECT)) {
093: int index = Integer.valueOf(
094: retVal.substring(I18nSqlParser.BINARY_STREAM_OBJECT
095: .length())).intValue();
096: //check for null values
097: if (binaryStreamObjectList.get(index - 1) != null)
098: retVal = binaryStreamObjectList.get(index - 1)
099: .toString();
100: else
101: retVal = null;
102: }
103: return retVal;
104: }
105:
106: public static String handleQuotedString(String quotedString) {
107: if (quotedString == null)
108: return null;
109: String retVal = quotedString;
110: if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
111: if (!retVal.equals("''")) {
112: retVal = retVal.substring(retVal.indexOf("'") + 1,
113: retVal.lastIndexOf("'"));
114: } else {
115: retVal = "";
116: }
117: } else {
118: if (retVal.equals("null"))
119: retVal = null;
120: }
121: return retVal;
122: }
123:
124: public static String[] replaceLineBrakesAndCarrReturn(
125: String[] toReplace, String lineBreakEscape,
126: String carriageReturnEscape) {
127: String[] retVal = new String[toReplace.length];
128: for (int i = 0; i < toReplace.length; i++) {
129: if (toReplace[i] != null) {
130: retVal[i] = replaceAll(toReplace[i], "\n",
131: lineBreakEscape);
132: retVal[i] = replaceAll(retVal[i], "\r",
133: carriageReturnEscape);
134: }
135: }
136: return retVal;
137: }
138:
139: /**
140: * Compare two values.
141: * @param valA first value
142: * @param valB second value
143: * @return true if values are equal, false otherwise
144: */
145: public static boolean compareValues(String valA, String valB) {
146: boolean retVal = false;
147:
148: if (valA == null && valB == null)
149: retVal = true;
150: else if (valA == null && valB != null)
151: retVal = false;
152: else if (valA != null && valB == null)
153: retVal = false;
154: else if (valA.equals(valB))
155: retVal = true;
156:
157: return retVal;
158: }
159:
160: /**
161: * This method transform string object to binary object (array of bytes)
162: * @param val is string representation of binary object
163: * @return binary object
164: */
165: public static byte[] hexStringToBytes(String val) {
166: byte[] buf = new byte[val.length() / 2];
167: final char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6',
168: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
169: byte[] hexMap = new byte[256];
170: for (int i = 0; i < hexBytes.length; i++) {
171: hexMap[hexBytes[i]] = (byte) i;
172: }
173: int pos = 0;
174: for (int i = 0; i < buf.length; i++) {
175: buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
176: buf[i] += hexMap[val.charAt(pos++)];
177: }
178: return buf;
179: }
180:
181: /**
182: *
183: * @param is
184: * @return String that represent InputStream is.
185: * @throws IOException
186: */
187: public static String streamToHexString(InputStream is)
188: throws IOException {
189: try {
190: char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6', '7',
191: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
192: int c;
193: StringBuffer hexString = new StringBuffer();
194: while ((c = is.read()) >= 0) {
195: hexString.append(hexBytes[(c >> 4) & 0xf]);
196: hexString.append(hexBytes[c & 0xf]);
197: }
198: return hexString.toString();
199: } catch (Exception e) {
200: throw new IOException(e.getMessage());
201: }
202: }
203:
204: /**
205: * Method replace all keywords in string passed as parameter.
206: * @param s String within replace should be done.
207: * @param oldValues HashMap with old values.
208: * @return String with special character array instead of keywords.
209: */
210: public static String replaceKeywords(String s, HashMap oldValues) {
211: String retVal = s;
212: int index = 1;
213: for (int i = 0; i < keywords.length; i++) {
214: StringBuffer result = new StringBuffer();
215: boolean hasMore = true;
216: while (hasMore) {
217: int start = retVal.toUpperCase().indexOf(
218: keywords[i].toUpperCase());
219: int end = start + keywords[i].length();
220: if (start != -1) {
221: String newValue = keywordEscape
222: + String.valueOf(index);
223: while (oldValues.containsKey(newValue)) {
224: index++;
225: newValue = keywordEscape
226: + String.valueOf(index);
227: }
228: result
229: .append(retVal.substring(0, start)
230: + newValue);
231: oldValues.put(newValue, retVal
232: .substring(start, end));
233: retVal = retVal.substring(end);
234: } else {
235: hasMore = false;
236: result.append(retVal);
237: }
238: }
239: if (!result.toString().equals(""))
240: retVal = result.toString();
241: }
242: return retVal;
243: }
244:
245: /**
246: * Method replace all keywords in string passed as parameter.
247: * @param s String within replace should be done.
248: * @param oldValues HashMap with old values.
249: * @return String with special character array instead of keywords.
250: */
251: public static String replaceKeywordsBack(String s, HashMap oldValues) {
252: String retVal = s;
253: Set keys = oldValues.keySet();
254: Iterator it = keys.iterator();
255: Object key = "";
256: String value = "";
257: while (it.hasNext()) {
258: key = it.next();
259: value = (String) oldValues.get(key.toString());
260: if (value != null)
261: retVal = replaceAll(retVal, key.toString(), value
262: .toString());
263: }
264: return retVal;
265: }
266:
267: }
|