package Assignment2;
import java.io.*;
public class DirDumper {
private final String[] xml_tags = { "
",
"" };
private final CharSequence[] stopChars = { "<", ">", "&", "\"", "'" };
private final String[] entities = { "<", ">", "&", """,
"'" };
private BufferedWriter writer = null;
public void makeXMLFile(File directory) throws IOException {
writer = new BufferedWriter(new FileWriter(new File("/home/student/miller/home.xml")));
writer.write(xml_tags[4]
+ (xml_tags[0])
+ removeStopChars(directory.getName().equals("") ? directory
.getAbsolutePath() : directory.getName()) + "\">");
dump(directory);
writer.write(xml_tags[1]);
writer.flush();
writer.close();
}
private void dump(File file) throws IOException {
File[] dir = file.listFiles();
System.out.println("Ich arbeite...");
if (dir == null) {
return;
}
for (int i = 0; i < dir.length; i++) {
String name = removeStopChars(dir[i].getName());
if (dir[i].isDirectory()) {
writer.write((xml_tags[0]) + name + "\">"); //\n");
dump(dir[i].getAbsoluteFile());
writer.write(xml_tags[1]);
} else {
writer.write((xml_tags[2] + name + xml_tags[3]
+ dir[i].length() + "\" />")); //\n"));
}
}
}
private String removeStopChars(String name) {
for (int j = 0; j < stopChars.length; j++) {
if (name.contains(stopChars[j])) {
name = name.replace(stopChars[j], entities[j]);
}
}
return name;
}
public static void main(String args[]) throws IOException {
if (args[0] == null) {
System.out.println("Bitte Dateipfad als Parameter uebergeben");
} else {
DirDumper dir = new DirDumper();
dir.makeXMLFile(new File(args[0]));
}
}
}