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.enhydra.xml;
017:
018: import java.io.*;
019: import java.util.*;
020:
021: /**
022: * Utility methods for xml jdbc.
023: *
024: * @author Zoran Milakovic
025: * @version $Id: Utils.java,v 1.1 2006-06-23 10:40:51 sinisa Exp $
026: */
027:
028: public class Utils {
029:
030: public static String handleQuotedString(String quotedString) {
031: String retVal = quotedString;
032: if ((retVal.startsWith("'") && retVal.endsWith("'"))) {
033: if (!retVal.equals("''")) {
034: retVal = retVal.substring(retVal.indexOf("'") + 1,
035: retVal.lastIndexOf("'"));
036: } else {
037: retVal = "";
038: }
039: }
040: return retVal;
041: }
042:
043: /**
044: * Replace all occurence of forReplace with replaceWith in input string.
045: * @param input
046: * @param forReplace
047: * @param replaceWith
048: * @return String with replaced values
049: */
050: public static String replaceAll(String input, String forReplace,
051: String replaceWith) {
052: StringBuffer result = new StringBuffer();
053: boolean hasMore = true;
054: while (hasMore) {
055: int start = input.indexOf(forReplace);
056: int end = start + forReplace.length();
057: if (start != -1) {
058: result.append(input.substring(0, start) + replaceWith);
059: input = input.substring(end);
060: } else {
061: hasMore = false;
062: result.append(input);
063: }
064: }
065: if (result.toString().equals(""))
066: return input; //nothing is changed
067: else
068: return result.toString();
069: }
070:
071: //Helper methods to work with BinaryStream object.
072:
073: /**
074: * This method transform binary object to string object
075: * @param b is array of bytes which represents binary object
076: * @return string representation of binary object
077: */
078: public static String bytesToHexString(byte[] b) {
079: String hexString = null;
080: try {
081: if (b != null) {
082: ByteArrayInputStream is = new ByteArrayInputStream(b);
083: hexString = streamToHexString(is);
084: return hexString;
085: } else {
086: return null;
087: }
088: } catch (Exception e) {
089: }
090: return hexString;
091: }
092:
093: /**
094: * This method transform string object to binary object (array of bytes)
095: * @param val is string representation of binary object
096: * @return binary object
097: */
098: public static byte[] hexStringToBytes(String val) {
099: byte[] buf = new byte[val.length() / 2];
100: final char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6',
101: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
102: byte[] hexMap = new byte[256];
103: for (int i = 0; i < hexBytes.length; i++) {
104: hexMap[hexBytes[i]] = (byte) i;
105: }
106: int pos = 0;
107: for (int i = 0; i < buf.length; i++) {
108: buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
109: buf[i] += hexMap[val.charAt(pos++)];
110: }
111: return buf;
112: }
113:
114: /**
115: *
116: * @param is
117: * @return String that represent InputStream is.
118: * @throws IOException
119: */
120: public static String streamToHexString(InputStream is)
121: throws IOException {
122: try {
123: char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6', '7',
124: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
125: int c;
126: StringBuffer hexString = new StringBuffer();
127: while ((c = is.read()) >= 0) {
128: hexString.append(hexBytes[(c >> 4) & 0xf]);
129: hexString.append(hexBytes[c & 0xf]);
130: }
131: return hexString.toString();
132: } catch (Exception e) {
133: throw new IOException(e.getMessage());
134: }
135: }
136:
137: }
|