Search in sources :

Example 1 with DagElement

use of com.randomnoun.build.javaToGraphviz.dom.DagElement in project java-to-graphviz by randomnoun.

the class DagStyleApplier method setDagSubgraphLiterals.

// copy any literal elements into their containing subgraphs
private void setDagSubgraphLiterals(Document document, CSSStyleSheet stylesheet) {
    Stack<DagSubgraph> subgraphStack = new Stack<>();
    final CSSOMParser inlineParser = new CSSOMParser();
    inlineParser.setErrorHandler(new ExceptionErrorHandler());
    NodeTraversor.traverse(new NodeVisitor() {

        @Override
        public void head(Node node, int depth) {
            if (node instanceof DagElement) {
                DagElement dagElement = (DagElement) node;
                String tagName = ((Element) node).tagName();
                DagSubgraph dagSubgraph = dagElement.dagSubgraph;
                DagNode dagNode = dagElement.dagNode;
                if (dagSubgraph != null) {
                    subgraphStack.push(dagSubgraph);
                }
                if ("literal".equals(tagName)) {
                    if (subgraphStack.size() == 0) {
                        // could add to root subgraph instead
                        throw new IllegalStateException("literal outside of subgraph");
                    }
                    subgraphStack.peek().literals.add(dagNode.label);
                }
            }
        }

        @Override
        public void tail(Node node, int depth) {
            if (node instanceof DagElement) {
                DagElement dagElement = (DagElement) node;
                if (dagElement.dagSubgraph != null) {
                    subgraphStack.pop();
                }
            }
        }
    }, document.body());
}
Also used : CSSOMParser(com.steadystate.css.parser.CSSOMParser) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) Node(org.jsoup.nodes.Node) DagSubgraph(com.randomnoun.build.javaToGraphviz.dag.DagSubgraph) Stack(java.util.Stack) NodeVisitor(org.jsoup.select.NodeVisitor) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) DagElement(com.randomnoun.build.javaToGraphviz.dom.DagElement) ExceptionErrorHandler(com.randomnoun.build.javaToGraphviz.dom.StylesheetApplier.ExceptionErrorHandler)

Example 2 with DagElement

use of com.randomnoun.build.javaToGraphviz.dom.DagElement in project java-to-graphviz by randomnoun.

the class DagStyleApplier method getDagElements.

