Ignore:
Timestamp:
2008-12-23T15:07:05+01:00 (15 years ago)
Author:
stoecker
Message:

removed usage of tab stops

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/FallbackDateParser.java

    r627 r1169  
    1313 * based on similar code in JOSM. This class is not threadsafe, a separate
    1414 * instance must be created per thread.
    15  * 
     15 *
    1616 * @author Brett Henderson
    1717 */
    1818public class FallbackDateParser {
    19        
    20         private static final String[] formats = {
    21             "yyyy-MM-dd'T'HH:mm:ss'Z'",
    22                 "yyyy-MM-dd'T'HH:mm:ssZ",
    23                 "yyyy-MM-dd'T'HH:mm:ss",
    24                 "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
    25                 "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
    26                 "yyyy-MM-dd HH:mm:ss",
    27                 "MM/dd/yyyy HH:mm:ss",
    28                 "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",
    29                 "MM/dd/yyyy'T'HH:mm:ss.SSSZ",
    30                 "MM/dd/yyyy'T'HH:mm:ss.SSS",
    31                 "MM/dd/yyyy'T'HH:mm:ssZ",
    32                 "MM/dd/yyyy'T'HH:mm:ss",
    33                 "yyyy:MM:dd HH:mm:ss"
    34         };
    35        
    36        
    37         private List<DateFormat> dateParsers;
    38         private int activeDateParser;
    39        
    40        
    41         /**
    42         * Creates a new instance.
    43         */
    44         public FallbackDateParser() {
    45                 // Build a list of candidate date parsers.
    46                 dateParsers = new ArrayList<DateFormat>(formats.length);
    47                 for (int i = 0; i < formats.length; i++) {
    48                         dateParsers.add(new SimpleDateFormat(formats[i]));
    49                 }
    50                
    51                 // We haven't selected a date parser yet.
    52                 activeDateParser = -1;
    53         }
    54        
    55        
    56         /**
    57         * Attempts to parse the specified date.
    58          *
    59         * @param date
    60         *            The date to parse.
    61         * @return The date.
    62         * @throws ParseException
    63         *             Occurs if the date does not match any of the supported date
    64         *             formats.
    65         */
    66         public Date parse(String date) throws ParseException {
    67                 String correctedDate;
    68                
    69                 // Try to fix ruby's broken xmlschema - format
    70                 // Replace this:
    71                 // 2007-02-12T18:43:01+00:00
    72                 // With this:
    73                 // 2007-02-12T18:43:01+0000
    74                 if (date.length() == 25 && date.charAt(22) == ':') {
    75                         correctedDate = date.substring(0, 22) + date.substring(23, 25);
    76                 } else {
    77                         correctedDate = date;
    78                 }
    79                
    80                 // If we have previously successfully used a date parser, we'll try it
    81                 // first.
    82                 if (activeDateParser >= 0) {
    83                         try {
    84                                 return dateParsers.get(activeDateParser).parse(correctedDate);
    85                         } catch (ParseException e) {
    86                                 // The currently active parser didn't work, so we must clear it
    87                                 // and find a new appropriate parser.
    88                                 activeDateParser = -1;
    89                         }
    90                 }
    91                
    92                 // Try the date parsers one by one until a suitable format is found.
    93                 for (int i = 0; i < dateParsers.size(); i++) {
    94                         try {
    95                                 Date result;
    96                                
    97                                 // Attempt to parse with the current parser, if successful we
    98                                 // store its index for next time.
    99                                 result = dateParsers.get(i).parse(correctedDate);
    100                                 activeDateParser = i;
    101                                
    102                                 return result;
    103                                
    104                         } catch (ParseException pe) {
    105                                 // Ignore parsing errors and try the next pattern.
    106                         }
    107                 }
    108                
    109                 throw new ParseException("The date string (" + date + ") could not be parsed.", 0);
    110         }
     19
     20    private static final String[] formats = {
     21        "yyyy-MM-dd'T'HH:mm:ss'Z'",
     22        "yyyy-MM-dd'T'HH:mm:ssZ",
     23        "yyyy-MM-dd'T'HH:mm:ss",
     24        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
     25        "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
     26        "yyyy-MM-dd HH:mm:ss",
     27        "MM/dd/yyyy HH:mm:ss",
     28        "MM/dd/yyyy'T'HH:mm:ss.SSS'Z'",
     29        "MM/dd/yyyy'T'HH:mm:ss.SSSZ",
     30        "MM/dd/yyyy'T'HH:mm:ss.SSS",
     31        "MM/dd/yyyy'T'HH:mm:ssZ",
     32        "MM/dd/yyyy'T'HH:mm:ss",
     33        "yyyy:MM:dd HH:mm:ss"
     34    };
     35
     36
     37    private List<DateFormat> dateParsers;
     38    private int activeDateParser;
     39
     40
     41    /**
     42    * Creates a new instance.
     43    */
     44    public FallbackDateParser() {
     45        // Build a list of candidate date parsers.
     46        dateParsers = new ArrayList<DateFormat>(formats.length);
     47        for (int i = 0; i < formats.length; i++) {
     48            dateParsers.add(new SimpleDateFormat(formats[i]));
     49        }
     50
     51        // We haven't selected a date parser yet.
     52        activeDateParser = -1;
     53    }
     54
     55
     56    /**
     57    * Attempts to parse the specified date.
     58     *
     59    * @param date
     60    *            The date to parse.
     61    * @return The date.
     62    * @throws ParseException
     63    *             Occurs if the date does not match any of the supported date
     64    *             formats.
     65    */
     66    public Date parse(String date) throws ParseException {
     67        String correctedDate;
     68
     69        // Try to fix ruby's broken xmlschema - format
     70        // Replace this:
     71        // 2007-02-12T18:43:01+00:00
     72        // With this:
     73        // 2007-02-12T18:43:01+0000
     74        if (date.length() == 25 && date.charAt(22) == ':') {
     75            correctedDate = date.substring(0, 22) + date.substring(23, 25);
     76        } else {
     77            correctedDate = date;
     78        }
     79
     80        // If we have previously successfully used a date parser, we'll try it
     81        // first.
     82        if (activeDateParser >= 0) {
     83            try {
     84                return dateParsers.get(activeDateParser).parse(correctedDate);
     85            } catch (ParseException e) {
     86                // The currently active parser didn't work, so we must clear it
     87                // and find a new appropriate parser.
     88                activeDateParser = -1;
     89            }
     90        }
     91
     92        // Try the date parsers one by one until a suitable format is found.
     93        for (int i = 0; i < dateParsers.size(); i++) {
     94            try {
     95                Date result;
     96
     97                // Attempt to parse with the current parser, if successful we
     98                // store its index for next time.
     99                result = dateParsers.get(i).parse(correctedDate);
     100                activeDateParser = i;
     101
     102                return result;
     103
     104            } catch (ParseException pe) {
     105                // Ignore parsing errors and try the next pattern.
     106            }
     107        }
     108
     109        throw new ParseException("The date string (" + date + ") could not be parsed.", 0);
     110    }
    111111}
Note: See TracChangeset for help on using the changeset viewer.