JFlexXref.java revision 1354
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/*
1259N/A * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
1185N/A * Portions Copyright 2011 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.io.Writer;
1020N/Aimport java.lang.reflect.Field;
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 org.opensolaris.opengrok.analysis.Definitions.Tag;
857N/Aimport org.opensolaris.opengrok.configuration.Project;
850N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
850N/Aimport org.opensolaris.opengrok.history.Annotation;
1020N/Aimport org.opensolaris.opengrok.web.Util;
850N/A
850N/A/**
1020N/A * Base class for Xref lexers.
850N/A *
850N/A * @author Lubos Kosco
850N/A */
1020N/Apublic abstract class JFlexXref {
1121N/A public Writer out;
1121N/A public String urlPrefix = RuntimeEnvironment.getInstance().getUrlPrefix();
1121N/A public Annotation annotation;
1121N/A public Project project;
1121N/A protected Definitions defs;
1121N/A /** EOF value returned by yylex(). */
1121N/A private final int yyeof;
1185N/A /** See {@link RuntimeEnvironment#getUserPage()}. Per default initialized
1190N/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;
1190N/A /** See {@link RuntimeEnvironment#getUserPageSuffix()}. Per default
1190N/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;
1020N/A
1145N/A /**
1259N/A * Description of the style to use for a type of definitions.
1145N/A */
1259N/A private static class Style {
1259N/A /** Name of the style definition as given by CTags. */
1259N/A final String name;
1259N/A
1259N/A /** Class name used by the style sheets when rendering the xref. */
1259N/A final String ssClass;
1259N/A
1259N/A /**
1259N/A * The title of the section to which this type belongs, or {@code null}
1259N/A * if this type should not be listed in the navigation panel.
1259N/A */
1259N/A final String title;
1259N/A
1259N/A /** Construct a style description. */
1259N/A Style(String name, String ssClass, String title) {
1259N/A this.name = name;
1259N/A this.ssClass = ssClass;
1259N/A this.title = title;
1259N/A }
1259N/A }
1259N/A
1259N/A /**
1259N/A * Description of styles to use for different types of definitions.
1259N/A */
1259N/A private static final Style[] DEFINITION_STYLES = {
1259N/A new Style("macro", "xm", "Macro"),
1259N/A new Style("argument", "xa", null),
1259N/A new Style("local", "xl", null),
1259N/A new Style("variable", "xv", "Variable"),
1259N/A new Style("class", "xc", "Class"),
1259N/A new Style("package", "xp", "Package"),
1259N/A new Style("interface", "xi", "Interface"),
1259N/A new Style("namespace", "xn", "Namespace"),
1259N/A new Style("enumerator", "xer", null),
1259N/A new Style("enum", "xe", "Enum"),
1259N/A new Style("struct", "xs", "Struct"),
1259N/A new Style("typedefs", "xts", null),
1259N/A new Style("typedef", "xt", "Typedef"),
1259N/A new Style("union", "xu", null),
1259N/A new Style("field", "xfld", null),
1259N/A new Style("member", "xmb", null),
1259N/A new Style("function", "xf", "Function"),
1259N/A new Style("method", "xmt", "Method"),
1259N/A new Style("subroutine", "xsr", "Subroutine"),
1145N/A };
1145N/A
1121N/A protected JFlexXref() {
1121N/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);
1185N/A userPageLink = RuntimeEnvironment.getInstance().getUserPage();
1185N/A if (userPageLink != null && userPageLink.length() == 0) {
1185N/A userPageLink = null;
1185N/A }
1185N/A userPageSuffix = RuntimeEnvironment.getInstance().getUserPageSuffix();
1185N/A if (userPageSuffix != null && userPageSuffix.length() == 0) {
1185N/A userPageSuffix = null;
1185N/A }
1121N/A } catch (Exception e) {
1121N/A // The auto-generated constructors for the Xref classes don't
1121N/A // expect a checked exception, so wrap it in an AssertionError.
1121N/A // This should never happen, since all the Xref classes will get
1121N/A // a public static YYEOF field from JFlex.
1121N/A AssertionError ae = new AssertionError("Couldn't initialize yyeof");
1121N/A ae.initCause(e);
1121N/A throw ae; // NOPMD (stack trace is preserved by initCause(), but
1121N/A // PMD thinks it's lost)
1121N/A }
1121N/A }
1020N/A
1121N/A /**
1121N/A * Reinitialize the xref with new contents.
1121N/A *
1121N/A * @param contents a char buffer with text to analyze
1121N/A * @param length the number of characters to use from the char buffer
1121N/A */
1121N/A public void reInit(char[] contents, int length) {
1121N/A yyreset(new CharArrayReader(contents, 0, length));
1121N/A annotation = null;
1121N/A }
1121N/A
1121N/A public void setDefs(Definitions defs) {
1121N/A this.defs = defs;
1121N/A }
1121N/A
1121N/A protected void appendProject() throws IOException {
1121N/A if (project != null) {
1121N/A out.write("&project=");
1121N/A out.write(project.getDescription());
1121N/A }
1121N/A }
1121N/A
1121N/A protected String getProjectPostfix() {
1121N/A return project == null ? "" : ("&project=" + project.getDescription());
1121N/A }
1058N/A
1121N/A /** Get the next token from the scanner. */
1121N/A public abstract int yylex() throws IOException;
1121N/A
1121N/A /** Reset the scanner. */
1121N/A public abstract void yyreset(Reader reader);
1121N/A
1121N/A /** Get the value of {@code yyline}. */
1121N/A protected abstract int getLineNumber();
1121N/A
1121N/A /** Set the value of {@code yyline}. */
1121N/A protected abstract void setLineNumber(int x);
850N/A
1121N/A /**
1121N/A * Write xref to the specified {@code Writer}.
1121N/A *
1121N/A * @param out xref destination
1121N/A * @throws IOException on error when writing the xref
1121N/A */
1121N/A public void write(Writer out) throws IOException {
1121N/A this.out = out;
1145N/A writeSymbolTable();
1121N/A setLineNumber(0);
1121N/A startNewLine();
1121N/A while (yylex() != yyeof) { // NOPMD while statement intentionally empty
1121N/A // nothing to do here, yylex() will do the work
1121N/A }
1121N/A }
1020N/A
1121N/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
1145N/A Comparator<Tag> cmp = new Comparator<Tag>() {
1145N/A @Override
1145N/A public int compare(Tag tag1, Tag tag2) {
1145N/A // Order by symbol name, and then by line number if multiple
1145N/A // definitions use the same symbol name
1145N/A int ret = tag1.symbol.compareTo(tag2.symbol);
1145N/A if (ret == 0) {
1145N/A ret = tag1.line - tag2.line;
1145N/A }
1145N/A return ret;
1145N/A }
1145N/A };
1145N/A
1145N/A Map<String, SortedSet<Tag>> symbols =
1145N/A new HashMap<String, SortedSet<Tag>>();
1145N/A
1145N/A for (Tag tag : defs.getTags()) {
1259N/A Style style = getStyle(tag.type);
1259N/A if (style != null && style.title != null) {
1259N/A SortedSet<Tag> tags = symbols.get(style.name);
1145N/A if (tags == null) {
1145N/A tags = new TreeSet<Tag>(cmp);
1259N/A symbols.put(style.name, tags);
1145N/A }
1145N/A tags.add(tag);
1145N/A }
1145N/A }
1145N/A
1145N/A out.append("<script type=\"text/javascript\">/* <![CDATA[ */\n");
1354N/A out.append("O.symlist = [");
1145N/A
1145N/A boolean first = true;
1259N/A for (Style style : DEFINITION_STYLES) {
1259N/A SortedSet<Tag> tags = symbols.get(style.name);
1145N/A if (tags != null) {
1145N/A if (!first) {
1145N/A out.append(',');
1145N/A }
1145N/A out.append("[\"");
1259N/A out.append(style.title);
1145N/A out.append("\",\"");
1259N/A out.append(style.ssClass);
1145N/A out.append("\",[");
1145N/A
1145N/A boolean firstTag = true;
1145N/A for (Tag tag : tags) {
1145N/A if (!firstTag) {
1145N/A out.append(',');
1145N/A }
1145N/A out.append('[');
1145N/A out.append(Util.jsStringLiteral(tag.symbol));
1145N/A out.append(',');
1145N/A out.append(Integer.toString(tag.line));
1145N/A out.append(']');
1145N/A firstTag = false;
1145N/A }
1145N/A out.append("]]");
1145N/A first = false;
1145N/A }
1145N/A }
1185N/A /* no LF intentionally - xml is whitespace aware ... */
1354N/A out.append("]; /* ]]> */</script>");
1145N/A }
1145N/A
1145N/A /**
1145N/A * Get the style description for a definition type.
1145N/A *
1145N/A * @param type the definition type
1259N/A * @return the style of a definition type, or {@code null} if no style is
1259N/A * defined for the type
1145N/A * @see #DEFINITION_STYLES
1145N/A */
1259N/A private Style getStyle(String type) {
1259N/A for (Style style : DEFINITION_STYLES) {
1259N/A if (type.startsWith(style.name)) {
1145N/A return style;
1145N/A }
1145N/A }
1259N/A return null;
1145N/A }
1145N/A
1145N/A /**
1121N/A * Terminate the current line and insert preamble for the next line. The
1121N/A * line count will be incremented.
1121N/A *
1121N/A * @throws IOException on error when writing the xref
1121N/A */
1121N/A protected void startNewLine() throws IOException {
1121N/A int line = getLineNumber() + 1;
1121N/A setLineNumber(line);
1185N/A Util.readableLine(line, out, annotation, userPageLink, userPageSuffix);
1121N/A }
1020N/A
1121N/A /**
1121N/A * Write a symbol and generate links as appropriate.
1121N/A *
1121N/A * @param symbol the symbol to write
1121N/A * @param keywords a set of keywords recognized by this analyzer (no links
1121N/A * will be generated if the symbol is a keyword)
1121N/A * @param line the line number on which the symbol appears
1121N/A * @throws IOException if an error occurs while writing to the stream
1121N/A */
1121N/A protected void writeSymbol(String symbol, Set<String> keywords, int line)
1121N/A throws IOException {
1121N/A String[] strs = new String[1];
1121N/A strs[0] = "";
1020N/A
1121N/A if (keywords != null && keywords.contains(symbol)) {
1121N/A // This is a keyword, so we don't create a link.
1121N/A out.append("<b>").append(symbol).append("</b>");
1108N/A
1121N/A } else if (defs != null && defs.hasDefinitionAt(symbol, line, strs)) {
1121N/A // This is the definition of the symbol.
1121N/A String type = strs[0];
1121N/A String style_class = "d";
943N/A
1259N/A Style style = getStyle(type);
1145N/A if (style != null) {
1259N/A style_class = style.ssClass;
1121N/A }
1121N/A
1121N/A // 1) Create an anchor for direct links. (Perhaps we should only
1121N/A // do this when there's exactly one definition of the symbol in
1121N/A // this file? Otherwise, we may end up with multiple anchors with
1121N/A // the same name.)
1121N/A out.append("<a class=\"");
1121N/A out.append(style_class);
1121N/A out.append("\" name=\"");
1121N/A out.append(symbol);
1121N/A out.append("\"/>");
1108N/A
1121N/A // 2) Create a link that searches for all references to this symbol.
1121N/A out.append("<a href=\"");
1121N/A out.append(urlPrefix);
1121N/A out.append("refs=");
1121N/A out.append(symbol);
1121N/A appendProject();
1121N/A out.append("\" class=\"");
1121N/A out.append(style_class);
1121N/A out.append("\">");
1121N/A out.append(symbol);
1121N/A out.append("</a>");
1121N/A
1121N/A } else if (defs != null && defs.occurrences(symbol) == 1) {
1121N/A // This is a reference to a symbol defined exactly once in this file.
1121N/A String style_class = "d";
1121N/A
1121N/A // Generate a direct link to the symbol definition.
1121N/A out.append("<a class=\"");
1121N/A out.append(style_class);
1121N/A out.append("\" href=\"#");
1121N/A out.append(symbol);
1121N/A out.append("\">");
1121N/A out.append(symbol);
1121N/A out.append("</a>");
943N/A
1121N/A } else {
1121N/A // This is a symbol that is not defined in this file, or a symbol
1121N/A // that is defined more than once in this file. In either case, we
1121N/A // can't generate a direct link to the definition, so generate a
1121N/A // link to search for all definitions of that symbol instead.
1121N/A out.append("<a href=\"");
1121N/A out.append(urlPrefix);
1121N/A out.append("defs=");
1121N/A out.append(symbol);
1121N/A appendProject();
1121N/A out.append("\">");
1121N/A out.append(symbol);
1121N/A out.append("</a>");
1121N/A }
1121N/A }
943N/A
1121N/A /**
1121N/A * Write HTML escape sequence for the specified Unicode character, unless
1121N/A * it's an ISO control character, in which case it is ignored.
1121N/A *
1121N/A * @param c the character to write
1121N/A * @throws IOException if an error occurs while writing to the stream
1121N/A */
1121N/A protected void writeUnicodeChar(char c) throws IOException {
1121N/A if (!Character.isISOControl(c)) {
1185N/A out.append("&#").append(Integer.toString(c)).append(';');
1121N/A }
1121N/A }
1122N/A
1122N/A /**
1123N/A * Write an e-mail address. The address will be obfuscated if
1123N/A * {@link RuntimeEnvironment#isObfuscatingEMailAddresses()} returns
1123N/A * {@code true}.
1122N/A *
1122N/A * @param address the address to write
1122N/A * @throws IOException if an error occurs while writing to the stream
1122N/A */
1122N/A protected void writeEMailAddress(String address) throws IOException {
1123N/A if (RuntimeEnvironment.getInstance().isObfuscatingEMailAddresses()) {
1123N/A out.write(address.replace("@", " (at) "));
1123N/A } else {
1123N/A out.write(address);
1123N/A }
1122N/A }
850N/A}