source: trunk/t3d/src/elements/TTCN3Comment.java @ 7

Last change on this file since 7 was 7, checked in by phdmakk, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 13.3 KB
Line 
1package elements;
2
3import java.util.LinkedList;
4import java.util.regex.Matcher;
5import java.util.regex.Pattern;
6import org.apache.commons.lang.*;
7import org.etsi.t3d.T3D;
8
9import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST;
10import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes;
11
12public 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                                for (String d : desc)
119                                        if (d.length() <= 0)
120                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getDescTag()));
121                                for (String d : author)
122                                        if (d.length() <= 0)
123                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getAuthorTag()));
124                                for (String d : config)
125                                        if (d.length() <= 0)
126                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getConfigTag()));
127                                for (String d : exception)
128                                        if (d.length() <= 0)
129                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getExceptionTag()));
130                                for (String d : member)
131                                        if (d.length() <= 0)
132                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getMemberTag()));
133                                for (String d : param)
134                                        if (d.length() <= 0)
135                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getParamTag()));
136                                for (String d : purpose)
137                                        if (d.length() <= 0)
138                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getPurposeTag()));
139                                for (String d : remark)
140                                        if (d.length() <= 0)
141                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getRemarkTag()));
142                                for (String d : Return)
143                                        if (d.length() <= 0)
144                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getReturnTag()));
145                                for (String d : see)
146                                        if (d.length() <= 0)
147                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getSeeTag()));
148                                for (String d : since)
149                                        if (d.length() <= 0)
150                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getSinceTag()));
151                                for (String d : status)
152                                        if (d.length() <= 0)
153                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getStatusTag()));
154                                for (String d : url)
155                                        if (d.length() <= 0)
156                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getUrlTag()));
157                                for (String d : verdict)
158                                        if (d.length() <= 0)
159                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getVerdictTag()));
160                                for (String d : version)
161                                        if (d.length() <= 0)
162                                                warnings.add(getWarningEmptyTags("@" + T3D.activeProfile.getCommentTagsConfiguration().getVersionTag()));
163                        }
164                }
165       
166        for(String seeRef : see)
167                seeRefs.add(seeRef);
168        }
169       
170        private String getWarningMultipleTags(String tagname){
171                return "Multiple " + tagname + " tags found (may only contain one)";
172        }
173       
174        private String getWarningExtraTags(String tagname){
175                return tagname + " tag found (may not be used here)";
176        }
177       
178        private String getWarningEmptyTags(String tagname){
179                return "Empty tag found: " + tagname;
180        }
181       
182       
183        private String getIdentifier(String comment, int pos){
184                String subString = comment.substring(pos, comment.length());
185               
186                int endpos = subString.indexOf(" ");
187                if(endpos == -1)
188                        endpos = subString.length();
189//              System.out.println("-" + subString.substring(0, endpos) + "-");
190                return subString.substring(0, endpos).trim();           
191        }
192       
193        private String replaceTag(String c, String tag){
194                int pos;
195                c += " ";
196                while(c.contains(" @" + tag + " ")){
197                        pos = c.indexOf(" @" + tag + " ");
198                        if(pos != -1){
199                                String identifier = getIdentifier(c, pos+ (tag.length() + 3));
200                                c = c.replaceAll("@" + tag + " " + identifier + " ", "<" + tag + ">" + identifier + "</" + tag + ">");
201                        }else
202                                break;
203                }
204                return c.trim();
205        }
206       
207        private String replaceSee(String c){
208                return replaceTag(c, "esee");
209        }
210       
211        private String replaceUrl(String c){
212                return replaceTag(c, "eurl");
213        }
214       
215        private String formatComment(String c){
216                return StringEscapeUtils.escapeXml(c).replaceAll("  ", " ").replaceAll("\n", " ").replaceAll("\t", " ").replaceAll("\r", "");
217
218        }
219       
220        public String toString(){
221                commentsXML = "";
222                if(!isEmpty()){
223                        commentsXML += "\n<comment>";
224                        for(String d : desc)
225                                commentsXML += ("\n<desc>" + replaceUrl(replaceSee(formatComment(d))) + "</desc>");
226                       
227                        for(String d : author)
228                                commentsXML += ("\n<author>" + replaceUrl(replaceSee(formatComment(d))) + "</author>");
229                       
230                        for(String d : config)
231                                commentsXML += ("\n<config>" + replaceUrl(replaceSee(formatComment(d))) + "</config>");
232               
233                        for(String d : exception)
234                                commentsXML += ("\n<exception>" + replaceUrl(replaceSee(formatComment(d))) + "</exception>");
235               
236                        for(String d : member)
237                                commentsXML += ("\n<member>" + replaceUrl(replaceSee(formatComment(d))) + "</member>");
238
239                        for(String d : param)
240                                commentsXML += ("\n<param>" + replaceUrl(replaceSee(formatComment(d))) + "</param>");
241                        for(String d : purpose)
242                                commentsXML += ("\n<purpose>" + replaceUrl(replaceSee(formatComment(d))) + "</purpose>");
243                                       
244                        for(String d : remark)
245                                commentsXML += ("\n<remark>" + replaceUrl(replaceSee(formatComment(d))) + "</remark>");
246                       
247                        for(String d : Return)
248                                        commentsXML += ("\n<return>" + replaceUrl(replaceSee(formatComment(d))) + "</return>");
249                       
250                        for(String d : see)
251                                commentsXML += ("\n<see>" + formatComment(d) + "</see>");
252                       
253                        for(String d : since)
254                                commentsXML += ("\n<since>" + replaceUrl(replaceSee(formatComment(d))) + "</since>");
255                       
256                        for(String d : status)
257                                commentsXML += ("\n<status>" + replaceUrl(replaceSee(formatComment(d))) + "</status>");
258                       
259                        for(String d : url)
260                                commentsXML += ("\n<url>" + formatComment(d) + "</url>");
261                       
262                        for(String d : verdict)
263                                commentsXML += ("\n<verdict>" + replaceUrl(replaceSee(formatComment(d))) + "</verdict>");
264                       
265                        for(String d : version)
266                                commentsXML += ("\n<version>" + replaceUrl(replaceSee(formatComment(d))) + "</version>");
267                       
268                        commentsXML += "</comment>";
269                                               
270                }
271                if(commentsXML.length() > 20)
272                        return commentsXML;
273                else
274                        return "";
275        }
276       
277        private boolean isEmpty(){
278                return false;
279        }
280               
281        private void getComments(String commentblock, String tag, LinkedList<String> vector){
282                Pattern p = Pattern.compile("@" + tag + "(.|\\n)*\\r___NEWTAG___");
283                Matcher m = p.matcher(commentblock);
284                while (m.find() == true){
285                        String comment = m.group().replaceFirst("@"+tag, "").replaceFirst("\r___NEWTAG___", "").trim();
286                        vector.add(comment);
287                }               
288        }
289       
290        public LinkedList<String> getDocumentedParameters(){
291                LinkedList <String> docParNames = new LinkedList<String>();             
292                for(String parString : param){
293                        if(parString.indexOf(" ") > 0)
294                                docParNames.add(parString.substring(0, parString.indexOf(" ")));
295                }               
296                return docParNames;
297        }
298
299        public LinkedList<String> getDescriptions(){
300                LinkedList <String> descriptions = new LinkedList<String>();           
301                for(String descString : desc)
302                                descriptions.add(descString);
303                return descriptions;
304        }
305       
306        public LinkedList<String> getWarnings() {
307                return warnings;
308        }       
309}
Note: See TracBrowser for help on using the repository browser.