From b01a6095f8474133a296e9580d6fc84baaabac94 Mon Sep 17 00:00:00 2001 From: Norberto Lopez <106690103+nobes888@users.noreply.github.com> Date: Wed, 30 Jul 2025 18:06:33 -0400 Subject: [PATCH] Changes for 2.1.0: [1] Updates to improve the ParseUtil functionality. [2] Added check for null font argument. --- src/glum/app/AppInfo.java | 2 +- src/glum/gui/GuiUtil.java | 3 +- src/glum/io/ParseUtil.java | 69 +++++++++++++++++++++++++++++++-- src/glum/io/TokenParseAttr.java | 26 +++++++++++++ tools/buildRelease | 2 +- 5 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 src/glum/io/TokenParseAttr.java diff --git a/src/glum/app/AppInfo.java b/src/glum/app/AppInfo.java index 3c68852..9124aa3 100644 --- a/src/glum/app/AppInfo.java +++ b/src/glum/app/AppInfo.java @@ -26,7 +26,7 @@ public class AppInfo * Note we do not make the Version directly visible and final so other classes will not utilized a cached version * when built via Ant. */ - private static String Version = "2.0.0"; + private static String Version = "2.1.0"; /** * Returns the version of the application diff --git a/src/glum/gui/GuiUtil.java b/src/glum/gui/GuiUtil.java index 58943fd..0f69a49 100644 --- a/src/glum/gui/GuiUtil.java +++ b/src/glum/gui/GuiUtil.java @@ -76,7 +76,8 @@ public class GuiUtil { var tmpB = new JButton(aTitle); tmpB.addActionListener(aActionListener); - tmpB.setFont(aFont); + if (aFont != null) + tmpB.setFont(aFont); return tmpB; } diff --git a/src/glum/io/ParseUtil.java b/src/glum/io/ParseUtil.java index cd6b710..d904ebd 100644 --- a/src/glum/io/ParseUtil.java +++ b/src/glum/io/ParseUtil.java @@ -13,6 +13,9 @@ // limitations under the License. package glum.io; +import java.util.ArrayList; +import java.util.List; + /** * Collection of utility methods for parsing values from text input. * @@ -20,6 +23,15 @@ package glum.io; */ public class ParseUtil { + /** + * Utility method to strip the white space from an array of tokens. + */ + public static void cleanTokens(String[] aTokenArr) + { + for (int c1 = 0; c1 < aTokenArr.length; c1++) + aTokenArr[c1] = aTokenArr[c1].strip(); + } + /** * Reads a boolean from a string with out throwing a exception. */ @@ -137,12 +149,61 @@ public class ParseUtil } /** - * Utility method to strip the white space from an array of tokens. + * Utility method that strips out the 1st and last double quotes from a string. + *

+ * If there are no double quotes at the 1st and last position then the original string is returned. */ - public static void cleanTokens(String[] aTokenArr) + public static String stripQuotes(String aStr) { - for (int c1 = 0; c1 < aTokenArr.length; c1++) - aTokenArr[c1] = aTokenArr[c1].strip(); + if (aStr.startsWith("\"") == true && aStr.endsWith("\"") == true) + return aStr.substring(1, aStr.length() - 1); + + return aStr; + } + + /** + * Method to parse the specified input string and break it up based on commas. Note quoted string with the tokenizing + * char (aSplitChar) will be preserved. + *

+ * The basis of this method is from: + * https://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes + * + * @param aInputStr + * The input string to tokenize. + * @param aSplitChar + * The character to split the input string by. + * @param aParseTokenAttr + * Provides the behavior specific to tokenization. + * @return + */ + public static List tokenizeByChar(String aInputStr, char aSplitChar, TokenParseAttr aTokenParseAttr) + { + var begIdx = 0; + var inQuotes = false; + var resultL = new ArrayList(); + for (int currIdx = 0; currIdx < aInputStr.length(); currIdx++) + { + var evalChar = aInputStr.charAt(currIdx); + if (evalChar == '"') + inQuotes = !inQuotes; + else if (evalChar == aSplitChar && inQuotes == false) + { + var tokenStr = aTokenParseAttr.apply(aInputStr.substring(begIdx, currIdx)); + resultL.add(tokenStr); + begIdx = currIdx + 1; + } + } + + // Ensure the last char is not a comma. We do not want a trailing comma in the last string + var endIdx = aInputStr.length(); + if (endIdx > begIdx && aInputStr.charAt(endIdx - 1) == aSplitChar) + endIdx--; + + // Add the last string + var tokenStr = aTokenParseAttr.apply(aInputStr.substring(begIdx)); + if (aTokenParseAttr.keepEmptyTailToken() == true || tokenStr.length() > 0) + resultL.add(tokenStr); + return resultL; } } diff --git a/src/glum/io/TokenParseAttr.java b/src/glum/io/TokenParseAttr.java new file mode 100644 index 0000000..c58470b --- /dev/null +++ b/src/glum/io/TokenParseAttr.java @@ -0,0 +1,26 @@ +package glum.io; + +/** + * Record which defines configuration attributes to control parsing behavior. + * + * @author lopeznr1 + */ +public record TokenParseAttr(boolean stripTokenWhiteSpace, boolean keepEmptyTailToken) +{ + /** The default {@link TokenParseAttr}. */ + public static final TokenParseAttr Default = new TokenParseAttr(false, true); + + /** + * Given the specified input string returns the string with the relevant transformations as specified by this + * {@link TokenParseAttr}. + */ + public String apply(String aInputStr) + { + var retStr = aInputStr; + if (stripTokenWhiteSpace == true) + retStr = retStr.strip(); + + return retStr; + } + +} diff --git a/tools/buildRelease b/tools/buildRelease index 5cb03d2..e7d357b 100755 --- a/tools/buildRelease +++ b/tools/buildRelease @@ -15,7 +15,7 @@ import traceback # Define the (baseline) version -baseVersion = "2.0.0" +baseVersion = "2.1.0" # Define relevant base names appBaseName = 'Glum'