If you want to navigate with a description (language) on a object graph, than you can use the SOJO navigation language. SOJO has three kinds of navigations:
Examples for get properties:
// simple example
Node node = new Node("Node");
// result = Node
Object result = PathExecuter.getSimpleProperty(node, "name");
// more complex example (iterateale)
Node node = new Node("Node");
Node childNode1 = new Node("Child-Node-1");
Node childNode2 = new Node("Child-Node-2");
node.getChilds().add(childNode1);
node.getChilds().add(childNode2);
// result = Child-Node-1
Object result = PathExecuter.getNestedProperty(node, "childs[0].name");
Examples for set properties:
// simple example
Node node = new Node("Node");
// name of node is: New-Node
PathExecuter.setSimpleProperty(node, "name", "New-Node");
// more complex example (map)
Node node = new Node("Test-Node");
node.getNamedChilds().put("N1", new Node("N1"));
node.getNamedChilds().put("N2", new Node("N2"));
PathExecuter.setNestedProperty(node, "namedChilds(N3)", new Node("N3"));
// add element to a list, after the last element from the list (no index is set)
node.getChilds().add(new Node("N1-List"));
// is equals to call the PathExecuter
PathExecuter.setNestedProperty(node, "childs[]", new Node("N1-List"));
Combinate get and set properties (this is the basis, for interfaces, where read from source object (and optional convert the value) and write to the target value):
Node node = new Node("Test-Node");
node.getNamedChilds().put("N1", new Node("N1"));
node.getNamedChilds().put("N2", new Node("N2"));
node.getNamedChilds().put("N3", new Node("N3"));
Object node3 = PathExecuter.getNestedProperty(node, "namedChilds(N3)");
PathExecuter.setNestedProperty(node3, "name", "New-N3");
You can observe the "walk" about a object graph. A application for the case is, if you want to record all pathes, where use from the object graph:
Node node = new Node("Test-Node");
ObjectGraphWalker walker = new ObjectGraphWalker();
PathRecordInterceptor interceptor = new PathRecordInterceptor();
walker.addInterceptor(interceptor);
walker.walk(node);
Map pathes = interceptor.getAllRecordedPathes();
Iterator iter = pathes.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Entry) iter.next();
System.out.println(entry.getKey() + ": " + entry.getValue());
}
// the output is (all getter-method, with values):
childs[]: []
namedChilds(): {}
class: test.net.sf.sojo.model.Node
name: Test-Node
parent: null