All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dpfmanager.shell.interfaces.console.CommonController Maven / Gradle / Ivy

/**
 * 

CommonController.java

This program is free software: you can redistribute it and/or * modify it under the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or (at your option) any later version; or, at your * choice, under the terms of the Mozilla Public License, v. 2.0. SPDX GPL-3.0+ or MPL-2.0+.

*

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License and the Mozilla Public License for more details.

You should * have received a copy of the GNU General Public License and the Mozilla Public License along with * this program. If not, see http://www.gnu.org/licenses/ * and at http://mozilla.org/MPL/2.0 .

NB: for the * © statement, include Easy Innova SL or other company/Person contributing the code.

© * 2015 Easy Innova, SL

* * @author Adria Llorens * @version 1.0 * @since 13/10/2016 */ package dpfmanager.shell.interfaces.console; import dpfmanager.conformancechecker.configuration.Configuration; import dpfmanager.shell.core.config.BasicConfig; import dpfmanager.shell.core.context.ConsoleContext; import dpfmanager.shell.modules.messages.messages.LogMessage; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.Level; import org.apache.tools.zip.ZipEntry; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.ResourceBundle; import java.util.zip.ZipOutputStream; /** * Created by Adria Llorens on 14/10/2016. */ public class CommonController { /** * The Dpf Context */ private ConsoleContext context; /** * The Dpf resourceBundle */ private ResourceBundle bundle; /** * The parsed args */ private Map parameters; /** * The input files */ private List files; /** * Config related params */ private Configuration config; private boolean xml; private boolean json; private boolean html; private boolean pdf; private String outputFolder; private boolean explicitFormats; private boolean explicitOutput; public CommonController(ConsoleContext c, ResourceBundle b) { config = null; explicitFormats = false; explicitOutput = false; context = c; bundle = b; files = new ArrayList<>(); xml = true; html = true; parameters = (Map) AppContext.getApplicationContext().getBean("parameters"); } public void parseFiles(String arg){ files.add(parseFile(arg)); } public String parseFile(String arg){ String arg_mod = arg; if (!new File(arg_mod).isAbsolute() && !new File(arg_mod).exists() && new File("../" + arg_mod).exists()) { arg_mod = "../" + arg; } return arg_mod; } public boolean parseOutput(String output) { File tmp = new File(output); if (!tmp.exists()) { if (!tmp.mkdirs()) { printOut(bundle.getString("cannotOutput")); output = null; } } else if (!tmp.isDirectory()) { printOut(bundle.getString("outputMustDirectory")); output = null; } // else if (tmp.listFiles().length > 0) { // printOut(bundle.getString("outputMustEmpty")); // output = null; // } else if (!tmp.isAbsolute()){ printOut(bundle.getString("outputMustAbsolute")); output = null; } if (output != null) { setOutputFolder(output); setExplicitOutput(true); return true; } return false; } public boolean parseConfiguration(String xmlConfig) { Configuration configAux = new Configuration(); try { configAux.ReadFile(xmlConfig); if (configAux.getFormats().size() == 0) { printOut(bundle.getString("missingReportFormat")); return false; } else { setConfig(configAux); return true; } } catch (Exception ex) { printOut(bundle.getString("incorrectConfigFile").replace("%1", xmlConfig)); return false; } } public boolean parseFormats(String formats) { setXml(formats.contains("xml")); setJson(formats.contains("json")); setHtml(formats.contains("html")); setPdf(formats.contains("pdf")); String result = formats.replace("xml", "").replace("json", "").replace("html", "").replace("pdf", "").replaceAll(",", "").replaceAll(" ", ""); if (result.length() > 0) { printOut(bundle.getString("incorrectReportFormat")); return false; } else { setExplicitFormats(true); return true; } } public void parseConfig() { if (config != null) { if (explicitFormats) { config.getFormats().clear(); if (xml) config.getFormats().add("XML"); if (pdf) config.getFormats().add("PDF"); if (html) config.getFormats().add("HTML"); if (json) config.getFormats().add("JSON"); } if (explicitOutput) { config.setOutput(outputFolder); } } else { //Create it config = new Configuration(null, null, new ArrayList<>()); if (xml) config.getFormats().add("XML"); if (pdf) config.getFormats().add("PDF"); if (html) config.getFormats().add("HTML"); if (json) config.getFormats().add("JSON"); config.getIsos().add("TIFF_Baseline_Core_6_0"); config.setOutput(outputFolder); } } public List zipFolders() { List tmpFiles = new ArrayList<>(); for (String file : files) { if (new File(file).isDirectory()) { String zipPath = zipFolder(file); if (zipPath != null) { tmpFiles.add(zipPath); } } } return tmpFiles; } public String zipFolder(String folder) { try { if (!folder.endsWith("/") && !folder.endsWith("\\")){ folder = folder + "/"; } File outputFile = Files.createTempFile("input", ".zip").toFile(); ZipOutputStream zipFile = new ZipOutputStream(new FileOutputStream(outputFile)); compressDirectoryToZipfile(folder, folder, zipFile); IOUtils.closeQuietly(zipFile); return outputFile.getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); return null; } } private void compressDirectoryToZipfile(String rootDir, String sourceDir, ZipOutputStream out) throws IOException, FileNotFoundException { for (File file : new File(sourceDir).listFiles()) { if (file.isDirectory()) { compressDirectoryToZipfile(rootDir, sourceDir + file.getName() + File.separator, out); } else { ZipEntry entry = new ZipEntry(sourceDir.replace(rootDir, "") + file.getName()); out.putNextEntry(entry); FileInputStream in = new FileInputStream(sourceDir + file.getName()); IOUtils.copy(in, out); IOUtils.closeQuietly(in); } } } /** * Parameters */ public Map getParameters() { return parameters; } public boolean hasParameter(String key){ return parameters.containsKey(key); } public void putParameter(String key, String value){ parameters.put(key, value); } public String getParameter(String key){ return parameters.get(key); } public boolean containsParameter(String key){ return parameters.containsKey(key); } public boolean containsParameters(List keys){ for (String key : keys){ if (containsParameter(key)){ return true; } } return false; } /** * Getters / Setters */ public Configuration getConfig() { return config; } public void setConfig(Configuration config) { this.config = config; } public boolean isXml() { return xml; } public void setXml(boolean xml) { this.xml = xml; } public boolean isJson() { return json; } public void setJson(boolean json) { this.json = json; } public boolean isHtml() { return html; } public void setHtml(boolean html) { this.html = html; } public boolean isPdf() { return pdf; } public void setPdf(boolean pdf) { this.pdf = pdf; } public String getOutputFolder() { return outputFolder; } public void setOutputFolder(String outputFolder) { this.outputFolder = outputFolder; } public boolean isExplicitFormats() { return explicitFormats; } public void setExplicitFormats(boolean explicitFormats) { this.explicitFormats = explicitFormats; } public boolean isExplicitOutput() { return explicitOutput; } public void setExplicitOutput(boolean explicitOutput) { this.explicitOutput = explicitOutput; } public List getFiles() { return files; } public void addFile(String file) { files.add(file); } /** * Custom print lines */ private void printOut(String message) { context.send(BasicConfig.MODULE_MESSAGE, new LogMessage(getClass(), Level.DEBUG, message)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy