source: trunk/t3d/src/org/etsi/t3d/XMLPrinter.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: 4.9 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.etsi.common.logging.LoggingConfiguration;
9import org.etsi.common.logging.LoggingInterface;
10import org.etsi.common.logging.LoggingInterface.MessageClass;
11
12
13import elements.*;
14public class XMLPrinter {
15        private String currentTTCN3File;
16        private PrintStream stream;
17        private LinkedList<String> modules = new LinkedList<String>();
18        private int files = 0;
19        private String currentModule;
20        private LinkedList <String[]> descs = new LinkedList<String[]>();
21
22        private LoggingInterface logger = null;
23       
24        public XMLPrinter(){
25                this.logger = new LoggingInterface(T3D.activeProfile.getLoggingConfiguration());
26                this.logger.setMaximumLogLevel(T3D.getLogLevel());
27        }
28       
29        public int getFileCount(){
30                return files;
31        }
32       
33        public void setXMLPath(String xmlpath){
34                FileOutputStream file;
35                try {
36                        file = new FileOutputStream(xmlpath);
37                        stream = new PrintStream(file);
38                } catch (FileNotFoundException e) {
39                        e.printStackTrace();
40                }               
41                writeStream("\n<project t3dversion=\"" + T3D.versionNumber + "\">");           
42        }
43        //adds String content to project.xml
44        private void writeStream(String content){
45                stream.print(content);
46        }
47        //adds XML representation of element to project.xml
48        public void printElement(TTCN3Element element, int line){
49                addDescriptions(element.getDescs());
50                writeStream("\n" + element.toXML(currentModule));
51                files++;
52                if(element.getType().equals("testcase") || element.getType().equals("parameter"))
53                        files++;
54        }
55        // 0 = desc, 1 = file, 2 = line
56        private void addDescriptions(LinkedList<String[]> descs){
57                for(String[] desc : descs){                     
58                        if(!descsContain(desc))
59                                this.descs.add(desc);
60                }
61        }
62       
63        private boolean descsContain(String[] desc){
64                for(String[] stringArray : descs)
65                        if(stringArray[0].equals(desc[0])){
66                                //TODO: out of place here, needs to be moved
67                                if (T3D.activeProfile.isCheckIdenticalDescriptionTags()) {
68                                        String warning = "Identical @desc tag found: \""
69                                                        + desc[0].replaceAll("\\n", " ")
70                                                        + "\" ("
71                                                        + stringArray[1]
72                                                        + " "
73                                                        + stringArray[2]
74                                                        + ")";
75                                        this.getLoggingInterface()
76                                                .logWarning(Integer.valueOf(desc[2]).intValue(),
77                                                                Integer.valueOf(desc[2]).intValue(),
78                                                                MessageClass.DOCUMENTATION,
79                                                                warning);
80                                        // TODO: revise XML usage
81                                        // T3D.printLog(LEVEL0, getCurrentTTCN3File(),
82                                        // Integer.valueOf(desc[2]).intValue(),
83                                        // Integer.valueOf(desc[2]).intValue(), 1, warning);
84                                }
85                                return true;
86                        }
87                return false;
88        }
89       
90       
91        //adds XML representation of a module to project.xml and prints warnings
92        public void printModule(String name, TTCN3Comment comment, String behaviour, int line){
93                if (modules.contains(name)) {
94                        int repetitionIndex = 0;
95                        while(modules.contains(name + "_"+repetitionIndex)){
96                                repetitionIndex++;
97                        }
98                        this.getLoggingInterface().logWarning(line,
99                                        line,
100                                        MessageClass.DOCUMENTATION,
101                                        "Module \""+name+"\" (in file \""+this.getCurrentTTCN3File()+"\") is declared multiple times. The instance will be refererred to as \""+name+"_"+repetitionIndex+"\"!");
102
103                        printModule(name+"_"+repetitionIndex,comment, behaviour, line);
104                        modules.add(name+"_"+repetitionIndex);
105                } else {
106                        writeStream("\n<module>\n<name>" + name + "</name>" + comment.toString()
107                                        + "<behaviour>" + behaviour.replaceAll("<tab/>", "     ") + "</behaviour>"
108                                        + "<modulename>" + name + "</modulename>"
109                                        + "\n</module>");
110                        modules.add(name);
111                        files += 3;
112                        currentModule = name;
113                        for (String warning : comment.getWarnings()) {
114                                this.getLoggingInterface().logWarning(line,
115                                                line,
116                                                MessageClass.DOCUMENTATION,
117                                                warning);
118                                // TODO: revise XML usage
119                                // T3D.printLog(LEVEL0, getCurrentTTCN3File(), line, line, 1,
120                                // warning);
121                        }
122                }
123        }
124       
125        //adds XML representation of a group to project.xml
126        public void printGroup(String name, String location, TTCN3Comment comment, String behaviour, String path, int line){
127                        writeStream("\n<group>\n<name>" + name + "</name>\n"
128                                + "<location>" + location + "</location>"
129                                + comment.toString()
130                                + "<behaviour>" + behaviour.replaceAll("<tab/>", "     ") + "</behaviour>"
131                                + "<modulename>" + currentModule + "</modulename>"
132                                + path
133                                + "\n</group>");
134                        files += 2;
135        }
136        //adds <file> elements to project.xml finishes the root element
137        public void finishXML(){
138                files = files + 3;
139                writeStream("\n</project>");
140        }
141       
142        public void setCurrentTTCN3File(String currentTTCN3File) {
143                this.currentTTCN3File = currentTTCN3File;
144        }
145
146        public String getCurrentTTCN3File() {
147                return currentTTCN3File;
148        }
149
150        public void setLoggingInterface(LoggingInterface logger) {
151                this.logger = logger;
152        }
153
154        public LoggingInterface getLoggingInterface() {
155                return logger;
156        }
157       
158}
Note: See TracBrowser for help on using the repository browser.