-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileConverter.java
More file actions
267 lines (228 loc) · 6.35 KB
/
FileConverter.java
File metadata and controls
267 lines (228 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import java.io.*;
import java.util.*;
/** @author Gergely Kota
File conversion utility reads in all Files in the specified directory.
Upon covert(), it uses the DependencyTree of provided conversions to
convert all files to the final type in the least amount of steps.
*/
public class FileConverter
{
private File currentDir;
private String dirName;
private File[] files;
private DependencyTree dt;
private String endtype;
private ArrayList loggers;
private static HashMap filemap = new HashMap();
public FileConverter(File f)
{
currentDir = f;
loggers = new ArrayList();
dirName = clean(currentDir.getAbsolutePath());
// and damn windows for using an escape character in their filepaths
endtype = Config.read("endtype");
endtype = (endtype == null)? "txt": endtype;
files = f.listFiles(new TypeFilter());
dt = new DependencyTree("txt");
createTree();
dt.order();
}
public static void add(File became, File was)
{
filemap.put(became, was);
}
public static File getPrevious(File f)
{
return (File) filemap.get(f);
}
public static File getOriginal(File f)
{
File temp = (File) filemap.get(f);
if(temp == null)
return f;
while(filemap.get(temp) != null)
temp = (File) filemap.get(temp);
return temp;
}
private void log(String s)
{
for(int i = 0; i < loggers.size(); i++)
((Logger)loggers.get(i)).log(s);
}
public void addLogger(Logger l)
{
if(!loggers.contains(l))
loggers.add(l);
}
private String clean(String s)
{
char[] c = s.toCharArray();
for(int i = 0; i < c.length; i++)
if(c[i] == '\\')
c[i] = '/';
return new String(c);
}
// create the Tree of conversion possibilites. Each vertex is a filetype,
// each edge represents the ability to convert from the parent to the child
private void createTree()
{
String[] types = getTypes();
// for each type, log all conversion possibilities
// for the (i==j) case, use that to check conversion to the endtype
for(int i = 0; i < types.length; i++)
for(int j = 0; j < types.length; j++)
if(i != j)
addNode(types[i], types[j]);
else
addNode(types[i]);
}
private void addNode(String t1)
{
addNode(t1, endtype);
}
private void addNode(String t1, String t2)
{
if(Config.read(t1 + "2" + t2) != null)
dt.addPair(t1, t2);
}
/** performs the conversions on all the files based on the DependencyTree
result generated earlier.
*/
public void convert()
{
DependencyTree.Vertex[] v = dt.getOrder();
for(int i = 0; i < v.length-1; i++)
{
// get all files of the type to convert
// get the type to convert to
// call the conversion command for these types on the file
File[] f = currentDir.listFiles(new QuickFilter(v[i].name()));
String next = v[i].next().name();
for(int j = 0; j < f.length; j++)
convert(f[j], (v[i].name() + "2" + next));
}
File[] dirs = currentDir.listFiles(new DirFilter());
if(dirs == null)
return;
for(int i = 0; i < dirs.length; i++)
{
// add the same listeners
FileConverter recurser = new FileConverter(dirs[i]);
for(int j = 0; j < loggers.size(); j++)
recurser.addLogger((Logger)loggers.get(j));
recurser.convert();
}
}
// parses the generic command in the config file and replaces
// keywords with the appropriate values.
private void convert(File f, String key)
{
String command = Config.read(key);
String name = f.getName();
Debug.println(command);
command = command.replaceAll("workingdir", dirName);
Debug.println(command);
command = command.replaceAll("infile", name);
String infile = name;
Debug.println(command);
String end = key.substring(key.lastIndexOf("2")+1);
name = name.substring(0, name.lastIndexOf(".")+1) + end;
File test = new File(dirName + "/" + name);
while(test.exists())
test = new File(dirName + "/" + (name = "_" + name));
command = command.replaceAll("outfile", name);
String outfile = name;
Debug.println(command);
try
{
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
int exit = p.exitValue();
String action = exit == 0? "Succeeded" : "Failed";
// Debug.println("Succeeded: " + command);
log("Converted " + f.getAbsolutePath() + " to " + test.getAbsolutePath() + " ... " + action);
FileConverter.add(new File(outfile), new File(infile));
}
catch(Exception e)
{
Debug.println("Failed: " + command);
log("FAILED: " + command);
}
}
/** shows all the found types and what each is converted to in the process
*/
public String toString()
{
String s = "";
for(int i = 0; i < files.length; i++)
s += files[i].getName() + "\n";
s += dt.orderString();
return s;
}
// reads the types of files in use from the config file
// has a hardcoded default for ps and pdf.
private String[] getTypes()
{
String s = Config.read("filetype");
if(s == null)
return new String[]{"ps", "pdf"}; // default
StringTokenizer st = new StringTokenizer(s);
String[] types = new String[st.countTokens()];
for(int i = 0; i < types.length; i++)
types[i] = st.nextToken().toLowerCase();
return types;
}
/* ---------------------------------------------- */
/* ---------------------------------------------- */
/* ---------------------------------------------- */
// picks up all Files that are of the types specified in the config file
private class TypeFilter implements FileFilter
{
private String[] types;
public TypeFilter()
{
types = getTypes();
}
public boolean accept(File pathname)
{
String name = pathname.getName();
for(int i = 0; i < types.length; i++)
if(name.endsWith("." + types[i]))
return true;
return false;
}
}
private class DirFilter implements FileFilter
{
public boolean accept(File f)
{
return f.isDirectory();
}
}
// picks up all Files that are of the passed type
private class QuickFilter implements FileFilter
{
private String end;
public QuickFilter(String s)
{
end = "." + s;
}
public boolean accept(File pathname)
{
String name = pathname.getName().toLowerCase();
if(name.endsWith(end))
return true;
return false;
}
}
/* ---------------------------------------------- */
/* ---------------------------------------------- */
/* ---------------------------------------------- */
public static void main(String[] args)
{
FileConverter fc = new FileConverter(new File("Test/Test"));
// System.out.println(" ------------- ");
// System.out.println(fc);
fc.convert();
}
}