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