/
CDKGitTaglet.java
127 lines (109 loc) · 3.54 KB
/
CDKGitTaglet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* $Revision: 7973 $ $Author: egonw $ $Date: 2007-02-19 13:16:03 +0100 (Mon, 19 Feb 2007) $
*
* Copyright (C) 2007 Egon Willighagen <egonw@users.sf.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.sf.cdk.tools.doclets;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sun.javadoc.SourcePosition;
import com.sun.javadoc.Tag;
import com.sun.tools.doclets.Taglet;
/**
* Source for the cdk.githash JavaDoc tag. When a class is tagged with this
* tag, the JavaDoc will contain a link to the source code in the Git repository.
*
* <p>The syntax must be as follows:
* <pre>
* @cdk.githash
* </pre>
*/
public class CDKGitTaglet implements Taglet {
private static final String NAME = "cdk.githash";
private final static Pattern pathPattern = Pattern.compile(".*/(src/.*\\.java)");
public String getName() {
return NAME;
}
public boolean inField() {
return false;
}
public boolean inConstructor() {
return false;
}
public boolean inMethod() {
return false;
}
public boolean inOverview() {
return false;
}
public boolean inPackage() {
return false;
}
public boolean inType() {
return true;
}
public boolean isInlineTag() {
return false;
}
public static void register(Map<String, CDKGitTaglet> tagletMap) {
CDKGitTaglet tag = new CDKGitTaglet();
Taglet t = (Taglet) tagletMap.get(tag.getName());
if (t != null) {
tagletMap.remove(tag.getName());
}
tagletMap.put(tag.getName(), tag);
}
public String toString(Tag tag) {
return "<DT><B>Source code: </B><DD>"
+ expand(tag) + "</DD>\n";
}
public String toString(Tag[] tags) {
if (tags.length == 0) {
return null;
} else {
return toString(tags[0]);
}
}
private String expand(Tag tag) {
// get the version number
String text = tag.text();
// create the URL
SourcePosition file = tag.position();
String path = correctSlashes(file.file().getAbsolutePath());
Matcher matcher = pathPattern.matcher(path);
if (matcher.matches()) {
String url = "http://cdk.git.sourceforge.net/git/gitweb.cgi?p=cdk;a=blob;f=" +
matcher.group(1) + ";hb=HEAD";
return "<a href=\"" + url + "\" target=\"_blank\">HEAD</a>";
} else {
System.out.println("Could not resolve class name from: " + path);
}
return "";
}
private String correctSlashes(String absolutePath) {
StringBuffer buffer = new StringBuffer();
for (int i=0; i<absolutePath.length(); i++) {
char character = absolutePath.charAt(i);
if (character == '\\') {
buffer.append('/');
} else {
buffer.append(character);
}
}
return buffer.toString();
}
}