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