1 15 16 package org.webdocwf.util.xml; 17 18 import java.io.*; 19 import java.util.*; 20 21 26 27 public class Utils { 28 29 public static final String [] keywords = { "AND", "WHERE", "FROM", "SET", "IS", "CREATE TABLE", 31 "PRIMARYKEY", "NOT NULL", "INT0", "INSERT", "VALUES" }; 32 public static final String keywordEscape = "~#~KEYWORD~#~"; 33 34 35 public static String handleBinaryString(String binaryString, List binaryStreamObjectList) { 36 String retVal = binaryString; 37 if (retVal.startsWith(XmlSqlParser.BINARY_STREAM_OBJECT)) { 38 int index = Integer.valueOf(retVal.substring(XmlSqlParser. 39 BINARY_STREAM_OBJECT.length())).intValue(); 40 if( binaryStreamObjectList.get(index - 1 ) != null ) 42 retVal = binaryStreamObjectList.get(index - 1).toString(); 43 else retVal = null; 44 } 45 return retVal; 46 } 47 48 49 public static String handleQuotedString( String quotedString ) { 50 String retVal = quotedString; 51 if ( ( retVal.startsWith( "'" ) && retVal.endsWith( "'" ) ) ) { 52 if ( !retVal.equals( "''" ) ) { 53 retVal = retVal.substring( retVal.indexOf( "'" ) + 1, 54 retVal.lastIndexOf( "'" ) ); 55 } 56 else { 57 retVal = ""; 58 } 59 } 60 return retVal; 61 } 62 63 66 public static String replaceAll( 67 String input, 68 String forReplace, 69 String replaceWith ) { 70 StringBuffer result = new StringBuffer (); 71 boolean hasMore = true; 72 while ( hasMore ) { 73 int start = input.indexOf( forReplace ); 74 int end = start + forReplace.length(); 75 if ( start != -1 ) { 76 result.append( input.substring( 0, start ) + replaceWith ); 77 input = input.substring( end ); 78 } 79 else { 80 hasMore = false; 81 result.append( input ); 82 } 83 } 84 if ( result.toString().equals( "" ) ) 85 return input; else 87 return result.toString(); 88 } 89 90 92 97 public static String bytesToHexString( byte[] b ) { 98 String hexString = null; 99 try { 100 if ( b != null ) { 101 ByteArrayInputStream is = new ByteArrayInputStream( b ); 102 hexString = streamToHexString( is ); 103 return hexString; 104 } 105 else { 106 return null; 107 } 108 } 109 catch ( Exception e ) { 110 } 111 return hexString; 112 } 113 114 119 public static byte[] hexStringToBytes( String val ) { 120 byte[] buf = new byte[val.length() / 2]; 121 final char[] hexBytes = { 122 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 123 'E', 'F' 124 }; 125 byte[] hexMap = new byte[256]; 126 for ( int i = 0; i < hexBytes.length; i++ ) { 127 hexMap[hexBytes[i]] = ( byte ) i; 128 } 129 int pos = 0; 130 for ( int i = 0; i < buf.length; i++ ) { 131 buf[i] = ( byte ) ( hexMap[val.charAt( pos++ )] << 4 ); 132 buf[i] += hexMap[val.charAt( pos++ )]; 133 } 134 return buf; 135 } 136 137 143 public static String streamToHexString( InputStream is ) throws IOException { 144 try { 145 char[] hexBytes = { 146 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 147 'E', 'F'}; 148 int c; 149 StringBuffer hexString = new StringBuffer (); 150 while ( ( c = is.read() ) >= 0 ) { 151 hexString.append( hexBytes[ ( c >> 4 ) & 0xf] ); 152 hexString.append( hexBytes[c & 0xf] ); 153 } 154 return hexString.toString(); 155 } 156 catch ( Exception e ) { 157 throw new IOException( e.getMessage() ); 158 } 159 } 160 161 166 public static String replaceKeywords(String s, HashMap oldValues) { 167 String retVal = s; 168 int index = 1; 169 for (int i = 0; i < keywords.length; i++) { 170 StringBuffer result = new StringBuffer (); 171 boolean hasMore = true; 172 while (hasMore) { 173 int start = retVal.toUpperCase().indexOf(keywords[i].toUpperCase()); 174 int end = start + keywords[i].length(); 175 if (start != -1) { 176 String newValue = keywordEscape + String.valueOf(index); 177 while( oldValues.containsKey(newValue) ) { 178 index++; 179 newValue = keywordEscape + String.valueOf(index); 180 } 181 result.append(retVal.substring(0, start) + newValue); 182 oldValues.put(newValue, retVal.substring(start, end)); 183 retVal = retVal.substring(end); 184 } 185 else { 186 hasMore = false; 187 result.append(retVal); 188 } 189 } 190 if (!result.toString().equals("")) 191 retVal = result.toString(); 192 } 193 return retVal; 194 } 195 196 197 202 public static String replaceKeywordsBack(String s, HashMap oldValues) { 203 String retVal = s; 204 Set keys = oldValues.keySet(); 205 Iterator it = keys.iterator(); 206 Object key = ""; 207 String value = ""; 208 while( it.hasNext() ) { 209 key = it.next(); 210 value = (String )oldValues.get(key.toString()); 211 if( value != null ) 212 retVal = replaceAll(retVal, key.toString(), value.toString()); 213 } 214 return retVal; 215 } 216 217 } 218 219 | Popular Tags |