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