-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackageGen.java
More file actions
150 lines (119 loc) · 5.35 KB
/
packageGen.java
File metadata and controls
150 lines (119 loc) · 5.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
import java.io.File;
import java.nio.file.*;
import java.io.IOException;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
class packageGen {
private static Map <String, ArrayList<String>> fileDirectory = new HashMap<>();
private static Set<String> suffixes = packageHelper.suffixHelper();
private static Set<String> directoryNames = packageHelper.directoryNameHelper();
private static Map<String, String> directoryMapping = packageHelper.directoryMappingHelper();
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Probably added too many args, try again");
System.exit(0);
}
PrintFiles pf = new PrintFiles();
Path startingDir = Paths.get(args[0]);
if (Files.exists(startingDir)) {
try {
Files.walkFileTree(startingDir, pf);
} catch (Exception e) {
System.out.println("An error has occurred while scanning the directory: " + e);
}
try {
generateXML();
} catch (Exception e) {
System.out.println("Error generating the XML File: " + e);
}
} else {
System.out.println("Invalid file path, try again");
System.exit(0);
}
}
private static void generateXML() {
DocumentBuilderFactory icFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder icBuilder;
try {
icBuilder = icFactory.newDocumentBuilder();
Document doc = icBuilder.newDocument();
Element mainRootElement = doc.createElementNS("http://soap.sforce.com/2006/04/metadata", "Package");
doc.appendChild(mainRootElement);
for (Map.Entry<String, ArrayList<String>> entry : fileDirectory.entrySet()) {
String key = entry.getKey();
ArrayList<String> value = entry.getValue();
Element types = doc.createElement("types");
mainRootElement.appendChild(types);
for (String s: value) {
Element members = doc.createElement("members");
types.appendChild(members);
members.setTextContent(s);
}
Element name = doc.createElement("name");
types.appendChild(name);
name.setTextContent(key);
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("package.xml"));
transformer.transform(source, result);
System.out.println("\nXML DOM Created Successfully..");
} catch (Exception e) {
e.printStackTrace();
}
}
private static class PrintFiles extends SimpleFileVisitor<Path> {
// Print information about
// each type of file.
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
String[] filename = file.getFileName().toString().split("\\.", 2);
String directoryName = file.getName(file.getNameCount() - 2).toString();
String mappedDirectory = directoryMapping.get(directoryName);
if (attr.isRegularFile() && suffixes.contains(filename[1])) {
if (!fileDirectory.containsKey(directoryMapping.get(directoryName))) {
fileDirectory.put(mappedDirectory, new ArrayList<>());
} else {
fileDirectory.get(directoryMapping.get(directoryName)).add(filename[0]);
}
}
return FileVisitResult.CONTINUE;
}
// Print each directory visited.
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
if (!directoryNames.contains(dir)) {
return FileVisitResult.SKIP_SUBTREE;
} else {
System.out.format("Directory: %s%n", dir);
return FileVisitResult.CONTINUE;
}
}
// If there is some error accessing
// the file, let the user know.
// If you don't override this method
// and an error occurs, an IOException
// is thrown.
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
System.err.println(exc);
return FileVisitResult.CONTINUE;
}
}
}