1 16 package org.apache.cocoon.components.language.markup.xsp; 17 18 import org.apache.avalon.framework.CascadingRuntimeException; 19 20 import java.io.BufferedInputStream ; 21 import java.io.BufferedReader ; 22 import java.io.Reader ; 23 import java.sql.CallableStatement ; 24 import java.sql.ResultSet ; 25 import java.io.InputStream ; 26 import java.sql.Blob ; 27 import java.sql.Clob ; 28 import java.sql.Types ; 29 30 38 39 public class EsqlHelper { 40 41 43 public final static byte[] getBlob(ResultSet set, String column) throws RuntimeException { 44 45 try { 46 return EsqlHelper.getBlob(set, set.findColumn(column)); 47 } catch (Exception e) { 48 throw new CascadingRuntimeException("Error getting blob data for column " + column, e); 49 } 50 } 51 52 54 public final static byte[] getBlob(ResultSet set, int column) throws java.lang.Exception { 55 56 InputStream reader = null; 57 byte[] buffer = null; 58 Blob dbBlob = null; 59 60 try { 61 if (set.getMetaData().getColumnType(column) == java.sql.Types.BLOB) { 62 dbBlob = set.getBlob(column); 63 int length = (int) dbBlob.length(); 64 reader = dbBlob.getBinaryStream(); 65 buffer = new byte[length]; 66 reader.read(buffer); 67 reader.close(); 68 return buffer != null ? buffer : null; 69 } else { 70 return set.getString(column).getBytes(); 71 } 72 } catch (Exception e) { 73 throw new CascadingRuntimeException("Error getting blob data for column " + column, e); 74 } finally { 75 if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) { 77 if (dbBlob 78 .getClass() 79 .getMethod("isTemporary", new Class [0]) 80 .invoke(dbBlob, new Object [0]) 81 .equals(Boolean.TRUE)) 82 dbBlob.getClass().getMethod("freeTemporary", new Class [0]).invoke( 83 dbBlob, 84 new Object [0]); 85 } 86 } 87 } 88 89 91 public final static byte[] getBlob(CallableStatement cs, int column, String defaultString) 92 throws java.lang.Exception { 93 94 InputStream reader = null; 95 byte[] buffer = null; 96 byte[] result = null; 97 Blob dbBlob = null; 98 99 try { 100 dbBlob = cs.getBlob(column); 101 int length = (int) dbBlob.length(); 102 reader = dbBlob.getBinaryStream(); 103 buffer = new byte[length]; 104 reader.read(buffer); 105 reader.close(); 106 if (buffer != null) { 107 result = buffer; 108 } else if (defaultString != null && !defaultString.equals("_null_")) { 109 result = defaultString.getBytes(); 110 } else { 111 result = null; 112 } 113 } catch (Exception e) { 114 throw new CascadingRuntimeException("Error getting blob data for column " + column, e); 115 } finally { 116 if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) { 118 if (dbBlob 119 .getClass() 120 .getMethod("isTemporary", new Class [0]) 121 .invoke(dbBlob, new Object [0]) 122 .equals(Boolean.TRUE)) { 123 dbBlob.getClass().getMethod("freeTemporary", new Class [0]).invoke( 124 dbBlob, 125 new Object [0]); 126 } 127 } 128 } 129 return result; 130 } 131 132 134 public final static String getStringOrClob(ResultSet set, String column, String defaultString) 135 throws RuntimeException { 136 137 try { 138 return EsqlHelper.getStringOrClob(set, set.findColumn(column), defaultString); 139 } catch (Exception e) { 140 throw new CascadingRuntimeException("Error getting text from column " + column, e); 141 } 142 } 143 144 146 public final static String getStringOrClob(ResultSet set, int column, String defaultString) 147 throws java.lang.Exception { 148 149 Reader reader = null; 150 char[] buffer = null; 151 String result = null; 152 Clob dbClob = null; 153 154 try { 155 if (set.getMetaData().getColumnType(column) == java.sql.Types.CLOB) { 156 dbClob = set.getClob(column); 157 int length = (int) dbClob.length(); 158 reader = new BufferedReader (dbClob.getCharacterStream()); 159 buffer = new char[length]; 160 reader.read(buffer); 161 if (buffer != null) { 162 result = new String (buffer); 163 } else if (defaultString != null && !defaultString.equals("_null_")) { 164 result = defaultString; 165 } else { 166 result = null; 167 } 168 } else { 169 result = set.getString(column); 170 if (result == null && defaultString != null && !defaultString.equals("_null_")) 171 result = defaultString; 172 } 173 } catch (Exception e) { 174 throw new CascadingRuntimeException("Error getting text from column " + column, e); 175 } finally { 176 if (reader != null) { 177 reader.close(); 178 } 179 if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) { 181 try { 182 if (dbClob 183 .getClass() 184 .getMethod("isTemporary", new Class [0]) 185 .invoke(dbClob, new Object [0]) 186 .equals(Boolean.TRUE)) { 187 dbClob.getClass().getMethod("freeTemporary", new Class [0]).invoke( 188 dbClob, 189 new Object [0]); 190 } 191 } catch (Exception e1) { 192 } 194 } 195 } 196 return result; 197 } 198 199 201 public final static String getStringOrClob( 202 CallableStatement cs, 203 int column, 204 String defaultString) 205 throws java.lang.Exception { 206 207 Reader reader = null; 208 char[] buffer = null; 209 String result = null; 210 Clob dbClob = null; 211 212 try { 213 dbClob = cs.getClob(column); 214 int length = (int) dbClob.length(); 215 reader = new BufferedReader (dbClob.getCharacterStream()); 216 buffer = new char[length]; 217 reader.read(buffer); 218 if (buffer != null) { 219 result = new String (buffer); 220 } else if (defaultString != null && !defaultString.equals("_null_")) { 221 result = defaultString; 222 } else { 223 result = null; 224 } 225 } catch (Exception e) { 226 throw new CascadingRuntimeException("Error getting text from column " + column, e); 227 } finally { 228 if (reader != null) { 229 reader.close(); 230 } 231 if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) { 233 try { 234 if (dbClob 235 .getClass() 236 .getMethod("isTemporary", new Class [0]) 237 .invoke(dbClob, new Object [0]) 238 .equals(Boolean.TRUE)) 239 dbClob.getClass().getMethod("freeTemporary", new Class [0]).invoke( 240 dbClob, 241 new Object [0]); 242 } catch (Exception e1) { 243 } 245 } 246 } 247 return result; 248 } 249 250 252 public final static String getAscii(ResultSet set, String column, String defaultString) 253 throws RuntimeException { 254 255 try { 256 int colIndex = set.findColumn(column); 257 return EsqlHelper.getAscii(set, colIndex, defaultString); 258 } catch (Exception e) { 259 throw new CascadingRuntimeException("Error getting ascii data for column " + column, e); 260 } 261 } 262 263 265 public final static String getAscii(ResultSet set, int column, String defaultString) { 266 InputStream asciiStream = null; 267 String result = null; 268 Clob dbClob = null; 269 270 try { 271 if (set.getMetaData().getColumnType(column) == Types.CLOB) { 272 byte[] buffer = null; 273 dbClob = set.getClob(column); 274 int length = (int) dbClob.length(); 275 asciiStream = new BufferedInputStream (dbClob.getAsciiStream()); 276 buffer = new byte[length]; 277 asciiStream.read(buffer); 278 asciiStream.close(); 279 if (buffer != null) { 280 result = new String (buffer); 281 } else if (defaultString != null && !defaultString.equals("_null_")) { 282 result = defaultString; 283 } else { 284 result = null; 285 } 286 } else { 287 result = set.getString(column); 288 if (result == null && defaultString != null && !defaultString.equals("_null_")) { 289 result = defaultString; 290 } 291 } 292 } catch (Exception e) { 293 throw new CascadingRuntimeException( 294 "Error getting ascii data from column " + column, e); 295 } finally { 296 if (asciiStream != null) { 297 try { 298 asciiStream.close(); 299 } catch (Exception ase) { 300 throw new CascadingRuntimeException("Error closing clob stream", ase); 301 } 302 } 303 if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) { 305 try { 306 if (dbClob 307 .getClass() 308 .getMethod("isTemporary", new Class [0]) 309 .invoke(dbClob, new Object [0]) 310 .equals(Boolean.TRUE)) { 311 dbClob.getClass().getMethod("freeTemporary", new Class [0]).invoke( 312 dbClob, 313 new Object [0]); 314 } 315 } catch (Exception e1) { 316 } 318 } 319 } 320 return result; 321 } 322 323 325 public final static String getAscii(CallableStatement cs, int column, String defaultString) { 326 InputStream asciiStream = null; 327 String result = null; 328 Clob dbClob = null; 329 330 try { 331 byte[] buffer = null; 332 dbClob = cs.getClob(column); 333 int length = (int) dbClob.length(); 334 asciiStream = new BufferedInputStream (dbClob.getAsciiStream()); 335 buffer = new byte[length]; 336 asciiStream.read(buffer); 337 asciiStream.close(); 338 if (buffer != null) { 339 result = new String (buffer); 340 } else if (defaultString != null && !defaultString.equals("_null_")) { 341 result = defaultString; 342 } else { 343 result = null; 344 } 345 } catch (Exception e) { 346 throw new CascadingRuntimeException("Error getting ascii data for column " + column, e); 347 } finally { 348 if (asciiStream != null) { 349 try { 350 asciiStream.close(); 351 } catch (Exception ase) { 352 throw new CascadingRuntimeException("Error closing clob stream", ase); 353 } 354 } 355 if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) { 357 try { 358 if (dbClob 359 .getClass() 360 .getMethod("isTemporary", new Class [0]) 361 .invoke(dbClob, new Object [0]) 362 .equals(Boolean.TRUE)) { 363 dbClob.getClass().getMethod("freeTemporary", new Class [0]).invoke( 364 dbClob, 365 new Object [0]); 366 } 367 } catch (Exception e1) { 368 } 370 } 371 } 372 return result; 373 } 374 375 public final static String getStringFromByteArray( 376 byte[] bytes, 377 String encoding, 378 String defaultString) { 379 if (bytes != null) { 380 try { 381 return new String (bytes, encoding); 382 } catch (java.io.UnsupportedEncodingException uee) { 383 throw new CascadingRuntimeException("Unsupported Encoding Exception", uee); 384 } 385 } else { 386 if (defaultString != null && !defaultString.equals("_null_")) { 387 return defaultString; 388 } else { 389 return null; 390 } 391 } 392 } 393 } 394 | Popular Tags |