// recursively within this subgraph only
public List<DagElement> getDagElements(Map<DagNode, DagElement> dagNodesToElements, DagSubgraph sg, DagNode node) {
    List<DagElement> result = new ArrayList<>();
    // clear applied styles
    node.gvStyles = new HashMap<>();
    boolean isLiteral = node.classes.contains("gv-literal");
    DagElement nodeElement = new DagElement(isLiteral ? "literal" : "node", node, node.gvAttributes);
    dagNodesToElements.put(node, nodeElement);
    for (int i = 0; i < node.children.size(); i++) {
        DagNode childNode = node.children.get(i);
        if (dagNodesToElements.containsKey(childNode)) {
            // should never happen, but skip it
            logger.warn("repeated node in Dag");
        } else if (!sg.nodes.contains(childNode)) {
        // wrong subgraph, skip it
        } else {
            nodeElement.appendChildren(getDagElements(dagNodesToElements, sg, childNode));
        }
    }
    result.add(nodeElement);
    String inNodeIds = null;
    String outNodeIds = null;
    Set<String> inNodeClasses = new HashSet<>();
    Set<String> outNodeClasses = new HashSet<>();
    for (int j = 0; j < dag.edges.size(); j++) {
        /* TODO: outrageously inefficient */
        DagEdge edge = dag.edges.get(j);
        // if (sg.nodes.contains(edge.n1)) {
        if (edge.n1 == node) {
            // clear applied styles
            edge.gvStyles = new HashMap<>();
            DagElement edgeElement = new DagElement(edge, edge.gvAttributes);
            // child.attr("id", edge.name);
            if (includeIncomingIdAttributes) {
                edgeElement.attr("inNodeId", edge.n1.name);
            }
            if (includeOutgoingIdAttributes) {
                edgeElement.attr("outNodeId", edge.n2.name);
            }
            if (includeIncomingClassAttributes) {
                edgeElement.attr("inNodeClass", Text.join(edge.n1.classes, " "));
            }
            if (includeOutgoingClassAttributes) {
                edgeElement.attr("outNodeClass", Text.join(edge.n2.classes, " "));
            }
            outNodeIds = outNodeIds == null ? edge.n2.name : outNodeIds + " " + edge.n2.name;
            outNodeClasses.addAll(edge.n2.classes);
            result.add(edgeElement);
        }
        if (edge.n2 == node) {
            inNodeIds = inNodeIds == null ? edge.n1.name : inNodeIds + " " + edge.n1.name;
            inNodeClasses.addAll(edge.n1.classes);
        }
    }
    if (includeIncomingIdAttributes) {
        nodeElement.attr("inNodeId", inNodeIds);
    }
    if (includeOutgoingIdAttributes) {
        nodeElement.attr("outNodeId", outNodeIds);
    }
    if (includeIncomingClassAttributes) {
        nodeElement.attr("inNodeClass", Text.join(inNodeClasses, " "));
    }
    if (includeOutgoingClassAttributes) {
        nodeElement.attr("outNodeClass", Text.join(outNodeClasses, " "));
    }
    return result;
}
Also used : DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) DagElement(com.randomnoun.build.javaToGraphviz.dom.DagElement) ArrayList(java.util.ArrayList) DagEdge(com.randomnoun.build.javaToGraphviz.dag.DagEdge) HashSet(java.util.HashSet)

Example 3 with DagElement

use of com.randomnoun.build.javaToGraphviz.dom.DagElement in project java-to-graphviz by randomnoun.

the class DagStyleApplier method setDagStyles.

