TimestampFileNameExtractor extends RegExPropertiesCollector { @Override public void setProperties(SimpleFeature feature) { final List<Date> dates = new ArrayList<Date>(); for (String match : getMatches()) { try { if (customFormat != null) { Date parsed = customFormat.parse(match); dates.add(parsed); } else { List<Date> parsed = parser.parse(match); dates.addAll(parsed); } } catch (ParseException e) { if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, e.getLocalizedMessage(), e); } } if (dates.size() <= 0) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("No matches found for this property extractor:"); } throw new IllegalArgumentException("No matches found for this property extractor"); } int index = 0; for (String propertyName : getPropertyNames()) { feature.setAttribute(propertyName, dates.get(index++)); if (index >= dates.size()) return; } } TimestampFileNameExtractor(PropertiesCollectorSPI spi, List<String> propertyNames,
            String regex, String format);  TimestampFileNameExtractor(PropertiesCollectorSPI spi, List<String> propertyNames,
            String regex, String format, boolean fullPath); @Override void setProperties(SimpleFeature feature);  }
@Test public void testParseIsoTimestamp() { PropertiesCollectorSPI spi = getTimestampSpi(); PropertiesCollector collector = spi.create("regex=[0-9]{8}T[0-9]{6}", Arrays.asList("time")); File file = new File("polyphemus_20130301T000000.nc"); collector.collect(file); collector.setProperties(feature); Date time = (Date) feature.getAttribute("time"); assertNotNull(time); assertEquals("2013-03-01T00:00:00.000Z", df.format(time)); }
@Test public void testUnableToParse() { PropertiesCollectorSPI spi = getTimestampSpi(); PropertiesCollector collector = spi.create("regex=[0-9]{8}T[0-9]{6}", Arrays.asList("time")); File file = new File("polyphemus_20130301T00000.nc"); boolean parsed = true; try { collector.collect(file); collector.setProperties(feature); } catch (IllegalArgumentException ie) { parsed = false; } assertFalse(parsed); }
@Test public void testParseCustomTimestamp() { PropertiesCollectorSPI spi = getTimestampSpi(); PropertiesCollector collector = spi.create("regex=[0-9]{14},format=yyyyMMddHHmmss", Arrays.asList("time")); File file = new File("polyphemus_20130301000000.nc"); collector.collect(file); collector.setProperties(feature); Date time = (Date) feature.getAttribute("time"); assertNotNull(time); assertEquals("2013-03-01T00:00:00.000Z", df.format(time)); }
@Test public void testParseFullPathTimestamp() { PropertiesCollectorSPI spi = getTimestampSpi(); String regex = "(?:\\\\)(\\d{8})(?:\\\\)(?:file.)(T\\d{6})(?:.txt)"; PropertiesCollector collector = spi.create("regex=" + regex + ",fullPath=true", Arrays.asList("time")); File file = new File("c:\\data\\20120602\\file.T120000.txt"); collector.collect(file); collector.setProperties(feature); Date time = (Date) feature.getAttribute("time"); assertNotNull(time); assertEquals("2012-06-02T12:00:00.000Z", df.format(time)); }
@Test public void testParseFullPathTimestampWithCustomFormat() { PropertiesCollectorSPI spi = getTimestampSpi(); String regex = "(?:\\\\)(\\d{8})(?:\\\\)(?:file.)(t\\d{2}z)(?:.txt)"; PropertiesCollector collector = spi.create("regex=" + regex + ",format=yyyyMMdd't'HH'z',fullPath=true", Arrays.asList("time")); File file = new File("c:\\data\\20120602\\file.t12z.txt"); collector.collect(file); collector.setProperties(feature); Date time = (Date) feature.getAttribute("time"); assertNotNull(time); assertEquals("2012-06-02T12:00:00.000Z", df.format(time)); }