1 |
779 |
jeremybenn |
/* gnu.classpath.tools.taglets.AuthorTaglet
|
2 |
|
|
Copyright (C) 2001 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
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)
|
9 |
|
|
any later version.
|
10 |
|
|
|
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.
|
15 |
|
|
|
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
|
19 |
|
|
02111-1307 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package gnu.classpath.tools.taglets;
|
39 |
|
|
|
40 |
|
|
import java.util.Map;
|
41 |
|
|
|
42 |
|
|
import java.util.regex.Pattern;
|
43 |
|
|
import java.util.regex.Matcher;
|
44 |
|
|
|
45 |
|
|
import com.sun.tools.doclets.Taglet;
|
46 |
|
|
|
47 |
|
|
import com.sun.javadoc.Tag;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* The default Taglet which handles Author information.
|
51 |
|
|
*
|
52 |
|
|
* @author Julian Scheid (julian@sektor37.de)
|
53 |
|
|
*/
|
54 |
|
|
public class AuthorTaglet implements Taglet {
|
55 |
|
|
|
56 |
|
|
/**
|
57 |
|
|
* Enum class which denotes whether and how to replace email
|
58 |
|
|
* addresses in author tags.
|
59 |
|
|
*/
|
60 |
|
|
public static class EmailReplacement {
|
61 |
|
|
private EmailReplacement() {}
|
62 |
|
|
|
63 |
|
|
/**
|
64 |
|
|
* Specifies that email addresses should not be replaced.
|
65 |
|
|
*/
|
66 |
|
|
public static final EmailReplacement NO_REPLACEMENT = new EmailReplacement();
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
|
|
* Specifies that author tag text matching "Real Name
|
70 |
|
|
* (user@domain.tld)" is converted to "<a
|
71 |
|
|
* href="mailto:user@domain.tld">Real Name</a>.
|
72 |
|
|
*/
|
73 |
|
|
public static final EmailReplacement MAILTO_NAME = new EmailReplacement();
|
74 |
|
|
|
75 |
|
|
/**
|
76 |
|
|
* Specifies that author tag text matching "Real Name
|
77 |
|
|
* (user@domain.tld)" is converted to "Real Name (<a
|
78 |
|
|
* href="mailto:user@domain.tld">user@domain.tld</a>).
|
79 |
|
|
*/
|
80 |
|
|
public static final EmailReplacement NAME_MAILTO_ADDRESS = new EmailReplacement();
|
81 |
|
|
|
82 |
|
|
/**
|
83 |
|
|
* Specifies that author tag text matching "Real Name
|
84 |
|
|
* (user@domain.tld)" is converted to "Real Name (user AT
|
85 |
|
|
* domain DOT tld)", where the "AT" and "DOT" replacement are
|
86 |
|
|
* specified by AuthorTaglet.emailAtReplacement and
|
87 |
|
|
* AuthorTaglet.emailDotReplacement.
|
88 |
|
|
*/
|
89 |
|
|
public static final EmailReplacement NAME_MANGLED_ADDRESS = new EmailReplacement();
|
90 |
|
|
}
|
91 |
|
|
|
92 |
|
|
private static EmailReplacement emailReplacementType = EmailReplacement.NO_REPLACEMENT;
|
93 |
|
|
private static String atReplacement = " <b>at</b> ";
|
94 |
|
|
private static String dotReplacement = " <b>dot</b> ";
|
95 |
|
|
|
96 |
|
|
private static final String NAME = "author";
|
97 |
|
|
private static final String SINGLE_HEADER = "Author:";
|
98 |
|
|
private static final String MULTI_HEADER = "Authors:";
|
99 |
|
|
|
100 |
|
|
private static boolean enabled = true;
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* Matches <code>.</code> (dot).
|
104 |
|
|
*/
|
105 |
|
|
private static final Pattern dotPattern = Pattern.compile("[.]");
|
106 |
|
|
|
107 |
|
|
/**
|
108 |
|
|
* Matches <code>@</code> (at sign).
|
109 |
|
|
*/
|
110 |
|
|
private static final Pattern atPattern = Pattern.compile("[@]");
|
111 |
|
|
|
112 |
|
|
/**
|
113 |
|
|
* Matches <code>Real Name (user@domain.tld)</code>.
|
114 |
|
|
*/
|
115 |
|
|
private static final Pattern authorEmailPattern
|
116 |
|
|
= Pattern.compile("^"
|
117 |
|
|
+ "\\s*" // optional whitespace
|
118 |
|
|
+ "(" // group #1 start (real name)
|
119 |
|
|
+ "(?:[^\t\r\n ]|\\()+" // first name
|
120 |
|
|
+ "(?:\\s+(?:[^\t\r\n ]|\\()+)*" // additional names
|
121 |
|
|
+ ")" // group #1 end
|
122 |
|
|
+ "\\s*" // optional whitespace
|
123 |
|
|
+ "[(<]" // opening paren
|
124 |
|
|
+ "\\s*" // optional whitespace
|
125 |
|
|
+ "(" // group #2 start (email address)
|
126 |
|
|
+ "(" // group #3 start (email user)
|
127 |
|
|
+ "[A-z0-9_\\-\\.]+" // username
|
128 |
|
|
+ ")" // group #3 end
|
129 |
|
|
+ "[@]" // at sign
|
130 |
|
|
+ "[A-z0-9_\\-]+(?:[.][A-z0-9_\\-]+)+[A-z]" // domain
|
131 |
|
|
+ ")" // group #2 end
|
132 |
|
|
+ "\\s*" // optional whitespace
|
133 |
|
|
+ "(?:\\)|>)" // closing paren
|
134 |
|
|
+ "$");
|
135 |
|
|
|
136 |
|
|
public String getName() {
|
137 |
|
|
return NAME;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
public boolean inField() {
|
141 |
|
|
return true;
|
142 |
|
|
}
|
143 |
|
|
|
144 |
|
|
public boolean inConstructor() {
|
145 |
|
|
return true;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
public boolean inMethod() {
|
149 |
|
|
return true;
|
150 |
|
|
}
|
151 |
|
|
|
152 |
|
|
public boolean inOverview() {
|
153 |
|
|
return true;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
public boolean inPackage() {
|
157 |
|
|
return true;
|
158 |
|
|
}
|
159 |
|
|
|
160 |
|
|
public boolean inType() {
|
161 |
|
|
return true;
|
162 |
|
|
}
|
163 |
|
|
|
164 |
|
|
public boolean isInlineTag() {
|
165 |
|
|
return false;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
public static void register(Map tagletMap) {
|
169 |
|
|
AuthorTaglet authorTaglet = new AuthorTaglet();
|
170 |
|
|
tagletMap.put(authorTaglet.getName(), authorTaglet);
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
public String toString(Tag tag) {
|
174 |
|
|
if (enabled) {
|
175 |
|
|
return toString(new Tag[] { tag });
|
176 |
|
|
}
|
177 |
|
|
else {
|
178 |
|
|
return null;
|
179 |
|
|
}
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
public String toString(Tag[] tags) {
|
183 |
|
|
if (!enabled || tags.length == 0) {
|
184 |
|
|
return null;
|
185 |
|
|
}
|
186 |
|
|
else {
|
187 |
|
|
boolean haveValidTag = false;
|
188 |
|
|
for (int i = 0; i < tags.length && !haveValidTag; ++i) {
|
189 |
|
|
if (tags[i].text().length() > 0) {
|
190 |
|
|
haveValidTag = true;
|
191 |
|
|
}
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
if (haveValidTag) {
|
195 |
|
|
StringBuffer result = new StringBuffer();
|
196 |
|
|
result.append("<dl class=\"tag list\">");
|
197 |
|
|
result.append("<dt class=\"tag section header\"><b>");
|
198 |
|
|
if (tags.length == 1) {
|
199 |
|
|
result.append(SINGLE_HEADER);
|
200 |
|
|
}
|
201 |
|
|
else {
|
202 |
|
|
result.append(MULTI_HEADER);
|
203 |
|
|
}
|
204 |
|
|
result.append("</b></dt>");
|
205 |
|
|
for (int i = 0; i < tags.length; i++) {
|
206 |
|
|
result.append("<dd class=\"tag item\">");
|
207 |
|
|
result.append(replaceEmail(tags[i].text()));
|
208 |
|
|
result.append("</dd>");
|
209 |
|
|
}
|
210 |
|
|
result.append("</dl>");
|
211 |
|
|
return result.toString();
|
212 |
|
|
}
|
213 |
|
|
else {
|
214 |
|
|
return null;
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* Reformat the tag text according to {@link #emailReplacementType}.
|
221 |
|
|
*/
|
222 |
|
|
private String replaceEmail(String text) {
|
223 |
|
|
|
224 |
|
|
if (EmailReplacement.NO_REPLACEMENT == emailReplacementType) {
|
225 |
|
|
return text;
|
226 |
|
|
}
|
227 |
|
|
else {
|
228 |
|
|
Matcher matcher = authorEmailPattern.matcher(text);
|
229 |
|
|
if (matcher.matches()) {
|
230 |
|
|
String realName = matcher.group(1);
|
231 |
|
|
String emailAddress = matcher.group(2);
|
232 |
|
|
if (EmailReplacement.MAILTO_NAME == emailReplacementType) {
|
233 |
|
|
return "<a href=\"mailto:" + emailAddress + "\">" + realName + "</a>";
|
234 |
|
|
}
|
235 |
|
|
else if (EmailReplacement.NAME_MAILTO_ADDRESS == emailReplacementType) {
|
236 |
|
|
return realName + " (<a href=\"mailto:" + emailAddress + "\">" + emailAddress + "</a>)";
|
237 |
|
|
}
|
238 |
|
|
else if (EmailReplacement.NAME_MANGLED_ADDRESS == emailReplacementType) {
|
239 |
|
|
Matcher dotMatcher = dotPattern.matcher(emailAddress);
|
240 |
|
|
Matcher atMatcher = atPattern.matcher(dotMatcher.replaceAll(dotReplacement));
|
241 |
|
|
String mangledAddress = atMatcher.replaceAll(atReplacement);
|
242 |
|
|
return realName + " (" + mangledAddress + ")";
|
243 |
|
|
}
|
244 |
|
|
else {
|
245 |
|
|
// this shouldn't happen
|
246 |
|
|
return text;
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
else {
|
250 |
|
|
return text;
|
251 |
|
|
}
|
252 |
|
|
}
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
/**
|
256 |
|
|
* Set the email replacement type.
|
257 |
|
|
*/
|
258 |
|
|
public static void setEmailReplacementType(EmailReplacement emailReplacementType)
|
259 |
|
|
{
|
260 |
|
|
if (null == emailReplacementType) {
|
261 |
|
|
throw new NullPointerException();
|
262 |
|
|
}
|
263 |
|
|
AuthorTaglet.emailReplacementType = emailReplacementType;
|
264 |
|
|
}
|
265 |
|
|
|
266 |
|
|
/**
|
267 |
|
|
* Set the HTML text by which the <code>@</code> (at sign) in email
|
268 |
|
|
* addresses should be replaced if the email replacement type is
|
269 |
|
|
* <code>NAME_MANGLED_ADDRESS</code>.
|
270 |
|
|
*/
|
271 |
|
|
public static void setAtReplacement(String atReplacement)
|
272 |
|
|
{
|
273 |
|
|
AuthorTaglet.atReplacement = atReplacement;
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
/**
|
277 |
|
|
* Set the HTML text by which the <code>.</code> (dot) in email
|
278 |
|
|
* addresses should be replaced if the email replacement type is
|
279 |
|
|
* <code>NAME_MANGLED_ADDRESS</code>.
|
280 |
|
|
*/
|
281 |
|
|
public static void setDotReplacement(String dotReplacement)
|
282 |
|
|
{
|
283 |
|
|
AuthorTaglet.dotReplacement = dotReplacement;
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
/**
|
287 |
|
|
* Enables/disables this taglet.
|
288 |
|
|
*/
|
289 |
|
|
public static void setTagletEnabled(boolean enabled)
|
290 |
|
|
{
|
291 |
|
|
AuthorTaglet.enabled = enabled;
|
292 |
|
|
}
|
293 |
|
|
}
|