KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > EsqlHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.language.markup.xsp;
17
18 import org.apache.avalon.framework.CascadingRuntimeException;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.BufferedReader JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.sql.CallableStatement JavaDoc;
24 import java.sql.ResultSet JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.sql.Blob JavaDoc;
27 import java.sql.Clob JavaDoc;
28 import java.sql.Types JavaDoc;
29
30 /**
31  * This is a helper class to remove redundant code in
32  * esql pages
33  *
34  * based on the orginal esql.xsl
35  * @author <a HREF="mailto:tcurdt@dff.st">Torsten Curdt</a>
36  * @version CVS $Id: EsqlHelper.java 125961 2005-01-21 20:50:08Z antonio $
37  */

38
39 public class EsqlHelper {
40
41     /** returns byte array from BLOB
42      */

43     public final static byte[] getBlob(ResultSet JavaDoc set, String JavaDoc column) throws RuntimeException JavaDoc {
44
45         try {
46             return EsqlHelper.getBlob(set, set.findColumn(column));
47         } catch (Exception JavaDoc e) {
48             throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
49         }
50     }
51
52     /** returns byte array from BLOB
53      */

54     public final static byte[] getBlob(ResultSet JavaDoc set, int column) throws java.lang.Exception JavaDoc {
55
56         InputStream JavaDoc reader = null;
57         byte[] buffer = null;
58         Blob JavaDoc 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 JavaDoc e) {
73             throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
74         } finally {
75             // ORACLE 'temporary lob' problem patch start
76
if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) {
77                 if (dbBlob
78                     .getClass()
79                     .getMethod("isTemporary", new Class JavaDoc[0])
80                     .invoke(dbBlob, new Object JavaDoc[0])
81                     .equals(Boolean.TRUE))
82                     dbBlob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]).invoke(
83                         dbBlob,
84                         new Object JavaDoc[0]);
85             }
86         }
87     }
88
89     /** returns byte array from BLOB
90      */

91     public final static byte[] getBlob(CallableStatement JavaDoc cs, int column, String JavaDoc defaultString)
92         throws java.lang.Exception JavaDoc {
93
94         InputStream JavaDoc reader = null;
95         byte[] buffer = null;
96         byte[] result = null;
97         Blob JavaDoc 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 JavaDoc e) {
114             throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
115         } finally {
116             // ORACLE 'temporary lob' problem patch start
117
if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) {
118                 if (dbBlob
119                     .getClass()
120                     .getMethod("isTemporary", new Class JavaDoc[0])
121                     .invoke(dbBlob, new Object JavaDoc[0])
122                     .equals(Boolean.TRUE)) {
123                     dbBlob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]).invoke(
124                         dbBlob,
125                         new Object JavaDoc[0]);
126                 }
127             }
128         }
129         return result;
130     }
131
132     /** returns Unicode encoded string from CLOB or String column
133      */

134     public final static String JavaDoc getStringOrClob(ResultSet JavaDoc set, String JavaDoc column, String JavaDoc defaultString)
135         throws RuntimeException JavaDoc {
136
137         try {
138             return EsqlHelper.getStringOrClob(set, set.findColumn(column), defaultString);
139         } catch (Exception JavaDoc e) {
140             throw new CascadingRuntimeException("Error getting text from column " + column, e);
141         }
142     }
143
144     /** returns Unicode encoded string from CLOB or String column
145      */

146     public final static String JavaDoc getStringOrClob(ResultSet JavaDoc set, int column, String JavaDoc defaultString)
147         throws java.lang.Exception JavaDoc {
148
149         Reader JavaDoc reader = null;
150         char[] buffer = null;
151         String JavaDoc result = null;
152         Clob JavaDoc 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 JavaDoc(dbClob.getCharacterStream());
159                 buffer = new char[length];
160                 reader.read(buffer);
161                 if (buffer != null) {
162                     result = new String JavaDoc(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 JavaDoc e) {
174             throw new CascadingRuntimeException("Error getting text from column " + column, e);
175         } finally {
176             if (reader != null) {
177                 reader.close();
178             }
179             // ORACLE 'temporary lob' problem patch start
180
if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
181                 try {
182                     if (dbClob
183                         .getClass()
184                         .getMethod("isTemporary", new Class JavaDoc[0])
185                         .invoke(dbClob, new Object JavaDoc[0])
186                         .equals(Boolean.TRUE)) {
187                         dbClob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]).invoke(
188                             dbClob,
189                             new Object JavaDoc[0]);
190                     }
191                 } catch (Exception JavaDoc e1) {
192                     // swallow
193
}
194             }
195         }
196         return result;
197     }
198
199     /** returns Unicode encoded string from CLOB or String column
200      */

