From 088456c524a6dab713f43d3d3b9e3e76765a23b1 Mon Sep 17 00:00:00 2001 From: "eric.jarosch" Date: Mon, 21 Sep 2026 02:13:59 +0200 Subject: [PATCH] Fix #56 --- .../org/codejive/properties/Properties.java | 29 +++++++++++++++---- .../codejive/properties/PropertiesParser.java | 12 ++++---- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/codejive/properties/Properties.java b/src/main/java/org/codejive/properties/Properties.java index 5b2e9e5..5692096 100644 --- a/src/main/java/org/codejive/properties/Properties.java +++ b/src/main/java/org/codejive/properties/Properties.java @@ -617,14 +617,23 @@ private List findPropertyCommentLines(Cursor pos) { return Collections.unmodifiableList(result); } - private Cursor indexOf(String key) { + /** + * @param key the key of the property whose token is searched. + * @return a Cursor pointing to the {@link PropertiesParser.Type#KEY} token of the property. + *
Or a Cursor pointing to {@code -1} if no such property exists. + */ + public Cursor indexOf(String key) { return index( tokens.indexOf( new PropertiesParser.Token( PropertiesParser.Type.KEY, escapeKey(key), key))); } - private static String escapeValue(String value) { + /** + * @param value the value to escape + * @return the value, escaped such that it can be stored in a .properties file + */ + public static String escapeValue(String value) { return value.replace("\\", "\\\\") .replace("\n", "\\n") .replace("\r", "\\r") @@ -632,7 +641,11 @@ private static String escapeValue(String value) { .replace("\f", "\\f"); } - private static String escapeKey(String key) { + /** + * @param key the key to escape + * @return the key, escaped such that it can be stored in a .properties file + */ + public static String escapeKey(String key) { return escapeValue(key).replace(" ", "\\ "); } @@ -997,11 +1010,17 @@ Cursor index(int index) { return Cursor.index(tokens, index); } - Cursor first() { + /** + * @return a Cursor pointing to the first token, or to {@code -1} if no tokens have been loaded. + */ + public Cursor first() { return Cursor.first(tokens); } - Cursor last() { + /** + * @return a Cursor pointing to the last token, or to {@code -1} if no tokens have been loaded. + */ + public Cursor last() { return Cursor.last(tokens); } diff --git a/src/main/java/org/codejive/properties/PropertiesParser.java b/src/main/java/org/codejive/properties/PropertiesParser.java index aada459..c43ec85 100644 --- a/src/main/java/org/codejive/properties/PropertiesParser.java +++ b/src/main/java/org/codejive/properties/PropertiesParser.java @@ -17,7 +17,7 @@ * input which makes it possible to exactly recreate the original, including all whitespace and * comments. */ -class PropertiesParser { +public class PropertiesParser { /** The type of token. */ public enum Type { @@ -59,7 +59,7 @@ public static class Token { * @param type The token's type * @param raw The token's value */ - Token(Type type, String raw) { + public Token(Type type, String raw) { this(type, raw, null); } @@ -71,7 +71,7 @@ public static class Token { * @param raw The token's raw value (including escape sequences) * @param text The token's processed value (no escape sequences) */ - Token(Type type, String raw, String text) { + public Token(Type type, String raw, String text) { this.type = type; this.raw = raw; if (raw.equals(text)) { @@ -310,7 +310,7 @@ private String string() { * @param escape Input string * @return Decoded string */ - static String unescape(String escape) { + public static String unescape(String escape) { StringBuilder txt = new StringBuilder(); for (int i = 0; i < escape.length(); i++) { char ch = escape.charAt(i); @@ -358,7 +358,7 @@ && isWhitespaceChar(ch = escape.charAt(i + 1))) { return txt.toString(); } - private static boolean isSeparatorChar(int ch) { + public static boolean isSeparatorChar(int ch) { return ch == '=' || ch == ':'; } @@ -370,7 +370,7 @@ private static boolean isWhitespaceEolChar(int ch) { return isWhitespaceChar(ch) || isEol(ch); } - private static boolean isCommentChar(int ch) { + public static boolean isCommentChar(int ch) { return ch == '#' || ch == '!'; }