Search in sources :

Example 1 with ExceptionErrorHandler

use of com.randomnoun.build.javaToGraphviz.dom.StylesheetApplier.ExceptionErrorHandler 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 ExceptionErrorHandler

use of com.randomnoun.build.javaToGraphviz.dom.StylesheetApplier.ExceptionErrorHandler 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 3 with ExceptionErrorHandler

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

the class DagStyleApplier method getElementsWithStyleProperty.

private Map<DagElement, String> getElementsWithStyleProperty(Document document, final String propertyName, final String propertyValue) {
    Map<DagElement, String> result = new LinkedHashMap<>();
    // 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;
                DagNode dagNode = dagElement.dagNode;
                String nodePropertyValue = declaration.getPropertyValue(propertyName);
                if (propertyValue != null) {
                    // match specific value
                    if (dagNode != null && nodePropertyValue.equals(propertyValue)) {
                        result.put(dagElement, nodePropertyValue);
                    }
                } else {
                    // match any value
                    if (dagNode != null && !Text.isBlank(nodePropertyValue)) {
                        result.put(dagElement, nodePropertyValue);
                    }
                }
            }
        }

        @Override
        public void tail(Node node, int depth) {
        }
    }, document.body());
    return result;
}
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) IOException(java.io.IOException) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) LinkedHashMap(java.util.LinkedHashMap) 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)

Aggregations

DagNode (com.randomnoun.build.javaToGraphviz.dag.DagNode)3 DagElement (com.randomnoun.build.javaToGraphviz.dom.DagElement)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 DagSubgraph (com.randomnoun.build.javaToGraphviz.dag.DagSubgraph)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 InputSource (org.w3c.css.sac.InputSource)2 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)2 DagEdge (com.randomnoun.build.javaToGraphviz.dag.DagEdge)1 LinkedHashMap (java.util.LinkedHashMap)1 Stack (java.util.Stack)1