private void setDagStyles(Document document, CSSStyleSheet stylesheet, boolean setIds) {
    // copy the calculated styles from the DOM back into the gvStyles field
    final CSSOMParser inlineParser = new CSSOMParser();
    inlineParser.setErrorHandler(new ExceptionErrorHandler());
    NodeTraversor.traverse(new NodeVisitor() {

        @Override
        public void head(Node node, int depth) {
            if (node instanceof DagElement && node.hasAttr("style")) {
                // parse the CSS into a CSSStyleDeclaration
                InputSource input = new InputSource(new StringReader(node.attr("style")));
                CSSStyleDeclaration declaration = null;
                try {
                    declaration = inlineParser.parseStyleDeclaration(input);
                } catch (IOException e) {
                    throw new IllegalStateException("IOException on string", e);
                }
                DagElement dagElement = (DagElement) node;
                String tagName = ((Element) node).tagName();
                // Dag dag = dagElement.dag;
                DagNode dagNode = dagElement.dagNode;
                DagEdge dagEdge = dagElement.dagEdge;
                DagSubgraph dagSubgraph = dagElement.dagSubgraph;
                if (dagSubgraph != null) {
                    for (int i = 0; i < declaration.getLength(); i++) {
                        String prop = declaration.item(i);
                        if (tagName.equals("graph") || tagName.equals("subgraph")) {
                            logger.debug("setting graph prop " + prop + " to " + declaration.getPropertyValue(prop));
                            dagSubgraph.gvStyles.put(prop, declaration.getPropertyValue(prop));
                        } else if (tagName.equals("graphNode")) {
                            logger.debug("setting graphNode prop " + prop + " to " + declaration.getPropertyValue(prop));
                            dagSubgraph.gvNodeStyles.put(prop, declaration.getPropertyValue(prop));
                        } else if (tagName.equals("graphEdge")) {
                            logger.debug("setting graphEdge prop " + prop + " to " + declaration.getPropertyValue(prop));
                            dagSubgraph.gvEdgeStyles.put(prop, declaration.getPropertyValue(prop));
                        }
                    }
                    if ((tagName.equals("graph") || tagName.equals("subgraph")) && setIds) {
                        setIdLabel(dagSubgraph);
                    }
                } else if (dagNode != null) {
                    for (int i = 0; i < declaration.getLength(); i++) {
                        String prop = declaration.item(i);
                        logger.debug("setting " + dagNode.name + " prop " + prop + " to " + declaration.getPropertyValue(prop));
                        dagNode.gvStyles.put(prop, declaration.getPropertyValue(prop));
                    }
                    if (setIds) {
                        setIdLabel(dagNode);
                    }
                } else if (dagEdge != null) {
                    for (int i = 0; i < declaration.getLength(); i++) {
                        String prop = declaration.item(i);
                        logger.debug("setting dagEdge prop " + prop + " to " + declaration.getPropertyValue(prop));
                        dagEdge.gvStyles.put(prop, declaration.getPropertyValue(prop));
                    }
                    if (setIds) {
                        setIdLabel(dagEdge);
                    }
                }
            }
        }

        @Override
        public void tail(Node node, int depth) {
        }
    }, document.body());
}
Also used : InputSource(org.w3c.css.sac.InputSource) CSSOMParser(com.steadystate.css.parser.CSSOMParser) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) Node(org.jsoup.nodes.Node) DagSubgraph(com.randomnoun.build.javaToGraphviz.dag.DagSubgraph) IOException(java.io.IOException) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) NodeVisitor(org.jsoup.select.NodeVisitor) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) DagElement(com.randomnoun.build.javaToGraphviz.dom.DagElement) StringReader(java.io.StringReader) ExceptionErrorHandler(com.randomnoun.build.javaToGraphviz.dom.StylesheetApplier.ExceptionErrorHandler) DagEdge(com.randomnoun.build.javaToGraphviz.dag.DagEdge)

Example 4 with DagElement

use of com.randomnoun.build.javaToGraphviz.dom.DagElement in project java-to-graphviz by randomnoun.

the class DagStyleApplier method inlineStyles.

/**
 * Apply the rules in the stylesheet to the Dag, and populates the gvStyles
 * fields on the Dag, DagEdge and DagNode objects.
 *
 * @param stylesheet
 *
 * @throws IOException
 */
