java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 21:41:15 +0200
changeset 90 c96459e02690
parent 64 e2c691eedf4d
child 98 944a81a54bb9
permissions -rw-r--r--
CLI: supprt combination of --input-file and --system-id (to choose proper input module for given SystemId but actually read stream from the file)
franta-hg@54
     1
/**
franta-hg@54
     2
 * Alt2XML
franta-hg@54
     3
 * Copyright © 2014 František Kučera (frantovo.cz)
franta-hg@54
     4
 *
franta-hg@54
     5
 * This program is free software: you can redistribute it and/or modify
franta-hg@54
     6
 * it under the terms of the GNU General Public License as published by
franta-hg@54
     7
 * the Free Software Foundation, either version 3 of the License, or
franta-hg@54
     8
 * (at your option) any later version.
franta-hg@54
     9
 *
franta-hg@54
    10
 * This program is distributed in the hope that it will be useful,
franta-hg@54
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@54
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@54
    13
 * GNU General Public License for more details.
franta-hg@54
    14
 *
franta-hg@54
    15
 * You should have received a copy of the GNU General Public License
franta-hg@54
    16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
franta-hg@54
    17
 */
franta-hg@54
    18
package cz.frantovo.alt2xml.cli;
franta-hg@54
    19
franta-hg@54
    20
import java.io.File;
franta-hg@90
    21
import java.io.FileInputStream;
franta-hg@90
    22
import java.io.FileNotFoundException;
franta-hg@54
    23
import java.io.InputStream;
franta-hg@54
    24
import java.io.OutputStream;
franta-hg@54
    25
import java.util.ArrayList;
franta-hg@54
    26
import java.util.List;
franta-hg@54
    27
import java.util.Properties;
franta-hg@90
    28
import java.util.logging.Level;
franta-hg@90
    29
import java.util.logging.LogRecord;
franta-hg@90
    30
import java.util.logging.Logger;
franta-hg@55
    31
import org.xml.sax.InputSource;
franta-hg@54
    32
franta-hg@54
    33
/**
franta-hg@54
    34
 * Holds options from command line, validates them, combines with configuration and provides derived
franta-hg@54
    35
 * objects.
franta-hg@54
    36
 *
franta-hg@54
    37
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@54
    38
 */
franta-hg@54
    39
public class CLIOptions {
franta-hg@54
    40
franta-hg@90
    41
	private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
franta-hg@90
    42
franta-hg@54
    43
	private File inputFile;
franta-hg@54
    44
	private InputStream inputStream;
franta-hg@54
    45
franta-hg@54
    46
	private String systemId;
franta-hg@54
    47
	private final Properties readerProperties = new Properties();
franta-hg@54
    48
franta-hg@54
    49
	private OutputStream outputStream;
franta-hg@54
    50
	private String action;
franta-hg@54
    51
	private final Properties actionProperties = new Properties();
franta-hg@54
    52
	private final List<String> actionData = new ArrayList<>();
franta-hg@54
    53
franta-hg@54
    54
	public void validate() throws InvalidOptionsException {
franta-hg@54
    55
		InvalidOptionsException e = new InvalidOptionsException();
franta-hg@64
    56
franta-hg@58
    57
		if (inputFile == null && systemId == null) {
franta-hg@58
    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
    59
		}
franta-hg@54
    60
franta-hg@54
    61
		/**
franta-hg@55
    62
		 * TODO: validate
franta-hg@54
    63
		 */
franta-hg@54
    64
		if (e.hasProblems()) {
franta-hg@54
    65
			throw e;
franta-hg@54
    66
		}
franta-hg@54
    67
	}
franta-hg@54
    68
franta-hg@54
    69
	public void setInputFile(File inputFile) {
franta-hg@54
    70
		this.inputFile = inputFile;
franta-hg@54
    71
	}
franta-hg@54
    72
franta-hg@54
    73
	public void setInputStream(InputStream inputStream) {
franta-hg@54
    74
		this.inputStream = inputStream;
franta-hg@54
    75
	}
franta-hg@54
    76
franta-hg@54
    77
	public void setSystemId(String systemId) {
franta-hg@54
    78
		this.systemId = systemId;
franta-hg@54
    79
	}
franta-hg@55
    80
franta-hg@55
    81
	public InputSource getInputSource() {
franta-hg@55
    82
		InputSource is = new InputSource();
franta-hg@55
    83
franta-hg@90
    84
		if (systemId != null) {
franta-hg@90
    85
			is.setSystemId(systemId);
franta-hg@90
    86
		}
franta-hg@90
    87
franta-hg@55
    88
		if (inputFile != null) {
franta-hg@90
    89
			if (systemId == null) {
franta-hg@90
    90
				is.setSystemId(inputFile.toURI().toASCIIString());
franta-hg@90
    91
			} else {
franta-hg@90
    92
				try {
franta-hg@90
    93
					is.setByteStream(new FileInputStream(inputFile));
franta-hg@90
    94
				} catch (FileNotFoundException e) {
franta-hg@90
    95
					LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
franta-hg@90
    96
					message.setParameters(new Object[]{inputFile, systemId});
franta-hg@90
    97
					message.setThrown(e);
franta-hg@90
    98
					log.log(message);
franta-hg@90
    99
				}
franta-hg@90
   100
			}
franta-hg@55
   101
		}
franta-hg@55
   102
franta-hg@55
   103
		if (inputStream != null) {
franta-hg@55
   104
			is.setByteStream(inputStream);
franta-hg@56
   105
		}
franta-hg@56
   106
franta-hg@55
   107
		return is;
franta-hg@55
   108
	}
franta-hg@55
   109
franta-hg@54
   110
	public void setOutputStream(OutputStream outputStream) {
franta-hg@54
   111
		this.outputStream = outputStream;
franta-hg@54
   112
	}
franta-hg@55
   113
franta-hg@55
   114
	public OutputStream getOutputStream() {
franta-hg@55
   115
		return outputStream;
franta-hg@55
   116
	}
franta-hg@55
   117
franta-hg@54
   118
	public void setAction(String action) {
franta-hg@54
   119
		this.action = action;
franta-hg@54
   120
	}
franta-hg@54
   121
franta-hg@55
   122
	public String getAction() {
franta-hg@55
   123
		return action;
franta-hg@55
   124
	}
franta-hg@55
   125
franta-hg@54
   126
	public void addReaderProperty(String name, String value) {
franta-hg@54
   127
		readerProperties.put(name, value);
franta-hg@54
   128
	}
franta-hg@54
   129
franta-hg@64
   130
	public Properties getReaderProperties() {
franta-hg@64
   131
		return readerProperties;
franta-hg@64
   132
	}
franta-hg@64
   133
franta-hg@54
   134
	public void addActionProperty(String name, String value) {
franta-hg@54
   135
		actionProperties.put(name, value);
franta-hg@54
   136
	}
franta-hg@54
   137
franta-hg@64
   138
	public Properties getActionProperties() {
franta-hg@64
   139
		return actionProperties;
franta-hg@64
   140
	}
franta-hg@64
   141
franta-hg@54
   142
	public void addActionData(String value) {
franta-hg@54
   143
		actionData.add(value);
franta-hg@54
   144
	}
franta-hg@54
   145
franta-hg@64
   146
	public List<String> getActionData() {
franta-hg@64
   147
		return actionData;
franta-hg@64
   148
	}
franta-hg@54
   149
}