* Philippe LAPLANCHE:
> > > Updates in the source:
> > >
> > > Correction: The generator now looks for the sitemap
> > > parameter "process-headers" (with the s at the end) as told
> > > in the documentation
> > >
> > > New feature: The generator now accepts a new sitemap
> > > parameter "max-records" which allows to limit the number of
> > > records to read. The default is 0 ( = read all records)
> >
> > Thanks for your contribution. Have you been able to enter an
> > entry in our Jira bug tracking system? If not, I would be
> > glad to submit it myself.
>
> Non je n'ai pas utilisé Jira. Je regarderai à l'occasion mais en
> attendant je veux bien que tu y mettes le .java en pièce jointe
> plutôt que celui que j'ai mis sur la mailing list. Je n'avais
> pas mis à jour la méthode recycle, elle remet maintenant
> this.maxrecords à 0.
>
> Depuis mon post sur la mailing list cocoon dev, j'ai découvert
> un autre problème en utilisant le générateur : les "sitemap
> parameters" du composant sont lus une fois pour toutes.
>
> Je m'explique par un exemple car je débute avec Cocoon et je
> n'ai pas d'expert avec moi qui pourrait mieux formuler le
> probleme :
>
> A un premier endroit d'un pipeline, dans un premier matcher,
> j'utilise le cvs generator avec des paramètres sitemap tels que
> separator=; A un autre endroit j'utilise le générateur avec
> separator=, Et bien malheureusement, au deuxième endroit, le
> separateur utilisé est encore un ; et non un ,
>
> Bon en réalité c'est plutôt avec le nouveau sitemap parameter
> que j'ai créé (c-à-d max-records) que cela pose vraiment
> problème (dans un map:match, je fais un preview du fichier csv,
> dans un suivant j'importe tout le fichier dans une base de
> données) mais le problème reste le même quel que soit le sitemap
> parameter.
>
> Pour contourner le problème je ne vois pas d'autre solution
> que de déclarer deuxième générateur dans la déclaration des
> composants.
La discussion continue sur users-fr.
J'ai donc essayé la modification sur CSVGenerator, le patch me
paraît bien.
Par contre en effet je vois un problème dans la gestion du cache:
la méthode getKey() qui identifie un object du cache ne prend pas
en compte les paramètres separator, max-records et escape. Ce qui
explique que le même résultat soit toujours retourné quels que
soient les paramètres.
Retester avec le patch ci-joint. Si cela convient, je peux
committer dans Cocoon.
Merci,
--
Jean-Baptiste Quenot
http://caraldi.com/jbq/
Index: src/java/org/apache/cocoon/generation/CSVGenerator.java
===================================================================
--- src/java/org/apache/cocoon/generation/CSVGenerator.java (revision
369409)
+++ src/java/org/apache/cocoon/generation/CSVGenerator.java (working copy)
@@ -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>
@@ -110,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();
@@ -125,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 (0 = read all records)</p> */
+ private int maxrecords;
/** <p>A flag indicating whether the <record> tag was opened.</p> */
private boolean openrecord = false;
/** <p>The character buffer for the current field.</p> */
@@ -164,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);
@@ -181,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;
}
@@ -209,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) {
---------------------------------------------------------------------
Liste francophone Apache Cocoon -- http://cocoon.apache.org/fr/
Pour vous desinscrire : mailto:[EMAIL PROTECTED]
Autres commandes : mailto:[EMAIL PROTECTED]