java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java
author František Kučera <franta-hg@frantovo.cz>
Wed, 17 Sep 2014 19:13:47 +0200
changeset 98 944a81a54bb9
parent 90 c96459e02690
child 111 e4900596abdb
permissions -rw-r--r--
cli: parse reader features option
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@98
    26
import java.util.HashMap;
franta-hg@54
    27
import java.util.List;
franta-hg@98
    28
import java.util.Map;
franta-hg@54
    29
import java.util.Properties;
franta-hg@90
    30
import java.util.logging.Level;
franta-hg@90
    31
import java.util.logging.LogRecord;
franta-hg@90
    32
import java.util.logging.Logger;
franta-hg@55
    33
import org.xml.sax.InputSource;
franta-hg@54
    34
franta-hg@54
    35
/**
franta-hg@54
    36
 * Holds options from command line, validates them, combines with configuration and provides derived
franta-hg@54
    37
 * objects.
franta-hg@54
    38
 *
franta-hg@54
    39
 * @author Ing. František Kučera (frantovo.cz)
franta-hg@54
    40
 */
franta-hg@54
    41
public class CLIOptions {
franta-hg@54
    42
franta-hg@90
    43
	private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
franta-hg@90
    44
franta-hg@54
    45
	private File inputFile;
franta-hg@54
    46
	private InputStream inputStream;
franta-hg@54
    47
franta-hg@54
    48
	private String systemId;
franta-hg@54
    49
	private final Properties readerProperties = new Properties();
franta-hg@98
    50
	private final Map<String, Boolean> readerFeatures = new HashMap<>();
franta-hg@54
    51
franta-hg@54
    52
	private OutputStream outputStream;
franta-hg@54
    53
	private String action;
franta-hg@54
    54
	private final Properties actionProperties = new Properties();
franta-hg@54
    55
	private final List<String> actionData = new ArrayList<>();
franta-hg@54
    56
franta-hg@54
    57
	public void validate() throws InvalidOptionsException {
franta-hg@54
    58
		InvalidOptionsException e = new InvalidOptionsException();
franta-hg@64
    59
franta-hg@58
    60
		if (inputFile == null && systemId == null) {
franta-hg@58
    61
			e.addProblem(new InvalidOptionsException.OptionProblem("Please specify also systemId when reading from a stream (like STDIN) instead of from a file."));
franta-hg@58
    62
		}
franta-hg@54
    63
franta-hg@54
    64
		/**
franta-hg@55
    65
		 * TODO: validate
franta-hg@54
    66
		 */
franta-hg@54
    67
		if (e.hasProblems()) {
franta-hg@54
    68
			throw e;
franta-hg@54
    69
		}
franta-hg@54
    70
	}
franta-hg@54
    71
franta-hg@54
    72
	public void setInputFile(File inputFile) {
franta-hg@54
    73
		this.inputFile = inputFile;
franta-hg@54
    74
	}
franta-hg@54
    75
franta-hg@54
    76
	public void setInputStream(InputStream inputStream) {
franta-hg@54
    77
		this.inputStream = inputStream;
franta-hg@54
    78
	}
franta-hg@54
    79
franta-hg@54
    80
	public void setSystemId(String systemId) {
franta-hg@54
    81
		this.systemId = systemId;
franta-hg@54
    82
	}
franta-hg@55
    83
franta-hg@55
    84
	public InputSource getInputSource() {
franta-hg@55
    85
		InputSource is = new InputSource();
franta-hg@55
    86
franta-hg@90
    87
		if (systemId != null) {
franta-hg@90
    88
			is.setSystemId(systemId);
franta-hg@90
    89
		}
franta-hg@90
    90
franta-hg@55
    91
		if (inputFile != null) {
franta-hg@90
    92
			if (systemId == null) {
franta-hg@90
    93
				is.setSystemId(inputFile.toURI().toASCIIString());
franta-hg@90
    94
			} else {
franta-hg@90
    95
				try {
franta-hg@90
    96
					is.setByteStream(new FileInputStream(inputFile));
franta-hg@90
    97
				} catch (FileNotFoundException e) {
franta-hg@90
    98
					LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
franta-hg@90
    99
					message.setParameters(new Object[]{inputFile, systemId});
franta-hg@90
   100
					message.setThrown(e);
franta-hg@90
   101
					log.log(message);
franta-hg@90
   102
				}
franta-hg@90
   103
			}
franta-hg@55
   104
		}
franta-hg@55
   105
franta-hg@55
   106
		if (inputStream != null) {
franta-hg@55
   107
			is.setByteStream(inputStream);
franta-hg@56
   108
		}
franta-hg@56
   109
franta-hg@55
   110
		return is;
franta-hg@55
   111
	}
franta-hg@55
   112
franta-hg@54
   113
	public void setOutputStream(OutputStream outputStream) {
franta-hg@54
   114
		this.outputStream = outputStream;
franta-hg@54
   115
	}
franta-hg@55
   116
franta-hg@55
   117
	public OutputStream getOutputStream() {
franta-hg@55
   118
		return outputStream;
franta-hg@55
   119
	}
franta-hg@55
   120
franta-hg@54
   121
	public void setAction(String action) {
franta-hg@54
   122
		this.action = action;
franta-hg@54
   123
	}
franta-hg@54
   124
franta-hg@55
   125
	public String getAction() {
franta-hg@55
   126
		return action;
franta-hg@55
   127
	}
franta-hg@55
   128
franta-hg@54
   129
	public void addReaderProperty(String name, String value) {
franta-hg@54
   130
		readerProperties.put(name, value);
franta-hg@54
   131
	}
franta-hg@54
   132
franta-hg@64
   133
	public Properties getReaderProperties() {
franta-hg@64
   134
		return readerProperties;
franta-hg@64
   135
	}
franta-hg@64
   136
franta-hg@98
   137
	public void addReaderFeature(String name, boolean value) {
franta-hg@98
   138
		readerFeatures.put(name, value);
franta-hg@98
   139
	}
franta-hg@98
   140
franta-hg@98
   141
	public Map<String, Boolean> getReaderFeatures() {
franta-hg@98
   142
		return readerFeatures;
franta-hg@98
   143
	}
franta-hg@98
   144
franta-hg@54
   145
	public void addActionProperty(String name, String value) {
franta-hg@54
   146
		actionProperties.put(name, value);
franta-hg@54
   147
	}
franta-hg@54
   148
franta-hg@64
   149
	public Properties getActionProperties() {
franta-hg@64
   150
		return actionProperties;
franta-hg@64
   151
	}
franta-hg@64
   152
franta-hg@54
   153
	public void addActionData(String value) {
franta-hg@54
   154
		actionData.add(value);
franta-hg@54
   155
	}
franta-hg@54
   156
franta-hg@64
   157
	public List<String> getActionData() {
franta-hg@64
   158
		return actionData;
franta-hg@64
   159
	}
franta-hg@54
   160
}