cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject svn commit: r370199 - /cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/generation/CSVGenerator.java
Date Wed, 18 Jan 2006 17:40:32 GMT
Author: jbq
Date: Wed Jan 18 09:40:28 2006
New Revision: 370199

URL: http://svn.apache.org/viewcvs?rev=370199&view=rev
Log:
CSVGenerator now looks for the sitemap parameter "process-headers" (with the s
at the end) as told in the documentation.  Accepts a new sitemap parameter
"max-records" which allows to limit the number of records to read.  Fixes a
caching bug: the same result was returned notwithstanding the modification of
separator or escape character.

Reported by Philippe Laplanche <philippe.laplanche@horus-si.com>

Modified:
    cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/generation/CSVGenerator.java

Modified: cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/generation/CSVGenerator.java
URL: http://svn.apache.org/viewcvs/cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/generation/CSVGenerator.java?rev=370199&r1=370198&r2=370199&view=diff
==============================================================================
--- cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/generation/CSVGenerator.java
(original)
+++ cocoon/trunk/cocoon-core/src/main/java/org/apache/cocoon/generation/CSVGenerator.java
Wed Jan 18 09:40:28 2006
@@ -44,6 +44,10 @@
  *     different if this is <i>true</i> or <i>false</i> (default:
<i>false</i>).
  *   </li>
  *   <li>
+ *     <b>max-records</b>: the maximum number of records to read
+ *     (default: <i>-1</i> read all records).
+ *   </li>
+ *   <li>
  *     <b>encoding</b>: the character encoding (UTF-8, ISO8859-1, ...) used to
  *     interpret the input CSV source file (default: <i>system default</i>).
  *   </li>
@@ -90,6 +94,9 @@
  * any standard body, so the interpretation of the format might be slightly
  * different in cases.</p>
  *
+ * @author <a href="mailto:pier@apache.org">Pier Fumagalli</a>
+ * @author Copyright &copy; 2000-2004 <a href="http://www.apache.org/">The Apache
+ *         Software Foundation</a>. All rights reserved.
  */
 public class CSVGenerator extends FileGenerator {
 
@@ -107,6 +114,7 @@
     private static final String DEFAULT_ESCAPE = "\"";
     /** <p>The default field separator character.</p> */
     private static final int DEFAULT_BUFFER_SIZE = 4096;
+    private static final int UNLIMITED_MAXRECORDS = -1;
     /** <p>A string used for indenting.</p> */
     private static final char INDENT_STRING[] = "\n          ".toCharArray();
 
@@ -122,6 +130,8 @@
     private int fieldnumber = 1;
     /** <p>The current record (line) number in the current CSV.</p> */
     private int recordnumber = 1;
+    /** <p>The maximum number of records to read (-1 = read all records)</p>
*/
+    private int maxrecords;
     /** <p>A flag indicating whether the &lt;record&gt; tag was opened.</p>
*/
     private boolean openrecord = false;
     /** <p>The character buffer for the current field.</p> */
@@ -161,12 +171,13 @@
     throws ProcessingException, SAXException, IOException {
         super.setup(resolver, object_model, source, parameters);
 
-        boolean header = parameters.getParameterAsBoolean("process-header", false);
+        boolean header = parameters.getParameterAsBoolean("process-headers", false);
 
         this.encoding = parameters.getParameter("encoding", DEFAULT_ENCODING);
         this.separator = parameters.getParameter("separator", DEFAULT_SEPARATOR).charAt(0);
         this.escape = parameters.getParameter("escape", DEFAULT_ESCAPE).charAt(0);
         this.buffersize = parameters.getParameterAsInteger("buffer-size", DEFAULT_BUFFER_SIZE);
+        this.maxrecords = parameters.getParameterAsInteger("max-records", UNLIMITED_MAXRECORDS);
         this.buffer = new CharArrayWriter();
         this.columns =  (header ? new HashMap() : null);
         this.recordnumber = (header ? 0 : 1);
@@ -178,8 +189,11 @@
      * <p>Generate the unique key.</p>
      */
     public Serializable getKey() {
-        String key = this.inputSource.getURI();
-        if (this.columns != null) return (key + "+headers");
+        StringBuffer key = new StringBuffer(this.inputSource.getURI());
+        if (this.columns != null) key.append("headers");
+        key.append(separator);
+        key.append(maxrecords);
+        key.append(escape);
         return key;
     }
 
@@ -206,7 +220,7 @@
             int curr = -1;
 
             /* Parse the file reading characters one-by-one */
-            while ((curr = csv.read()) >= 0) {
+            while ((curr = csv.read()) >= 0 && (this.maxrecords == UNLIMITED_MAXRECORDS
|| recordnumber <= this.maxrecords)) {
 
                 /* Process any occurrence of the escape character */
                 if (curr == this.escape) {



Mime
View raw message