diff --git a/src/main/java/io/github/kale_ko/ejcl/PathResolver.java b/src/main/java/io/github/kale_ko/ejcl/PathResolver.java index c95aa0e..ba21a62 100644 --- a/src/main/java/io/github/kale_ko/ejcl/PathResolver.java +++ b/src/main/java/io/github/kale_ko/ejcl/PathResolver.java @@ -5,16 +5,20 @@ import io.github.kale_ko.bjsl.elements.ParsedObject; import io.github.kale_ko.bjsl.elements.ParsedPrimitive; import java.util.*; +import java.util.regex.Pattern; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; /** * Contains methods useful for getting/setting nested values * - * @version 1.0.0 + * @version 4.0.0 * @since 1.0.0 */ public class PathResolver { + protected static final Pattern arrayPathFixPattern = Pattern.compile("\\[([0-9]*)]"); + protected static final Pattern pathSplitPattern = Pattern.compile("(? keys = new ArrayList<>(); - int i2 = 0; - for (int i = 1; i <= path.length(); i++) { - if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) { - keys.add(path.substring(i2, i).replace("\\.", ".")); - i2 = i + 1; - } - } + List keys = Arrays.asList(pathSplitPattern.split(path)); ParsedElement resolved = element; @@ -67,7 +49,7 @@ private PathResolver() { } } else if (resolved.isArray()) { if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) { - int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1")); + int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]")); if (index >= 0 && index < resolved.asArray().getSize()) { resolved = resolved.asArray().get(index); @@ -85,17 +67,11 @@ private PathResolver() { } } - if (resolved == null) { - return null; - } else if (resolved.isObject() && returnObjArrValues) { - return "{obj}," + String.join(",", resolved.asObject().getKeys()); - } else if (resolved.isArray() && returnObjArrValues) { - return "{arr}," + resolved.asArray().getSize(); - } else if (resolved.isPrimitive()) { + if (resolved != null && resolved.isPrimitive()) { return resolved.asPrimitive().get(); + } else { + return null; } - - return null; } /** @@ -109,16 +85,9 @@ private PathResolver() { * @since 1.0.0 */ public static @Nullable ParsedElement resolveElement(@NotNull ParsedElement element, @NotNull String path) { - path = path.replaceAll("\\[([0-9]*)]", ".[$1]"); - - List keys = new ArrayList<>(); - int i2 = 0; - for (int i = 1; i <= path.length(); i++) { - if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) { - keys.add(path.substring(i2, i).replace("\\.", ".")); - i2 = i + 1; - } - } + path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]"); + + List keys = Arrays.asList(pathSplitPattern.split(path)); ParsedElement resolved = element; @@ -132,7 +101,7 @@ private PathResolver() { } } else if (resolved.isArray()) { if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) { - int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1")); + int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]")); if (index >= 0 && index < resolved.asArray().getSize()) { resolved = resolved.asArray().get(index); @@ -165,7 +134,7 @@ private PathResolver() { * @since 1.0.0 */ public static @NotNull ParsedElement update(@NotNull ParsedElement element, @NotNull String path, @Nullable Object value) { - return PathResolver.update(element, path, value, true); + return update(element, path, value, true); } /** @@ -181,16 +150,9 @@ private PathResolver() { * @since 1.0.0 */ public static @NotNull ParsedElement update(@NotNull ParsedElement element, @NotNull String path, @Nullable Object value, boolean force) { - path = path.replaceAll("\\[([0-9]*)]", ".[$1]"); - - List keys = new ArrayList<>(); - int i2 = 0; - for (int i = 1; i <= path.length(); i++) { - if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) { - keys.add(path.substring(i2, i).replace("\\.", ".")); - i2 = i + 1; - } - } + path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]"); + + List keys = Arrays.asList(pathSplitPattern.split(path)); ParsedElement resolved = element; @@ -200,10 +162,12 @@ private PathResolver() { resolved = resolved.asObject().get(keys.get(i)); } else { if (force) { - if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) { + if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) { + resolved.asObject().set(keys.get(i), ParsedArray.create()); + } else if (keys.size() > i + 1) { resolved.asObject().set(keys.get(i), ParsedObject.create()); } else { - resolved.asObject().set(keys.get(i), ParsedArray.create()); + resolved.asObject().set(keys.get(i), ParsedPrimitive.fromNull()); } resolved = resolved.asObject().get(keys.get(i)); @@ -214,17 +178,19 @@ private PathResolver() { } } else if (resolved.isArray()) { if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) { - int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1")); + int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]")); if (index >= 0 && index < resolved.asArray().getSize()) { resolved = resolved.asArray().get(index); } else { if (force) { while (resolved.asArray().getSize() <= index) { - if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) { + if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) { + resolved.asArray().add(ParsedArray.create()); + } else if (keys.size() > i + 1) { resolved.asArray().add(ParsedObject.create()); } else { - resolved.asArray().add(ParsedArray.create()); + resolved.asArray().add(ParsedPrimitive.fromNull()); } } @@ -253,31 +219,18 @@ private PathResolver() { resolved.asObject().set(valueKey, ParsedPrimitive.from(value)); } } else { - if (value instanceof String && ((String) value).startsWith("{obj},")) { - resolved.asObject().set(valueKey, ParsedObject.create()); - } else if (value instanceof String && ((String) value).startsWith("{arr},")) { - resolved.asObject().set(valueKey, ParsedArray.create()); - } else { - resolved.asObject().set(valueKey, ParsedPrimitive.from(value)); - } + resolved.asObject().set(valueKey, ParsedPrimitive.from(value)); } } else if (resolved.isArray() && (valueKey.startsWith("[") && valueKey.endsWith("]"))) { - int resolvedValueKey = Integer.parseInt(valueKey.replaceAll("\\[([0-9]*)]", "$1")); + int resolvedValueKey = Integer.parseInt(arrayPathFixPattern.matcher(valueKey).replaceAll(".[$1]")); if (resolvedValueKey >= 0 && resolvedValueKey < resolved.asArray().getSize()) { if (resolved.asArray().get(resolvedValueKey).isPrimitive()) { resolved.asArray().set(resolvedValueKey, ParsedPrimitive.from(value)); } } else { - if (value instanceof String && ((String) value).startsWith("{obj},")) { - resolved.asArray().add(ParsedObject.create()); - } else if (value instanceof String && ((String) value).startsWith("{arr},")) { - resolved.asArray().add(ParsedArray.create()); - } else { - resolved.asArray().add(ParsedPrimitive.from(value)); - } + resolved.asArray().add(ParsedPrimitive.from(value)); } - } } @@ -296,7 +249,7 @@ private PathResolver() { * @since 1.0.0 */ public static @NotNull ParsedElement updateElement(@NotNull ParsedElement element, @NotNull String path, @NotNull ParsedElement value) { - return PathResolver.updateElement(element, path, value, true); + return updateElement(element, path, value, true); } /** @@ -312,16 +265,11 @@ private PathResolver() { * @since 1.0.0 */ public static @NotNull ParsedElement updateElement(@NotNull ParsedElement element, @NotNull String path, @NotNull ParsedElement value, boolean force) { - path = path.replaceAll("\\[([0-9]*)]", ".[$1]"); - - List keys = new ArrayList<>(); - int i2 = 0; - for (int i = 1; i <= path.length(); i++) { - if (i == path.length() || (path.charAt(i - 1) != '\\' && path.charAt(i) == '.')) { - keys.add(path.substring(i2, i).replace("\\.", ".")); - i2 = i + 1; - } - } + path = arrayPathFixPattern.matcher(path).replaceAll(".[$1]"); + + List keys = Arrays.asList(pathSplitPattern.split(path)); + + System.out.println(keys); ParsedElement resolved = element; @@ -331,10 +279,12 @@ private PathResolver() { resolved = resolved.asObject().get(keys.get(i)); } else { if (force) { - if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) { + if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) { + resolved.asObject().set(keys.get(i), ParsedArray.create()); + } else if (keys.size() > i + 1) { resolved.asObject().set(keys.get(i), ParsedObject.create()); } else { - resolved.asObject().set(keys.get(i), ParsedArray.create()); + resolved.asObject().set(keys.get(i), ParsedPrimitive.fromNull()); } resolved = resolved.asObject().get(keys.get(i)); @@ -345,17 +295,19 @@ private PathResolver() { } } else if (resolved.isArray()) { if (keys.get(i).startsWith("[") && keys.get(i).endsWith("]")) { - int index = Integer.parseInt(keys.get(i).replaceAll("\\[([0-9]*)]", "$1")); + int index = Integer.parseInt(arrayPathFixPattern.matcher(keys.get(i)).replaceAll(".[$1]")); if (index >= 0 && index < resolved.asArray().getSize()) { resolved = resolved.asArray().get(index); } else { if (force) { while (resolved.asArray().getSize() <= index) { - if (keys.get(i + 1).equals(keys.get(i + 1).replaceAll("\\[([0-9]*)]", "$1"))) { + if (keys.get(i + 1).startsWith("[") && keys.get(i + 1).endsWith("]")) { + resolved.asArray().add(ParsedArray.create()); + } else if (keys.size() > i + 1) { resolved.asArray().add(ParsedObject.create()); } else { - resolved.asArray().add(ParsedArray.create()); + resolved.asArray().add(ParsedPrimitive.fromNull()); } } @@ -381,7 +333,7 @@ private PathResolver() { if (resolved.isObject()) { resolved.asObject().set(valueKey, value); } else if (resolved.isArray() && (valueKey.startsWith("[") && valueKey.endsWith("]"))) { - int resolvedValueKey = Integer.parseInt(valueKey.replaceAll("\\[([0-9]*)]", "$1")); + int resolvedValueKey = Integer.parseInt(arrayPathFixPattern.matcher(valueKey).replaceAll(".[$1]")); if (resolvedValueKey >= 0 && resolvedValueKey < resolved.asArray().getSize()) { resolved.asArray().set(resolvedValueKey, value); @@ -405,7 +357,7 @@ private PathResolver() { * @since 1.0.0 */ public static @NotNull Set getKeys(@NotNull ParsedElement element) { - return getKeys(element, true); + return getKeys(element, "", false); } /** @@ -441,10 +393,16 @@ private PathResolver() { for (Map.Entry entry : object.getEntries()) { if (entry.getValue().isObject()) { + if (returnObjArrKeys) { + keys.add(path + entry.getKey().replace(".", "\\.")); // FIXME \. -> \\. -> split + } keys.addAll(getKeys(entry.getValue(), path + entry.getKey().replace(".", "\\.") + ".", returnObjArrKeys)); } else if (entry.getValue().isArray()) { + if (returnObjArrKeys) { + keys.add(path + entry.getKey().replace(".", "\\.")); + } keys.addAll(getKeys(entry.getValue(), path + entry.getKey().replace(".", "\\."), returnObjArrKeys)); - } else if (returnObjArrKeys || entry.getValue().isPrimitive()) { + } else if (entry.getValue().isPrimitive()) { keys.add(path + entry.getKey().replace(".", "\\.")); } } @@ -453,10 +411,16 @@ private PathResolver() { for (int i = 0; i < array.getSize(); i++) { if (array.get(i).isObject()) { + if (returnObjArrKeys) { + keys.add(path + "[" + i + "]"); + } keys.addAll(getKeys(array.get(i), path + "[" + i + "]" + ".", returnObjArrKeys)); } else if (array.get(i).isArray()) { + if (returnObjArrKeys) { + keys.add(path + "[" + i + "]"); + } keys.addAll(getKeys(array.get(i), path + "[" + i + "]", returnObjArrKeys)); - } else if (returnObjArrKeys || array.get(i).isPrimitive()) { + } else if (array.get(i).isPrimitive()) { keys.add(path + "[" + i + "]"); } }