Search in sources :

Example 1 with GvStyleComment

use of com.randomnoun.build.javaToGraphviz.comment.GvStyleComment in project java-to-graphviz by randomnoun.

the class CommentExtractor method getComments.

/**
 * Return a list of processed comments from the source file.
 *
 * GvStyleComment:    "// gv-style: { xxx }"
 * GvKeepNodeComment: "// gv-keepNode: xxx"
 * GvLiteralComment:  "// gv-literal:  xxx"
 * GvDigraphComment:  "// gv-graph:    xxx"
 * GvSubgraphComment: "// gv-subgraph: xxx"
 * GvComment:         "// gv.className.className.className#id: xxx { xxx }"
 *
 * @param cu
 * @param src
 * @return
 */
@SuppressWarnings("unchecked")
public List<CommentText> getComments(CompilationUnit cu, String src) {
    // @TODO better regex
    // Pattern gvPattern = Pattern.compile("^gv(\\.[a-zA-Z]+)*:");  // gv.some.class.names:
    // probably do this right at the end as gv.literal affects how we parse it
    // Pattern valPattern = Pattern.compile("(([a-zA-Z]+)\\s*([^;]*);\\s*)*"); // things;separated;by;semicolons;
    List<CommentText> comments = new ArrayList<>();
    for (Comment c : (List<Comment>) cu.getCommentList()) {
        // comment.accept(cv);
        boolean eolComment = (c instanceof LineComment);
        int start = c.getStartPosition();
        int end = start + c.getLength();
        String text = src.substring(start, end);
        int line = cu.getLineNumber(start);
        int column = cu.getColumnNumber(start);
        if (c.isBlockComment()) {
            if (text.startsWith("/*") && text.endsWith("*/")) {
                text = text.substring(2, text.length() - 2).trim();
            } else {
                throw new IllegalStateException("Block comment does not start with '/*' and end with '*/':  '" + text + "'");
            }
        }
        if (c.isLineComment()) {
            if (text.startsWith("//")) {
                text = text.substring(2).trim();
            } else {
                throw new IllegalStateException("Line comment does not start with '//': '" + text + "'");
            }
        }
        if (text.startsWith("gv-style:")) {
            String s = text.substring(9).trim();
            if (s.startsWith("{") && s.endsWith("}")) {
                s = s.substring(1, s.length() - 1).trim();
                // here be the css
                // remove inline comments
                // logger.info("maybe here ? " +  s);
                comments.add(new GvStyleComment(c, line, column, eolComment, text, s));
            } else {
                throw new IllegalStateException("gv-style does not start with '{' and end with '}':  '" + text + "'");
            }
        } else if (text.startsWith("gv-endGraph")) {
            comments.add(new GvEndGraphComment(c, line, column, eolComment));
        } else if (text.startsWith("gv-endSubgraph")) {
            comments.add(new GvEndSubgraphComment(c, line, column, eolComment));
        } else if (text.startsWith("gv-literal:")) {
            String s = text.substring(11).trim();
            comments.add(new GvLiteralComment(c, line, column, eolComment, s));
        } else if (text.startsWith("gv-keepNode:")) {
            String s = text.substring(12).trim();
            comments.add(new GvKeepNodeComment(c, line, column, eolComment, s));
        } else if (text.startsWith("gv-option:")) {
            String s = text.substring(10).trim();
            comments.add(new GvOptionComment(c, line, column, eolComment, s));
        } else {
            Matcher fgm;
            CommentText gvc = null;
            fgm = gvGraphClassPattern.matcher(text);
            if (fgm.find()) {
                gvc = getGvComment("gv-graph", c, line, column, fgm, text);
            } else {
                fgm = gvSubgraphClassPattern.matcher(text);
                if (fgm.find()) {
                    gvc = getGvComment("gv-subgraph", c, line, column, fgm, text);
                } else {
                    fgm = gvNodeClassPattern.matcher(text);
                    if (fgm.find()) {
                        gvc = getGvComment("gv", c, line, column, fgm, text);
                    } else {
                    // regular comment, ignore
                    }
                }
            }
            if (gvc != null) {
                comments.add(gvc);
            }
        }
    }
    return comments;
}
Also used : GvSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) GvEndGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment) LineComment(org.eclipse.jdt.core.dom.LineComment) GvGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvGraphComment) GvKeepNodeComment(com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment) GvEndSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment) GvStyleComment(com.randomnoun.build.javaToGraphviz.comment.GvStyleComment) GvLiteralComment(com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment) GvOptionComment(com.randomnoun.build.javaToGraphviz.comment.GvOptionComment) Comment(org.eclipse.jdt.core.dom.Comment) Matcher(java.util.regex.Matcher) CommentText(com.randomnoun.build.javaToGraphviz.comment.CommentText) ArrayList(java.util.ArrayList) GvEndSubgraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment) LineComment(org.eclipse.jdt.core.dom.LineComment) GvKeepNodeComment(com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment) GvEndGraphComment(com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment) GvStyleComment(com.randomnoun.build.javaToGraphviz.comment.GvStyleComment) GvOptionComment(com.randomnoun.build.javaToGraphviz.comment.GvOptionComment) GvLiteralComment(com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with GvStyleComment

use of com.randomnoun.build.javaToGraphviz.comment.GvStyleComment 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;
}
Also used : SACParserCSS3(com.steadystate.css.parser.SACParserCSS3) InputSource(org.w3c.css.sac.InputSource) CSSStyleSheetImpl(com.steadystate.css.dom.CSSStyleSheetImpl) CommentText(com.randomnoun.build.javaToGraphviz.comment.CommentText) CSSOMParser(com.steadystate.css.parser.CSSOMParser) RestrictedCssStyleSheetImpl(com.randomnoun.build.javaToGraphviz.dom.RestrictedCssStyleSheetImpl) GvStyleComment(com.randomnoun.build.javaToGraphviz.comment.GvStyleComment) GvComment(com.randomnoun.build.javaToGraphviz.comment.GvComment) CSSStyleSheet(org.w3c.dom.css.CSSStyleSheet) StringReader(java.io.StringReader)

Aggregations

CommentText (com.randomnoun.build.javaToGraphviz.comment.CommentText)2 GvComment (com.randomnoun.build.javaToGraphviz.comment.GvComment)2 GvStyleComment (com.randomnoun.build.javaToGraphviz.comment.GvStyleComment)2 GvEndGraphComment (com.randomnoun.build.javaToGraphviz.comment.GvEndGraphComment)1 GvEndSubgraphComment (com.randomnoun.build.javaToGraphviz.comment.GvEndSubgraphComment)1 GvGraphComment (com.randomnoun.build.javaToGraphviz.comment.GvGraphComment)1 GvKeepNodeComment (com.randomnoun.build.javaToGraphviz.comment.GvKeepNodeComment)1 GvLiteralComment (com.randomnoun.build.javaToGraphviz.comment.GvLiteralComment)1 GvOptionComment (com.randomnoun.build.javaToGraphviz.comment.GvOptionComment)1 GvSubgraphComment (com.randomnoun.build.javaToGraphviz.comment.GvSubgraphComment)1 RestrictedCssStyleSheetImpl (com.randomnoun.build.javaToGraphviz.dom.RestrictedCssStyleSheetImpl)1 CSSStyleSheetImpl (com.steadystate.css.dom.CSSStyleSheetImpl)1 CSSOMParser (com.steadystate.css.parser.CSSOMParser)1 SACParserCSS3 (com.steadystate.css.parser.SACParserCSS3)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Comment (org.eclipse.jdt.core.dom.Comment)1 LineComment (org.eclipse.jdt.core.dom.LineComment)1