JFlexXref.java revision 1384
850N/A/*
850N/A * CDDL HEADER START
850N/A *
850N/A * The contents of this file are subject to the terms of the
850N/A * Common Development and Distribution License (the "License").
850N/A * You may not use this file except in compliance with the License.
850N/A *
850N/A * See LICENSE.txt included in this distribution for the specific
850N/A * language governing permissions and limitations under the License.
850N/A *
850N/A * When distributing Covered Code, include this CDDL HEADER in each
850N/A * file and include the License file at LICENSE.txt.
850N/A * If applicable, add the following below this CDDL HEADER, with the
850N/A * fields enclosed by brackets "[]" replaced with your own identifying
850N/A * information: Portions Copyright [yyyy] [name of copyright owner]
850N/A *
850N/A * CDDL HEADER END
850N/A */
850N/A
850N/A/*
1058N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1185N/A * Portions Copyright 2011, 2012 Jens Elkner.
850N/A */
850N/A
850N/Apackage org.opensolaris.opengrok.analysis;
850N/A
1058N/Aimport java.io.CharArrayReader;
850N/Aimport java.io.IOException;
1058N/Aimport java.io.Reader;
850N/Aimport java.lang.reflect.Field;
1020N/Aimport java.util.ArrayList;
1145N/Aimport java.util.Comparator;
1145N/Aimport java.util.HashMap;
1145N/Aimport java.util.Map;
943N/Aimport java.util.Set;
1145N/Aimport java.util.SortedSet;
1145N/Aimport java.util.TreeSet;
1145N/Aimport java.util.logging.Logger;
857N/A
850N/Aimport org.opensolaris.opengrok.analysis.Definitions.Tag;
850N/Aimport org.opensolaris.opengrok.configuration.Project;
1020N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
850N/Aimport org.opensolaris.opengrok.history.Annotation;
850N/Aimport org.opensolaris.opengrok.web.Util;
1020N/A
850N/A/**
850N/A * Base class for Xref lexers.
850N/A *
1020N/A * @author Lubos Kosco
1121N/A */
1121N/Apublic abstract class JFlexXref {
1121N/A private static final Logger logger = Logger.getLogger(JFlexXref.class.getName());
1121N/A public XrefWriter out;
1121N/A public String urlPrefix = RuntimeEnvironment.getInstance().getUrlPrefix();
1121N/A public Annotation annotation;
1121N/A public Project project;
1185N/A protected Definitions defs;
1190N/A /**
1185N/A * A stack of <span ...> CSS class names currently opened.
1185N/A * It is used to close all open spans for a line and re-open them at the
1185N/A * start of the next line to produce wellformed XML. So if a
1190N/A * <span ...> is opened and do not closed on the same line, the class
1190N/A * name used needs to be pushed onto this stack. If the <span ...>
1185N/A * gets closed on a different line, it needs to be popped from this stack.
1185N/A * If properly done, the stack should be empty when the last line has been
1185N/A * written.
1020N/A */
1145N/A @SuppressWarnings("serial")
1145N/A protected class SpanStack extends ArrayList<String> {
1145N/A /**
1145N/A * Create a new empty instance with the default capacity.
1145N/A */
1145N/A public SpanStack() {
1145N/A super();
1145N/A }
1145N/A /**
1145N/A * The preferred method to add a new span class name to this instance.
1145N/A * @param s class name to add.
1145N/A * @see #pop()
1145N/A * @throws IllegalArgumentException if the given argument is {@code null}.
1145N/A */
1145N/A public void push(String s) {
1145N/A if (s == null) {
1145N/A throw new IllegalArgumentException("null is not allowed");
1145N/A }
1147N/A super.add(s);
1145N/A }
1145N/A
1147N/A /**
1145N/A * The preferred method to remove the last element from this stack.
1145N/A * @return {@code null} if the stack is empty, the removed element
1145N/A * otherwise.
1145N/A * @see #push(String)
1145N/A */
1145N/A public String pop() {
1145N/A return isEmpty() ? null : remove(size() - 1);
1145N/A }
1145N/A }
1121N/A
1121N/A /** the {@link SpanStack} for this and only this instance */
1121N/A protected final SpanStack spans;
1121N/A /** EOF value returned by yylex(). */
1121N/A private final int yyeof;
1121N/A /** See {@link RuntimeEnvironment#getUserPage()}. Per default initialized
1121N/A * in the constructor and here to be consistent and avoid lot of
1185N/A * unnecessary lookups.
1185N/A * @see #startNewLine() */
1185N/A protected String userPageLink;
1185N/A /** See {@link RuntimeEnvironment#getUserPageSuffix()}. Per default
1185N/A * initialized in the constructor and here to be consistent and avoid lot of
1185N/A * unnecessary lookups.
1185N/A * @see #startNewLine() */
1185N/A protected String userPageSuffix;
1121N/A
1121N/A /**
1121N/A * Description of the style to use for a type of definitions.
1121N/A */
1121N/A private static class Style {
1121N/A /** Name of the style definition as given by CTags. */
1121N/A final String name;
1121N/A
1121N/A /** Class name used by the style sheets when rendering the xref. */
1121N/A final String ssClass;
1121N/A
1020N/A /**
1121N/A * The title of the section to which this type belongs, or {@code null}
1121N/A * if this type should not be listed in the navigation panel.
1121N/A */
1121N/A final String title;
1121N/A
1121N/A /** Construct a style description. */
1121N/A Style(String name, String ssClass, String title) {
1121N/A this.name = name;
1121N/A this.ssClass = ssClass;
1121N/A this.title = title;
1121N/A }
1121N/A }
1121N/A
1121N/A /**
1121N/A * Description of styles to use for different types of definitions.
1121N/A */
1121N/A private static final Style[] DEFINITION_STYLES = {
1121N/A new Style("macro", "xm", "Macro"),
1121N/A new Style("argument", "xa", null),
1121N/A new Style("local", "xl", null),
1121N/A new Style("variable", "xv", "Variable"),
1121N/A new Style("class", "xc", "Class"),
1121N/A new Style("package", "xp", "Package"),
1121N/A new Style("interface", "xi", "Interface"),
1121N/A new Style("namespace", "xn", "Namespace"),
1058N/A new Style("enumerator", "xer", null),
1121N/A new Style("enum", "xe", "Enum"),
1121N/A new Style("struct", "xs", "Struct"),
1121N/A new Style("typedefs", "xts", null),
1121N/A new Style("typedef", "xt", "Typedef"),
1121N/A new Style("union", "xu", null),
1121N/A new Style("field", "xfld", null),
1121N/A new Style("member", "xmb", null),
1121N/A new Style("function", "xf", "Function"),
1121N/A new Style("method", "xmt", "Method"),
1121N/A new Style("subroutine", "xsr", "Subroutine"),
1121N/A };
850N/A
1121N/A /**
1121N/A * Create a new lexer instance. Initializes {@link #userPageLink},
1121N/A * {@link #userPageSuffix} using the runtime environment and creates a new
1121N/A * empty {@link SpanStack}.
1121N/A * @see RuntimeEnvironment#getUserPage()
1121N/A * @see RuntimeEnvironment#getUserPageSuffix()
1121N/A */
1121N/A protected JFlexXref() {
1145N/A try {
1121N/A // TODO when bug #16053 is fixed, we should add a getter to a file
1121N/A // that's included from all the Xref classes so that we avoid the
1121N/A // reflection.
1121N/A Field f = getClass().getField("YYEOF");
1121N/A yyeof = f.getInt(null);
1121N/A userPageLink = RuntimeEnvironment.getInstance().getUserPage();
1020N/A if (userPageLink != null && userPageLink.length() == 0) {
1121N/A userPageLink = null;
1145N/A }
1145N/A userPageSuffix = RuntimeEnvironment.getInstance().getUserPageSuffix();
1145N/A if (userPageSuffix != null && userPageSuffix.length() == 0) {
1145N/A userPageSuffix = null;
1145N/A }
1145N/A } catch (Exception e) {
1145N/A // The auto-generated constructors for the Xref classes don't
1145N/A // expect a checked exception, so wrap it in an AssertionError.
1145N/A // This should never happen, since all the Xref classes will get
1145N/A // a public static YYEOF field from JFlex.
1145N/A AssertionError ae = new AssertionError("Couldn't initialize yyeof");
1145N/A ae.initCause(e);
1145N/A throw ae; // NOPMD (stack trace is preserved by initCause(), but
1145N/A // PMD thinks it's lost)
1145N/A }
1145N/A spans = new SpanStack();
1145N/A }
1145N/A
1145N/A /**
1145N/A * Reinitialize the xref with new contents.
1145N/A *
1145N/A * @param contents a char buffer with text to analyze
1145N/A * @param length the number of characters to use from the char buffer
1145N/A */
1145N/A public void reInit(char[] contents, int length) {
1145N/A yyreset(new CharArrayReader(contents, 0, length));
1145N/A annotation = null;
1145N/A }
1145N/A
1145N/A public void setDefs(Definitions defs) {
1145N/A this.defs = defs;
1145N/A }
1145N/A
1145N/A protected void appendProject() throws IOException {
1145N/A if (project != null) {
1145N/A out.write("&amp;project=");
1145N/A out.write(project.getDescription());
1145N/A }
1145N/A }
1145N/A
1145N/A protected String getProjectPostfix() {
1145N/A return project == null ? "" : ("&amp;project=" + project.getDescription());
1145N/A }
1145N/A
1145N/A /** Get the next token from the scanner. */
1145N/A public abstract int yylex() throws IOException;
1145N/A
1145N/A /** Reset the scanner. */
1145N/A public abstract void yyreset(Reader reader);
1145N/A
1145N/A /** Get the value of {@code yyline}. */
1145N/A protected abstract int getLineNumber();
1145N/A
1145N/A /** Set the value of {@code yyline}. */
1145N/A protected abstract void setLineNumber(int x);
1145N/A
1145N/A /**
1145N/A * Write the crossfile content to the specified {@code Writer}.
1145N/A *
1145N/A * @param out xref destination
1145N/A * @throws IOException on error when writing the xref
1145N/A */
1145N/A public void write(XrefWriter out) throws IOException {
1145N/A this.out = out;
1145N/A writeSymbolTable();
1145N/A setLineNumber(0);
1145N/A out.write("<div id='lines'\n>");
1145N/A startNewLine();
1145N/A while (yylex() != yyeof) { // NOPMD while statement intentionally empty
1145N/A // nothing to do here, yylex() will do the work
1145N/A }
1145N/A finishLine();
1145N/A out.write("</div\n>");
1185N/A out.setLines(getLineNumber());
1190N/A if (spans.size() != 0) {
1145N/A logger.info("The SpanStack for " + out.getFile()
1145N/A + " is not empty! May be the " + getClass().getSimpleName()
1145N/A + " lexer is not perfect yet!");
1145N/A spans.clear();
1145N/A }
1145N/A }
1145N/A
1145N/A /**
1145N/A * Write a JavaScript function that returns an array with the definitions
1145N/A * to list in the navigation panel. Each element of the array is itself an
1145N/A * array containing the name of the definition type, the CSS class name for
1145N/A * the type, and an array of (symbol, line) pairs for the definitions of
1145N/A * that type.
1145N/A */
1145N/A private void writeSymbolTable() throws IOException {
1145N/A if (defs == null) {
1145N/A // No definitions, no symbol table to write
1145N/A return;
1145N/A }
1145N/A
1145N/A // We want the symbol table to be sorted
1121N/A Comparator<Tag> cmp = new Comparator<Tag>() {
1121N/A @Override
1121N/A public int compare(Tag tag1, Tag tag2) {
1121N/A // Order by symbol name, and then by line number if multiple
1121N/A // definitions use the same symbol name
1121N/A int ret = tag1.symbol.compareTo(tag2.symbol);
1121N/A if (ret == 0) {
1121N/A ret = tag1.line - tag2.line;
1185N/A }
1121N/A return ret;
1020N/A }
1121N/A };
1121N/A
1121N/A Map<String, SortedSet<Tag>> symbols =
1121N/A new HashMap<String, SortedSet<Tag>>();
1121N/A
1121N/A for (Tag tag : defs.getTags()) {
1121N/A Style style = getStyle(tag.type);
1121N/A if (style != null && style.title != null) {
1121N/A SortedSet<Tag> tags = symbols.get(style.name);
1121N/A if (tags == null) {
1121N/A tags = new TreeSet<Tag>(cmp);
1121N/A symbols.put(style.name, tags);
1121N/A }
1020N/A tags.add(tag);
1121N/A }
1121N/A }
1121N/A
1108N/A out.append("<script type=\"text/javascript\">/* <![CDATA[ */\n");
1121N/A out.append("O.symlist = [");
1121N/A
1121N/A boolean first = true;
1121N/A for (Style style : DEFINITION_STYLES) {
943N/A SortedSet<Tag> tags = symbols.get(style.name);
1145N/A if (tags != null) {
1145N/A if (!first) {
1145N/A out.append(',');
1121N/A }
1121N/A out.append("[\"");
1121N/A out.append(style.title);
1121N/A out.append("\",\"");
1121N/A out.append(style.ssClass);
1121N/A out.append("\",[");
1121N/A
1121N/A boolean firstTag = true;
1121N/A for (Tag tag : tags) {
1121N/A if (!firstTag) {
1121N/A out.append(',');
1108N/A }
1121N/A out.append('[');
1121N/A out.append(Util.jsStringLiteral(tag.symbol));
1121N/A out.append(',');
1121N/A out.append(Integer.toString(tag.line));
1121N/A out.append(']');
1121N/A firstTag = false;
1121N/A }
1121N/A out.append("]]");
1121N/A first = false;
1121N/A }
1121N/A }
1121N/A /* no LF intentionally - xml is whitespace aware ... */
1121N/A out.append("]; /* ]]> */</script>");
1121N/A }
1121N/A
1121N/A /**
1121N/A * Get the style description for a definition type.
1121N/A *
1121N/A * @param type the definition type
1121N/A * @return the style of a definition type, or {@code null} if no style is
1121N/A * defined for the type
1121N/A * @see #DEFINITION_STYLES
1121N/A */
1121N/A private static Style getStyle(String type) {
943N/A for (Style style : DEFINITION_STYLES) {
1121N/A if (type.startsWith(style.name)) {
1121N/A return style;
1121N/A }
1121N/A }
1121N/A return null;
1121N/A }
1121N/A
1121N/A /**
1121N/A * Write out annotation infos for the given line.
1121N/A *
1121N/A * @param num linenumber to print
1121N/A * @throws IOException depends on the destination (<var>out</var>).
1121N/A */
1121N/A private final void writeAnnotationInfos(int num) throws IOException {
1121N/A String r = annotation.getRevision(num);
943N/A out.write("<span class='blame'>");
1121N/A out.write("<a class='r' href=\"");
1121N/A out.write(Util.URIEncodePath(annotation.getFilename()));
1121N/A out.write("?a=true&amp;r=");
1121N/A out.write(Util.URIEncode(r));
1121N/A String msg = annotation.getDesc(r);
1121N/A if (msg != null) {
1121N/A out.write("\" title=\"");
1121N/A out.write(msg);
1121N/A }
1185N/A out.write("\">");
1121N/A StringBuilder buf = new StringBuilder();
1121N/A Util.htmlize(r, buf);
1122N/A out.write(buf.toString());
1122N/A buf.setLength(0);
1123N/A out.write("</a>");
1123N/A String a = annotation.getAuthor(num);
1123N/A if (userPageLink == null) {
1122N/A out.write("<span class='a'>");
1122N/A Util.htmlize(a, buf);
1122N/A out.write(buf.toString());
1122N/A out.write("</span>");
1122N/A buf.setLength(0);
1123N/A } else {
1123N/A out.write("<a class='a' href='");
1123N/A out.write(userPageLink);
1123N/A out.write(Util.URIEncode(a));
1123N/A if (userPageSuffix != null) {
1122N/A out.write(userPageSuffix);
850N/A }
out.write("'>");
Util.htmlize(a, buf);
out.write(buf.toString());
buf.setLength(0);
out.write("</a>");
}
out.write("</span>");
}
private final void finishLine() throws IOException {
if (out.getMark() == out.getCount()) {
out.write(' '); // <div></div> doesn't produce a line-height block
}
if (spans.size() != 0) {
for (int i=spans.size()-1; i >= 0; i--) {
out.write("</span>");
}
}
out.write("</div\n>");
}
/**
* Terminate the current line and insert preamble for the next line. The
* line count will be incremented.
*
* @throws IOException on error when writing the xref
*/
protected final void startNewLine() throws IOException {
int line = getLineNumber();
if (line != 0) {
finishLine();
}
setLineNumber(++line);
/* <div id="N">...</div>
* -> uncompressed size ~6%, compressed size ~26% bigger;
* <div id="N"><b>N</b>...</div>
* -> uncompressed size ~12%, compressed size ~52% bigger;
* <span class="[h]l" id="N">N</span>...
* -> is about the same as previous one
*/
out.write("<div>");
if (annotation != null) {
writeAnnotationInfos(line);
}
if (spans.size() != 0) {
for (String cname : spans) {
if (cname.isEmpty()) {
out.write("<span>");
} else {
out.write("<span class='");
out.write(cname);
out.write("'>");
}
}
}
out.mark();
}
/**
* Write a symbol and generate links as appropriate.
*
* @param symbol the symbol to write
* @param keywords a set of keywords recognized by this analyzer (no links
* will be generated if the symbol is a keyword)
* @param line the line number on which the symbol appears
* @throws IOException if an error occurs while writing to the stream
*/
protected void writeSymbol(String symbol, Set<String> keywords, int line)
throws IOException {
String[] strs = new String[1];
strs[0] = "";
if (keywords != null && keywords.contains(symbol)) {
// This is a keyword, so we don't create a link.
out.append("<b>").append(symbol).append("</b>");
} else if (defs != null && defs.hasDefinitionAt(symbol, line, strs)) {
// This is the definition of the symbol.
String type = strs[0];
String style_class = "d";
Style style = getStyle(type);
if (style != null) {
style_class = style.ssClass;
}
// 1) Create an anchor for direct links. (Perhaps we should only
// do this when there's exactly one definition of the symbol in
// this file? Otherwise, we may end up with multiple anchors with
// the same name.)
out.append("<a class=\"");
out.append(style_class);
out.append("\" name=\"");
out.append(symbol);
out.append("\"/>");
// 2) Create a link that searches for all references to this symbol.
out.append("<a href=\"");
out.append(urlPrefix);
out.append("refs=");
out.append(symbol);
appendProject();
out.append("\" class=\"");
out.append(style_class);
out.append("\">");
out.append(symbol);
out.append("</a>");
} else if (defs != null && defs.occurrences(symbol) == 1) {
// This is a reference to a symbol defined exactly once in this file.
String style_class = "d";
// Generate a direct link to the symbol definition.
out.append("<a class=\"");
out.append(style_class);
out.append("\" href=\"#");
out.append(symbol);
out.append("\">");
out.append(symbol);
out.append("</a>");
} else {
// This is a symbol that is not defined in this file, or a symbol
// that is defined more than once in this file. In either case, we
// can't generate a direct link to the definition, so generate a
// link to search for all definitions of that symbol instead.
out.append("<a href=\"");
out.append(urlPrefix);
out.append("defs=");
out.append(symbol);
appendProject();
out.append("\">");
out.append(symbol);
out.append("</a>");
}
}
/**
* Write HTML escape sequence for the specified Unicode character, unless
* it's an ISO control character, in which case it is ignored.
*
* @param c the character to write
* @throws IOException if an error occurs while writing to the stream
*/
protected void writeUnicodeChar(char c) throws IOException {
if (!Character.isISOControl(c)) {
out.append("&#").append(Integer.toString(c)).append(';');
}
}
/**
* Write an e-mail address. The address will be obfuscated if
* {@link RuntimeEnvironment#isObfuscatingEMailAddresses()} returns
* {@code true}.
*
* @param address the address to write
* @throws IOException if an error occurs while writing to the stream
*/
protected void writeEMailAddress(String address) throws IOException {
if (RuntimeEnvironment.getInstance().isObfuscatingEMailAddresses()) {
out.write(address.replace("@", " (at) "));
} else {
out.write(address);
}
}
}