1
2
3
4 package net.sourceforge.pmd.lang.java.ast;
5
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Collections;
9 import java.util.HashMap;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.regex.Matcher;
13 import java.util.regex.Pattern;
14
15 import net.sourceforge.pmd.util.StringUtil;
16
17 public final class CommentUtil {
18
19 private CommentUtil() {}
20
21 private static final String CR = "\n";
22
23 public static String wordAfter(String text, int position) {
24
25 if (position >= text.length()) return null;
26
27 int end = ++position;
28 char ch = text.charAt(end);
29
30 while (Character.isLetterOrDigit(ch) && end < text.length()) {
31 ch = text.charAt(++end);
32 }
33
34 return text.substring(position, end);
35 }
36
37 public static String javadocContentAfter(String text, int position) {
38
39 int endPos = text.indexOf('\n', position);
40 if (endPos < 0) return null;
41
42 if (StringUtil.isNotEmpty(text.substring(position, endPos))) {
43 return text.substring(position, endPos).trim();
44 }
45
46 if (text.indexOf('@', endPos) >= 0) return null;
47
48
49 int nextEndPos = text.indexOf('\n', endPos + 1);
50
51 if (StringUtil.isNotEmpty(text.substring(endPos, nextEndPos))) {
52 return text.substring(endPos, nextEndPos).trim();
53 }
54
55 return null;
56 }
57
58 private static Pattern JAVADOC_TAG = Pattern.compile("@[A-Za-z0-9]+");
59 private static Map<String, String> JAVADOC_CACHE = new HashMap<String, String>();
60
61 public static Map<String, Integer> javadocTagsIn(String comment) {
62 Matcher m = JAVADOC_TAG.matcher(comment);
63 Map<String, Integer> tags = null;
64 while (m.find()) {
65 if (tags == null ) { tags = new HashMap<String, Integer>(); }
66 String match = comment.substring(m.start() + 1, m.end());
67 String tag = JAVADOC_CACHE.get(match);
68 if (tag == null) {
69 JAVADOC_CACHE.put(match, match);
70 }
71 tags.put(tag, m.start());
72 }
73 if ( tags == null ) {
74 return Collections.emptyMap();
75 }
76 return tags;
77 }
78
79 public static List<String> multiLinesIn(String comment) {
80
81 String[] lines = comment.split(CR);
82 List<String> filteredLines = new ArrayList<String>(lines.length);
83
84 for (String rawLine : lines) {
85 String line = rawLine.trim();
86
87 if (line.startsWith("//")) {
88 filteredLines.add(line.substring(2));
89 continue;
90 }
91
92 if (line.endsWith("*/")) {
93 int end = line.length()-2;
94 int start = line.startsWith("/**") ? 3 : line.startsWith("/*") ? 2 : 0;
95 filteredLines.add(line.substring(start, end));
96 continue;
97 }
98
99 if (line.charAt(0) == '*') {
100 filteredLines.add(line.substring(1));
101 continue;
102 }
103
104 if (line.startsWith("/**")) {
105 filteredLines.add(line.substring(3));
106 continue;
107 }
108
109 if (line.startsWith("/*")) {
110 filteredLines.add(line.substring(2));
111 continue;
112 }
113
114 filteredLines.add(line);
115 }
116
117 return filteredLines;
118 }
119
120
121
122
123
124
125
126 public static List<String> trim(List<String> lines) {
127
128 int firstNonEmpty = 0;
129 for (; firstNonEmpty<lines.size(); firstNonEmpty++) {
130 if (StringUtil.isNotEmpty(lines.get(firstNonEmpty))) break;
131 }
132
133
134 if (firstNonEmpty == lines.size()) return Collections.emptyList();
135
136 int lastNonEmpty = lines.size() - 1;
137 for (; lastNonEmpty>0; lastNonEmpty--) {
138 if (StringUtil.isNotEmpty(lines.get(lastNonEmpty))) break;
139 }
140
141 List<String> filtered = new ArrayList<String>();
142 for (int i=firstNonEmpty; i<lastNonEmpty; i++) {
143 filtered.add( lines.get(i) );
144 }
145
146 return filtered;
147 }
148
149 public static void main(String[] args) {
150
151 Collection<String> tags = javadocTagsIn(args[0]).keySet();
152
153 for (String tag : tags) {
154 System.out.println( tag );
155 }
156 }
157 }