DateTimeFormatParserLegacy.java

  1. package emissary.util;

  2. import emissary.config.ConfigUtil;
  3. import emissary.config.Configurator;

  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;

  6. import java.io.IOException;
  7. import java.text.SimpleDateFormat;
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;

  11. @Deprecated
  12. @SuppressWarnings("all")
  13. public class DateTimeFormatParserLegacy {

  14.     protected static final Logger logger = LoggerFactory.getLogger(DateTimeFormatParserLegacy.class);

  15.     /** Date formats we can use to parse event date strings */
  16.     @Deprecated
  17.     protected static final List<SimpleDateFormat> dateFormats = new ArrayList<>();

  18.     static {
  19.         configure();
  20.     }

  21.     protected static void configure() {
  22.         try {
  23.             Configurator configG = ConfigUtil.getConfigInfo(DateTimeFormatParserLegacy.class);
  24.             for (final String dfentry : configG.findEntries("DATE_FORMAT")) {
  25.                 try {
  26.                     final SimpleDateFormat sdf = new SimpleDateFormat(dfentry);
  27.                     sdf.setLenient(true);
  28.                     dateFormats.add(sdf);
  29.                 } catch (Exception ex) {
  30.                     logger.debug("DATE_FORMAT entry '{}' cannot be parsed", dfentry, ex);
  31.                 }
  32.             }
  33.             logger.debug("Loaded {} DATE_FORMAT entries", dateFormats.size());
  34.         } catch (IOException e) {
  35.             logger.error("Cannot open default config file", e);
  36.         }
  37.     }


  38.     /**
  39.      * Parse an RFC-822 Date or one of the thousands of variants make a quick attempt to normalize the timezone information
  40.      * and get the timestamp in GMT. Should change to pass in a default from the U124 header
  41.      *
  42.      * @param dateString the string date from the RFC 822 Date header
  43.      * @param supplyDefaultOnBad when true use current date if sentDate is unparseable
  44.      * @return the GMT time of the event or NOW if unparseable, or null if supplyDefaultOnBad is false
  45.      */
  46.     @Deprecated
  47.     public static Date parseEventDate(final String dateString, final boolean supplyDefaultOnBad) {
  48.         Date date = null;
  49.         if (dateString != null && dateString.length() > 0) {
  50.             // Take it apart and stick it back together to get
  51.             // get rid of multiple contiguous spaces
  52.             String instr = dateString.replaceAll("\t+", " "); // tabs
  53.             instr = instr.replaceAll("[ ]+", " "); // multiple spaces
  54.             instr = instr.replaceAll("=0D$", ""); // common qp'ified ending

  55.             // try each date format in turn until one works
  56.             synchronized (dateFormats) {
  57.                 for (final SimpleDateFormat sdf : dateFormats) {
  58.                     try {
  59.                         date = sdf.parse(instr);
  60.                         break;
  61.                     } catch (Exception e) {
  62.                         // Ignore.
  63.                         logger.debug("Error parsing date", e);
  64.                     }
  65.                 }
  66.             }
  67.         }

  68.         // Use the default if required
  69.         if (date == null && supplyDefaultOnBad) {
  70.             date = new Date();
  71.         }

  72.         // Let them have it.
  73.         return date;
  74.     }


  75.     private DateTimeFormatParserLegacy() {}
  76. }