source: trunk/t3d/src/org/etsi/t3d/visitor/T3DVisitor.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.0 KB
Line 
1package org.etsi.t3d.visitor;
2
3import java.util.ArrayList;
4import java.util.LinkedList;
5import java.util.List;
6
7import org.etsi.t3d.T3D;
8import org.etsi.t3d.XMLPrinter;
9import org.etsi.t3d.exceptions.TTCN3BehaviorException;
10
11import de.ugoe.cs.swe.trex.core.analyzer.rfparser.ASTUtil;
12import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST;
13import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes;
14import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TokenWithIndex;
15
16import elements.*;
17
18public class T3DVisitor extends AbstractVisitor {
19
20        private XMLPrinter xmlPrinter;
21        private String filename;
22
23        public T3DVisitor(XMLPrinter xmlPrinter) {
24                this.xmlPrinter = xmlPrinter;
25        }
26
27        @Override
28        public void finish() {
29        }
30
31        @Override
32        public void init() {
33                xmlPrinter.getLoggingInterface().setLogSourceName(this.getFilename());
34                xmlPrinter.setCurrentTTCN3File(this.getFilename());
35        }
36
37        // creates TTCN3Element instance from first child of Moduledefinition-node
38        // and passes it to the XMLPrinter
39        // TODO: Move content to element and pass on the module definition node
40        // TODO: move calls from defNodes to moduleDefs
41        // TODO: watchout for module definitions that shall be disregarded or
42        // handled differently
43        private void visitElementDefinition(LocationAST node) {
44                if (node.getParent().getType() != TTCN3ParserTokenTypes.ModuleDefinition)
45                        return;
46                LinkedList<LocationAST> parameterList = ASTUtil.findTypeNodes(node,
47                                TTCN3ParserTokenTypes.FormalValuePar);
48                parameterList.addAll(ASTUtil.findTypeNodes(node,
49                                TTCN3ParserTokenTypes.FormalPortPar));
50                parameterList.addAll(ASTUtil.findTypeNodes(node,
51                                TTCN3ParserTokenTypes.FormalTimerPar));
52                parameterList.addAll(ASTUtil.findTypeNodes(node,
53                                TTCN3ParserTokenTypes.FormalTemplatePar));
54
55                LinkedList<String> parNames = new LinkedList<String>();
56                LocationAST tempAST;
57                for (LocationAST parNode : parameterList) {
58                        tempAST = parNode.getFirstChild();
59                        while (tempAST.getNextSibling() != null
60                                        && tempAST.getType() != TTCN3ParserTokenTypes.Identifier)
61                                tempAST = tempAST.getNextSibling();
62                        parNames.add(tempAST.getFirstChild().getText()
63                                        + " "
64                                        + tempAST.getLine());
65                }
66
67                ArrayList<LocationAST> elList = LocationAST.getModuleDefinitionIdentifiersList(node.getParent());
68
69                for (LocationAST elNode : elList) {
70                        String paramView = "";
71                        if (node.getType() == TTCN3ParserTokenTypes.ModuleParDef)
72                                paramView = getParamview(elNode.getFirstChild(),
73                                                new LinkedList<String>());
74                        TTCN3Element element = new TTCN3Element(elNode  .getFirstChild()
75                                                                                                                        .getText(),
76                                        LocationAST.getTTCN3ParserTokenTypeTypePrettyName(node.getType()),
77                                        VisitorCommonFunctions.getLocationFromIdentifier(elNode.getFirstChild()),
78                                        VisitorCommonFunctions.getBehaviour(node,
79                                                        0,
80                                                        true,
81                                                        T3D.activeProfile.isIncludeConstructBody()),
82                                        getCommentFromNode(node),
83                                        parNames,
84                                        xmlPrinter.getCurrentTTCN3File(),
85                                        paramView,
86                                        getGroupNavigationPath(node),
87                                        elNode.getLine());
88                        xmlPrinter.printElement(element, node.getLine());
89                }
90        }
91
92        // returns XML representation of the group navigation path of a node
93        private String getGroupNavigationPath(LocationAST node) {
94                LocationAST currentNode = node.getParent();
95                String path = "\n<path>";
96                int groups = 0;
97                while (currentNode.getParent() != null) {
98                        if (currentNode.getType() == TTCN3ParserTokenTypes.GroupDef) {
99                                path += "\n<path_group loc=\""
100                                                + VisitorCommonFunctions.getLocationFromIdentifier(VisitorCommonFunctions.getIdentifier(currentNode))
101                                                + "\" name=\""
102                                                + VisitorCommonFunctions.getName(currentNode)
103                                                + "\">";
104                                groups++;
105                        }
106                        currentNode = currentNode.getParent();
107                }
108                for (int i = 0; i < groups; i++)
109                        path += "</path_group>";
110                return path + "</path>";
111        }
112
113        // returns XML representation of the Module Parameter View from a
114        // ModuleParDef node
115        private String getParamview(LocationAST node, LinkedList<String> locations) {
116                String pview = "";
117                LinkedList<LocationAST> refList = VisitorCommonFunctions.getReferenceList(node);
118                String loc = VisitorCommonFunctions.getLocationFromIdentifier(node);
119                String type = "mpview_"
120                                + LocationAST.resolveParentsUntilType(node, TTCN3ParserTokenTypes.ModuleDefinition)
121                                                                                .getFirstChild()
122                                                                                .getText();
123                pview += "\n<"
124                                + type
125                                + " loc=\""
126                                + loc
127                                + "\" name=\""
128                                + VisitorCommonFunctions.getName(node)
129                                + "\">";
130                if (refList != null) {
131                        for (LocationAST n : refList) {
132                                LocationAST moduleDef = LocationAST.resolveParentsUntilType(n, TTCN3ParserTokenTypes.ModuleDefinition);
133                                if (moduleDef != null
134                                                && (moduleDef.getFirstChild().getType() == TTCN3ParserTokenTypes.TestcaseDef
135                                                                || moduleDef.getFirstChild().getType() == TTCN3ParserTokenTypes.FunctionDef || moduleDef.getFirstChild()
136                                                                                                                                                                                                                                                .getType() == TTCN3ParserTokenTypes.AltstepDef)) {
137                                        String loc1 = VisitorCommonFunctions.getLocationFromIdentifier(VisitorCommonFunctions.getIdentifier(moduleDef));
138
139                                        if (!locations.contains(loc1)) {
140                                                locations.add(loc1);
141                                                pview += getParamview(VisitorCommonFunctions.getIdentifier(moduleDef),
142                                                                locations);
143                                        }
144                                }
145                        }
146                }
147                pview += "</" + type + ">";
148
149                return pview;
150        }
151
152        public ContinueStatus visitConstDef(LocationAST node)
153                        throws TTCN3BehaviorException {
154                visitElementDefinition(node);
155                return ContinueStatus.getInstance(true, true);
156        }
157
158        public ContinueStatus visitTemplateDef(LocationAST node)
159                        throws TTCN3BehaviorException {
160                visitElementDefinition(node);
161                return ContinueStatus.getInstance(true, true);
162        }
163
164        public ContinueStatus visitFunctionDef(LocationAST node)
165                        throws TTCN3BehaviorException {
166                visitElementDefinition(node);
167                return ContinueStatus.getInstance(true, true);
168        }
169        public ContinueStatus visitExtFunctionDef(LocationAST node)
170                throws TTCN3BehaviorException {
171                visitElementDefinition(node);
172                return ContinueStatus.getInstance(true, true);
173        }
174        public ContinueStatus visitExtConstDef(LocationAST node)
175                throws TTCN3BehaviorException {
176                visitElementDefinition(node);
177                return ContinueStatus.getInstance(true, true);
178        }
179       
180        public ContinueStatus visitTestcaseDef(LocationAST node)
181                        throws TTCN3BehaviorException {
182                visitElementDefinition(node);
183                return ContinueStatus.getInstance(true, true);
184        }
185
186        public ContinueStatus visitAltstepDef(LocationAST node)
187                        throws TTCN3BehaviorException {
188                visitElementDefinition(node);
189                return ContinueStatus.getInstance(true, true);
190        }
191
192        public ContinueStatus visitTypeDef(LocationAST node)
193                        throws TTCN3BehaviorException {
194                visitElementDefinition(node);
195                return ContinueStatus.getInstance(true, true);
196        }
197
198        public ContinueStatus visitSignatureDef(LocationAST node)
199                        throws TTCN3BehaviorException {
200                visitElementDefinition(node);
201                return ContinueStatus.getInstance(true, true);
202        }
203
204        public ContinueStatus visitModuleParDef(LocationAST node)
205                        throws TTCN3BehaviorException {
206                ArrayList<LocationAST> elList = LocationAST.getModuleDefinitionIdentifiersList(node.getParent());
207
208                for (LocationAST elNode : elList) {
209                        String paramView = getParamview(elNode.getFirstChild(),
210                                        new LinkedList<String>());
211                        TTCN3Element element = new TTCN3Element(elNode  .getFirstChild()
212                                                                                                                        .getText(),
213                                        LocationAST.getTTCN3ParserTokenTypeTypePrettyName(node.getType()),
214                                        VisitorCommonFunctions.getLocationFromIdentifier(elNode.getFirstChild()),
215                                        VisitorCommonFunctions.getBehaviour(getModuleParNode(elNode),
216                                                        0,
217                                                        true,
218                                                        true),
219                                        getCommentFromNode(node),
220                                        new LinkedList<String>(),
221                                        xmlPrinter.getCurrentTTCN3File(),
222                                        paramView,
223                                        getGroupNavigationPath(node),
224                                        elNode.getLine());
225                        xmlPrinter.printElement(element, node.getLine());
226                }
227                return ContinueStatus.getInstance(true, true);
228        }
229
230        private LocationAST getModuleParNode(LocationAST node) {
231                while (node.getType() != TTCN3ParserTokenTypes.ModulePar)
232                        node = node.getParent();
233                return node;
234        }
235
236        public ContinueStatus visitTTCN3Module(LocationAST node)
237                        throws TTCN3BehaviorException {
238                xmlPrinter.printModule(VisitorCommonFunctions.getName(node),
239                                getCommentFromNode(node),
240                                getModuleBehaviour(node, "module"),
241                                node.getLine());
242                return ContinueStatus.getInstance(true, true);
243        }
244
245        public ContinueStatus visitGroupDef(LocationAST node)
246                        throws TTCN3BehaviorException {
247                ArrayList<LocationAST> elList = LocationAST.getModuleDefinitionIdentifiersList(node.getParent());
248
249                for (LocationAST elNode : elList) {
250                        xmlPrinter.printGroup(elNode.getFirstChild().getText(),
251                                        VisitorCommonFunctions.getLocationFromIdentifier(elNode.getFirstChild()),
252                                        getCommentFromNode(node),
253                                        getModuleBehaviour(node, "group"),
254                                        getGroupNavigationPath(node),
255                                        node.getLine());
256                }
257                return ContinueStatus.getInstance(true, true);
258        }
259
260        // returns the XML representation(<behaviour>) of a ModuleDefinitionList
261        // node
262        private String getModDefListBehaviour(LocationAST node, int tabs,
263                        String controlPart) {
264                String tabString = "";
265                for (int i = 0; i < tabs; i++)
266                        tabString += "<tab/>";
267                String b = "";
268                LocationAST ModDefNode = node.getFirstChild();
269                while (ModDefNode != null) {
270                        if (ModDefNode.getType() == TTCN3ParserTokenTypes.ModuleDefinition) {
271                                if (ModDefNode.getFirstChild().getType() == TTCN3ParserTokenTypes.GroupDef) {
272                                        LocationAST modDefListNode = ModDefNode.getFirstChild();
273                                        if (modDefListNode != null)
274                                                modDefListNode = modDefListNode.getFirstChild();
275                                        if (modDefListNode.getNextSibling() == null)
276                                                b += "\n"
277                                                                + tabString
278                                                                + "<keyword>group</keyword> "
279                                                                + "<link loc=\""
280                                                                + VisitorCommonFunctions.getLocationFromIdentifier(VisitorCommonFunctions.getIdentifier(ModDefNode))
281                                                                + "\">"
282                                                                + VisitorCommonFunctions.getName(ModDefNode)
283                                                                + "</link>"
284                                                                + "<constructbody id=\"o_"
285                                                                + ModDefNode.getOffset()
286                                                                + "\">"
287                                                                + " {\n"
288                                                                + tabString
289                                                                + "}</constructbody>\n";
290
291                                        if (modDefListNode != null)
292                                                modDefListNode = modDefListNode.getNextSibling();
293                                        if (modDefListNode != null)
294                                                modDefListNode = modDefListNode.getFirstChild();
295                                        if (modDefListNode != null)
296                                                b += "\n"
297                                                                + tabString
298                                                                + "<keyword>group</keyword> "
299                                                                + "<link loc=\""
300                                                                + VisitorCommonFunctions.getLocationFromIdentifier(VisitorCommonFunctions.getIdentifier(ModDefNode))
301                                                                + "\">"
302                                                                + VisitorCommonFunctions.getName(ModDefNode)
303                                                                + "</link>"
304                                                                + "<constructbody id=\"o_"
305                                                                + ModDefNode.getOffset()
306                                                                + "\">"
307                                                                + " {\n"
308                                                                + getModDefListBehaviour(modDefListNode,
309                                                                                tabs + 1,
310                                                                                "");
311                                } else {
312                                        LocationAST elementDefNode = ModDefNode.getFirstChild();
313                                        if (elementDefNode.getType() == TTCN3ParserTokenTypes.Visibility)
314                                                elementDefNode = elementDefNode.getNextSibling();
315                                        if (elementDefNode.getType() == TTCN3ParserTokenTypes.ModuleParDef)
316                                                b += VisitorCommonFunctions.getBehaviour(elementDefNode,
317                                                                tabs,
318                                                                true,
319                                                                true);
320                                        else
321                                                b += VisitorCommonFunctions.getBehaviour(elementDefNode,
322                                                                tabs,
323                                                                true,
324                                                                T3D.activeProfile.isIncludeConstructBody());
325                                }
326                        }
327                        ModDefNode = ModDefNode.getNextSibling();
328                }
329                return b
330                                + controlPart
331                                + "\n"
332                                + tabString.substring(6)
333                                + "}</constructbody>\n";
334        }
335
336        // returns the XML representation(<behaviour>) of a TTCN3Module node
337        private String getModuleBehaviour(LocationAST node, String type) {
338                LocationAST modDefListNode = null;
339                LinkedList<LocationAST> modDefListNodeList = ASTUtil.findTypeNodes(node,
340                                TTCN3ParserTokenTypes.ModuleDefinitionList);
341                if (!modDefListNodeList.isEmpty())
342                        modDefListNode = modDefListNodeList.getFirst();
343
344                if (modDefListNode == null)
345                        return "<keyword>"
346                                        + type
347                                        + "</keyword> "
348                                        + VisitorCommonFunctions.getName(node)
349                                        + "<constructbody id=\"modulebody\">{\n}</constructbody>\n";
350
351                LinkedList<LocationAST> controlPartList = ASTUtil.findTypeNodes(node,
352                                TTCN3ParserTokenTypes.ModuleControlPart);
353                LocationAST controlPartNode = null;
354                if (!controlPartList.isEmpty())
355                        controlPartNode = controlPartList.getFirst();
356                String controlPartBehaviour = "";
357                if (controlPartNode != null)
358                        controlPartBehaviour = VisitorCommonFunctions.getBehaviour(controlPartNode,
359                                        1,
360                                        true,
361                                        true);
362                String b = "<keyword>"
363                                + type
364                                + "</keyword> "
365                                + VisitorCommonFunctions.getName(node)
366                                + " <constructbody id=\"modulebody\">{\n"
367                                + getModDefListBehaviour(modDefListNode,
368                                                1,
369                                                controlPartBehaviour.replaceFirst("\\n<tab/><tab/>}\\n<tab/><tab/> ;",
370                                                                "\n<tab/>};"));
371                modDefListNode = modDefListNode.getNextSibling();
372                return b + " ";
373        }
374
375        // returns the TTCN3Comment representation of T3Doc documentation preceding
376        // node
377        private TTCN3Comment getCommentFromNode(LocationAST node) {
378                List<TokenWithIndex> commentList = node.getCommentsBefore();
379                String rawComments = "";
380                for (TokenWithIndex c : commentList) {
381                        rawComments = c.getText() + rawComments;
382                }
383                return new TTCN3Comment(rawComments, node.getType());
384        }
385
386        public void setFilename(String filename) {
387                this.filename = filename;
388        }
389
390        public String getFilename() {
391                return filename;
392        }
393
394}
Note: See TracBrowser for help on using the repository browser.