Skip to content

Commit ad93cc5

Browse files
committed
finalize tree navigator
1 parent 1649130 commit ad93cc5

File tree

5 files changed

+25
-34
lines changed

5 files changed

+25
-34
lines changed

json-smart/src/main/java/net/minidev/json/actions/JSONBuilder.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

json-smart/src/main/java/net/minidev/json/actions/navigate/CopyPathsAction.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,10 @@ public boolean start(JSONObject source, Collection<String> pathsToCopy)
4242
destTree = null;
4343
return false;
4444
}
45-
4645
destTree = new JSONObject();
4746
if (pathsToCopy == null || pathsToCopy.size() == 0) {
4847
return false;
4948
}
50-
51-
/**
52-
* using ARRAY_PUSH which adds any new entry encountered in arrays, effectively
53-
* allowing duplicate objects in the array, which is supported by Gigya
54-
*/
5549
return true;
5650
}
5751

@@ -86,10 +80,15 @@ public boolean recurInto(TreePath jp, JSONArray o)
8680
}
8781

8882
@Override
89-
public void handlePrematureNavigatedBranchEnd(TreePath jp, Object source) {
83+
public void foundLeafBeforePathEnd(TreePath jp, Object obj) {
9084
throw new IllegalArgumentException("branch is shorter than path - path not found in source: '" + jp.origin() + "'");
9185
}
9286

87+
@Override
88+
public void pathTailNotFound(TreePath jp, Object source) {
89+
throw new IllegalArgumentException("cannot find next element of path - path not found in source: '" + jp.origin() + "'");
90+
}
91+
9392
@Override
9493
public void handleLeaf(TreePath jp, Object o) {
9594
((JSONObject) destNodeStack.peek()).put(jp.curr(), o);

json-smart/src/main/java/net/minidev/json/actions/navigate/NavigateAction.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ public interface NavigateAction<M extends Map<String, Object>, L extends List<Ob
2929
boolean start(M objectToNavigate, Collection<String> pathsToNavigate);
3030

3131
/**
32-
* reached end of branch in source before end of specified json path -
33-
* the specified path does not exist in the source.
32+
* reached end of branch in source before end of specified path -
33+
* the specified path does not exist in the source
3434
*/
35-
void handlePrematureNavigatedBranchEnd(TreePath tp, Object source);
35+
void pathTailNotFound(TreePath tp, Object source);
3636

3737
/**
3838
* called after the navigation of a path ends
@@ -63,6 +63,12 @@ public interface NavigateAction<M extends Map<String, Object>, L extends List<Ob
6363
*/
6464
boolean recurInto(TreePath tp, L sourceNode);
6565

66+
/**
67+
* reached leaf node (not a container) in source but specified path expects children -
68+
* the specified path does not exist in the source
69+
*/
70+
void foundLeafBeforePathEnd(TreePath jp, Object obj);
71+
6672
/**
6773
* called when a leaf node is reached in a M.
6874
* a leaf in a M is a key-value pair where the value is not a container itself
@@ -101,4 +107,5 @@ public interface NavigateAction<M extends Map<String, Object>, L extends List<Ob
101107
* holds the result of the navigation, as assigned by the action implementing this interface
102108
*/
103109
Object result();
110+
104111
}

json-smart/src/main/java/net/minidev/json/actions/navigate/TreeNavigator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ else if (action.failFast(path, e)) {
8282
action.end();
8383
}
8484

85-
private void nav(TreePath jp, M map)
85+
public void nav(TreePath jp, M map)
8686
{
8787
if (map == null || !action.recurInto(jp, map))
8888
{
@@ -95,9 +95,9 @@ private void nav(TreePath jp, M map)
9595
String key = jp.next();
9696
if (!map.containsKey(key))
9797
{
98-
// reached end of branch in source before end of specified json path -
99-
// the specified path is illegal because it does not exist in the source.
100-
action.handlePrematureNavigatedBranchEnd(jp, map);
98+
// cannot find next element of path in the source -
99+
// the specified path does not exist in the source
100+
action.pathTailNotFound(jp, map);
101101
}
102102
else if (map.get(key) instanceof Map)
103103
{
@@ -113,7 +113,7 @@ else if (jp.hasNext())
113113
{
114114
// reached leaf node (not a container) in source but specified path expects children -
115115
// the specified path is illegal because it does not exist in the source.
116-
action.handlePrematureNavigatedBranchEnd(jp, map.get(key));
116+
action.foundLeafBeforePathEnd(jp, map.get(key));
117117
}
118118
else if (!jp.hasNext())
119119
{
@@ -128,7 +128,7 @@ else if (!jp.hasNext())
128128
action.recurEnd(jp, (M) map);
129129
}
130130

131-
private void nav(TreePath jp, L list)
131+
public void nav(TreePath jp, L list)
132132
{
133133
if (list == null || !action.recurInto(jp, (L) list))
134134
{

json-smart/src/test/java/net/minidev/json/actions/traverse/KeysPrintActionTest.java renamed to json-smart/src/test/java/net/minidev/json/test/actions/KeysPrintActionTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
package net.minidev.json.actions.traverse;
1+
package net.minidev.json.test.actions;
22

33
import net.minidev.json.JSONObject;
44
import net.minidev.json.JSONValue;
5+
import net.minidev.json.actions.traverse.JSONTraverser;
6+
import net.minidev.json.actions.traverse.KeysPrintAction;
57
import net.minidev.json.parser.ParseException;
68
import org.junit.Test;
79

0 commit comments

Comments
 (0)