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());
}
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());
}
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;
}
Aggregations