public void inlineStyles(CSSStyleSheet stylesheet) throws IOException {
    // OK so before we create the subgraphs, create dag edges for things that are enabled by style properties
    // applyDomStyles(document, stylesheet);
    // List<DagElement> elementsToExpandInExcruciatingDetail = getElementsWithStyleProperty(document, "gv-fluent", "true"); // enable method nodes
    // use the gv-newSubgraph CSS properties to add subgraphs into both the Dag and the DOM
    resetDomStyles(document);
    applyDomStyles(document, stylesheet);
    Set<DagElement> elementsToCreateSubgraphsInside = getElementsWithStyleProperty(document, "gv-newSubgraph", "true").keySet();
    Map<DagElement, String> elementsTruncateEdges = getElementsWithStyleProperty(document, "gv-truncateEdges", null);
    Set<DagElement> elementsToCreateSubgraphsFrom = getElementsWithStyleProperty(document, "gv-beginOuterSubgraph", "true").keySet();
    Set<DagElement> elementsToCreateSubgraphsTo = getElementsWithStyleProperty(document, "gv-endOuterSubgraph", "true").keySet();
    // DagElement outsideEl = elementsToCreateSubgraphsInside.get(i);
    for (DagElement outsideEl : elementsToCreateSubgraphsInside) {
        DagNode outsideNode = outsideEl.dagNode;
        DagSubgraph newSg;
        DagSubgraph sg = dag.dagNodeToSubgraph.get(outsideNode);
        if (sg == null) {
            throw new IllegalStateException("this shouldn't happen any more");
        } else {
            newSg = new DagSubgraph(dag, sg);
            sg.subgraphs.add(newSg);
        }
        newSg.lineNumber = outsideNode.lineNumber;
        newSg.gvAttributes = new HashMap<>(outsideNode.gvAttributes);
        // moves the nodes in the dom
        DagElement newSgEl = new DagElement("subgraph", newSg, newSg.gvAttributes);
        while (outsideEl.childrenSize() > 0) {
            Element c = outsideEl.child(0);
            c.remove();
            newSgEl.appendChild(c);
        }
        outsideEl.appendChild(newSgEl);
        // moves the nodes in the dag subgraphs
        moveToSubgraph(newSg, newSgEl, dag, dagNodesToElements, outsideNode);
        String truncateEdges = elementsTruncateEdges.get(outsideEl);
        if (truncateEdges != null && !truncateEdges.equals("none")) {
            // outsideEl.addClass("red");
            // outsideNode.classes.add("red");
            boolean truncateIncoming = false;
            boolean truncateOutgoing = false;
            if (truncateEdges.equals("incoming")) {
                truncateIncoming = true;
            } else if (truncateEdges.equals("outgoing")) {
                truncateOutgoing = true;
            } else if (truncateEdges.equals("both")) {
                truncateIncoming = true;
                truncateOutgoing = true;
            } else {
                throw new IllegalArgumentException("Invalid 'gv-truncateEdges' property '" + truncateEdges + "'; expected " + "'none', 'incoming', 'outgoing' or 'both'");
            }
            List<DagEdge> inEdges = new ArrayList<>();
            List<DagEdge> outEdges = new ArrayList<>();
            // actually maybe I do now
            for (DagEdge e : dag.edges) {
                if (truncateOutgoing && newSg.nodes.contains(e.n1) && !newSg.nodes.contains(e.n2)) {
                    outEdges.add(e);
                }
                if (truncateIncoming && newSg.nodes.contains(e.n2) && !newSg.nodes.contains(e.n1)) {
                    inEdges.add(e);
                }
            }
            for (DagEdge e : outEdges) {
                // e.n1 = newSg; // needs to be node -> node, not subgraph -> node
                // instead, outgoing edges need 'ltail' with the name of the source subgraph
                // don't know the name of this subgraph yet
                e.gvObjectStyles.put("ltail", newSg);
            }
            for (DagEdge e : inEdges) {
                // don't know the name of this subgraph yet
                e.gvObjectStyles.put("lhead", newSg);
            }
        }
    }
    // DagElement fromEl = elementsToCreateSubgraphsFrom.get(i);
    for (DagElement fromEl : elementsToCreateSubgraphsFrom) {
        Element fromParentEl = fromEl.parent();
        DagNode fromNode = fromEl.dagNode;
        DagSubgraph newSg;
        DagSubgraph sg = dag.dagNodeToSubgraph.get(fromNode);
        if (sg == null) {
            throw new IllegalStateException("this shouldn't happen any more");
        } else {
            newSg = new DagSubgraph(dag, sg);
            sg.subgraphs.add(newSg);
        }
        newSg.lineNumber = fromNode.lineNumber;
        newSg.gvAttributes = new HashMap<>(fromNode.gvAttributes);
        // moves the nodes in the dom
        DagElement newSgEl = new DagElement("subgraph", newSg, newSg.gvAttributes);
        int idx = fromEl.elementSiblingIndex();
        boolean done = false;
        while (idx < fromParentEl.childrenSize() && !done) {
            // TODO: could probably remove the from and to nodes completely, but then I'll have to rejig the edges,
            // although they should always be straight-through so that should be easy enough
            // and then apply the 'from' styles to the newly-created subgraph
            DagElement c = (DagElement) fromParentEl.child(idx);
            if (elementsToCreateSubgraphsTo.contains(c)) {
                elementsToCreateSubgraphsTo.remove(c);
                done = true;
            }
            c.remove();
            newSgEl.appendChild(c);
            // moves the nodes in the dag subgraphs
            if (c.dagNode != null) {
                // dag subgraphs don't contain edges
                moveToSubgraph(newSg, newSgEl, dag, dagNodesToElements, c.dagNode);
            }
        }
        if (!done) {
            logger.warn("gv-subgraph without gv-end, closing subgraph at AST boundary");
        }
        fromParentEl.insertChildren(idx, newSgEl);
    }
    if (elementsToCreateSubgraphsTo.size() > 0) {
        throw new IllegalStateException("gv-end without gv-subgraph");
    }
    // reapply styles now the DOM contains CSS-defined subgraph elements
    resetDomStyles(document);
    applyDomStyles(document, stylesheet);
    // true = set IDs
    setDagStyles(document, stylesheet, true);
    // TODO: arguably now that the node IDs have changed, couple reapply a third time in case there are any
    // ID-specific CSS rules
    // TODO: also recreate element inNodeId, outNodeId inNodeIds, outNodeIds attributes
    // applyStyles(document, stylesheet, dag);
    setDagSubgraphLiterals(document, stylesheet);
}
Also used : Element(org.jsoup.nodes.Element) DagElement(com.randomnoun.build.javaToGraphviz.dom.DagElement) ArrayList(java.util.ArrayList) DagSubgraph(com.randomnoun.build.javaToGraphviz.dag.DagSubgraph) DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) DagElement(com.randomnoun.build.javaToGraphviz.dom.DagElement) DagEdge(com.randomnoun.build.javaToGraphviz.dag.DagEdge)

