CLI: supprt combination of --input-file and --system-id (to choose proper input module for given SystemId but actually read stream from the file)
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 06 Sep 2014 21:41:15 +0200
changeset 90c96459e02690
parent 89 46c7cc4863c1
child 91 8146ad99fc67
CLI: supprt combination of --input-file and --system-id (to choose proper input module for given SystemId but actually read stream from the file)
java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java
     1.1 --- a/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java	Sat Sep 06 21:24:53 2014 +0200
     1.2 +++ b/java/alt2xml-cli/src/cz/frantovo/alt2xml/cli/CLIOptions.java	Sat Sep 06 21:41:15 2014 +0200
     1.3 @@ -18,11 +18,16 @@
     1.4  package cz.frantovo.alt2xml.cli;
     1.5  
     1.6  import java.io.File;
     1.7 +import java.io.FileInputStream;
     1.8 +import java.io.FileNotFoundException;
     1.9  import java.io.InputStream;
    1.10  import java.io.OutputStream;
    1.11  import java.util.ArrayList;
    1.12  import java.util.List;
    1.13  import java.util.Properties;
    1.14 +import java.util.logging.Level;
    1.15 +import java.util.logging.LogRecord;
    1.16 +import java.util.logging.Logger;
    1.17  import org.xml.sax.InputSource;
    1.18  
    1.19  /**
    1.20 @@ -33,6 +38,8 @@
    1.21   */
    1.22  public class CLIOptions {
    1.23  
    1.24 +	private static final Logger log = Logger.getLogger(CLIOptions.class.getName());
    1.25 +
    1.26  	private File inputFile;
    1.27  	private InputStream inputStream;
    1.28  
    1.29 @@ -74,18 +81,29 @@
    1.30  	public InputSource getInputSource() {
    1.31  		InputSource is = new InputSource();
    1.32  
    1.33 +		if (systemId != null) {
    1.34 +			is.setSystemId(systemId);
    1.35 +		}
    1.36 +
    1.37  		if (inputFile != null) {
    1.38 -			is.setSystemId(inputFile.toURI().toASCIIString());
    1.39 +			if (systemId == null) {
    1.40 +				is.setSystemId(inputFile.toURI().toASCIIString());
    1.41 +			} else {
    1.42 +				try {
    1.43 +					is.setByteStream(new FileInputStream(inputFile));
    1.44 +				} catch (FileNotFoundException e) {
    1.45 +					LogRecord message = new LogRecord(Level.WARNING, "Unable to open file „{0}“ → fallback to SystemId „{1}“");
    1.46 +					message.setParameters(new Object[]{inputFile, systemId});
    1.47 +					message.setThrown(e);
    1.48 +					log.log(message);
    1.49 +				}
    1.50 +			}
    1.51  		}
    1.52  
    1.53  		if (inputStream != null) {
    1.54  			is.setByteStream(inputStream);
    1.55  		}
    1.56  
    1.57 -		if (systemId != null) {
    1.58 -			is.setSystemId(systemId);
    1.59 -		}
    1.60 -
    1.61  		return is;
    1.62  	}
    1.63