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