You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by he...@apache.org on 2005/05/04 10:29:32 UTC

cvs commit: jakarta-turbine-2/xdocs changes.xml

henning     2005/05/04 01:29:32

  Modified:    src/java/org/apache/turbine/services/intake/model Tag:
                        TURBINE_2_3_BRANCH DateStringField.java
               src/java/org/apache/turbine/services/intake/validator Tag:
                        TURBINE_2_3_BRANCH DateStringValidator.java
               xdocs    Tag: TURBINE_2_3_BRANCH changes.xml
  Log:
  Fix the DateString formatter and field not to throw NPEs at any possible
  occasion.
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.9.2.3   +9 -9      jakarta-turbine-2/src/java/org/apache/turbine/services/intake/model/DateStringField.java
  
  Index: DateStringField.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/intake/model/DateStringField.java,v
  retrieving revision 1.9.2.2
  retrieving revision 1.9.2.3
  diff -u -r1.9.2.2 -r1.9.2.3
  --- DateStringField.java	20 May 2004 03:16:39 -0000	1.9.2.2
  +++ DateStringField.java	4 May 2005 08:29:32 -0000	1.9.2.3
  @@ -41,8 +41,14 @@
   public class DateStringField
           extends Field
   {
  -    /** date format */
  -    private DateFormat df = null;
  +    /** date format. Fallback if no validator is defined */
  +    private static DateFormat df;
  +
  +    static
  +    { 
  +        df = DateFormat.getInstance();
  +        df.setLenient(true);
  +    }
   
       /**
        * Constructor.
  @@ -55,12 +61,6 @@
               throws IntakeException
       {
           super(field, group);
  -
  -        if (validator == null || !(validator instanceof DateStringValidator))
  -        {
  -            df = DateFormat.getInstance();
  -            df.setLenient(true);
  -        }
       }
   
       /**
  
  
  
  No                   revision
  No                   revision
  1.7.2.7   +54 -31    jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/DateStringValidator.java
  
  Index: DateStringValidator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/src/java/org/apache/turbine/services/intake/validator/DateStringValidator.java,v
  retrieving revision 1.7.2.6
  retrieving revision 1.7.2.7
  diff -u -r1.7.2.6 -r1.7.2.7
  --- DateStringValidator.java	20 May 2004 03:06:47 -0000	1.7.2.6
  +++ DateStringValidator.java	4 May 2005 08:29:32 -0000	1.7.2.7
  @@ -49,6 +49,7 @@
    * @author <a href="mailto:jmcnally@collab.net">John McNally</a>
    * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    * @author <a href="mailto:Colin.Chalmers@maxware.nl">Colin Chalmers</a>
  + * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
    * @version $Id$
    */
   public class DateStringValidator
  @@ -72,7 +73,7 @@
       /**  */
       private SimpleDateFormat sdf = null;
   
  -    public DateStringValidator(Map paramMap)
  +    public DateStringValidator(final Map paramMap)
               throws IntakeException
       {
           init(paramMap);
  @@ -92,7 +93,7 @@
        * @param paramMap
        * @throws InvalidMaskException
        */
  -    public void init(Map paramMap)
  +    public void init(final Map paramMap)
               throws InvalidMaskException
       {
           super.init(paramMap);
  @@ -133,11 +134,13 @@
           if (dateFormats.size() == 0)
           {
               df = DateFormat.getInstance();
  +            sdf = null;
               df.setLenient(flexible);
           }
           else
           {
               sdf = new SimpleDateFormat();
  +            df = null;
               sdf.setLenient(flexible);
           }
       }
  @@ -150,7 +153,7 @@
        * @exception ValidationException containing an error message if the
        * testValue did not pass the validation tests.
        */
  -    public void assertValidity(String testValue)
  +    public void assertValidity(final String testValue)
               throws ValidationException
       {
           super.assertValidity(testValue);
  @@ -179,7 +182,7 @@
        * @throws ParseException indicates that the string could not be
        * parsed into a date.
        */
  -    public Date parse(String s)
  +    public Date parse(final String s)
               throws ParseException
       {
           Date date = null;
  @@ -189,34 +192,47 @@
               throw new ParseException("Input string was null", -1);
           }
   
  -        for (int i = 1; i < dateFormats.size() && date == null; i++)
  +        if (sdf != null) // This implies dateFormats.size() > 0
           {
  -            sdf.applyPattern((String) dateFormats.get(i));
  -
  -            try
  -            {
  -                date = sdf.parse(s);
  -            }
  -            catch (ParseException e)
  +            // First test the FORMATx patterns. If any of these match, break
  +            // the loop.
  +            for (int i = 1 ; i < dateFormats.size() - 1; i++)
               {
  -                // ignore
  -            }
  -        }
  -
  -        if (date == null)
  -        {
  -            sdf.applyPattern((String) dateFormats.get(0));
  +                sdf.applyPattern((String) dateFormats.get(i));
   
  -            try
  -            {
  -                date = sdf.parse(s);
  +                try
  +                {
  +                    date = sdf.parse(s);
  +                    break; // We got a matching date. Break the loop
  +                }
  +                catch (ParseException e)
  +                {
  +                    // ignore
  +                }
               }
  -            catch (ParseException e)
  +
  +            // Now test the FORMAT pattern which is the first one in the array.
  +            // if no format but just FORMATx has been given, all of the patterns
  +            // have been shifted "one down", e.g. tested as format2, format3, format4, format1 
  +            // in sequence.
  +            if (date == null)
               {
  -                // ignore
  +                sdf.applyPattern((String) dateFormats.get(0));
  +
  +                try
  +                {
  +                    date = sdf.parse(s);
  +                }
  +                catch (ParseException e)
  +                {
  +                    // ignore
  +                }
               }
           }
   
  +        // Still no match. Either we had no format patterns or no pattern matched.
  +        // See if we have a DateFormat object around. If there were patterns given
  +        // and just none matched, that we might have date==null and df==null...
           if (date == null && df != null)
           {
               date = df.parse(s);
  @@ -239,13 +255,20 @@
        * @param date the Date object to convert into a string.
        * @return formatted date
        */
  -    public String format(Date date)
  +    public String format(final Date date)
       {
           String s = null;
           if (date != null)
           {
  -            sdf.applyPattern((String) dateFormats.get(0));
  -            s = sdf.format(date);
  +            if (sdf != null) // implies dateFormats.size() > 0
  +            {
  +                sdf.applyPattern((String) dateFormats.get(0));
  +                s = sdf.format(date);
  +            }
  +            else // implied df != null
  +            {
  +                s = df.format(date);
  +            }
           }
           return s;
       }
  @@ -272,7 +295,7 @@
        *
        * @param message  Value to assign to minLengthMessage.
        */
  -    public void setDateFormatMessage(String message)
  +    public void setDateFormatMessage(final String message)
       {
           if (StringUtils.isNotEmpty(message))
           {
  @@ -295,7 +318,7 @@
        *
        * @param formats  Value to assign to dateFormats.
        */
  -    public void setDateFormats(List formats)
  +    public void setDateFormats(final List formats)
       {
           this.dateFormats = formats;
       }
  @@ -315,7 +338,7 @@
        *
        * @param flexible  Value to assign to flexible.
        */
  -    public void setFlexible(boolean flexible)
  +    public void setFlexible(final boolean flexible)
       {
           this.flexible = flexible;
       }
  
  
  
  No                   revision
  No                   revision
  1.60.2.27 +6 -0      jakarta-turbine-2/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-2/xdocs/changes.xml,v
  retrieving revision 1.60.2.26
  retrieving revision 1.60.2.27
  diff -u -r1.60.2.26 -r1.60.2.27
  --- changes.xml	29 Mar 2005 03:24:25 -0000	1.60.2.26
  +++ changes.xml	4 May 2005 08:29:32 -0000	1.60.2.27
  @@ -37,6 +37,12 @@
       <action type="update" dev="seade">
         commons-codec was upgraded to 1.3.
       </action>
  +    <action type="update" dev="henning">
  +      Fix the Intake DateString validator not to throw an NPE whenever a
  +      slightest mistake was done in configuring a date field. Make sure
  +      that even fields without a formatting rule will parse at least standard
  +      date formats.
  +    </action>
     </release>
   
     <release version="2.3.1" date="2004-10-29">
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org