001: /*
002: * Copyright 2002-2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.doclets.internal.toolkit.util;
027:
028: import com.sun.tools.doclets.internal.toolkit.*;
029: import com.sun.javadoc.*;
030: import java.util.*;
031:
032: /**
033: * Provides methods for creating an array of class, method and
034: * field names to be included as meta keywords in the HTML header
035: * of class pages. These keywords improve search results
036: * on browsers that look for keywords.
037: *
038: * This code is not part of an API.
039: * It is implementation that is subject to change.
040: * Do not use it as an API
041: *
042: * @author Doug Kramer
043: */
044: public class MetaKeywords {
045:
046: private static MetaKeywords instance = null;
047:
048: /**
049: * The global configuration information for this run.
050: */
051: private final Configuration configuration;
052:
053: /**
054: * Constructor
055: */
056: private MetaKeywords(Configuration configuration) {
057: this .configuration = configuration;
058: }
059:
060: /**
061: * Return an instance of MetaKeywords. This class is a singleton.
062: *
063: * @param configuration the current configuration of the doclet.
064: */
065: public static MetaKeywords getInstance(Configuration configuration) {
066: if (instance == null) {
067: instance = new MetaKeywords(configuration);
068: }
069: return instance;
070: }
071:
072: /**
073: * Returns an array of strings where each element
074: * is a class, method or field name. This array is
075: * used to create one meta keyword tag for each element.
076: * Method parameter lists are converted to "()" and
077: * overloads are combined.
078: *
079: * Constructors are not included because they have the same
080: * name as the class, which is already included.
081: * Nested class members are not included because their
082: * definitions are on separate pages.
083: */
084: public String[] getMetaKeywords(ClassDoc classdoc) {
085: ArrayList results = new ArrayList();
086:
087: // Add field and method keywords only if -keywords option is used
088: if (configuration.keywords) {
089: results.addAll(getClassKeyword(classdoc));
090: results.addAll(getMemberKeywords(classdoc.fields()));
091: results.addAll(getMemberKeywords(classdoc.methods()));
092: }
093: return (String[]) results.toArray(new String[] {});
094: }
095:
096: /**
097: * Get the current class for a meta tag keyword, as the first
098: * and only element of an array list.
099: */
100: protected ArrayList getClassKeyword(ClassDoc classdoc) {
101: String cltypelower = classdoc.isInterface() ? "interface"
102: : "class";
103: ArrayList metakeywords = new ArrayList(1);
104: metakeywords.add(classdoc.qualifiedName() + " " + cltypelower);
105: return metakeywords;
106: }
107:
108: /**
109: * Get the package keywords.
110: */
111: public String[] getMetaKeywords(PackageDoc packageDoc) {
112: if (configuration.keywords) {
113: String pkgName = Util.getPackageName(packageDoc);
114: return new String[] { pkgName + " " + "package" };
115: } else {
116: return new String[] {};
117: }
118: }
119:
120: /**
121: * Get the overview keywords.
122: */
123: public String[] getOverviewMetaKeywords(String title,
124: String docTitle) {
125: if (configuration.keywords) {
126: String windowOverview = configuration.getText(title);
127: String[] metakeywords = { windowOverview };
128: if (docTitle.length() > 0) {
129: metakeywords[0] += ", " + docTitle;
130: }
131: return metakeywords;
132: } else {
133: return new String[] {};
134: }
135: }
136:
137: /**
138: * Get members for meta tag keywords as an array,
139: * where each member name is a string element of the array.
140: * The parameter lists are not included in the keywords;
141: * therefore all overloaded methods are combined.<br>
142: * Example: getValue(Object) is returned in array as getValue()
143: *
144: * @param memberdocs array of MemberDoc objects to be added to keywords
145: */
146: protected ArrayList getMemberKeywords(MemberDoc[] memberdocs) {
147: ArrayList results = new ArrayList();
148: String membername;
149: for (int i = 0; i < memberdocs.length; i++) {
150: membername = memberdocs[i].name()
151: + (memberdocs[i].isMethod() ? "()" : "");
152: if (!results.contains(membername)) {
153: results.add(membername);
154: }
155: }
156: return results;
157: }
158: }
|