1 | package elements;
|
---|
2 |
|
---|
3 | import java.util.LinkedList;
|
---|
4 | import java.util.regex.Matcher;
|
---|
5 | import java.util.regex.Pattern;
|
---|
6 | import org.apache.commons.lang.*;
|
---|
7 | import org.etsi.t3d.T3D;
|
---|
8 |
|
---|
9 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST;
|
---|
10 | import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes;
|
---|
11 |
|
---|
12 | public class TTCN3Comment {
|
---|
13 | private String commentsXML;
|
---|
14 |
|
---|
15 |
|
---|
16 |
|
---|
17 | private LinkedList<String> desc = new LinkedList<String>();
|
---|
18 | private LinkedList<String> author = new LinkedList<String>();
|
---|
19 | private LinkedList<String> config = new LinkedList<String>();
|
---|
20 | private LinkedList<String> exception = new LinkedList<String>();
|
---|
21 | private LinkedList<String> member = new LinkedList<String>();
|
---|
22 | private LinkedList<String> param = new LinkedList<String>();
|
---|
23 | private LinkedList<String> purpose = new LinkedList<String>();
|
---|
24 | private LinkedList<String> remark = new LinkedList<String>();
|
---|
25 | private LinkedList<String> Return = new LinkedList<String>();
|
---|
26 | private LinkedList<String> see = new LinkedList<String>();
|
---|
27 | private LinkedList<String> since = new LinkedList<String>();
|
---|
28 | private LinkedList<String> status = new LinkedList<String>();
|
---|
29 | private LinkedList<String> url = new LinkedList<String>();
|
---|
30 | private LinkedList<String> verdict = new LinkedList<String>();
|
---|
31 | private LinkedList<String> version = new LinkedList<String>();
|
---|
32 |
|
---|
33 | private LinkedList<String> warnings = new LinkedList<String>();
|
---|
34 | private LinkedList<String> seeRefs = new LinkedList<String>();
|
---|
35 |
|
---|
36 |
|
---|
37 | public TTCN3Comment(LocationAST node){
|
---|
38 |
|
---|
39 | }
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | //TODO: WHY WHY WHY OH WHY?!?!?!?!?!??! REORGANIZE AND REFACTOR
|
---|
44 | public TTCN3Comment(String rawComments, int elementType){
|
---|
45 | if(rawComments != null && !rawComments.equals("")){
|
---|
46 | rawComments = "\n" + rawComments;
|
---|
47 | rawComments = rawComments.replaceAll("\\r", "");
|
---|
48 | rawComments = rawComments.replaceAll("\\n(\\s|\\*|/)*", "\n").replaceAll("\\n+","\n");
|
---|
49 | rawComments = rawComments.trim() + "\r___NEWTAG___";
|
---|
50 | rawComments = rawComments.replaceAll(" @see", " @esee").replaceAll(" @url", " @eurl");
|
---|
51 | rawComments = rawComments.replaceAll("\\n@", "\r___NEWTAG___@");
|
---|
52 |
|
---|
53 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getDescTag(), desc);
|
---|
54 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getAuthorTag(), author);
|
---|
55 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getConfigTag(), config);
|
---|
56 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getExceptionTag(), exception);
|
---|
57 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getMemberTag(), member);
|
---|
58 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getParamTag(), param);
|
---|
59 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getPurposeTag(), purpose);
|
---|
60 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getRemarkTag(), remark);
|
---|
61 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getReturnTag(), Return);
|
---|
62 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getSeeTag(), see);
|
---|
63 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getSinceTag(), since);
|
---|
64 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getStatusTag(), status);
|
---|
65 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getUrlTag(), url);
|
---|
66 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getVerdictTag(), verdict);
|
---|
67 | getComments(rawComments, T3D.activeProfile.getCommentTagsConfiguration().getVersionTag(), version);
|
---|
68 |
|
---|
69 |
|
---|
70 | if (T3D.activeProfile.isCheckConsistentTagUsage()) {
|
---|
71 | if (config.size() >= 2)
|
---|
72 | warnings.add(getWarningMultipleTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getConfigTag()));
|
---|
73 | if (purpose.size() >= 2)
|
---|
74 | warnings.add(getWarningMultipleTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getPurposeTag()));
|
---|
75 | if (Return.size() >= 2)
|
---|
76 | warnings.add(getWarningMultipleTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getReturnTag()));
|
---|
77 | if (since.size() >= 2)
|
---|
78 | warnings.add(getWarningMultipleTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getSinceTag()));
|
---|
79 | if (status.size() >= 2)
|
---|
80 | warnings.add(getWarningMultipleTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getStatusTag()));
|
---|
81 | if (version.size() >= 2)
|
---|
82 | warnings.add(getWarningMultipleTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getVersionTag()));
|
---|
83 |
|
---|
84 | if (elementType != TTCN3ParserTokenTypes.TestcaseDef && config.size() >= 1) {
|
---|
85 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getConfigTag()));
|
---|
86 | }
|
---|
87 | if (elementType != TTCN3ParserTokenTypes.SignatureDef && exception.size() >= 1) {
|
---|
88 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getExceptionTag()));
|
---|
89 | }
|
---|
90 | if (elementType != TTCN3ParserTokenTypes.TypeDef
|
---|
91 | && elementType != TTCN3ParserTokenTypes.TemplateDef
|
---|
92 | && elementType != TTCN3ParserTokenTypes.ModuleParDef
|
---|
93 | && elementType != TTCN3ParserTokenTypes.ConstDef
|
---|
94 | && member.size() >= 1) {
|
---|
95 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getMemberTag()));
|
---|
96 | }
|
---|
97 | if (elementType != TTCN3ParserTokenTypes.TemplateDef
|
---|
98 | && elementType != TTCN3ParserTokenTypes.SignatureDef
|
---|
99 | && elementType != TTCN3ParserTokenTypes.FunctionDef
|
---|
100 | && elementType != TTCN3ParserTokenTypes.AltstepDef
|
---|
101 | && elementType != TTCN3ParserTokenTypes.TestcaseDef
|
---|
102 | && param.size() >= 1) {
|
---|
103 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getParamTag()));
|
---|
104 | }
|
---|
105 | if (elementType != TTCN3ParserTokenTypes.TestcaseDef && purpose.size() >= 1) {
|
---|
106 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getPurposeTag()));
|
---|
107 | }
|
---|
108 | if (elementType != TTCN3ParserTokenTypes.FunctionDef && elementType != TTCN3ParserTokenTypes.SignatureDef && Return.size() >= 1) {
|
---|
109 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getReturnTag()));
|
---|
110 | }
|
---|
111 | if (elementType != TTCN3ParserTokenTypes.FunctionDef
|
---|
112 | && elementType != TTCN3ParserTokenTypes.TestcaseDef
|
---|
113 | && elementType != TTCN3ParserTokenTypes.AltstepDef
|
---|
114 | && verdict.size() >= 1) {
|
---|
115 | warnings.add(getWarningExtraTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getVerdictTag()));
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (T3D.activeProfile.isCheckFunctionDescTagsRequired())
|
---|
119 | if (elementType != TTCN3ParserTokenTypes.FunctionDef
|
---|
120 | // && elementType != TTCN3ParserTokenTypes.TestcaseDef
|
---|
121 | // && elementType != TTCN3ParserTokenTypes.AltstepDef
|
---|
122 | && desc.size() <= 0) {
|
---|
123 | warnings.add(getWarningMissingTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getDescTag()));
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | for (String d : desc)
|
---|
128 | if (d.length() <= 0)
|
---|
129 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getDescTag()));
|
---|
130 | for (String d : author)
|
---|
131 | if (d.length() <= 0)
|
---|
132 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getAuthorTag()));
|
---|
133 | for (String d : config)
|
---|
134 | if (d.length() <= 0)
|
---|
135 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getConfigTag()));
|
---|
136 | for (String d : exception)
|
---|
137 | if (d.length() <= 0)
|
---|
138 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getExceptionTag()));
|
---|
139 | for (String d : member)
|
---|
140 | if (d.length() <= 0)
|
---|
141 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getMemberTag()));
|
---|
142 | for (String d : param)
|
---|
143 | if (d.length() <= 0)
|
---|
144 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getParamTag()));
|
---|
145 | for (String d : purpose)
|
---|
146 | if (d.length() <= 0)
|
---|
147 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getPurposeTag()));
|
---|
148 | for (String d : remark)
|
---|
149 | if (d.length() <= 0)
|
---|
150 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getRemarkTag()));
|
---|
151 | for (String d : Return)
|
---|
152 | if (d.length() <= 0)
|
---|
153 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getReturnTag()));
|
---|
154 | for (String d : see)
|
---|
155 | if (d.length() <= 0)
|
---|
156 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getSeeTag()));
|
---|
157 | for (String d : since)
|
---|
158 | if (d.length() <= 0)
|
---|
159 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getSinceTag()));
|
---|
160 | for (String d : status)
|
---|
161 | if (d.length() <= 0)
|
---|
162 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getStatusTag()));
|
---|
163 | for (String d : url)
|
---|
164 | if (d.length() <= 0)
|
---|
165 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getUrlTag()));
|
---|
166 | for (String d : verdict)
|
---|
167 | if (d.length() <= 0)
|
---|
168 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getVerdictTag()));
|
---|
169 | for (String d : version)
|
---|
170 | if (d.length() <= 0)
|
---|
171 | warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getVersionTag()));
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | for(String seeRef : see)
|
---|
176 | seeRefs.add(seeRef);
|
---|
177 | }
|
---|
178 |
|
---|
179 | private String getWarningMultipleTags(String tagname){
|
---|
180 | return "Multiple " + tagname + " tags found (may only contain one)";
|
---|
181 | }
|
---|
182 |
|
---|
183 | private String getWarningExtraTags(String tagname){
|
---|
184 | return tagname + " tag found (may not be used here)";
|
---|
185 | }
|
---|
186 |
|
---|
187 | private String getWarningEmptyTags(String tagname){
|
---|
188 | return "Empty tag found: " + tagname;
|
---|
189 | }
|
---|
190 |
|
---|
191 | private String getWarningMissingTags(String tagname){
|
---|
192 | return "Required " + tagname + " tag is missing";
|
---|
193 | }
|
---|
194 |
|
---|
195 |
|
---|
196 | private String getIdentifier(String comment, int pos){
|
---|
197 | String subString = comment.substring(pos, comment.length());
|
---|
198 |
|
---|
199 | int endpos = subString.indexOf(" ");
|
---|
200 | if(endpos == -1)
|
---|
201 | endpos = subString.length();
|
---|
202 | // System.out.println("-" + subString.substring(0, endpos) + "-");
|
---|
203 | return subString.substring(0, endpos).trim();
|
---|
204 | }
|
---|
205 |
|
---|
206 | private String replaceTag(String c, String tag){
|
---|
207 | int pos;
|
---|
208 | c += " ";
|
---|
209 | while(c.contains(" @" + tag + " ")){
|
---|
210 | pos = c.indexOf(" @" + tag + " ");
|
---|
211 | if(pos != -1){
|
---|
212 | String identifier = getIdentifier(c, pos+ (tag.length() + 3));
|
---|
213 | c = c.replaceAll("@" + tag + " " + identifier + " ", "<" + tag + ">" + identifier + "</" + tag + ">");
|
---|
214 | }else
|
---|
215 | break;
|
---|
216 | }
|
---|
217 | return c.trim();
|
---|
218 | }
|
---|
219 |
|
---|
220 | private String replaceSee(String c){
|
---|
221 | return replaceTag(c, "esee");
|
---|
222 | }
|
---|
223 |
|
---|
224 | private String replaceUrl(String c){
|
---|
225 | return replaceTag(c, "eurl");
|
---|
226 | }
|
---|
227 |
|
---|
228 | private String formatComment(String c){
|
---|
229 | return StringEscapeUtils.escapeXml(c).replaceAll(" ", " ").replaceAll("\n", " ").replaceAll("\t", " ").replaceAll("\r", "");
|
---|
230 |
|
---|
231 | }
|
---|
232 |
|
---|
233 | public String toString(){
|
---|
234 | commentsXML = "";
|
---|
235 | if(!isEmpty()){
|
---|
236 | commentsXML += "\n<comment>";
|
---|
237 | for(String d : desc)
|
---|
238 | commentsXML += ("\n<desc>" + replaceUrl(replaceSee(formatComment(d))) + "</desc>");
|
---|
239 |
|
---|
240 | for(String d : author)
|
---|
241 | commentsXML += ("\n<author>" + replaceUrl(replaceSee(formatComment(d))) + "</author>");
|
---|
242 |
|
---|
243 | for(String d : config)
|
---|
244 | commentsXML += ("\n<config>" + replaceUrl(replaceSee(formatComment(d))) + "</config>");
|
---|
245 |
|
---|
246 | for(String d : exception)
|
---|
247 | commentsXML += ("\n<exception>" + replaceUrl(replaceSee(formatComment(d))) + "</exception>");
|
---|
248 |
|
---|
249 | for(String d : member)
|
---|
250 | commentsXML += ("\n<member>" + replaceUrl(replaceSee(formatComment(d))) + "</member>");
|
---|
251 |
|
---|
252 | for(String d : param)
|
---|
253 | commentsXML += ("\n<param>" + replaceUrl(replaceSee(formatComment(d))) + "</param>");
|
---|
254 | for(String d : purpose)
|
---|
255 | commentsXML += ("\n<purpose>" + replaceUrl(replaceSee(formatComment(d))) + "</purpose>");
|
---|
256 |
|
---|
257 | for(String d : remark)
|
---|
258 | commentsXML += ("\n<remark>" + replaceUrl(replaceSee(formatComment(d))) + "</remark>");
|
---|
259 |
|
---|
260 | for(String d : Return)
|
---|
261 | commentsXML += ("\n<return>" + replaceUrl(replaceSee(formatComment(d))) + "</return>");
|
---|
262 |
|
---|
263 | for(String d : see)
|
---|
264 | commentsXML += ("\n<see>" + formatComment(d) + "</see>");
|
---|
265 |
|
---|
266 | for(String d : since)
|
---|
267 | commentsXML += ("\n<since>" + replaceUrl(replaceSee(formatComment(d))) + "</since>");
|
---|
268 |
|
---|
269 | for(String d : status)
|
---|
270 | commentsXML += ("\n<status>" + replaceUrl(replaceSee(formatComment(d))) + "</status>");
|
---|
271 |
|
---|
272 | for(String d : url)
|
---|
273 | commentsXML += ("\n<url>" + formatComment(d) + "</url>");
|
---|
274 |
|
---|
275 | for(String d : verdict)
|
---|
276 | commentsXML += ("\n<verdict>" + replaceUrl(replaceSee(formatComment(d))) + "</verdict>");
|
---|
277 |
|
---|
278 | for(String d : version)
|
---|
279 | commentsXML += ("\n<version>" + replaceUrl(replaceSee(formatComment(d))) + "</version>");
|
---|
280 |
|
---|
281 | commentsXML += "</comment>";
|
---|
282 |
|
---|
283 | }
|
---|
284 | if(commentsXML.length() > 20)
|
---|
285 | return commentsXML;
|
---|
286 | else
|
---|
287 | return "";
|
---|
288 | }
|
---|
289 |
|
---|
290 | private boolean isEmpty(){
|
---|
291 | return false;
|
---|
292 | }
|
---|
293 |
|
---|
294 | private void getComments(String commentblock, String tag, LinkedList<String> vector){
|
---|
295 | Pattern p = Pattern.compile("@" + tag + "(.|\\n)*\\r___NEWTAG___");
|
---|
296 | Matcher m = p.matcher(commentblock);
|
---|
297 | while (m.find() == true){
|
---|
298 | String comment = m.group().replaceFirst("@"+tag, "").replaceFirst("\r___NEWTAG___", "").trim();
|
---|
299 | vector.add(comment);
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | public LinkedList<String> getDocumentedParameters(){
|
---|
304 | LinkedList <String> docParNames = new LinkedList<String>();
|
---|
305 | for(String parString : param){
|
---|
306 | if(parString.indexOf(" ") > 0)
|
---|
307 | docParNames.add(parString.substring(0, parString.indexOf(" ")));
|
---|
308 | }
|
---|
309 | return docParNames;
|
---|
310 | }
|
---|
311 |
|
---|
312 | public LinkedList<String> getDescriptions(){
|
---|
313 | LinkedList <String> descriptions = new LinkedList<String>();
|
---|
314 | for(String descString : desc)
|
---|
315 | descriptions.add(descString);
|
---|
316 | return descriptions;
|
---|
317 | }
|
---|
318 |
|
---|
319 | public LinkedList<String> getWarnings() {
|
---|
320 | return warnings;
|
---|
321 | }
|
---|
322 | }
|
---|