|
1 | 1 | package io.teaql.core.checker; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
3 | 6 | public class ObjectLocation { |
4 | 7 | private ObjectLocation parent; |
5 | 8 |
|
@@ -45,4 +48,56 @@ public boolean isSecondLevel() { |
45 | 48 | public boolean isThirdLevel() { |
46 | 49 | return getLevel() == 3; |
47 | 50 | } |
| 51 | + |
| 52 | + /** Canonical casing-neutral path using KSML property names. */ |
| 53 | + public String modelPath() { |
| 54 | + return render(false, false); |
| 55 | + } |
| 56 | + |
| 57 | + /** Java diagnostic path using lower-camel property names. */ |
| 58 | + public String nativePath() { |
| 59 | + return render(true, false); |
| 60 | + } |
| 61 | + |
| 62 | + /** RFC 6901 JSON pointer using TeaQL's default lower-camel wire policy. */ |
| 63 | + public String instancePath() { |
| 64 | + return render(true, true); |
| 65 | + } |
| 66 | + |
| 67 | + private String render(boolean lowerCamel, boolean pointer) { |
| 68 | + List<ObjectLocation> locations = new ArrayList<>(); |
| 69 | + for (ObjectLocation current = this; current != null; current = current.getParent()) { |
| 70 | + locations.add(0, current); |
| 71 | + } |
| 72 | + StringBuilder result = new StringBuilder(); |
| 73 | + for (ObjectLocation location : locations) { |
| 74 | + if (location instanceof HashLocation hash) { |
| 75 | + String member = lowerCamel ? lowerCamel(hash.getMember()) : hash.getMember(); |
| 76 | + if (pointer) { |
| 77 | + result.append('/').append(escapePointer(member)); |
| 78 | + } else { |
| 79 | + if (!result.isEmpty()) result.append('.'); |
| 80 | + result.append(member); |
| 81 | + } |
| 82 | + } else if (location instanceof ArrayLocation array) { |
| 83 | + if (pointer) result.append('/').append(array.getIndex()); |
| 84 | + else result.append('[').append(array.getIndex()).append(']'); |
| 85 | + } |
| 86 | + } |
| 87 | + return result.toString(); |
| 88 | + } |
| 89 | + |
| 90 | + private static String lowerCamel(String name) { |
| 91 | + String[] parts = name.split("_", -1); |
| 92 | + StringBuilder result = new StringBuilder(parts.length == 0 ? "" : parts[0]); |
| 93 | + for (int i = 1; i < parts.length; i++) { |
| 94 | + if (parts[i].isEmpty()) continue; |
| 95 | + result.append(Character.toUpperCase(parts[i].charAt(0))).append(parts[i].substring(1)); |
| 96 | + } |
| 97 | + return result.toString(); |
| 98 | + } |
| 99 | + |
| 100 | + private static String escapePointer(String value) { |
| 101 | + return value.replace("~", "~0").replace("/", "~1"); |
| 102 | + } |
48 | 103 | } |
0 commit comments