View Javadoc

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