java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 03 Sep 2014 15:50:16 +0200
changeset 58 c11792ae6489
parent 56 5e79450a521e
child 64 e2c691eedf4d
permissions -rw-r--r--
options validation: --input-stdin requires --system-id
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@54
    21
import java.io.InputStream;
franta-hg@54
    22
import java.io.OutputStream;
franta-hg@54
    23
import java.util.ArrayList;
franta-hg@54
    24
import java.util.List;
franta-hg@54
    25
import java.util.Properties;
franta-hg@55
    26
import org.xml.sax.InputSource;
franta-hg@54
    27
franta-hg@54
    28
/**
franta-hg@54
    29
 * Holds options from command line, validates them, combines with configuration and provides derived
franta-hg@54
    30
 * objects.
franta-hg@54
    31
 *
franta-hg@54
    32
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@54
    33
 */
franta-hg@54
    34
public class CLIOptions {
franta-hg@54
    35
franta-hg@54
    36
	private File inputFile;
franta-hg@54
    37
	private InputStream inputStream;
franta-hg@54
    38
franta-hg@54
    39
	private String systemId;
franta-hg@54
    40
	private final Properties readerProperties = new Properties();
franta-hg@54
    41
franta-hg@54
    42
	private OutputStream outputStream;
franta-hg@54
    43
	private String action;
franta-hg@54
    44
	private final Properties actionProperties = new Properties();
franta-hg@54
    45
	private final List<String> actionData = new ArrayList<>();
franta-hg@54
    46
franta-hg@54
    47
	public void validate() throws InvalidOptionsException {
franta-hg@54
    48
		InvalidOptionsException e = new InvalidOptionsException();
franta-hg@58
    49
		
franta-hg@58
    50
		if (inputFile == null && systemId == null) {
franta-hg@58
    51
			e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file."));
franta-hg@58
    52
		}
franta-hg@54
    53
franta-hg@54
    54
		/**
franta-hg@55
    55
		 * TODO: validate
franta-hg@54
    56
		 */
franta-hg@54
    57
		if (e.hasProblems()) {
franta-hg@54
    58
			throw e;
franta-hg@54
    59
		}
franta-hg@54
    60
	}
franta-hg@54
    61
franta-hg@54
    62
	public void setInputFile(File inputFile) {
franta-hg@54
    63
		this.inputFile = inputFile;
franta-hg@54
    64
	}
franta-hg@54
    65
franta-hg@54
    66
	public void setInputStream(InputStream inputStream) {
franta-hg@54
    67
		this.inputStream = inputStream;
franta-hg@54
    68
	}
franta-hg@54
    69
franta-hg@54
    70
	public void setSystemId(String systemId) {
franta-hg@54
    71
		this.systemId = systemId;
franta-hg@54
    72
	}
franta-hg@55
    73
franta-hg@55
    74
	public InputSource getInputSource() {
franta-hg@55
    75
		InputSource is = new InputSource();
franta-hg@55
    76
franta-hg@55
    77
		if (inputFile != null) {
franta-hg@55
    78
			is.setSystemId(inputFile.toURI().toASCIIString());
franta-hg@55
    79
		}
franta-hg@55
    80
franta-hg@55
    81
		if (inputStream != null) {
franta-hg@55
    82
			is.setByteStream(inputStream);
franta-hg@56
    83
		}
franta-hg@56
    84
franta-hg@56
    85
		if (systemId != null) {
franta-hg@55
    86
			is.setSystemId(systemId);
franta-hg@55
    87
		}
franta-hg@55
    88
franta-hg@55
    89
		return is;
franta-hg@55
    90
	}
franta-hg@55
    91
franta-hg@54
    92
	public void setOutputStream(OutputStream outputStream) {
franta-hg@54
    93
		this.outputStream = outputStream;
franta-hg@54
    94
	}
franta-hg@55
    95
franta-hg@55
    96
	public OutputStream getOutputStream() {
franta-hg@55
    97
		return outputStream;
franta-hg@55
    98
	}
franta-hg@55
    99
franta-hg@54
   100
	public void setAction(String action) {
franta-hg@54
   101
		this.action = action;
franta-hg@54
   102
	}
franta-hg@54
   103
franta-hg@55
   104
	public String getAction() {
franta-hg@55
   105
		return action;
franta-hg@55
   106
	}
franta-hg@55
   107
franta-hg@54
   108
	public void addReaderProperty(String name, String value) {
franta-hg@54
   109
		readerProperties.put(name, value);
franta-hg@54
   110
	}
franta-hg@54
   111
franta-hg@54
   112
	public void addActionProperty(String name, String value) {
franta-hg@54
   113
		actionProperties.put(name, value);
franta-hg@54
   114
	}
franta-hg@54
   115
franta-hg@54
   116
	public void addActionData(String value) {
franta-hg@54
   117
		actionData.add(value);
franta-hg@54
   118
	}
franta-hg@54
   119
franta-hg@54
   120
}