Parsing UserData in org.apache.myfaces.custom.date.HtmlInputDate.UserData 
throws java.lang.NumberFormatException, not java.text.ParseException
----------------------------------------------------------------------------------------------------------------------------------------------

         Key: MYFACES-934
         URL: http://issues.apache.org/jira/browse/MYFACES-934
     Project: MyFaces
        Type: Bug
  Components: Tomahawk  
    Versions: 1.1.1, Nightly    
 Environment: windows, java 1.5.0_05, tomcat 5.5
    Reporter: Ulf Helmke
    Priority: Minor


Hello,

The UserData.parse method declares to throw aParseException. But it never 
throws a ParseException, the Integer.parseInt throws a NumberFormatException 
instead:

        public Date parse() throws ParseException{
            Calendar tempCalendar=Calendar.getInstance();
            if (timeZone != null)
                   tempCalendar.setTimeZone(timeZone);
            
            tempCalendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
            tempCalendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
            tempCalendar.set(Calendar.YEAR,Integer.parseInt(year));
            if (uses_ampm) {
                int int_hours = Integer.parseInt(hours);
                // ampm hours must be in range 0-11 to be handled right; we 
have to handle "12" specially
                if (int_hours == 12) {
                                int_hours = 0;
                }
                tempCalendar.set(Calendar.HOUR,int_hours);
                tempCalendar.set(Calendar.AM_PM,Integer.parseInt(ampm));
            } else {
                tempCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
            }
            tempCalendar.set(Calendar.MINUTE,Integer.parseInt(minutes));
            tempCalendar.set(Calendar.SECOND,Integer.parseInt(seconds));
            tempCalendar.set(Calendar.MILLISECOND, 0);
            
            return tempCalendar.getTime();
        }

 The most simple approach to solve it: Catch the NumberFormatException inside 
the parse method and throw a new ParseException:

        public Date parse() throws ParseException{
            Calendar tempCalendar=Calendar.getInstance();
            if (timeZone != null)
                   tempCalendar.setTimeZone(timeZone);
            
            try {
                                
tempCalendar.set(Calendar.DAY_OF_MONTH,Integer.parseInt(day));
                                
tempCalendar.set(Calendar.MONTH,Integer.parseInt(month)-1);
                                
tempCalendar.set(Calendar.YEAR,Integer.parseInt(year));
                                if (uses_ampm) {
                                        int int_hours = Integer.parseInt(hours);
                                        // ampm hours must be in range 0-11 to 
be handled right; we have to handle "12" specially
                                        if (int_hours == 12) {
                                                int_hours = 0;
                                        }
                                        
tempCalendar.set(Calendar.HOUR,int_hours);
                                    
tempCalendar.set(Calendar.AM_PM,Integer.parseInt(ampm));
                                } else {
                                        
tempCalendar.set(Calendar.HOUR_OF_DAY,Integer.parseInt(hours));
                                }
                                
tempCalendar.set(Calendar.MINUTE,Integer.parseInt(minutes));
                                
tempCalendar.set(Calendar.SECOND,Integer.parseInt(seconds));
                                tempCalendar.set(Calendar.MILLISECOND, 0);
                        } catch (NumberFormatException e) {
                                throw new ParseException(e.getMessage(),0);
                        }
            
            return tempCalendar.getTime();
        }

It would be a good idea to write some new tests. The existing Tests do not deal 
with invalid form data.

Greetings Ulf


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira

Reply via email to