JFlexXref.java revision 963
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/*
850N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
850N/A * Use is subject to license terms.
850N/A */
850N/A
850N/Apackage org.opensolaris.opengrok.analysis;
850N/A
850N/Aimport java.io.IOException;
850N/Aimport java.io.Writer;
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;
850N/A
850N/A/**
850N/A *
850N/A * @author Lubos Kosco
850N/A */
850N/Apublic class JFlexXref {
850N/A public Writer out;
850N/A public String urlPrefix = RuntimeEnvironment.getInstance().getUrlPrefix();
850N/A public Annotation annotation;
850N/A public Project project;
850N/A protected Definitions defs;
850N/A
850N/A public void setDefs(Definitions defs) {
850N/A this.defs = defs;
850N/A }
850N/A
850N/A protected void appendProject() throws IOException {
850N/A if (project != null) {
850N/A out.write("&project=");
853N/A out.write(project.getDescription());
850N/A }
850N/A }
850N/A
850N/A protected String getProjectPostfix() {
853N/A return project == null ? "" : ("&project=" + project.getDescription());
850N/A }
850N/A
943N/A /**
943N/A * Write a symbol and generate links as appropriate.
943N/A *
943N/A * @param symbol the symbol to write
943N/A * @param keywords a set of keywords recognized by this analyzer (no links
943N/A * will be generated if the symbol is a keyword)
943N/A * @param line the line number on which the symbol appears
943N/A * @throws IOException if an error occurs while writing to the stream
943N/A */
943N/A protected void writeSymbol(String symbol, Set<String> keywords, int line)
943N/A throws IOException {
943N/A if (keywords.contains(symbol)) {
943N/A // This is a keyword, so we don't create a link.
943N/A out.append("<b>").append(symbol).append("</b>");
943N/A
943N/A } else if (defs != null && defs.hasDefinitionAt(symbol, line)) {
943N/A // This is the definition of the symbol.
943N/A
943N/A // 1) Create an anchor for direct links. (Perhaps, we should only
943N/A // do this when there's exactly one definition of the symbol in
943N/A // this file? Otherwise, we may end up with multiple anchors with
943N/A // the same name.)
943N/A out.append("<a class=\"d\" name=\"").append(symbol).append("\"/>");
943N/A
943N/A // 2) Create a link that searches for all references to this symbol.
943N/A out.append("<a href=\"").append(urlPrefix).append("refs=");
943N/A out.append(symbol);
943N/A appendProject();
943N/A out.append("\" class=\"d\">").append(symbol).append("</a>");
943N/A
944N/A } else if (defs != null && defs.occurrences(symbol) == 1) {
943N/A // This is a reference to a symbol defined exactly once in this file.
943N/A
943N/A // Generate a direct link to the symbol definition.
943N/A out.append("<a class=\"f\" href=\"#").append(symbol).append("\">")
943N/A .append(symbol).append("</a>");
943N/A
943N/A } else {
944N/A // This is a symbol that is not defined in this file, or a symbol
944N/A // that is defined more than once in this file. In either case, we
944N/A // can't generate a direct link to the definition, so generate a
944N/A // link to search for all definitions of that symbol instead.
944N/A out.append("<a href=\"").append(urlPrefix).append("defs=");
944N/A out.append(symbol);
944N/A appendProject();
944N/A out.append("\">").append(symbol).append("</a>");
943N/A }
943N/A }
943N/A
963N/A /**
963N/A * Write HTML escape sequence for the specified Unicode character, unless
963N/A * it's an ISO control character, in which case it is ignored.
963N/A *
963N/A * @param c the character to write
963N/A * @throws IOException if an error occurs while writing to the stream
963N/A */
963N/A protected void writeUnicodeChar(char c) throws IOException {
963N/A if (!Character.isISOControl(c)) {
963N/A out.append("&#").append(Integer.toString((int) c)).append(';');
963N/A }
963N/A }
850N/A}