validation: test if prefix/suffix are valid regular expressions v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 26 Dec 2013 00:58:00 +0100
branchv_0
changeset 63 3b9ec9c23a37
parent 62 7a88ac6ba40c
child 64 fcc499518dc7
validation: test if prefix/suffix are valid regular expressions
java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java
java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java
java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Thu Dec 26 00:18:03 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIOptions.java	Thu Dec 26 00:58:00 2013 +0100
@@ -25,6 +25,8 @@
 import java.util.Collection;
 import java.util.EnumSet;
 import java.util.List;
+import java.util.regex.Pattern;
+import java.util.regex.PatternSyntaxException;
 
 /**
  *
@@ -104,6 +106,11 @@
 			e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
 		}
 
+		try {
+			Pattern.compile(namePrefix + "test" + nameSuffix);
+		} catch (PatternSyntaxException regexException) {
+			e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
+		}
 
 		if (e.hasProblems()) {
 			throw e;
--- a/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Thu Dec 26 00:18:03 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/CLIStarter.java	Thu Dec 26 00:58:00 2013 +0100
@@ -29,6 +29,7 @@
 import java.io.IOException;
 import java.sql.SQLException;
 import java.util.logging.Level;
+import java.util.logging.LogRecord;
 import java.util.logging.Logger;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.Unmarshaller;
@@ -49,7 +50,7 @@
 	public static void main(String[] args) {
 		log.log(Level.FINE, "Starting " + Constants.PROGRAM_NAME);
 		int exitCode = EXIT_EXPECTED_ERROR;
-		
+
 		if (args.length == 0) {
 			args = new String[]{CLIParser.Tokens.INFO_HELP};
 		}
@@ -68,7 +69,10 @@
 		} catch (InvalidOptionsException e) {
 			log.log(Level.SEVERE, "Invalid CLI options", e);
 			for (InvalidOptionsException.OptionProblem p : e.getProblems()) {
-				log.log(Level.SEVERE, "Option problem: {0}", p.getDescription());
+				LogRecord r = new LogRecord(Level.SEVERE, "Option problem: {0}");
+				r.setThrown(p.getException());
+				r.setParameters(new Object[]{p.getDescription()});
+				log.log(r);
 			}
 		} catch (ConfigurationException e) {
 			log.log(Level.SEVERE, "Configuration problem", e);
--- a/java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java	Thu Dec 26 00:18:03 2013 +0100
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InvalidOptionsException.java	Thu Dec 26 00:58:00 2013 +0100
@@ -44,13 +44,23 @@
 	public static class OptionProblem {
 
 		private String description;
+		private Throwable exception;
 
 		public OptionProblem(String description) {
 			this.description = description;
 		}
 
+		public OptionProblem(String description, Throwable exception) {
+			this.description = description;
+			this.exception = exception;
+		}
+
 		public String getDescription() {
 			return description;
 		}
+
+		public Throwable getException() {
+			return exception;
+		}
 	}
 }