Example 5 with DagElement

use of com.randomnoun.build.javaToGraphviz.dom.DagElement in project java-to-graphviz by randomnoun.

the class DagStyleApplier method getDagElement.

public DagElement getDagElement(Map<DagNode, DagElement> dagNodesToElements, DagSubgraph sg) {
    DagElement graphEl = new DagElement(sg.container == null ? "graph" : "subgraph", sg, sg.gvAttributes);
    // bodyEl.appendChild(graphEl);
    DagElement graphNodeEl = new DagElement("graphNode", sg, sg.gvAttributes);
    graphEl.appendChild(graphNodeEl);
    DagElement graphEdgeEl = new DagElement("graphEdge", sg, sg.gvAttributes);
    graphEl.appendChild(graphEdgeEl);
    for (int i = 0; i < sg.nodes.size(); i++) {
        DagNode node = sg.nodes.get(i);
        if (dagNodesToElements.containsKey(node)) {
        // skip it
        } else {
            graphEl.appendChildren(getDagElements(dagNodesToElements, sg, node));
        }
    }
    return graphEl;
}
Also used : DagNode(com.randomnoun.build.javaToGraphviz.dag.DagNode) DagElement(com.randomnoun.build.javaToGraphviz.dom.DagElement)

Aggregations

DagElement (com.randomnoun.build.javaToGraphviz.dom.DagElement)7 DagNode (com.randomnoun.build.javaToGraphviz.dag.DagNode)6 DagEdge (com.randomnoun.build.javaToGraphviz.dag.DagEdge)3 DagSubgraph (com.randomnoun.build.javaToGraphviz.dag.DagSubgraph)3 ExceptionErrorHandler (com.randomnoun.build.javaToGraphviz.dom.StylesheetApplier.ExceptionErrorHandler)3 CSSOMParser (com.steadystate.css.parser.CSSOMParser)3 Node (org.jsoup.nodes.Node)3 NodeVisitor (org.jsoup.select.NodeVisitor)3 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Element (org.jsoup.nodes.Element)2 InputSource (org.w3c.css.sac.InputSource)2 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Stack (java.util.Stack)1