Fork me on GitHub

Tutorial 13

E13 Creating subprojections with element templates

This tutorial demonstrates the creation of elements via template elements. This is achieved by reading template documents and using this projections as sub projections in other template projections. The transition from document projections to element sub projections works just as desired.

Projections

public interface Node {
    @XBRead("/node")
    Node rootElement();
     
    @XBWrite("./@id")
    Node setID(String id);
 
    @XBWrite("./data/y:ShapeNode/y:Geometry/@height")
    Node setHeight(float h);
 
    @XBWrite("./data/y:ShapeNode/y:Geometry/@width")
    Node setWidth(float w);
 
    @XBWrite("./data/y:ShapeNode/y:Geometry/@x")
    Node setX(float x);
 
    @XBWrite("./data/y:ShapeNode/y:Geometry/@y")
    Node setY(float y);
 
    @XBWrite("./data/y:ShapeNode/y:NodeLabel")
    void setLabel(String string);
 
    @XBRead("./data/y:ShapeNode/y:NodeLabel")
    String getLabel();
     
    @XBRead("./@id")
    String getID();
     
    @XBRead("{0}")
    String xpath(String path);
}
//END SNIPPET: Edge
public interface Edge {
    @XBRead("/edge")
    Edge rootElement();
     
    @XBWrite("/g:edge/@id")
    Edge setID(String id);
 
    @XBWrite("/g:edge/@source")
    Edge setSource(String id);
 
    @XBWrite("/g:edge/@target")
    Edge setTarget(String id);
 
    @XBRead("/g:edge/@id")
    String getID();
 
}
public interface GraphML {
 
    @XBWrite("/graphml/graph/node[@id='{0}']")
    GraphML addNode(String id, @XBValue Node node);
 
    @XBWrite("/graphml/graph/edge[@id='{0}']")
    GraphML addEdge(String id, @XBValue Edge edge);
 
    @XBRead("//edge[@target='{0}']/@source")
    String getParentOf(String node);
 
    @XBRead(value="//edge[@source='{0}']/@target")
    List<String> getChildrenOf(String node);
 
    @XBRead("//node[@id='{0}']")
    Node getNode(String id);
     
    @XBRead(value="//node")
    List<Node> getAllNodes();
     
    @XBRead("{0}")
    String xpath(String path);
     
}

Example Code

public class TestGraphMLCreation {
 
    @Test
    public void testGraphCreation() throws IOException {
        Node node = new XBProjector(Flags.TO_STRING_RENDERS_XML).io().fromURLAnnotation(Node.class).rootElement();
        node.setLabel("NodeLabel");
        assertEquals("NodeLabel", node.getLabel());
        node.setID("Nodeid");
        assertEquals("Nodeid", node.getID());
    }
 
    @Test
    public void testGraphNodeSetting() throws IOException {
        GraphML graph = new XBProjector().io().fromURLAnnotation(GraphML.class);
        Edge edge = new XBProjector().io().fromURLAnnotation(Edge.class).rootElement();
        Node node = new XBProjector().io().fromURLAnnotation(Node.class).rootElement();
        graph.addEdge("wutz"   , edge);
        graph.addNode("huhu", node);       
    }
 
}