1 /* gnu.classpath.tools.taglets.AuthorTaglet
\r
2 Copyright (C) 2001 Free Software Foundation, Inc.
\r
4 This file is part of GNU Classpath.
\r
6 GNU Classpath is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2, or (at your option)
\r
11 GNU Classpath is distributed in the hope that it will be useful, but
\r
12 WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
14 General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with GNU Classpath; see the file COPYING. If not, write to the
\r
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
\r
21 package gnu.classpath.tools.taglets;
\r
23 import java.util.Map;
\r
25 import java.util.regex.Pattern;
\r
26 import java.util.regex.Matcher;
\r
28 import com.sun.tools.doclets.Taglet;
\r
30 import com.sun.javadoc.Tag;
\r
33 * The default Taglet which handles Author information.
\r
35 * @author Julian Scheid (julian@sektor37.de)
\r
37 public class AuthorTaglet implements Taglet {
\r
40 * Enum class which denotes whether and how to replace email
\r
41 * addresses in author tags.
\r
43 public static class EmailReplacement {
\r
44 private EmailReplacement() {}
\r
47 * Specifies that email addresses should not be replaced.
\r
49 public static final EmailReplacement NO_REPLACEMENT = new EmailReplacement();
\r
52 * Specifies that author tag text matching "Real Name
\r
53 * (user@domain.tld)" is converted to "<a
\r
54 * href="mailto:user@domain.tld">Real Name</a>.
\r
56 public static final EmailReplacement MAILTO_NAME = new EmailReplacement();
\r
59 * Specifies that author tag text matching "Real Name
\r
60 * (user@domain.tld)" is converted to "Real Name (<a
\r
61 * href="mailto:user@domain.tld">user@domain.tld</a>).
\r
63 public static final EmailReplacement NAME_MAILTO_ADDRESS = new EmailReplacement();
\r
66 * Specifies that author tag text matching "Real Name
\r
67 * (user@domain.tld)" is converted to "Real Name (user AT
\r
68 * domain DOT tld)", where the "AT" and "DOT" replacement are
\r
69 * specified by AuthorTaglet.emailAtReplacement and
\r
70 * AuthorTaglet.emailDotReplacement.
\r
72 public static final EmailReplacement NAME_MANGLED_ADDRESS = new EmailReplacement();
\r
75 private static EmailReplacement emailReplacementType = EmailReplacement.NO_REPLACEMENT;
\r
76 private static String atReplacement = " <b>at</b> ";
\r
77 private static String dotReplacement = " <b>dot</b> ";
\r
79 private static final String NAME = "author";
\r
80 private static final String SINGLE_HEADER = "Author:";
\r
81 private static final String MULTI_HEADER = "Authors:";
\r
83 private static boolean enabled = true;
\r
86 * Matches <code>.</code> (dot).
\r
88 private static final Pattern dotPattern = Pattern.compile("[.]");
\r
91 * Matches <code>@</code> (at sign).
\r
93 private static final Pattern atPattern = Pattern.compile("[@]");
\r
96 * Matches <code>Real Name (user@domain.tld)</code>.
\r
98 private static final Pattern authorEmailPattern
\r
99 = Pattern.compile("^"
\r
100 + "\\s*" // optional whitespace
\r
101 + "(" // group #1 start (real name)
\r
102 + "(?:[^\t\r\n ]|\\()+" // first name
\r
103 + "(?:\\s+(?:[^\t\r\n ]|\\()+)*" // additional names
\r
104 + ")" // group #1 end
\r
105 + "\\s*" // optional whitespace
\r
106 + "[(<]" // opening paren
\r
107 + "\\s*" // optional whitespace
\r
108 + "(" // group #2 start (email address)
\r
109 + "(" // group #3 start (email user)
\r
110 + "[A-z0-9_\\-\\.]+" // username
\r
111 + ")" // group #3 end
\r
113 + "[A-z0-9_\\-]+(?:[.][A-z0-9_\\-]+)+[A-z]" // domain
\r
114 + ")" // group #2 end
\r
115 + "\\s*" // optional whitespace
\r
116 + "(?:\\)|>)" // closing paren
\r
119 public String getName() {
\r
123 public boolean inField() {
\r
127 public boolean inConstructor() {
\r
131 public boolean inMethod() {
\r
135 public boolean inOverview() {
\r
139 public boolean inPackage() {
\r
143 public boolean inType() {
\r
147 public boolean isInlineTag() {
\r
151 public static void register(Map tagletMap) {
\r
152 AuthorTaglet authorTaglet = new AuthorTaglet();
\r
153 tagletMap.put(authorTaglet.getName(), authorTaglet);
\r
156 public String toString(Tag tag) {
\r
158 return toString(new Tag[] { tag });
\r
165 public String toString(Tag[] tags) {
\r
166 if (!enabled || tags.length == 0) {
\r
170 boolean haveValidTag = false;
\r
171 for (int i = 0; i < tags.length && !haveValidTag; ++i) {
\r
172 if (tags[i].text().length() > 0) {
\r
173 haveValidTag = true;
\r
177 if (haveValidTag) {
\r
178 StringBuffer result = new StringBuffer();
\r
179 result.append("<dl class=\"tag list\">");
\r
180 result.append("<dt class=\"tag section header\"><b>");
\r
181 if (tags.length == 1) {
\r
182 result.append(SINGLE_HEADER);
\r
185 result.append(MULTI_HEADER);
\r
187 result.append("</b></dt>");
\r
188 for (int i = 0; i < tags.length; i++) {
\r
189 result.append("<dd class=\"tag item\">");
\r
190 result.append(replaceEmail(tags[i].text()));
\r
191 result.append("</dd>");
\r
193 result.append("</dl>");
\r
194 return result.toString();
\r
203 * Reformat the tag text according to {@link #emailReplacementType}.
\r
205 private String replaceEmail(String text) {
\r
207 if (EmailReplacement.NO_REPLACEMENT == emailReplacementType) {
\r
211 Matcher matcher = authorEmailPattern.matcher(text);
\r
212 if (matcher.matches()) {
\r
213 String realName = matcher.group(1);
\r
214 String emailAddress = matcher.group(2);
\r
215 if (EmailReplacement.MAILTO_NAME == emailReplacementType) {
\r
216 return "<a href=\"mailto:" + emailAddress + "\">" + realName + "</a>";
\r
218 else if (EmailReplacement.NAME_MAILTO_ADDRESS == emailReplacementType) {
\r
219 return realName + " (<a href=\"mailto:" + emailAddress + "\">" + emailAddress + "</a>)";
\r
221 else if (EmailReplacement.NAME_MANGLED_ADDRESS == emailReplacementType) {
\r
222 Matcher dotMatcher = dotPattern.matcher(emailAddress);
\r
223 Matcher atMatcher = atPattern.matcher(dotMatcher.replaceAll(dotReplacement));
\r
224 String mangledAddress = atMatcher.replaceAll(atReplacement);
\r
225 return realName + " (" + mangledAddress + ")";
\r
228 // this shouldn't happen
\r
239 * Set the email replacement type.
\r
241 public static void setEmailReplacementType(EmailReplacement emailReplacementType)
\r
243 if (null == emailReplacementType) {
\r
244 throw new NullPointerException();
\r
246 AuthorTaglet.emailReplacementType = emailReplacementType;
\r
250 * Set the HTML text by which the <code>@</code> (at sign) in email
\r
251 * addresses should be replaced if the email replacement type is
\r
252 * <code>NAME_MANGLED_ADDRESS</code>.
\r
254 public static void setAtReplacement(String atReplacement)
\r
256 AuthorTaglet.atReplacement = atReplacement;
\r
260 * Set the HTML text by which the <code>.</code> (dot) in email
\r
261 * addresses should be replaced if the email replacement type is
\r
262 * <code>NAME_MANGLED_ADDRESS</code>.
\r
264 public static void setDotReplacement(String dotReplacement)
\r
266 AuthorTaglet.dotReplacement = dotReplacement;
\r
270 * Enables/disables this taglet.
\r
272 public static void setTagletEnabled(boolean enabled)
\r
274 AuthorTaglet.enabled = enabled;
\r