Author: blafond
Date: 2009-12-03 17:09:38 -0500 (Thu, 03 Dec 2009)
New Revision: 1391
Modified:
trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java
trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphTest.java
Log:
DNA-569 enhanced the subgraph's toString() method to show recursive nodes and
properties.
Modified: trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java
===================================================================
--- trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java 2009-12-03 22:08:04 UTC
(rev 1390)
+++ trunk/dna-graph/src/main/java/org/jboss/dna/graph/Graph.java 2009-12-03 22:09:38 UTC
(rev 1391)
@@ -63,6 +63,7 @@
import org.jboss.dna.graph.property.Property;
import org.jboss.dna.graph.property.PropertyFactory;
import org.jboss.dna.graph.property.Reference;
+import org.jboss.dna.graph.property.ValueFactory;
import org.jboss.dna.graph.property.ValueFormatException;
import org.jboss.dna.graph.property.Path.Segment;
import org.jboss.dna.graph.query.QueryContext;
@@ -6969,8 +6970,42 @@
@Override
public String toString() {
- return "Subgraph " + getLocation().toString();
+ return "Subgraph\n" + getToString(context);
//ExecutionContext.DEFAULT_CONTEXT);//getLocation().toString();
}
+
+ /**
+ * Get the string representation of this subgraph tree.
+ *
+ * @param context the execution context in which the conversion is to take place
+ * @return the string representation; never null
+ */
+ public String getToString( ExecutionContext context ) {
+ StringBuilder sb = new StringBuilder();
+ getRecursiveString(context, getRoot(), sb, 0);
+ return sb.toString();
+ }
+
+ private void getRecursiveString( ExecutionContext context,
+ SubgraphNode node,
+ StringBuilder str,
+ int indentLevel ) {
+ for (int i = 0; i < indentLevel; ++i) {
+ str.append(" ");
+ }
+ str.append(node.toString());
+
+ // Recursively add children at one greater tab level
+ for (Location nextLoc : node.getChildren()) {
+ SubgraphNode childNode = getNode(nextLoc);
+ // child node location may exist, but the subgraph may not have
+ // been constructed deep enough to instantiate the subnode, so
+ // check for null
+ if( childNode != null ) {
+ getRecursiveString(context, childNode, str, indentLevel + 1);
+ }
+ }
+ }
+
}
protected static final List<Location> NO_CHILDREN = Collections.emptyList();
@@ -7064,8 +7099,71 @@
@Override
public String toString() {
- return "Node " + getLocation().toString();
+ return getNodeString(context, location);
}
+
+ private String getNodeString( ExecutionContext context,
+ Location location) {
+ StringBuilder sb = new StringBuilder();
+ sb.append('<'); // Bracket the node
+ ValueFactory<String> strings =
context.getValueFactories().getStringFactory();
+
+ String name = "";
+ if( location.getPath().getLastSegment() != null ) {
+ name = strings.create(location.getPath().getLastSegment());
+ } else {
+ name = strings.create(location.getPath());
+ }
+
+ if( name.startsWith("{")) {
+ // Remove {xxxx} namespace prefix
+ int end = name.indexOf('}');
+ name = name.substring(end+1, name.length());
+ }
+
+ // Surround name in double quotes
+ sb.append("name =
").append('\"').append(name).append('\"').append("
");
+ boolean first = true;
+ if (getProperties() != null) {
+ for ( Property entry : getProperties()) {
+
+ if( first ) {
+ first = false;
+ } else sb.append(" ");
+ sb.append(getPropertyString(entry));
+ }
+ }
+ sb.append(">\n");
+
+ return sb.toString();
+ }
+
+ private String getPropertyString(Property property) {
+ // Surround property value in double quotes so final property looks like:
+ // color = "blue" (single valued property)
+ // colors = ["blue", "red", "green"]
(multi-valued property)
+
+ StringBuilder sb = new StringBuilder();
+ sb.append(property.getName().getLocalName());
+ sb.append(" = ");
+ if (property.isEmpty()) {
+ sb.append("null");
+ } else if( property.isSingle() ) {
+ String valueStr =
getContext().getValueFactories().getStringFactory().create(property.getValues().next());
+ sb.append('\"').append(valueStr).append('\"');
+ } else {
+ sb.append('[');
+ boolean first = true;
+ for (Object value : property.getValuesAsArray()) {
+ if (first) first = false;
+ else sb.append(",");
+ String valueStr =
getContext().getValueFactories().getStringFactory().create(value);
+
sb.append('\"').append(valueStr).append('\"');
+ }
+ if (property.isMultiple()) sb.append(']');
+ }
+ return sb.toString();
+ }
}
//
----------------------------------------------------------------------------------------------------------------
Modified: trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphTest.java
===================================================================
--- trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphTest.java 2009-12-03 22:08:04
UTC (rev 1390)
+++ trunk/dna-graph/src/test/java/org/jboss/dna/graph/GraphTest.java 2009-12-03 22:09:38
UTC (rev 1391)
@@ -877,7 +877,47 @@
assertThat(node3.getChildren().isEmpty(), is(true));
assertThat(node3.getProperties().isEmpty(), is(true));
}
+
+ @Test
+ public void shouldConstructValidSubgraphToString() {
+ Location child1 = Location.create(createPath(validPath, "x"));
+ Location child2 = Location.create(createPath(validPath, "y"));
+ Location child3 = Location.create(createPath(validPath, "z"));
+ setChildrenToReadOn(Location.create(validPath), child1, child2, child3);
+ Location child11 = Location.create(createPath(child1.getPath(), "h"));
+ Location child12 = Location.create(createPath(child1.getPath(), "i"));
+ Location child13 = Location.create(createPath(child1.getPath(), "j"));
+ setChildrenToReadOn(child1, child11, child12, child13);
+ Location child121 = Location.create(createPath(child12.getPath(),
"m"));
+ Location child122 = Location.create(createPath(child12.getPath(),
"n"));
+ Location child123 = Location.create(createPath(child12.getPath(),
"o"));
+ setChildrenToReadOn(child12, child121, child122, child123);
+ setPropertiesToReadOn(Location.create(validPath), validIdProperty1,
validIdProperty2);
+ setPropertiesToReadOn(child1, validIdProperty1);
+ setPropertiesToReadOn(child2, validIdProperty2);
+ setPropertiesToReadOn(child11, validIdProperty1);
+ setPropertiesToReadOn(child12, validIdProperty2);
+ setPropertiesToReadOn(child121, validIdProperty1);
+ setPropertiesToReadOn(child122, validIdProperty2);
+
+ Subgraph subgraph = graph.getSubgraphOfDepth(2).at(validPath);
+ assertThat(subgraph, is(notNullValue()));
+ assertThat(subgraph.getMaximumDepth(), is(2));
+
+ String expectedToStringValue =
+ "Subgraph\n"
+ + "<name = \"c\" id2 = \"2\" id1 =
\"1\">\n"
+ + " <name = \"x\" id1 = \"1\">\n"
+ + " <name = \"y\" id2 = \"2\">\n"
+ + " <name = \"z\" >\n";
+
+ // Get nodes by relative path ...
+ Node root = subgraph.getNode("./");
+ assertThat(root.getChildren(), hasItems(child1, child2, child3));
+ assertThat(subgraph.toString(), is(expectedToStringValue));
+ }
+
//
----------------------------------------------------------------------------------------------------------------
// Batched requests
//
----------------------------------------------------------------------------------------------------------------