001: /*
002: Copyright (C) 2003 Together
003: This library is free software; you can redistribute it and/or
004: modify it under the terms of the GNU Lesser General Public
005: License as published by the Free Software Foundation; either
006: version 2.1 of the License, or (at your option) any later version.
007: This library is distributed in the hope that it will be useful,
008: but WITHOUT ANY WARRANTY; without even the implied warranty of
009: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010: Lesser General Public License for more details.
011: You should have received a copy of the GNU Lesser General Public
012: License along with this library; if not, write to the Free Software
013: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
014: */
015:
016: package org.webdocwf.util.xml;
017:
018: import java.io.*;
019: import java.util.*;
020:
021: /**
022: * Utility methods for xml jdbc.
023: *
024: * @author Zoran Milakovic
025: */
026:
027: public class Utils {
028:
029: //keywords escape
030: public static final String[] keywords = { "AND", "WHERE", "FROM",
031: "SET", "IS", "CREATE TABLE", "PRIMARYKEY", "NOT NULL",
032: "INT0", "INSERT", "VALUES" };
033: public static final String keywordEscape = "~#~KEYWORD~#~";
034:
035: public static String handleBinaryString(String binaryString,
036: List binaryStreamObjectList) {
037: String retVal = binaryString;
038: if (retVal.startsWith(XmlSqlParser.BINARY_STREAM_OBJECT)) {
039: int index = Integer.valueOf(
040: retVal.substring(XmlSqlParser.BINARY_STREAM_OBJECT
041: .length())).intValue();
042: //check for null values
043: if (binaryStreamObjectList.get(index - 1) != null)
044: retVal = binaryStreamObjectList.get(index - 1)
045: .toString();
046: else
047: retVal = null;
048: }
049: return retVal;
050: }
051:
052: public static String handleQuotedString(String quotedString) {
053: String retVal = quotedString;
054: if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
055: if (!retVal.equals("''")) {
056: retVal = retVal.substring(retVal.indexOf("'") + 1,
057: retVal.lastIndexOf("'"));
058: } else {
059: retVal = "";
060: }
061: }
062: return retVal;
063: }
064:
065: /**
066: * Replace all occurence of forReplace with replaceWith in input string.
067: */
068: public static String replaceAll(String input, String forReplace,
069: String replaceWith) {
070: StringBuffer result = new StringBuffer();
071: boolean hasMore = true;
072: while (hasMore) {
073: int start = input.indexOf(forReplace);
074: int end = start + forReplace.length();
075: if (start != -1) {
076: result.append(input.substring(0, start) + replaceWith);
077: input = input.substring(end);
078: } else {
079: hasMore = false;
080: result.append(input);
081: }
082: }
083: if (result.toString().equals(""))
084: return input; //nothing is changed
085: else
086: return result.toString();
087: }
088:
089: //Helper methods to work with BinaryStream object.
090:
091: /**
092: * This method transform binary object to string object
093: * @param b is array of bytes which represents binary object
094: * @return string representation of binary object
095: */
096: public static String bytesToHexString(byte[] b) {
097: String hexString = null;
098: try {
099: if (b != null) {
100: ByteArrayInputStream is = new ByteArrayInputStream(b);
101: hexString = streamToHexString(is);
102: return hexString;
103: } else {
104: return null;
105: }
106: } catch (Exception e) {
107: }
108: return hexString;
109: }
110:
111: /**
112: * This method transform string object to binary object (array of bytes)
113: * @param val is string representation of binary object
114: * @return binary object
115: */
116: public static byte[] hexStringToBytes(String val) {
117: byte[] buf = new byte[val.length() / 2];
118: final char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6',
119: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
120: byte[] hexMap = new byte[256];
121: for (int i = 0; i < hexBytes.length; i++) {
122: hexMap[hexBytes[i]] = (byte) i;
123: }
124: int pos = 0;
125: for (int i = 0; i < buf.length; i++) {
126: buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
127: buf[i] += hexMap[val.charAt(pos++)];
128: }
129: return buf;
130: }
131:
132: /**
133: *
134: * @param is
135: * @return String that represent InputStream is.
136: * @throws IOException
137: */
138: public static String streamToHexString(InputStream is)
139: throws IOException {
140: try {
141: char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6', '7',
142: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
143: int c;
144: StringBuffer hexString = new StringBuffer();
145: while ((c = is.read()) >= 0) {
146: hexString.append(hexBytes[(c >> 4) & 0xf]);
147: hexString.append(hexBytes[c & 0xf]);
148: }
149: return hexString.toString();
150: } catch (Exception e) {
151: throw new IOException(e.getMessage());
152: }
153: }
154:
155: /**
156: * Method replace all keywords in string passed as parameter.
157: * @param s String within replace should be done.
158: * @return String with special character array instead of keywords.
159: */
160: public static String replaceKeywords(String s, HashMap oldValues) {
161: String retVal = s;
162: int index = 1;
163: for (int i = 0; i < keywords.length; i++) {
164: StringBuffer result = new StringBuffer();
165: boolean hasMore = true;
166: while (hasMore) {
167: int start = retVal.toUpperCase().indexOf(
168: keywords[i].toUpperCase());
169: int end = start + keywords[i].length();
170: if (start != -1) {
171: String newValue = keywordEscape
172: + String.valueOf(index);
173: while (oldValues.containsKey(newValue)) {
174: index++;
175: newValue = keywordEscape
176: + String.valueOf(index);
177: }
178: result
179: .append(retVal.substring(0, start)
180: + newValue);
181: oldValues.put(newValue, retVal
182: .substring(start, end));
183: retVal = retVal.substring(end);
184: } else {
185: hasMore = false;
186: result.append(retVal);
187: }
188: }
189: if (!result.toString().equals(""))
190: retVal = result.toString();
191: }
192: return retVal;
193: }
194:
195: /**
196: * Method replace all keywords in string passed as parameter.
197: * @param s String within replace should be done.
198: * @return String with special character array instead of keywords.
199: */
200: public static String replaceKeywordsBack(String s, HashMap oldValues) {
201: String retVal = s;
202: Set keys = oldValues.keySet();
203: Iterator it = keys.iterator();
204: Object key = "";
205: String value = "";
206: while (it.hasNext()) {
207: key = it.next();
208: value = (String) oldValues.get(key.toString());
209: if (value != null)
210: retVal = replaceAll(retVal, key.toString(), value
211: .toString());
212: }
213: return retVal;
214: }
215:
216: }
|