cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgritse...@apache.org
Subject svn commit: r414820 - in /cocoon/trunk/blocks/cocoon-databases: cocoon-databases-impl/src/main/java/org/apache/cocoon/transformation/SQLTransformer.java status.xml
Date Fri, 16 Jun 2006 12:58:54 GMT
Author: vgritsenko
Date: Fri Jun 16 05:58:54 2006
New Revision: 414820

URL: http://svn.apache.org/viewvc?rev=414820&view=rev
Log:
   <action dev="VG" type="fix">
     Read CLOBs using getCharacterStream, delegate character decoding
     to the database. Revert to old behavior if clob-encoding parameter is present.
   </action>

Modified:
    cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/transformation/SQLTransformer.java
    cocoon/trunk/blocks/cocoon-databases/status.xml

Modified: cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/transformation/SQLTransformer.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/transformation/SQLTransformer.java?rev=414820&r1=414819&r2=414820&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/transformation/SQLTransformer.java
(original)
+++ cocoon/trunk/blocks/cocoon-databases/cocoon-databases-impl/src/main/java/org/apache/cocoon/transformation/SQLTransformer.java
Fri Jun 16 05:58:54 2006
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2005 The Apache Software Foundation.
+ * Copyright 1999-2006 The Apache Software Foundation.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,8 +15,9 @@
  */
 package org.apache.cocoon.transformation;
 
-import java.io.IOException;
 import java.io.InputStream;
+import java.io.IOException;
+import java.io.Reader;
 import java.io.StringReader;
 import java.lang.reflect.Field;
 import java.sql.CallableStatement;
@@ -46,6 +47,7 @@
 import org.apache.cocoon.components.sax.XMLByteStreamCompiler;
 import org.apache.cocoon.components.sax.XMLByteStreamInterpreter;
 import org.apache.cocoon.environment.SourceResolver;
+import org.apache.cocoon.transformation.helpers.TextRecorder;
 import org.apache.cocoon.xml.IncludeXMLConsumer;
 
 import org.apache.commons.lang.StringEscapeUtils;
@@ -100,7 +102,7 @@
  * <p>
  * The following DTD is valid:
  * <code>
- * &lt;!ENTITY % param  "(own-connection?,(use-connection|(dburl,username,password))?,show-nr-or-rows?,doc-element?,row-element?,namespace-uri?,namespace-prefix?,clob-encoding?)"&gt;<br>
+ * &lt;!ENTITY % param "(own-connection?,(use-connection|(dburl,username,password))?,show-nr-or-rows?,doc-element?,row-element?,namespace-uri?,namespace-prefix?,clob-encoding?)"&gt;<br>
  * &lt;!ELEMENT execute-query (query,(in-parameter|out-parameter)*,execute-query?, %param;)&gt;<br>
  * &lt;!ELEMENT own-connection (#PCDATA)&gt;<br>
  * &lt;!ELEMENT use-connection (#PCDATA)&gt;<br>
@@ -131,6 +133,13 @@
  * </p>
  *
  * <p>
+ * By default, CLOBs are read from the database using character stream, so that character
+ * decoding is performed by the database. Using <code>clob-encoding</code> parameter,
+ * this behavior can be overrided, so that data is read as byte stream and decoded using
+ * specified character encoding.
+ * </p>
+ *
+ * <p>
  * TODO: Support inserting of the XML data into the database without need to escape it.
  *       Can be implemented by introducing new &lt;sql:xml/&gt; tag to indicate that
  *       startSerializedXMLRecording(...) should be used.
@@ -1123,6 +1132,9 @@
 
             this.showNrOfRows = parameters.getParameterAsBoolean(SQLTransformer.MAGIC_NR_OF_ROWS,
false);
             this.clobEncoding = parameters.getParameter(SQLTransformer.CLOB_ENCODING, "");
+            if (this.clobEncoding.length() == 0) {
+                this.clobEncoding = null;
+            }
 
             // Start prefix mapping for output namespace, only if it's not mapped yet
             final String prefix = SQLTransformer.this.findPrefixMapping(this.outUri);
@@ -1291,6 +1303,7 @@
         // for a given "name" versus number.  That being said this shouldn't be an issue
         // as this function is only called for ancestor lookups.
         protected String getColumnValue(String name) throws SQLException {
+            //noinspection UnnecessaryLocalVariable
             String retval = getStringValue(rs.getObject(name));
             // if (rs.getMetaData().getColumnType( name ) == java.sql.Types.DOUBLE)
             // retval = transformer.getStringValue( rs.getBigDecimal( name ) );
@@ -1484,9 +1497,14 @@
             if (object instanceof byte[]) {
                 // FIXME Encoding?
                 return new String((byte[]) object);
-            } else if (object instanceof char[]) {
+            }
+
+            if (object instanceof char[]) {
                 return new String((char[]) object);
-            } else if (object instanceof Clob) {
+            }
+
+            // Old behavior: Read bytes & decode
+            if (object instanceof Clob && this.clobEncoding != null) {
                 Clob clob = (Clob) object;
                 StringBuffer buffer = new StringBuffer();
                 InputStream is = clob.getAsciiStream();
@@ -1500,7 +1518,26 @@
                     throw new SQLException("Error reading stream from CLOB");
                 }
                 return buffer.toString();
-            } else if (object != null) {
+            }
+
+            // Correct behavior: Read character data
+            if (object instanceof Clob) {
+                Clob clob = (Clob) object;
+                StringBuffer buffer = new StringBuffer();
+                Reader cs = clob.getCharacterStream();
+                try {
+                    char[] chars = new char[BUFFER_SIZE];
+                    int n;
+                    while ((n = cs.read(chars)) > -1) {
+                        buffer.append(chars, 0, n);
+                    }
+                } catch (IOException e) {
+                    throw new SQLException("Error reading stream from CLOB");
+                }
+                return buffer.toString();
+            }
+
+            if (object != null) {
                 return object.toString();
             }
 

Modified: cocoon/trunk/blocks/cocoon-databases/status.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-databases/status.xml?rev=414820&r1=414819&r2=414820&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-databases/status.xml (original)
+++ cocoon/trunk/blocks/cocoon-databases/status.xml Fri Jun 16 05:58:54 2006
@@ -65,6 +65,10 @@
  <!-- The following list contains the changes since the latest 2.1.x version. -->
  <changes>
   <release version="@version@" date="@date@">
+   <action dev="VG" type="fix">
+     Read CLOBs using getCharacterStream, delegate character decoding
+     to the database. Revert to old behavior if clob-encoding parameter is present.
+   </action>
    <action dev="VG" type="fix" fixes-bug="COCOON-1247">
      Support multiple results in SQLTransformer.
    </action>



Mime
View raw message