From 0be891100df83544c89651c815105832e3e11eb9 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Thu, 6 Apr 2017 16:02:40 -0400 Subject: Start moving things to properties This cuts down on the amount of regex escapes --- BJC-Utils2/data/formats.sprop | 11 ++ BJC-Utils2/data/regexes.sprop | 19 +++ BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java | 128 +++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 BJC-Utils2/data/formats.sprop create mode 100644 BJC-Utils2/data/regexes.sprop create mode 100644 BJC-Utils2/src/main/java/bjc/utils/PropertyDB.java (limited to 'BJC-Utils2') diff --git a/BJC-Utils2/data/formats.sprop b/BJC-Utils2/data/formats.sprop new file mode 100644 index 0000000..bdd9286 --- /dev/null +++ b/BJC-Utils2/data/formats.sprop @@ -0,0 +1,11 @@ +# File storage for format strings + +#################################################### +# Format strings for handling double-quoted strings. +#################################################### + +## Format the three types of string escapes into a valid pattern. +stringEscape \\(%1$s|%2$s|%3$s) + +## Format the parts of a regex into one that matches java-style double-quoted strings +doubleQuotes ("(%1$s|%2$s)*") \ No newline at end of file diff --git a/BJC-Utils2/data/regexes.sprop b/BJC-Utils2/data/regexes.sprop new file mode 100644 index 0000000..bece4d3 --- /dev/null +++ b/BJC-Utils2/data/regexes.sprop @@ -0,0 +1,19 @@ +# File storage for static regular expressions. + +######################################################## +# Regular expressions for handling double-quoted strings +######################################################## + +## Match a possible single character escape +possibleStringEscape \\. + +## Match valid string escapes +shortFormStringEscape [btnfr"'\\] +octalStringEscape [0-3]?[0-7]{1,2} +unicodeStringEscape u[0-9a-fA-F]{4} + +## Match an unescaped quote in a string. +unescapedQuote (? compiledRegexes; + + private static SimpleProperties formats; + + /* + * The lock to use to ensure a read can't happen during a reload + */ + private static LambdaLock loadLock = new LambdaLock(); + + static { + reloadProperties(); + } + + /** + * Reload all the properties from their files. + * + * NOTE: Any attempts to read from the property DB while properties are + * being loaded will block, to prevent reads from partial states. + */ + public static void reloadProperties() { + loadLock.write(() -> { + regexes = new SimpleProperties(); + regexes.loadFrom(PropertyDB.class.getResourceAsStream("/regexes.sprop"), false); + + compiledRegexes = new HashMap<>(); + + formats = new SimpleProperties(); + formats.loadFrom(PropertyDB.class.getResourceAsStream("/formats.sprop"), false); + }); + } + + /** + * Retrieve a persisted regular expression. + * + * @param key + * The name of the regular expression. + * + * @return The regular expression with that name. + */ + public static String getRegex(String key) { + return loadLock.read(() -> { + if(!regexes.containsKey(key)) { + String msg = String.format("No regular expression named '%s' found", key); + + throw new NoSuchElementException(msg); + } + + return regexes.get(key); + }); + } + + /** + * Retrieve a persisted regular expression, compiled into a regular + * expression. + * + * @param key + * The name of the regular expression. + * + * @return The regular expression with that name. + */ + public static Pattern getCompiledRegex(String key) { + return loadLock.read(() -> { + if(!regexes.containsKey(key)) { + String msg = String.format("No regular expression named '%s' found", key); + + throw new NoSuchElementException(msg); + } + + return compiledRegexes.computeIfAbsent(key, strang -> { + return Pattern.compile(regexes.get(strang)); + }); + }); + } + + /** + * Retrieve a persisted format string. + * + * @param key + * The name of the format string. + * + * @return The format string with that name. + */ + public static String getFormat(String key) { + return loadLock.read(() -> { + if(!formats.containsKey(key)) { + String msg = String.format("No format string named '%s' found", key); + + throw new NoSuchElementException(msg); + } + + return formats.get(key); + }); + } + + /** + * Retrieve a persisted format string, and apply it to a set of + * arguments. + * + * @param key + * The name of the format string. + * + * @param objects + * The parameters to the format string. + * + * @return The format string with that name. + */ + public static String applyFormat(String key, Object... objects) { + return String.format(getFormat(key), objects); + } +} \ No newline at end of file -- cgit v1.2.3