franta-hg@54: /** franta-hg@54: * Alt2XML franta-hg@54: * Copyright © 2014 František Kučera (frantovo.cz) franta-hg@54: * franta-hg@54: * This program is free software: you can redistribute it and/or modify franta-hg@54: * it under the terms of the GNU General Public License as published by franta-hg@111: * the Free Software Foundation, version 3 of the License. franta-hg@54: * franta-hg@54: * This program is distributed in the hope that it will be useful, franta-hg@54: * but WITHOUT ANY WARRANTY; without even the implied warranty of franta-hg@54: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the franta-hg@54: * GNU General Public License for more details. franta-hg@54: * franta-hg@54: * You should have received a copy of the GNU General Public License franta-hg@54: * along with this program. If not, see . franta-hg@54: */ franta-hg@54: package cz.frantovo.alt2xml.cli; franta-hg@54: franta-hg@54: import java.io.File; franta-hg@90: import java.io.FileInputStream; franta-hg@90: import java.io.FileNotFoundException; franta-hg@54: import java.io.InputStream; franta-hg@54: import java.io.OutputStream; franta-hg@54: import java.util.ArrayList; franta-hg@98: import java.util.HashMap; franta-hg@54: import java.util.List; franta-hg@98: import java.util.Map; franta-hg@54: import java.util.Properties; franta-hg@90: import java.util.logging.Level; franta-hg@90: import java.util.logging.LogRecord; franta-hg@90: import java.util.logging.Logger; franta-hg@55: import org.xml.sax.InputSource; franta-hg@54: franta-hg@54: /** franta-hg@54: * Holds options from command line, validates them, combines with configuration and provides derived franta-hg@54: * objects. franta-hg@54: * franta-hg@54: * @author Ing. František Kučera (frantovo.cz) franta-hg@54: */ franta-hg@54: public class CLIOptions { franta-hg@54: franta-hg@90: private static final Logger log = Logger.getLogger(CLIOptions.class.getName()); franta-hg@90: franta-hg@54: private File inputFile; franta-hg@54: private InputStream inputStream; franta-hg@54: franta-hg@54: private String systemId; franta-hg@54: private final Properties readerProperties = new Properties(); franta-hg@98: private final Map readerFeatures = new HashMap<>(); franta-hg@54: franta-hg@54: private OutputStream outputStream; franta-hg@54: private String action; franta-hg@54: private final Properties actionProperties = new Properties(); franta-hg@54: private final List actionData = new ArrayList<>(); franta-hg@54: franta-hg@54: public void validate() throws InvalidOptionsException { franta-hg@54: InvalidOptionsException e = new InvalidOptionsException(); franta-hg@64: franta-hg@58: if (inputFile == null && systemId == null) { franta-hg@58: e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file.")); franta-hg@58: } franta-hg@54: franta-hg@54: /** franta-hg@55: * TODO: validate franta-hg@54: */ franta-hg@54: if (e.hasProblems()) { franta-hg@54: throw e; franta-hg@54: } franta-hg@54: } franta-hg@54: franta-hg@54: public void setInputFile(File inputFile) { franta-hg@54: this.inputFile = inputFile; franta-hg@54: } franta-hg@54: franta-hg@54: public void setInputStream(InputStream inputStream) { franta-hg@54: this.inputStream = inputStream; franta-hg@54: } franta-hg@54: franta-hg@54: public void setSystemId(String systemId) { franta-hg@54: this.systemId = systemId; franta-hg@54: } franta-hg@55: franta-hg@55: public InputSource getInputSource() { franta-hg@55: InputSource is = new InputSource(); franta-hg@55: franta-hg@90: if (systemId != null) { franta-hg@90: is.setSystemId(systemId); franta-hg@90: } franta-hg@90: franta-hg@55: if (inputFile != null) { franta-hg@90: if (systemId == null) { franta-hg@90: is.setSystemId(inputFile.toURI().toASCIIString()); franta-hg@90: } else { franta-hg@90: try { franta-hg@90: is.setByteStream(new FileInputStream(inputFile)); franta-hg@90: } catch (FileNotFoundException e) { franta-hg@90: LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“"); franta-hg@90: message.setParameters(new Object[]{inputFile, systemId}); franta-hg@90: message.setThrown(e); franta-hg@90: log.log(message); franta-hg@90: } franta-hg@90: } franta-hg@55: } franta-hg@55: franta-hg@55: if (inputStream != null) { franta-hg@55: is.setByteStream(inputStream); franta-hg@56: } franta-hg@56: franta-hg@55: return is; franta-hg@55: } franta-hg@55: franta-hg@54: public void setOutputStream(OutputStream outputStream) { franta-hg@54: this.outputStream = outputStream; franta-hg@54: } franta-hg@55: franta-hg@55: public OutputStream getOutputStream() { franta-hg@55: return outputStream; franta-hg@55: } franta-hg@55: franta-hg@54: public void setAction(String action) { franta-hg@54: this.action = action; franta-hg@54: } franta-hg@54: franta-hg@55: public String getAction() { franta-hg@55: return action; franta-hg@55: } franta-hg@55: franta-hg@54: public void addReaderProperty(String name, String value) { franta-hg@54: readerProperties.put(name, value); franta-hg@54: } franta-hg@54: franta-hg@64: public Properties getReaderProperties() { franta-hg@64: return readerProperties; franta-hg@64: } franta-hg@64: franta-hg@98: public void addReaderFeature(String name, boolean value) { franta-hg@98: readerFeatures.put(name, value); franta-hg@98: } franta-hg@98: franta-hg@98: public Map getReaderFeatures() { franta-hg@98: return readerFeatures; franta-hg@98: } franta-hg@98: franta-hg@54: public void addActionProperty(String name, String value) { franta-hg@54: actionProperties.put(name, value); franta-hg@54: } franta-hg@54: franta-hg@64: public Properties getActionProperties() { franta-hg@64: return actionProperties; franta-hg@64: } franta-hg@64: franta-hg@54: public void addActionData(String value) { franta-hg@54: actionData.add(value); franta-hg@54: } franta-hg@54: franta-hg@64: public List getActionData() { franta-hg@64: return actionData; franta-hg@64: } franta-hg@54: }