1 package org.overturetool.traces.utility;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileReader;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.PrintWriter;
9 import java.io.StringBufferInputStream;
10 import java.io.Writer;
11
12 public class ClassExstractorFromTexFiles
13 {
14 private static final String CLASS_START = "class ";
15 private static final String CLASS_END = "end ";
16
17 private static final String VDM_START = "//begin{vdm_al}";
18 private static final String VDM_END = "//end{vdm_al}";
19
20 public static String[] exstract(String[] files, String outputDir) throws IOException
21 {
22 File outputDirrectory = new File(outputDir);
23 if(outputDir.length()==0 )
24 return null;
25
26 if(!outputDirrectory.exists())
27 outputDirrectory.mkdir();
28
29
30 String[] newFiles = new String[files.length];
31 for (int i = 0; i < files.length; i++)
32 {
33
34
35 newFiles[i] = files[i];
36
37
38 File f = new File(files[i]);
39
40 newFiles[i] = outputDirrectory.getAbsolutePath() + File.separatorChar + f.getName();
41 if(newFiles[i].endsWith(".tex"))
42 newFiles[i] = newFiles[i]+".vpp";
43
44 System.out.println(newFiles[i]);
45
46 FileReader inputFileReader = new FileReader(files[i]);
47 FileWriter outputFileReader = new FileWriter(newFiles[i]);
48
49
50 BufferedReader inputStream = new BufferedReader(inputFileReader);
51 PrintWriter outputStream = new PrintWriter(outputFileReader);
52
53 String inLine = null;
54 Boolean skip = true;
55 String currentClass = "";
56 Boolean enabled = false;
57 Boolean texTagsFound= false;
58 while ((inLine = inputStream.readLine()) != null)
59 {
60 if (inLine.trim().startsWith(CLASS_START))
61 {
62 String classString = inLine.trim().substring(CLASS_START.length()).trim();
63 int indexOfInh = classString.indexOf(':');
64 int indexOfSpace = classString.indexOf(' ');
65 if(indexOfInh >= 0 || indexOfSpace >=0)
66 {
67
68 if(indexOfInh >= 0)
69 currentClass = classString.substring(0,indexOfInh);
70 else
71 currentClass = classString.substring(0,indexOfSpace);
72
73 }else
74 currentClass = classString;
75
76 enabled = true;
77 texTagsFound= true;
78
79 }
80 else if (inLine.trim().startsWith(CLASS_END+currentClass))
81 {
82 outputStream.println(inLine);
83 enabled = false;
84 continue;
85
86 }
87 if (inLine.trim().startsWith(VDM_START))
88 {
89 enabled = true;
90 outputStream.println("");
91 continue;
92
93 }
94 if (inLine.trim().startsWith(VDM_END))
95 {
96 enabled = false;
97 outputStream.println("");
98 continue;
99
100 }
101
102
103 if (enabled)
104 outputStream.println(inLine);
105 else
106 outputStream.println("");
107 }
108 outputStream.close();
109 inputStream.close();
110 if(!texTagsFound)
111 new File(newFiles[i]).delete();
112
113 }
114 return newFiles;
115 }
116
117 public static String exstractAsString(String file) throws IOException
118 {
119 FileReader inputFileReader = new FileReader(file);
120
121 BufferedReader inputStream = new BufferedReader(inputFileReader);
122
123 StringBuilder outputStream =new StringBuilder();
124
125 String inLine = null;
126 Boolean skip = true;
127 String currentClass = "";
128 Boolean enabled = false;
129 Boolean texTagsFound= false;
130 Boolean fileContainsTex=false;
131 while ((inLine = inputStream.readLine()) != null)
132 {
133 if(inLine.trim().startsWith("//") )
134 fileContainsTex=true;
135
136 if (inLine.trim().startsWith(CLASS_START) && ((fileContainsTex && texTagsFound)||(!fileContainsTex && !texTagsFound)))
137 {
138 String classString = inLine.trim().substring(CLASS_START.length()).trim();
139 int indexOfInh = classString.indexOf(':');
140 int indexOfSpace = classString.indexOf(' ');
141 if(indexOfInh >= 0 || indexOfSpace >=0)
142 {
143
144 if(indexOfInh >= 0)
145 currentClass = classString.substring(0,indexOfInh);
146 else
147 currentClass = classString.substring(0,indexOfSpace);
148
149 }else
150 currentClass = classString;
151
152 enabled = true;
153
154
155 }
156 else if (inLine.trim().startsWith(CLASS_END+currentClass))
157 {
158 outputStream.append("\n"+inLine);
159 enabled = false;
160 texTagsFound= false;
161 continue;
162
163 }
164 if (inLine.trim().startsWith(VDM_START))
165 {
166 enabled = true;
167 texTagsFound= true;
168 outputStream.append("\n"+"");
169 continue;
170
171 }
172 if (inLine.trim().startsWith(VDM_END))
173 {
174 enabled = false;
175 outputStream.append("\n"+"");
176 continue;
177
178 }
179
180
181 if (enabled)
182 outputStream.append("\n"+inLine);
183 else
184 outputStream.append("\n"+"");
185 }
186 inputStream.close();
187
188 return outputStream.toString();
189 }
190 }