You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by vg...@apache.org on 2006/10/03 14:48:52 UTC
svn commit: r452460 -
/cocoon/trunk/blocks/cocoon-xsp/cocoon-xsp-impl/src/main/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java
Author: vgritsenko
Date: Tue Oct 3 05:48:51 2006
New Revision: 452460
URL: http://svn.apache.org/viewvc?view=rev&rev=452460
Log:
copy from branch
Modified:
cocoon/trunk/blocks/cocoon-xsp/cocoon-xsp-impl/src/main/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java
Modified: cocoon/trunk/blocks/cocoon-xsp/cocoon-xsp-impl/src/main/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-xsp/cocoon-xsp-impl/src/main/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java?view=diff&rev=452460&r1=452459&r2=452460
==============================================================================
--- cocoon/trunk/blocks/cocoon-xsp/cocoon-xsp-impl/src/main/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java (original)
+++ cocoon/trunk/blocks/cocoon-xsp/cocoon-xsp-impl/src/main/java/org/apache/cocoon/components/language/markup/xsp/EsqlHelper.java Tue Oct 3 05:48:51 2006
@@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -16,377 +16,280 @@
*/
package org.apache.cocoon.components.language.markup.xsp;
-import org.apache.avalon.framework.CascadingRuntimeException;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.Reader;
-import java.sql.CallableStatement;
-import java.sql.ResultSet;
+import java.io.IOException;
import java.io.InputStream;
+import java.io.Reader;
+import java.lang.reflect.InvocationTargetException;
import java.sql.Blob;
+import java.sql.CallableStatement;
import java.sql.Clob;
+import java.sql.ResultSet;
+import java.sql.SQLException;
import java.sql.Types;
+import org.apache.avalon.framework.CascadingRuntimeException;
+
/**
* This is a helper class to remove redundant code in
- * esql pages
+ * esql pages.
+ *
+ * Based on the orginal esql.xsl.
*
- * based on the orginal esql.xsl
* @version $Id$
*/
-
public class EsqlHelper {
- private static Boolean TRUE;
+ private static void close(Object lob) {
+ if (lob == null) {
+ return;
+ }
- static {
- EsqlHelper.TRUE = Boolean.TRUE;
+ // ORACLE 'temporary lob' problem patch start
+ Class clazz = lob.getClass();
+ String name = clazz.getName();
+ if (name.equals("oracle.sql.BLOB") || name.equals("oracle.sql.CLOB")) {
+ try {
+ if (clazz.getMethod("isTemporary", new Class[0]).invoke(lob, new Object[0]).equals(Boolean.TRUE)) {
+ clazz.getMethod("freeTemporary", new Class[0]).invoke(lob, new Object[0]);
+ }
+ } catch (IllegalAccessException e) {
+ /* ignored */
+ } catch (InvocationTargetException e) {
+ /* ignored */
+ } catch (NoSuchMethodException e) {
+ /* ignored */
+ }
+ }
}
- /** returns byte array from BLOB
- */
- public final static byte[] getBlob(ResultSet set, String column) throws RuntimeException {
+ private static byte[] readBytes(Blob blob, byte[] defaultValue)
+ throws SQLException, IOException {
+ if (blob == null) {
+ return defaultValue;
+ }
- byte[] result = null;
+ InputStream is = null;
try {
- result = EsqlHelper.getBlob(set, set.findColumn(column));
- } catch (Exception e) {
- throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
+ int length = (int) blob.length();
+ if (length == 0) {
+ return defaultValue;
+ }
+
+ byte[] buffer = new byte[length];
+ is = blob.getBinaryStream();
+ is.read(buffer);
+
+ return buffer;
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException e) { /* ignored */ }
+ close(blob);
+ }
+ }
+
+ private static String readAscii(Clob clob, String defaultValue)
+ throws SQLException, IOException {
+ if (clob == null) {
+ return defaultValue;
+ }
+
+ InputStream is = null;
+ try {
+ int length = (int) clob.length();
+ if (length == 0) {
+ return defaultValue;
+ }
+
+ byte[] buffer = new byte[length];
+ is = clob.getAsciiStream();
+ is.read(buffer);
+
+ return new String(buffer, 0, length);
+ } finally {
+ try {
+ if (is != null) {
+ is.close();
+ }
+ } catch (IOException e) { /* ignored */ }
+ close(clob);
}
- return result;
}
- /** returns byte array from BLOB
- */
- public final static byte[] getBlob(ResultSet set, int column) throws java.lang.Exception {
+ private static String read(Clob clob, String defaultValue)
+ throws SQLException, IOException {
+ if (clob == null) {
+ return defaultValue;
+ }
- InputStream reader = null;
- byte[] buffer = null;
- Blob dbBlob = null;
+ Reader r = null;
+ try {
+ int length = (int) clob.length();
+ if (length == 0) {
+ return defaultValue;
+ }
+ char[] buffer = new char[length];
+ r = clob.getCharacterStream();
+ r.read(buffer);
+
+ return new String(buffer, 0, length);
+ } finally {
+ try {
+ if (r != null) {
+ r.close();
+ }
+ } catch (IOException e) { /* ignored */ }
+ close(clob);
+ }
+ }
+
+ /** returns byte array from BLOB */
+ public static byte[] getBlob(ResultSet set, String column)
+ throws RuntimeException {
+ try {
+ return EsqlHelper.getBlob(set, set.findColumn(column));
+ } catch (Exception e) {
+ throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
+ }
+ }
+
+ /** returns byte array from BLOB */
+ public static byte[] getBlob(ResultSet set, int column)
+ throws Exception {
try {
if (set.getMetaData().getColumnType(column) == java.sql.Types.BLOB) {
- dbBlob = set.getBlob(column);
- int length = (int) dbBlob.length();
- reader = dbBlob.getBinaryStream();
- buffer = new byte[length];
- reader.read(buffer);
- reader.close();
- if (reader != null)
- reader.close();
- if (buffer == null)
- return null;
- return buffer;
+ return readBytes(set.getBlob(column), null);
} else {
- return set.getString(column).getBytes();
+ String value = set.getString(column);
+ if (value == null) {
+ return null;
+ }
+ return value.getBytes();
}
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
- } finally {
- // ORACLE 'temporary lob' problem patch start
- if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) {
- if (dbBlob
- .getClass()
- .getMethod("isTemporary", new Class[0])
- .invoke(dbBlob, new Object[0])
- .equals(TRUE))
- dbBlob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
- dbBlob,
- new Object[0]);
- }
}
}
- /** returns byte array from BLOB
- */
- public final static byte[] getBlob(CallableStatement cs, int column, String defaultString)
- throws java.lang.Exception {
-
- InputStream reader = null;
- byte[] buffer = null;
- byte[] result = null;
- Blob dbBlob = null;
-
- try {
- dbBlob = cs.getBlob(column);
- int length = (int) dbBlob.length();
- reader = dbBlob.getBinaryStream();
- buffer = new byte[length];
- reader.read(buffer);
- reader.close();
- if (reader != null)
- reader.close();
- if (buffer != null)
- result = buffer;
- else if (defaultString != null && !defaultString.equals("_null_"))
- result = defaultString.getBytes();
- else
- result = null;
+ /** returns byte array from BLOB */
+ public static byte[] getBlob(CallableStatement cs, int column, String defaultString)
+ throws Exception {
+
+ byte[] defaultValue = null;
+ if (defaultString != null && !defaultString.equals("_null_")) {
+ defaultValue = defaultString.getBytes();
+ }
+
+ try {
+ if (cs.getMetaData().getColumnType(column) == java.sql.Types.BLOB) {
+ return readBytes(cs.getBlob(column), defaultValue);
+ } else {
+ String value = cs.getString(column);
+ if (value == null) {
+ return defaultValue;
+ }
+ return value.getBytes();
+ }
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting blob data for column " + column, e);
- } finally {
- // ORACLE 'temporary lob' problem patch start
- if (dbBlob != null && dbBlob.getClass().getName().equals("oracle.sql.BLOB")) {
- if (dbBlob
- .getClass()
- .getMethod("isTemporary", new Class[0])
- .invoke(dbBlob, new Object[0])
- .equals(TRUE))
- dbBlob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
- dbBlob,
- new Object[0]);
- }
}
- return result;
}
- /** returns Unicode encoded string from CLOB or String column
- */
- public final static String getStringOrClob(ResultSet set, String column, String defaultString)
- throws RuntimeException {
-
- String result = null;
+ /** returns Unicode encoded string from CLOB or String column */
+ public static String getStringOrClob(ResultSet set, String column, String defaultString)
+ throws RuntimeException {
try {
- result = EsqlHelper.getStringOrClob(set, set.findColumn(column), defaultString);
+ return EsqlHelper.getStringOrClob(set, set.findColumn(column), defaultString);
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting text from column " + column, e);
}
- return result;
}
- /** returns Unicode encoded string from CLOB or String column
- */
- public final static String getStringOrClob(ResultSet set, int column, String defaultString)
- throws java.lang.Exception {
-
- Reader reader = null;
- char[] buffer = null;
- String result = null;
- Clob dbClob = null;
+ /** returns Unicode encoded string from CLOB or String column */
+ public static String getStringOrClob(ResultSet set, int column, String defaultString)
+ throws Exception {
+ if (defaultString != null && defaultString.equals("_null_")) {
+ defaultString = null;
+ }
try {
if (set.getMetaData().getColumnType(column) == java.sql.Types.CLOB) {
- dbClob = set.getClob(column);
- int length = (int) dbClob.length();
- reader = new BufferedReader(dbClob.getCharacterStream());
- buffer = new char[length];
- reader.read(buffer);
- reader.close();
- if (reader != null)
- reader.close();
- if (buffer != null)
- result = new String(buffer);
- else if (defaultString != null && !defaultString.equals("_null_"))
- result = defaultString;
- else
- result = null;
+ return read(set.getClob(column), defaultString);
} else {
- result = set.getString(column);
- if (result == null && defaultString != null && !defaultString.equals("_null_"))
+ String result = set.getString(column);
+ if (result == null) {
result = defaultString;
+ }
+ return result;
}
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting text from column " + column, e);
- } finally {
- // ORACLE 'temporary lob' problem patch start
- if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
- try {
- if (dbClob
- .getClass()
- .getMethod("isTemporary", new Class[0])
- .invoke(dbClob, new Object[0])
- .equals(TRUE))
- dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
- dbClob,
- new Object[0]);
- } catch (Exception e1) {
- // swallow
- }
- }
}
- return result;
}
- /** returns Unicode encoded string from CLOB or String column
- */
- public final static String getStringOrClob(
- CallableStatement cs,
- int column,
- String defaultString)
- throws java.lang.Exception {
-
- Reader reader = null;
- char[] buffer = null;
- String result = null;
- Clob dbClob = null;
-
- try {
- dbClob = cs.getClob(column);
- int length = (int) dbClob.length();
- reader = new BufferedReader(dbClob.getCharacterStream());
- buffer = new char[length];
- reader.read(buffer);
- reader.close();
- if (reader != null)
- reader.close();
- if (buffer != null)
- result = new String(buffer);
- else if (defaultString != null && !defaultString.equals("_null_"))
- result = defaultString;
- else
- result = null;
+ /** returns Unicode encoded string from CLOB or String column */
+ public static String getStringOrClob(CallableStatement cs,
+ int column,
+ String defaultString)
+ throws Exception {
+ if (defaultString != null && defaultString.equals("_null_")) {
+ defaultString = null;
+ }
+
+ try {
+ return read(cs.getClob(column), defaultString);
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting text from column " + column, e);
- } finally {
- // ORACLE 'temporary lob' problem patch start
- if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
- try {
- if (dbClob
- .getClass()
- .getMethod("isTemporary", new Class[0])
- .invoke(dbClob, new Object[0])
- .equals(TRUE))
- dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
- dbClob,
- new Object[0]);
- } catch (Exception e1) {
- // swallow
- }
- }
}
- return result;
}
- /** returns ascii string from CLOB or String column
- */
- public final static String getAscii(ResultSet set, String column, String defaultString)
- throws RuntimeException {
-
- String result = null;
+ /** returns ascii string from CLOB or String column */
+ public static String getAscii(ResultSet set, String column, String defaultString)
+ throws RuntimeException {
try {
- result = EsqlHelper.getAscii(set, set.findColumn(column), defaultString);
+ int colIndex = set.findColumn(column);
+ return EsqlHelper.getAscii(set, colIndex, defaultString);
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting ascii data for column " + column, e);
}
- return result;
}
- /** returns ascii string from CLOB or String column
- */
- public final static String getAscii(ResultSet set, int column, String defaultString) {
- InputStream asciiStream = null;
- String result = null;
- Clob dbClob = null;
+ /** returns ascii string from CLOB or String column */
+ public static String getAscii(ResultSet set, int column, String defaultString) {
+ if (defaultString != null && defaultString.equals("_null_")) {
+ defaultString = null;
+ }
try {
if (set.getMetaData().getColumnType(column) == Types.CLOB) {
- byte[] buffer = null;
- dbClob = set.getClob(column);
- int length = (int) dbClob.length();
- asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
- buffer = new byte[length];
- asciiStream.read(buffer);
- asciiStream.close();
- if (buffer != null)
- result = new String(buffer);
- else if (defaultString != null && !defaultString.equals("_null_"))
- result = defaultString;
- else
- result = null;
+ return readAscii(set.getClob(column), defaultString);
} else {
- result = set.getString(column);
- if (result == null && defaultString != null && !defaultString.equals("_null_"))
+ String result = set.getString(column);
+ if (result == null) {
result = defaultString;
- }
- } catch (Exception e) {
- throw new CascadingRuntimeException(
- "Error getting ascii data from column " + column,
- e);
- } finally {
- if (asciiStream != null) {
- try {
- asciiStream.close();
- } catch (Exception ase) {
- throw new CascadingRuntimeException("Error closing clob stream", ase);
}
+ return result;
}
- // ORACLE 'temporary lob' problem patch start
- if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
- try {
- if (dbClob
- .getClass()
- .getMethod("isTemporary", new Class[0])
- .invoke(dbClob, new Object[0])
- .equals(TRUE))
- dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
- dbClob,
- new Object[0]);
- } catch (Exception e1) {
- // swallow
- }
- }
-
+ } catch (Exception e) {
+ throw new CascadingRuntimeException("Error getting ascii data from column " + column, e);
}
-
- return result;
}
- /** returns ascii string from CLOB or String column
- */
- public final static String getAscii(CallableStatement cs, int column, String defaultString) {
- InputStream asciiStream = null;
- String result = null;
- Clob dbClob = null;
-
- try {
- byte[] buffer = null;
- dbClob = cs.getClob(column);
- int length = (int) dbClob.length();
- asciiStream = new BufferedInputStream(dbClob.getAsciiStream());
- buffer = new byte[length];
- asciiStream.read(buffer);
- asciiStream.close();
- if (buffer != null)
- result = new String(buffer);
- else if (defaultString != null && !defaultString.equals("_null_"))
- result = defaultString;
- else
- result = null;
+ /** returns ascii string from CLOB or String column */
+ public static String getAscii(CallableStatement cs, int column, String defaultString) {
+ try {
+ return readAscii(cs.getClob(column), defaultString);
} catch (Exception e) {
throw new CascadingRuntimeException("Error getting ascii data for column " + column, e);
- } finally {
- if (asciiStream != null) {
- try {
- asciiStream.close();
- } catch (Exception ase) {
- throw new CascadingRuntimeException("Error closing clob stream", ase);
- }
- }
- // ORACLE 'temporary lob' problem patch start
- if (dbClob != null && dbClob.getClass().getName().equals("oracle.sql.CLOB")) {
- try {
- if (dbClob
- .getClass()
- .getMethod("isTemporary", new Class[0])
- .invoke(dbClob, new Object[0])
- .equals(TRUE))
- dbClob.getClass().getMethod("freeTemporary", new Class[0]).invoke(
- dbClob,
- new Object[0]);
- } catch (Exception e1) {
- // swallow
- }
- }
}
-
- return result;
}
- public final static String getStringFromByteArray(
- byte[] bytes,
- String encoding,
- String defaultString) {
+ public static String getStringFromByteArray(byte[] bytes,
+ String encoding,
+ String defaultString) {
if (bytes != null) {
try {
return new String(bytes, encoding);
@@ -394,11 +297,11 @@
throw new CascadingRuntimeException("Unsupported Encoding Exception", uee);
}
} else {
- if (defaultString != null && !defaultString.equals("_null_"))
+ if (defaultString != null && !defaultString.equals("_null_")) {
return defaultString;
- else
+ } else {
return null; /* before was "" but null is more consequent */
+ }
}
}
-
}