1 /*
2 Copyright (C) 2003 Together
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 2.1 of the License, or (at your option) any later version.
7 This library is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 Lesser General Public License for more details.
11 You should have received a copy of the GNU Lesser General Public
12 License along with this library; if not, write to the Free Software
13 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16 package org.enhydra.xml;
17
18 import java.io.*;
19 import java.util.*;
20
21 /***
22 * Utility methods for xml jdbc.
23 *
24 * @author Zoran Milakovic
25 * @version $Id: Utils.java,v 1.2 2004/07/22 12:49:38 zeljko Exp $
26 */
27
28 public class Utils {
29
30 public static String handleQuotedString( String quotedString ) {
31 String retVal = quotedString;
32 if ( ( retVal.startsWith( "'" ) && retVal.endsWith( "'" ) ) ) {
33 if ( !retVal.equals( "''" ) ) {
34 retVal = retVal.substring( retVal.indexOf( "'" ) + 1,
35 retVal.lastIndexOf( "'" ) );
36 }
37 else {
38 retVal = "";
39 }
40 }
41 return retVal;
42 }
43
44 /***
45 * Replace all occurence of forReplace with replaceWith in input string.
46 * @param input
47 * @param forReplace
48 * @param replaceWith
49 * @return String with replaced values
50 */
51 public static String replaceAll( String input, String forReplace,
52 String replaceWith ) {
53 StringBuffer result = new StringBuffer();
54 boolean hasMore = true;
55 while ( hasMore ) {
56 int start = input.indexOf( forReplace );
57 int end = start + forReplace.length();
58 if ( start != -1 ) {
59 result.append( input.substring( 0, start ) + replaceWith );
60 input = input.substring( end );
61 }
62 else {
63 hasMore = false;
64 result.append( input );
65 }
66 }
67 if ( result.toString().equals( "" ) )
68 return input; //nothing is changed
69 else
70 return result.toString();
71 }
72
73 //Helper methods to work with BinaryStream object.
74
75 /***
76 * This method transform binary object to string object
77 * @param b is array of bytes which represents binary object
78 * @return string representation of binary object
79 */
80 public static String bytesToHexString( byte[] b ) {
81 String hexString = null;
82 try {
83 if ( b != null ) {
84 ByteArrayInputStream is = new ByteArrayInputStream( b );
85 hexString = streamToHexString( is );
86 return hexString;
87 }
88 else {
89 return null;
90 }
91 }
92 catch ( Exception e ) {
93 }
94 return hexString;
95 }
96
97 /***
98 * This method transform string object to binary object (array of bytes)
99 * @param val is string representation of binary object
100 * @return binary object
101 */
102 public static byte[] hexStringToBytes( String val ) {
103 byte[] buf = new byte[val.length() / 2];
104 final char[] hexBytes = {
105 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
106 'E', 'F'
107 };
108 byte[] hexMap = new byte[256];
109 for ( int i = 0; i < hexBytes.length; i++ ) {
110 hexMap[hexBytes[i]] = ( byte ) i;
111 }
112 int pos = 0;
113 for ( int i = 0; i < buf.length; i++ ) {
114 buf[i] = ( byte ) ( hexMap[val.charAt( pos++ )] << 4 );
115 buf[i] += hexMap[val.charAt( pos++ )];
116 }
117 return buf;
118 }
119
120 /***
121 *
122 * @param is
123 * @return String that represent InputStream is.
124 * @throws IOException
125 */
126 public static String streamToHexString( InputStream is ) throws IOException {
127 try {
128 char[] hexBytes = {
129 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
130 'E', 'F'};
131 int c;
132 StringBuffer hexString = new StringBuffer();
133 while ( ( c = is.read() ) >= 0 ) {
134 hexString.append( hexBytes[ ( c >> 4 ) & 0xf] );
135 hexString.append( hexBytes[c & 0xf] );
136 }
137 return hexString.toString();
138 }
139 catch ( Exception e ) {
140 throw new IOException( e.getMessage() );
141 }
142 }
143
144 }
This page was automatically generated by Maven