I've got a couple of handy classes CSVParser and TSVParser ready to
commit that as the names suggest parse comma-separated value files and
tab-separated value files. Rather than reinvent the wheel, the neatest
way of me presenting a line of values through the API is by filling up a
ParameterParser. This way the values can be extracted as whatever type
the programmer wants, or a data object can be filled with the values through
setProperties.

In order to make this work I had to make a few changes to
ParameterParser (patch below). I added a default constructor, and made
the class hang on to just the character encoding of the
HttpServletRequest rather than the whole request object (this is all the
request is needed for after construction). Oh, and I added a clear()
method.

Let me know if anyone foresees any problem with this...

Sean


Index: ParameterParser.java
===================================================================
RCS file: 
/products/cvs/turbine/turbine/src/java/org/apache/turbine/util/ParameterParser.java,v
retrieving revision 1.24
diff -u -r1.24 ParameterParser.java
--- ParameterParser.java        2001/01/22 01:54:48     1.24
+++ ParameterParser.java        2001/01/24 22:44:49
@@ -1,7 +1,7 @@
 package org.apache.turbine.util;
 
 /*
- * Copyright (c) 1997-2000 The Java Apache Project.  All rights reserved.
+ * Copyright (c) 1997-2001 The Java Apache Project.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -105,7 +105,7 @@
     /**
      * The HttpServletRequest
      */
-    private HttpServletRequest req = null;
+    private String characterEncoding = "US-ASCII";
 
     public static final String URL_CASE_FOLDING = "url.case.folding";
     public static final String URL_CASE_FOLDING_NONE = "none";
@@ -118,6 +118,35 @@
     private byte[] uploadData = null;
 
     /**
+     * Create a new empty instance of ParameterParser.  Uses the
+     * default character encoding (US-ASCII).
+     *
+     * <p>To add name/value pairs to this set of parameters, use the
+     * <code>add()</code> methods.
+     *
+     */
+    public ParameterParser()
+    {
+        parameters = new Hashtable();
+    }
+    
+    /**
+     * Create a new empty instance of ParameterParser. Takes a
+     * character encoding name to use when converting strings to
+     * bytes.
+     *
+     * <p>To add name/value pairs to this set of parameters, use the
+     * <code>add()</code> methods.
+     *
+     * @param characterEncoding The character encoding of strings.
+     */
+    public ParameterParser(String characterEncoding)
+    {
+        this.characterEncoding = characterEncoding;
+        parameters = new Hashtable();
+    }
+
+    /**
      * Create a new instance of ParameterParser.  This requires a
      * valid HttpServletRequest object.  It will attempt to parse out
      * the GET/POST/PATH_INFO data and store the data into a Hashtable. 
@@ -132,7 +161,8 @@
      */
     public ParameterParser(HttpServletRequest req)
     {
-        this.req = req;
+        if (req.getCharacterEncoding() != null)
+            this.characterEncoding = req.getCharacterEncoding();
 
         parameters = new Hashtable();
 
@@ -198,6 +228,14 @@
     }
 
     /**
+     * Clear all name/value pairs out of this object.
+     */
+    public void clear()
+    {
+        parameters.clear();
+    }
+            
+    /**
      * Add a name/value pair into this object.
      *
      * @param name A String with the name.
@@ -841,7 +879,7 @@
     {
         String tempStr = getString(name);
         if ( tempStr != null )
-            return tempStr.getBytes(req.getCharacterEncoding());
+            return tempStr.getBytes(characterEncoding);
         return null;
     }
 
-- 
Sean Legassick
[EMAIL PROTECTED]
      Czlowiekiem jestem i nic co ludzkie nie jest mi obce  
      
      


------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?:           [EMAIL PROTECTED]

Reply via email to