source: trunk/t3d/src/org/etsi/t3d/ImportPrinter.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: 6.5 KB
Line 
1package org.etsi.t3d;
2
3import java.io.FileNotFoundException;
4import java.io.FileOutputStream;
5import java.io.PrintStream;
6import java.util.LinkedList;
7
8import org.apache.commons.lang.StringEscapeUtils;
9import org.etsi.common.logging.LoggingInterface;
10import org.etsi.common.logging.LoggingInterface.MessageClass;
11import org.etsi.t3d.visitor.VisitorCommonFunctions;
12
13import de.ugoe.cs.swe.trex.core.analyzer.rfparser.ASTUtil;
14import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST;
15import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes;
16
17public class ImportPrinter {
18        private PrintStream stream;
19        private boolean firstModule = true;
20        private LinkedList<String> modules = new LinkedList<String>();
21        private LinkedList<String> locationsGlobal = new LinkedList<String>();
22        private String currentTTCN3File;
23        private LoggingInterface logger = null;
24       
25        public ImportPrinter() {
26                this.logger = new LoggingInterface(T3D.activeProfile.getLoggingConfiguration());
27                this.logger.setMaximumLogLevel(T3D.getLogLevel());
28        }
29       
30        public void finishXML(){
31                writeStream("\n</module>\n</imports>");         
32        }
33
34        public void setXMLPath(String xmlpath){
35                FileOutputStream file;
36                try {
37                        file = new FileOutputStream(xmlpath);
38                        stream = new PrintStream(file);
39                } catch (FileNotFoundException e) {
40                        e.printStackTrace();
41                }
42                writeStream("\n<imports t3dversion=\"" + T3D.versionNumber + "\">");
43        }
44
45        private void writeStream(String content){
46                stream.print(content);
47        }
48       
49        public void setCurrentTTCN3File(String currentTTCN3File) {
50                this.currentTTCN3File = currentTTCN3File;
51        }
52
53        public String getCurrentTTCN3File() {
54                return currentTTCN3File;
55        }
56       
57        public void printImportView(LocationAST node){
58                LinkedList<LocationAST> ImportDefs = ASTUtil.findTypeNodes(node, TTCN3ParserTokenTypes.ImportDef);
59                for(LocationAST importDef : ImportDefs){
60                        LocationAST importModuleIdentifier = importDef;
61                        while(importModuleIdentifier.getFirstChild() != null)
62                                importModuleIdentifier = importModuleIdentifier.getFirstChild();                       
63                        LocationAST importedModuleIdentifier = VisitorCommonFunctions.getDeclarationNodeFromIdentifier(importModuleIdentifier);
64                       
65                        if(!locationsGlobal.contains(importModuleIdentifier.getText())){
66                                listAdd(locationsGlobal,VisitorCommonFunctions.getName(node.getFirstChild()));
67                               
68                                String behaviour = getImportBehavior(importDef);
69                               
70                                writeStream("\n<import name=\"" + importModuleIdentifier.getText() + "\">"
71                                        + "\n\t\t<import_behaviour>" + behaviour + "\n\t\t</import_behaviour>");
72                                if(!ASTUtil.findTypeNodes(importDef, TTCN3ParserTokenTypes.AllKeyword).isEmpty()){
73                                        listAdd(locationsGlobal, importModuleIdentifier.getText());
74                                        if(importedModuleIdentifier != null)
75                                                printImportView(VisitorCommonFunctions.getModule(importedModuleIdentifier));
76                                }
77                               
78                                writeStream("\n</import>");
79                        } else{
80                                String behaviour = getImportBehavior(importDef);
81                                writeStream("\n<import name=\"" + importModuleIdentifier.getText() + "\">"
82                                                + "\n\t\t<import_behaviour>" + behaviour + "\n\t\t</import_behaviour>\n</import>");
83                        }
84                }
85        }
86       
87        private String getImportBehavior(LocationAST importDef){
88                String b = VisitorCommonFunctions.getWholeElement(importDef, true).replaceAll("\\r", "");
89                b = StringEscapeUtils.escapeXml(b);
90                b = b.replaceAll("\\n", "").trim();
91                if(b.indexOf('{') > 0 && b.indexOf('}') > b.indexOf('{'))
92                        b = b.substring(b.indexOf('{') + 1, b.indexOf('}'));
93                else
94                        b = b.replaceFirst("import from \\w* ", "");
95                b = VisitorCommonFunctions.replaceReferences(b, VisitorCommonFunctions.getReferences(importDef));
96                return b.trim();
97        }
98       
99        private void listAdd(LinkedList<String> list, String string){
100                if(!list.contains(string))
101                        list.add(string);
102        }
103       
104        public void checkForCyclicImports(LocationAST node, LinkedList<String> locations){
105                LinkedList<LocationAST> ImportDefs = ASTUtil.findTypeNodes(node, TTCN3ParserTokenTypes.ImportDef);     
106                for(LocationAST importDef : ImportDefs){
107                        LocationAST importModuleIdentifier = importDef;
108                        while(importModuleIdentifier.getFirstChild() != null)
109                                importModuleIdentifier = importModuleIdentifier.getFirstChild();                       
110                        LocationAST importedModuleIdentifier = VisitorCommonFunctions.getDeclarationNodeFromIdentifier(importModuleIdentifier);                 
111                        if(!locations.contains(importModuleIdentifier.getText())){
112                                LinkedList<String> newLocations = new LinkedList<String>();
113                                for(String l : locations)
114                                        newLocations.add(l);
115                                listAdd(newLocations, VisitorCommonFunctions.getName(node.getFirstChild()));
116                                listAdd(newLocations, importModuleIdentifier.getText());
117                               
118                               
119                                String behaviour = VisitorCommonFunctions.getBehaviour(importDef, 0, false, true);
120
121                                behaviour = behaviour.substring(behaviour.indexOf("</link>") + 7).trim();
122                                if(behaviour.charAt(0) == '{' && behaviour.charAt(behaviour.length()-1) == '}')
123                                        behaviour = behaviour.substring(1, behaviour.length()-1);
124
125                                behaviour = behaviour.replaceAll("\\s;", "").replaceAll("<constructbody id=\".*\">", "").replaceAll("</constructbody>", "");;
126                               
127                                behaviour = behaviour.trim();
128                                if(behaviour.charAt(0) == '{')
129                                        behaviour = behaviour.replaceAll("\\{", "").replaceAll("\\}", "");
130                                behaviour = behaviour.replaceAll("\\n", "");
131
132                                if(behaviour.startsWith("all") || behaviour.startsWith("recursive all"))
133                                        checkForCyclicImports(VisitorCommonFunctions.getModule(importedModuleIdentifier), newLocations);
134                        } else if(locations.getFirst().equals(importModuleIdentifier.getText())){
135                                String warning = "";
136                                for(String l : locations) {
137                                        warning += " -> " + l;
138                                }
139                                warning = "Cyclic imports found: " + warning.substring(4) + " -> " + locations.getFirst();
140                                this.getLoggingInterface().logWarning(importModuleIdentifier.getLine(),
141                                                importModuleIdentifier.getLine(),
142                                                MessageClass.DOCUMENTATION,
143                                                warning);
144                                //TODO: revise XML usage
145                                //T3D.printLog(0, currentTTCN3File, importModuleIdentifier.getLine(), importModuleIdentifier.getLine(), 1, warning);
146                        }
147                }
148        }
149       
150        public void newModule(String name) {
151                locationsGlobal = new LinkedList<String>();
152                String moduleName = name;
153                if (modules.contains(name)) {
154                        int repetitionIndex = 0;
155                        while(modules.contains(name + "-"+repetitionIndex)){
156                                repetitionIndex++;
157                        }
158                        moduleName = name + "-" + repetitionIndex;
159                }
160                modules.add(moduleName);
161                if(!firstModule)
162                        writeStream("\n</module>");
163                else
164                        firstModule = false;
165                writeStream("\n<module name=\"" + moduleName + "\">");         
166        }
167
168        public void setLoggingInterface(LoggingInterface logger) {
169                this.logger = logger;
170        }
171
172        public LoggingInterface getLoggingInterface() {
173                return logger;
174        }
175}
Note: See TracBrowser for help on using the repository browser.