201     public final static String JavaDoc getStringOrClob(
202         CallableStatement JavaDoc cs,
203         int column,
204         String JavaDoc defaultString)
205         throws java.lang.Exception JavaDoc {
206
207         Reader JavaDoc reader = null;
208         char[] buffer = null;
209         String JavaDoc result = null;
210         Clob JavaDoc dbClob = null;
211
212         try {
213             dbClob = cs.getClob(column);
214             int length = (int) dbClob.length();
215             reader = new BufferedReader JavaDoc(dbClob.getCharacterStream());
216             buffer = new char[length];
217             reader.read(buffer);
218             if (buffer != null) {
219                 result = new String JavaDoc(buffer);
220             } else if (defaultString != null && !defaultString.equals("_null_")) {
221                 result = defaultString;
222             } else {
223                 result = null;
224             }
225         } catch (Exception JavaDoc e) {
226             throw new CascadingRuntimeException("Error getting text from column " + column, e);
227         } finally {
228             if (reader != null) {
229                 reader.close();
230             }
231             // ORACLE 'temporary lob' problem patch start
232
if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
233                 try {
234                     if (dbClob
235                         .getClass()
236                         .getMethod("isTemporary", new Class JavaDoc[0])
237                         .invoke(dbClob, new Object JavaDoc[0])
238                         .equals(Boolean.TRUE))
239                         dbClob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]).invoke(
240                             dbClob,
241                             new Object JavaDoc[0]);
242                 } catch (Exception JavaDoc e1) {
243                     // swallow
244
}
245             }
246         }
247         return result;
248     }
249
250     /** returns ascii string from CLOB or String column
251      */

252     public final static String JavaDoc getAscii(ResultSet JavaDoc set, String JavaDoc column, String JavaDoc defaultString)
253         throws RuntimeException JavaDoc {
254
255         try {
256             int colIndex = set.findColumn(column);
257             return EsqlHelper.getAscii(set, colIndex, defaultString);
258         } catch (Exception JavaDoc e) {
259             throw new CascadingRuntimeException("Error getting ascii data for column " + column, e);
260         }
261     }
262
263     /** returns ascii string from CLOB or String column
264      */

265     public final static String JavaDoc getAscii(ResultSet JavaDoc set, int column, String JavaDoc defaultString) {
266         InputStream JavaDoc asciiStream = null;
267         String JavaDoc result = null;
268         Clob JavaDoc 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 JavaDoc(dbClob.getAsciiStream());
276                 buffer = new byte[length];
277                 asciiStream.read(buffer);
278                 asciiStream.close();
279                 if (buffer != null) {
280                     result = new String JavaDoc(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 JavaDoc 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 JavaDoc ase) {
300                     throw new CascadingRuntimeException("Error closing clob stream", ase);
301                 }
302             }
303             // ORACLE 'temporary lob' problem patch start
304
if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
305                 try {
306                     if (dbClob
307                         .getClass()
308                         .getMethod("isTemporary", new Class JavaDoc[0])
309                         .invoke(dbClob, new Object JavaDoc[0])
310                         .equals(Boolean.TRUE)) {
311                         dbClob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]).invoke(
312                             dbClob,
313                             new Object JavaDoc[0]);
314                     }
315                 } catch (Exception JavaDoc e1) {
316                     // swallow
317
}
318             }
319         }
320         return result;
321     }
322
323     /** returns ascii string from CLOB or String column
324      */

325     public final static String JavaDoc getAscii(CallableStatement JavaDoc cs, int column, String JavaDoc defaultString) {
326         InputStream JavaDoc asciiStream = null;
327         String JavaDoc result = null;
328         Clob JavaDoc dbClob = null;
329
330         try {
331             byte[] buffer = null;
332             dbClob = cs.getClob(column);
333             int length = (int) dbClob.length();
334             asciiStream = new BufferedInputStream JavaDoc(dbClob.getAsciiStream());
335             buffer = new byte[length];
336             asciiStream.read(buffer);
337             asciiStream.close();
338             if (buffer != null) {
339                 result = new String JavaDoc(buffer);
340             } else if (defaultString != null && !defaultString.equals("_null_")) {
341                 result = defaultString;
342             } else {
343                 result = null;
344             }
345         } catch (Exception JavaDoc 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 JavaDoc ase) {
352                     throw new CascadingRuntimeException("Error closing clob stream", ase);
353                 }
354             }
355             // ORACLE 'temporary lob' problem patch start
356
if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
357                 try {
358                     if (dbClob
359                         .getClass()
360                         .getMethod("isTemporary", new Class JavaDoc[0])
361                         .invoke(dbClob, new Object JavaDoc[0])
362                         .equals(Boolean.TRUE)) {
363                         dbClob.getClass().getMethod("freeTemporary", new Class JavaDoc[0]).invoke(
364                             dbClob,
365                             new Object JavaDoc[0]);
366                     }
367                 } catch (Exception JavaDoc e1) {
368                     // swallow
369
}
370             }
371         }
372         return result;
373     }
374
375     public final static String JavaDoc getStringFromByteArray(
376         byte[] bytes,
377         String JavaDoc encoding,
378         String JavaDoc defaultString) {
379         if (bytes != null) {
380             try {
381                 return new String JavaDoc(bytes, encoding);
382             } catch (java.io.UnsupportedEncodingException JavaDoc 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; /* before was "" but null is more consequent */
390             }
391         }
392     }
393 }
394
Popular Tags