On 23 April 2014 17:02, <[email protected]> wrote:
> Author: chas
> Date: Wed Apr 23 16:02:52 2014
> New Revision: 1589446
>
> URL: http://svn.apache.org/r1589446
> Log:
> LANG-966 - FastDateParser should be case insensitive
>
> Modified:
>
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
>
> commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
>
> Modified:
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
> URL:
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDateParser.java?rev=1589446&r1=1589445&r2=1589446&view=diff
> ==============================================================================
> ---
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
> (original)
> +++
> commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/FastDateParser.java
> Wed Apr 23 16:02:52 2014
> @@ -25,6 +25,7 @@ import java.text.ParsePosition;
> import java.util.ArrayList;
> import java.util.Calendar;
> import java.util.Date;
> +import java.util.HashMap;
> import java.util.List;
> import java.util.Locale;
> import java.util.Map;
> @@ -586,6 +587,7 @@ public class FastDateParser implements D
> private static class TextStrategy extends Strategy {
> private final int field;
> private final Map<String, Integer> keyValues;
> + private final Map<String, Integer> lKeyValues;
This is an extra data area for ALL Text fields, but is only needed for
Month parsing.
I think a separate class is needed.
> /**
> * Construct a Strategy that parses a Text field
> @@ -596,6 +598,11 @@ public class FastDateParser implements D
> TextStrategy(final int field, final Calendar definingCalendar, final
> Locale locale) {
> this.field= field;
> this.keyValues= getDisplayNames(field, definingCalendar, locale);
> + this.lKeyValues= new HashMap<String,Integer>();
> +
> + for(Map.Entry<String, Integer> entry : keyValues.entrySet()) {
> + lKeyValues.put(entry.getKey().toLowerCase(),
> entry.getValue());
> + }
> }
>
> /**
> @@ -603,7 +610,7 @@ public class FastDateParser implements D
> */
> @Override
> boolean addRegex(final FastDateParser parser, final StringBuilder
> regex) {
> - regex.append('(');
> + regex.append("((?i)(?u)");
> for(final String textKeyValue : keyValues.keySet()) {
> escapeRegex(regex, textKeyValue, false).append('|');
-1
AFAICT, this will enable incorrect matching of text fields that are
not supposed to be case-blind.
Also, the case-blind matching is not disabled at the end of the
section, so can affect later matches.
Using a separate class for case-blind matching will fix the first
issue, and will avoid creating duplicate data unnecessarily.
> }
> @@ -616,7 +623,7 @@ public class FastDateParser implements D
> */
> @Override
> void setCalendar(final FastDateParser parser, final Calendar cal,
> final String value) {
> - final Integer iVal = keyValues.get(value);
> + final Integer iVal = lKeyValues.get(value.toLowerCase());
> if(iVal == null) {
> final StringBuilder sb= new StringBuilder(value);
> sb.append(" not in (");
>
> Modified:
> commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
> URL:
> http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java?rev=1589446&r1=1589445&r2=1589446&view=diff
> ==============================================================================
> ---
> commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
> (original)
> +++
> commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/FastDateParserTest.java
> Wed Apr 23 16:02:52 2014
> @@ -540,4 +540,16 @@ public class FastDateParserTest {
> final DateParser parser= getInstance(yMdHmsSZ, REYKJAVIK);
> assertEquals(REYKJAVIK, parser.getTimeZone());
> }
> +
> + @Test
> + public void testLang996() throws ParseException {
> + Calendar expected = Calendar.getInstance(NEW_YORK, Locale.US);
> + expected.clear();
> + expected.set(2014, 4, 14);
> +
> + final DateParser fdp = getInstance("ddMMMyyyy", NEW_YORK, Locale.US);
> + assertEquals(expected.getTime(), fdp.parse("14may2014"));
> + assertEquals(expected.getTime(), fdp.parse("14MAY2014"));
> + assertEquals(expected.getTime(), fdp.parse("14May2014"));
> + }
Need more tests to show that the parsing of other case-significant
dates is not affected.
> }
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]