# HG changeset patch # User František Kučera # Date 1416171423 -3600 # Node ID e417b37e6c08f13ce66d01e5b5a80cc771056575 # Parent 0568271a1174e26bc6e0b4ca0f94e284deab917a options, parser diff -r 0568271a1174 -r e417b37e6c08 .hgignore --- a/.hgignore Sun Nov 16 19:41:41 2014 +0100 +++ b/.hgignore Sun Nov 16 21:57:03 2014 +0100 @@ -4,4 +4,4 @@ syntax: regexp -^java/copy-image-resizer/(dist|build|private)/ +^java/copy-image-resizer/(dist|build|nbproject/private)/ diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/nbproject/private/private.xml --- a/java/copy-image-resizer/nbproject/private/private.xml Sun Nov 16 19:41:41 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ - - - - - - file:/home/fiki/projekty/copy-image-resizer/copy-image-resizer/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIResizer.java - - - diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIOptions.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIOptions.java Sun Nov 16 21:57:03 2014 +0100 @@ -0,0 +1,91 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLIOptions { + + /** + * Source directory + */ + private File input; + /** + * Output directory + */ + private File output; + /** + * Image resolutions to create + */ + private final Collection sizes = new ArrayList<>(); + + public File getSourceDir() { + return input; + } + + public void setInput(File input) { + this.input = input; + } + + public File getDestinationDir() { + return output; + } + + public void setOutput(File output) { + this.output = output; + } + + public Collection getSizes() { + return sizes; + } + + public void addSize(SizeSpecification size) { + sizes.add(size); + } + + public static class SizeSpecification { + + private final int width; + private final int height; + private final String directory; + + public SizeSpecification(int width, int height, String directory) { + this.width = width; + this.height = height; + this.directory = directory; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public String getDirectory() { + return directory; + } + } +} diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParser.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParser.java Sun Nov 16 21:57:03 2014 +0100 @@ -0,0 +1,139 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +import java.io.File; +import java.util.Arrays; +import java.util.Collection; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLIParser { + + public CLIOptions parseOptions(String[] args) throws CLIParserException { + CLIOptions options = new CLIOptions(); + + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + + boolean matches = false; + + for (Token t : Token.values()) { + if (t.matches(arg)) { + int parsedArgs = t.parse(args, i, options); + i = i + parsedArgs; + matches = true; + } + } + + if (!matches) { + throw new CLIParserException("Unknown option: " + arg); + } + } + + return options; + } + + private static String fetchNext(String[] args, int index) throws CLIParserException { + if (index < args.length) { + return args[index]; + } else { + throw new CLIParserException("Expecting value for option: " + args[index - 1]); + } + } + + private static enum Token { + + INPUT_DIR("--input-dir") { // bash-completion: option + @Override + public int parse(String[] args, int index, CLIOptions options) throws CLIParserException { + int originalIndex = index; + String name = fetchNext(args, ++index); + options.setInput(new File(name)); + return index - originalIndex; + } + }, + OUTPUT_DIR("--output-dir") { // bash-completion: option + @Override + public int parse(String[] args, int index, CLIOptions options) throws CLIParserException { + int originalIndex = index; + String name = fetchNext(args, ++index); + options.setOutput(new File(name)); + return index - originalIndex; + } + }, + SIZE("--size") { // bash-completion: option + @Override + public int parse(String[] args, int index, CLIOptions options) throws CLIParserException { + int originalIndex = index; + int width = parseInt(fetchNext(args, ++index)); + int height = parseInt(fetchNext(args, ++index)); + String directory = width + "x" + height; + options.addSize(new CLIOptions.SizeSpecification(width, height, directory)); + return index - originalIndex; + } + }, + SIZE_NAMED("--size-named") { // bash-completion: option + @Override + public int parse(String[] args, int index, CLIOptions options) throws CLIParserException { + int originalIndex = index; + int width = parseInt(fetchNext(args, ++index)); + int height = parseInt(fetchNext(args, ++index)); + String directory = fetchNext(args, ++index); + options.addSize(new CLIOptions.SizeSpecification(width, height, directory)); + return index - originalIndex; + } + }; + + private final Collection options; + + private Token(String... options) { + this.options = Arrays.asList(options); + } + + /** + * @param option e.g. „--input-file“ + * @return whether option is this token + */ + public boolean matches(String option) { + return options.contains(option); + } + + private static int parseInt(String number) throws CLIParserException { + try { + return Integer.parseInt(number); + } catch (Exception e) { + throw new CLIParserException("Unable to parse integer: " + number, e); + } + } + + /** + * Parse String arguments and fill values into the options object. + * + * @param args CLI arguments + * @param index index of the option matched by this token, like „--input-file“ + * @param options object to be filled + * @return number of parsed arguments – if option has no arguments (just boolean flag), + * return 0, otherwise return positive integer: number of eaten arguments. + * @throws CLIParserException + */ + public abstract int parse(String[] args, int index, CLIOptions options) throws CLIParserException; + } +} diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParserException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIParserException.java Sun Nov 16 21:57:03 2014 +0100 @@ -0,0 +1,41 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLIParserException extends CopyImageResizerException { + + public CLIParserException() { + } + + public CLIParserException(String message) { + super(message); + } + + public CLIParserException(Throwable cause) { + super(cause); + } + + public CLIParserException(String message, Throwable cause) { + super(message, cause); + } + +} diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIResizer.java --- a/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIResizer.java Sun Nov 16 19:41:41 2014 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -/** - * copy-image-resizer - * Copyright © 2014 František Kučera (frantovo.cz) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package cz.frantovo.copyImageResizer; - -/** - * - * @author Ing. František Kučera (frantovo.cz) - */ -public class CLIResizer { - - /** - * @param args the command line arguments - */ - public static void main(String[] args) { - // TODO code application logic here - System.out.println("TODO"); - } - -} diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIStarter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CLIStarter.java Sun Nov 16 21:57:03 2014 +0100 @@ -0,0 +1,34 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CLIStarter { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + System.out.println("TODO"); + } + +} diff -r 0568271a1174 -r e417b37e6c08 java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/copy-image-resizer/src/cz/frantovo/copyImageResizer/CopyImageResizerException.java Sun Nov 16 21:57:03 2014 +0100 @@ -0,0 +1,41 @@ +/** + * copy-image-resizer + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.copyImageResizer; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class CopyImageResizerException extends Exception { + + public CopyImageResizerException() { + } + + public CopyImageResizerException(String message) { + super(message); + } + + public CopyImageResizerException(Throwable cause) { + super(cause); + } + + public CopyImageResizerException(String message, Throwable cause) { + super(message, cause); + } + +}