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