option: --skip-errors
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 17 Nov 2014 20:23:58 +0100
changeset 14dec0dd934a64
parent 13 28aa5f597457
child 15 93fa6ce675e5
option: --skip-errors
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java
java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java
     1.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 20:13:15 2014 +0100
     1.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveImageResizer.java	Mon Nov 17 20:23:58 2014 +0100
     1.3 @@ -27,6 +27,7 @@
     1.4  import java.io.InputStream;
     1.5  import java.nio.channels.FileChannel;
     1.6  import java.util.logging.Level;
     1.7 +import java.util.logging.LogRecord;
     1.8  import java.util.logging.Logger;
     1.9  import javax.imageio.ImageIO;
    1.10  
    1.11 @@ -141,7 +142,20 @@
    1.12  			if (entry.isDirectory()) {
    1.13  				resizeDirectory(entry);
    1.14  			} else {
    1.15 -				resizeFile(entry);
    1.16 +				if (options.isSkipErrors()) {
    1.17 +					try {
    1.18 +						resizeFile(entry);
    1.19 +					} catch (Exception e) {
    1.20 +						counters.increment(Counters.COUNTER_TYPE.SKIPPED_ERROR);
    1.21 +						LogRecord record = new LogRecord(Level.WARNING, "Skipping error : {0}");
    1.22 +						record.setParameters(new Object[]{entry});
    1.23 +						record.setThrown(e);
    1.24 +						log.log(record);
    1.25 +					}
    1.26 +				} else {
    1.27 +					resizeFile(entry);
    1.28 +				}
    1.29 +
    1.30  			}
    1.31  		}
    1.32  
     2.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java	Mon Nov 17 20:13:15 2014 +0100
     2.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/RecursiveOptions.java	Mon Nov 17 20:23:58 2014 +0100
     2.3 @@ -39,6 +39,10 @@
     2.4  	 * Image resolutions to create
     2.5  	 */
     2.6  	private final Collection<SizeSpecification> sizes = new ArrayList<>();
     2.7 +	/**
     2.8 +	 * Whether errors (while resizing/copying particular images) should be just logged and skipped.
     2.9 +	 */
    2.10 +	private boolean skipErrors;
    2.11  
    2.12  	public File getInput() {
    2.13  		return input;
    2.14 @@ -64,6 +68,14 @@
    2.15  		sizes.add(size);
    2.16  	}
    2.17  
    2.18 +	public boolean isSkipErrors() {
    2.19 +		return skipErrors;
    2.20 +	}
    2.21 +
    2.22 +	public void setSkipErrors(boolean skipErrors) {
    2.23 +		this.skipErrors = skipErrors;
    2.24 +	}
    2.25 +
    2.26  	public void validate() throws InvalidOptionsException {
    2.27  		InvalidOptionsException e = new InvalidOptionsException();
    2.28  
     3.1 --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java	Mon Nov 17 20:13:15 2014 +0100
     3.2 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/cli/CLIParser.java	Mon Nov 17 20:23:58 2014 +0100
     3.3 @@ -114,6 +114,15 @@
     3.4  						options.addSize(new SizeSpecification(width, height, directory, resizeSmaller));
     3.5  						return index - originalIndex;
     3.6  					}
     3.7 +				},
     3.8 +		SKIP_ERRORS("--skip-errors") { // bash-completion: option
     3.9 +
    3.10 +					@Override
    3.11 +					public int parse(String[] args, int index, RecursiveOptions options) throws CLIParserException {
    3.12 +						options.setSkipErrors(true);
    3.13 +						return 0;
    3.14 +					}
    3.15 +
    3.16  				};
    3.17  
    3.18  		private final Collection<String> options;