use of com.randomnoun.build.javaToGraphviz.dom.RestrictedCssStyleSheetImpl in project java-to-graphviz by randomnoun.
the class CommentExtractor method getStyleSheet.
// when we construct the DagNodes, automatically add classes based on AST type
// and line number, which will make colouring these things in from jacoco output that much simpler
// and whatever JVMTI uses, which is probably line numbers as well
/**
* Return a stylesheet, composed of:
* <ul>
* <li>imported base CSS
* <li>imported user CSS
* <li>source code styles (gv-style blocks), may import other CSS
* <li>user CSS rules
* </ul>
*
* @param comments
* @param baseCssHref
* @param userCssHrefs
* @param userCssRules
* @return
* @throws IOException
*/
public CSSStyleSheet getStyleSheet(List<CommentText> comments, String baseCssHref, List<String> userCssHrefs, List<String> userCssRules) throws IOException {
String css = "";
if (!Text.isBlank(baseCssHref) && !baseCssHref.equals("-")) {
css = "@import \"" + baseCssHref + "\";\n";
}
if (userCssHrefs != null) {
for (String s : userCssHrefs) {
css += "@import \"" + s + "\";\n";
}
}
for (CommentText c : comments) {
// String t = c.text;
if (c instanceof GvStyleComment) {
// remove non-standard css comments
String[] ss = ((GvStyleComment) c).style.split("\n");
for (int i = 0; i < ss.length; i++) {
String s = ss[i];
if (s.contains("//") && !s.contains("://")) {
// double-slash can start a comment, but not if it might be a @import url(scheme://...) double-slash
s = s.substring(0, s.indexOf("//"));
}
css += (i == 0 ? "" : "\n") + s;
}
} else if (c instanceof GvComment) {
// could check inline styles here maybe
}
}
if (userCssRules != null) {
for (String s : userCssRules) {
css += s + "\n";
}
}
if (logger.isDebugEnabled()) {
logger.debug("here's the css: " + css);
}
// pre-process the // comments out of the block comments, which isn't standard CSS
CSSOMParser parser = new CSSOMParser(new SACParserCSS3());
InputSource source = new InputSource(new StringReader(css));
CSSStyleSheet stylesheet = parser.parseStyleSheet(source, null, null);
CSSStyleSheetImpl stylesheetImpl = (CSSStyleSheetImpl) stylesheet;
RestrictedCssStyleSheetImpl restrictedStylesheetImpl = new RestrictedCssStyleSheetImpl(stylesheetImpl);
// probably need to wrap this in something which overrides importImports
// public void importImports(final boolean recursive) throws DOMException {
// or provide my own URI handler and set a weird base URI
// stylesheetImpl.importImports(true); // recursive = true
restrictedStylesheetImpl.importImports(true);
if (logger.isDebugEnabled()) {
logger.debug("CSS rules: " + restrictedStylesheetImpl.getCssText());
}
return restrictedStylesheetImpl;
}
Aggregations