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@54: * the Free Software Foundation, either version 3 of the License, or
franta-hg@54: * (at your option) any later version.
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@54: import java.io.InputStream;
franta-hg@54: import java.io.OutputStream;
franta-hg@54: import java.util.ArrayList;
franta-hg@54: import java.util.List;
franta-hg@54: import java.util.Properties;
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@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@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@55: if (inputFile != null) {
franta-hg@55: is.setSystemId(inputFile.toURI().toASCIIString());
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@56: if (systemId != null) {
franta-hg@55: is.setSystemId(systemId);
franta-hg@55: }
franta-hg@55:
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@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: }