diff --git a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/wrapper/XmlReader.java b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/wrapper/XmlReader.java index 52ece38..59bb820 100644 --- a/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/wrapper/XmlReader.java +++ b/commons-serialize-xml/src/main/java/io/gitlab/jfronny/commons/serialize/xml/wrapper/XmlReader.java @@ -369,14 +369,41 @@ public class XmlReader extends SerializeReader implement return result; } + private String getPath(boolean usePreviousPath) { + if (nextTagNamePath != null) return nextTagNamePath; + StringBuilder result = new StringBuilder().append('$'); + for (int i = 0; i < stackSize; i++) { + int scope = stack[i]; + switch (scope) { + case WrapperScope.ARRAY -> { + int pathIndex = pathIndices[i]; + // If index is last path element it points to next array element; have to decrement + if (usePreviousPath && pathIndex > 0 && i == stackSize - 1) { + pathIndex--; + } + result.append('[').append(pathIndex).append(']'); + } + case WrapperScope.OBJECT -> { + result.append('.'); + if (pathNames[i] != null) { + result.append(pathNames[i]); + } + } + case WrapperScope.OBJECT_VALUE_WRAPPER, WrapperScope.OBJECT_VALUE_WRAPPER_USED, WrapperScope.DOCUMENT -> {} + default -> throw new AssertionError("Unknown scope value: " + scope); + } + } + return result.toString(); + } + @Override public String getPath() { - return nextTagName == null ? reader.getPath() : nextTagNamePath; + return getPath(false); } @Override public String getPreviousPath() { - return getPath(); // TODO this should be different when handling arrays + return getPath(true); } @Override