private void createWorkingDir() { logger.debug("Creating working directory..."); if (!inputParameters.getWorkingDirFile().exists()) { boolean result = inputParameters.getWorkingDirFile().mkdirs(); if (!result) { throw new ConversionException( String.format("Could not create working directory '%s'", inputParameters.getWorkingDirFile().getAbsolutePath())); } } logger.debug("Created working directory: OK\n"); }
private void initConfigXml() throws XmlParsingException, FileNotFoundException { logger.debug("Reading config.xml: {}", inputParameters.getConfigFile().getAbsolutePath()); this.configProvider = new ConfigXmlProvider(inputParameters.getConfigFile()); logger.debug("Config.xml is processed: OK\n"); }
/** * Checks whether all input parameters needed for conversion are specified (such as CPL, IMP) are specified. * These parameters can be set either via command line arguments or in config.xml. So, the method must be called * after config.xml is processed. * * @param inputParameters the input to be validated * @throws ArgumentValidationException if some of the required command lines arguments are missing. */ public static void validateInputParameters(ImfUtilityInputParameters inputParameters) throws ArgumentValidationException { File impDirectory = inputParameters.getImpDirectoryFile(); if (impDirectory == null) { throw new ArgumentValidationException("IMP directory must be specified either as a command line argument or in config.xml"); } if (!impDirectory.isDirectory()) { throw new ArgumentValidationException(String.format( "IMP directory '%s' must be an existing folder", impDirectory.getAbsolutePath())); } File cplFile = inputParameters.getCplFile(); if (cplFile == null) { throw new ArgumentValidationException("CPL file must be specified either as a command line argument or in config.xml"); } if (!cplFile.isFile()) { throw new ArgumentValidationException(String.format( "CPL file '%s' must be an existing file", cplFile.getAbsolutePath())); } File workingDir = inputParameters.getWorkingDirFile(); if (workingDir == null) { throw new ArgumentValidationException("Working directory must be specified either as a command line argument or in config.xml"); } }
private void buildCplContext() throws XmlParsingException, FileNotFoundException { logger.debug("Building CPL contexts..."); File assetMapFile = new File(inputParameters.getImpDirectoryFile(), CoreConstants.ASSETMAP_FILE); logger.debug("Parsing ASSETMAP.xml ('{}')...", assetMapFile.getAbsolutePath()); this.assetMap = new AssetMapParser().parse(inputParameters.getImpDirectoryFile(), assetMapFile); logger.debug("Parsed ASSETMAP.xml: OK"); logger.debug("Parsing CPL ('{}')...", inputParameters.getCplFile().getAbsolutePath()); this.cplContextBuilder = new CplContextBuilder(contextProvider, assetMap, inputParameters.getCplFile()); cplContextBuilder.build(); logger.debug("Parsed CPL: OK"); logger.debug("Built CPL contexts: OK\n"); }
private void buildDynamicContextPreCpl() { logger.debug("Building Dynamic context before parsing CPL..."); // build default dynamic parameters DynamicTemplateParameterContext dynamicContext = contextProvider.getDynamicContext(); dynamicContext.addParameter(DynamicContextParameters.IMP, inputParameters.getImpDirectoryFile().getAbsolutePath()); dynamicContext.addParameter(DynamicContextParameters.CPL, inputParameters.getCplFile().getAbsolutePath()); dynamicContext.addParameter(DynamicContextParameters.VALIDATION_TOOL, inputParameters.getImfValidationTool()); // build format-specific dynamic parameters doBuildDynamicContextPreCpl(); logger.debug("Built Dynamic context before parsing CPL: OK\n"); }
@Test public void testDefaultWorkingDir() { String[] args = new String[]{}; ImfUtilityInputParameters inputParameters = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, args), new FakeDefaultTools()); inputParameters.setDefaultWorkingDir("defaultWorkingDir"); assertEquals(new File("defaultWorkingDir"), inputParameters.getWorkingDirFile()); }
private void initInputParameters() { logger.debug("Initializing input parameters..."); // 1. setting working directory (if it's in config.xml) inputParameters.setDefaultWorkingDir(configProvider.getConfig().getWorkingDirectory()); // 2. setting IMP (if it's in config.xml) inputParameters.setDefaultImp(configProvider.getConfig().getImp()); // 3. setting CPL (if it's in config.xml) inputParameters.setDefaultCpl(configProvider.getConfig().getCpl()); // 4. custom IMF validation if (configProvider.getConfig().getExternalTools().getMap().containsKey(CoreConstants.IMF_VALIDATION_TOOL)) { String customImfValidation = configProvider.getConfig().getExternalTools().getMap() .get(CoreConstants.IMF_VALIDATION_TOOL).getValue(); if (!StringUtils.isEmpty(customImfValidation)) { inputParameters.setCustomImfValidationTool(customImfValidation); } } logger.debug("Initialized input parameters: OK\n"); }
@Test public void testDefaultCpl() throws Exception { String[] args = new String[]{}; ImfUtilityInputParameters inputParameters = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, args), new FakeDefaultTools()); inputParameters.setDefaultImp("defaultImp"); inputParameters.setDefaultCpl("defaultCpl"); assertEquals(new File("defaultImp", "defaultCpl"), inputParameters.getCplFile()); }
@Test public void testDefaultImpFolder() throws Exception { String[] args = new String[]{}; ImfUtilityInputParameters inputParameters = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, args), new FakeDefaultTools()); inputParameters.setDefaultImp("defaultImp"); assertEquals(new File("defaultImp"), inputParameters.getImpDirectoryFile()); }
@Test public void testCmdLineCplReplacesDefault() throws Exception { String[] args = new String[]{ "--cpl", ImpUtils.getCorrectCpl().getName(), "--imp", ImpUtils.getImpFolder().getAbsolutePath() }; ImfUtilityInputParameters inputParameters = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, args), new FakeDefaultTools()); inputParameters.setDefaultCpl("defaultCpl"); assertEquals(ImpUtils.getCorrectCpl(), inputParameters.getCplFile()); }
/** * Gets a full path to CPL file. It can be defined either in config.xml or via command line arguments. * Setting a parameter via command line argument overwrites value from config.xml. * * @return a file defining a full path to CPL or null if it's not specified neither in config.xml nor via command line arguments */ public File getCplFile() { // cmd line argument has first priority String cpl = cmdLineArgs.getCpl() != null ? cmdLineArgs.getCpl() : defaultCpl; if (cpl == null) { return null; } // try if a valid path for CPL is specified. File cplFile = new File(cpl); if (cplFile.isFile()) { return cplFile; } // assume that the CPL is relative to IMP. File impDirectory = getImpDirectoryFile(); if (impDirectory == null) { return cplFile; } return new File(impDirectory, cpl); }
@Test public void testCpl() throws Exception { String[] argsCplImp = new String[]{ "--cpl", ImpUtils.getCorrectCpl().getName(), "--imp", ImpUtils.getImpFolder().getAbsolutePath() }; String[] argsCplNoImp = new String[]{ "--cpl", ImpUtils.getCorrectCpl().getName() }; String[] argsNoCplNoImp = new String[]{ }; ImfUtilityInputParameters inputParametersCplImp = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsCplImp), new FakeDefaultTools()); ImfUtilityInputParameters inputParametersCplNoImp = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsCplNoImp), new FakeDefaultTools()); ImfUtilityInputParameters inputParametersNoCplNoImp = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsNoCplNoImp), new FakeDefaultTools()); assertEquals(ImpUtils.getCorrectCpl(), inputParametersCplImp.getCplFile()); assertEquals(new File(ImpUtils.getCorrectCpl().getName()), inputParametersCplNoImp.getCplFile()); assertNull(inputParametersNoCplNoImp.getCplFile()); }
@Test public void tesCmdLineWorkingDirReplacesDefault() { String[] args = new String[]{ "-w", TemplateParameterContextCreator.getWorkingDir().getAbsolutePath() }; ImfUtilityInputParameters inputParameters = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, args), new FakeDefaultTools()); inputParameters.setDefaultWorkingDir("defaultWorkingDir"); assertEquals(TemplateParameterContextCreator.getWorkingDir(), inputParameters.getWorkingDirFile()); }
@Test public void testCmdLineImpReplacesDefault() throws Exception { String[] args = new String[]{ "--imp", ImpUtils.getImpFolder().getAbsolutePath() }; ImfUtilityInputParameters inputParameters = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, args), new FakeDefaultTools()); inputParameters.setDefaultImp("defaultImp"); assertEquals(ImpUtils.getImpFolder(), inputParameters.getImpDirectoryFile()); }
@Test public void testImpFolder() throws Exception { String[] argsImp = new String[]{ "--imp", ImpUtils.getImpFolder().getAbsolutePath() }; String[] argsNoImp = new String[]{}; ImfUtilityInputParameters inputParametersImp = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsImp), new FakeDefaultTools()); ImfUtilityInputParameters inputParametersNoImp = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsNoImp), new FakeDefaultTools()); assertEquals(ImpUtils.getImpFolder(), inputParametersImp.getImpDirectoryFile()); assertNull(inputParametersNoImp.getImpDirectoryFile()); }
private void createLogsDir() { logger.debug("Creating external tools logging directory..."); File logsDir = new File(inputParameters.getWorkingDirFile(), CoreConstants.LOGS_DIR); logger.debug("External tools logging directory: {}", logsDir); if (!logsDir.mkdir()) { logger.warn("Couldn't create External tools logging directory!"); } logger.debug("Created external tools logging directory: OK\n"); }
/** * Checks whether all required command lines arguments needed for conversion are specified (such as path to config.xml). * So, the method must be called before config.xml is processed. * <p> * It doesn't check some required parameters that may be set either via command line arguments or config.xml * (see {@link #validateInputParameters(ImfUtilityInputParameters)}). * </p> * * @param inputParameters the input to be validated * @throws ArgumentValidationException if some of the required command lines arguments are missing. */ public static void validateCmdLineArguments(ImfUtilityInputParameters inputParameters) throws ArgumentValidationException { File configFile = inputParameters.getConfigFile(); if (configFile == null) { throw new ArgumentValidationException("Config.xml command line argument (-c, --config) must be specified"); } if (!configFile.isFile()) { throw new ArgumentValidationException("Config.xml must be an existing file"); } }
private void cleanWorkingDir() throws IOException { logger.debug("Cleaning working directory..."); if (isCleanWorkingDir()) { FileUtils.cleanDirectory(inputParameters.getWorkingDirFile()); logger.info("Cleaned working directory: OK\n"); } else { logger.info("Cleaning working directory is DISABLED in config.xml\n"); } }
@Test public void testConfigXml() { String[] argsConfig = new String[]{ "-c", ConfigUtils.getCorrectConfigXmlPath() }; String[] argsNoConfig = new String[]{}; ImfUtilityInputParameters inputParametersConfig = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsConfig), new FakeDefaultTools()); ImfUtilityInputParameters inputParametersNoConfig = new FakeInputParameters( CliFactory.parseArguments(ImfUtilityCmdLineArgs.class, argsNoConfig), new FakeDefaultTools()); assertEquals(new File(ConfigUtils.getCorrectConfigXmlPath()), inputParametersConfig.getConfigFile()); assertNull(inputParametersNoConfig.getConfigFile()); }
private void initContexts() { logger.debug("Initializing template parameter contexts..."); this.contextProvider = new TemplateParameterContextProvider(configProvider, conversionProvider, inputParameters.getWorkingDirFile()); logger.debug("Initialized template parameter contexts: OK\n"); }