JFlexXref.java revision 1121
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, 2010, Oracle and/or its affiliates. All rights reserved.
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;
943N/Aimport java.util.Set;
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;
1020N/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);
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;
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 /**
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);
1121N/A Util.readableLine(line, out, annotation);
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
1121N/A if (type.startsWith("macro")) {
1121N/A style_class = "xm";
1121N/A } else if (type.startsWith("argument")) {
1121N/A style_class = "xa";
1121N/A } else if (type.startsWith("local")) {
1121N/A style_class = "xl";
1121N/A } else if (type.startsWith("variable")) {
1121N/A style_class = "xv";
1121N/A } else if (type.startsWith("class")) {
1121N/A style_class = "xc";
1121N/A } else if (type.startsWith("interface")) {
1121N/A style_class = "xi";
1121N/A } else if (type.startsWith("namespace")) {
1121N/A style_class = "xn";
1121N/A } else if (type.startsWith("enum")) {
1121N/A style_class = "xe";
1121N/A } else if (type.startsWith("enumerator")) {
1121N/A style_class = "xer";
1121N/A } else if (type.startsWith("struct")) {
1121N/A style_class = "xs";
1121N/A } else if (type.startsWith("typedef")) {
1121N/A style_class = "xt";
1121N/A } else if (type.startsWith("typedefs")) {
1121N/A style_class = "xts";
1121N/A } else if (type.startsWith("union")) {
1121N/A style_class = "xu";
1121N/A } else if (type.startsWith("field")) {
1121N/A style_class = "xfld";
1121N/A } else if (type.startsWith("member")) {
1121N/A style_class = "xmb";
1121N/A } else if (type.startsWith("function")) {
1121N/A style_class = "xf";
1121N/A } else if (type.startsWith("method")) {
1121N/A style_class = "xmt";
1121N/A } else if (type.startsWith("subroutine")) {
1121N/A style_class = "xsr";
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 // May have multiple anchors with the same function name,
1121N/A // store line number for accurate location used in list.jsp.
1121N/A out.append("ln=\"");
1121N/A out.append(Integer.toString(line));
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)) {
1121N/A out.append("&#").append(Integer.toString((int) c)).append(';');
1121N/A }
1121N/A }
850N/A}