IngestionTool { static String formatPath(String rootDir, File file, Pattern pattern, String timeElements, String timeFormat, String productType, String revision, String pathTemplate) throws IOException { final String relPath; if (pattern.pattern().contains("/")) { relPath = file.getPath().replace(File.separator, "/").substring(rootDir.length()+1); } else { relPath = file.getName(); } final Matcher matcher = pattern.matcher(relPath); if (! matcher.matches()) { return null; } else if (pathTemplate != null) { String path = pathTemplate; if (timeElements != null && timeFormat != null) { try { final String timeString = expand(matcher, timeElements); final Date date = DateUtils.createDateFormat(timeFormat).parse(timeString); path = DateUtils.createDateFormat(path).format(date); } catch (ParseException e) { throw new IOException("parsing of date in " + file.getName() + " using " + pattern.pattern() + " " + timeElements + " " + timeFormat + " failed", e); } } return expand(matcher, path); } else { final String subPath = getDatePath(file, productType, pattern); return String.format("/calvalus/eodata/%s/%s/%s", productType, revision, subPath); } }  static int handleIngestionCommand(CommandLine commandLine, String[] files, FileSystem hdfs); static String expand(Matcher source, String template); static final String DEFAULT_PRODUCT_TYPE; static final String DEFAULT_REVISION; static final DateFormat YEAR_MONTH_DAY_FORMAT; static final DateFormat YEAR_DAY_OF_YEAR_FORMAT; static final DateFormat YEAR2_DAY_OF_YEAR_FORMAT; static final DateFormat MONTH_DAY_YEAR2_FORMAT; static final long MINIMUM_BLOCK_SIZE; static final long MAXIMUM_BLOCK_SIZE; }
@Test public void testFormatPath() throws IOException { final File inputFile = new File(new File(new File(new File("/mnt"), "usb"), "20090909"), "MER_RR__1POBCM20060718_091715_000001012049_00308_22907_0113.N1"); String path = IngestionTool.formatPath("/mnt/usb", inputFile, Pattern.compile("..............(....)(..)(..).*"), null, null, "MER_RR__1P", "r03", null); assertEquals("/calvalus/eodata/MER_RR__1P/r03/2006/07/18", path); path = IngestionTool.formatPath("/mnt/usb", inputFile, Pattern.compile("..............(....)(..)(..).*"), "\\1\\2\\3", "yyyyMMdd", null, null, "'/calvalus/eodata/MER_RR__1P/r03/'yyyy'/\\2/'dd"); assertEquals("/calvalus/eodata/MER_RR__1P/r03/2006/07/18", path); path = IngestionTool.formatPath("/mnt/usb", inputFile, Pattern.compile("(........)/.......................*"), "\\1", "yyyyMMdd", null, null, "'/calvalus/eodata/MER_RR__1P/r03/'yyyy'/'MM'/'dd"); assertEquals("/calvalus/eodata/MER_RR__1P/r03/2009/09/09", path); }