-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLUtil.java
157 lines (122 loc) · 5.38 KB
/
XMLUtil.java
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
package javaToolkit.lib.utils;
import java.io.File;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.*;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import smu.bowen.ct4j.datapreparation.Group;
import smu.bowen.ct4j.datapreparation.Constants;
public class XMLUtil {
public static Boolean writeProjectGroup(Group group, String xmlPath) {
try {
DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
// root element
Element root = document.createElement("list");
document.appendChild(root);
// projectGroup element
Element projectGroup = document.createElement("projectGroup");
root.appendChild(projectGroup);
// set an attribute to staff element
Attr groupAttr = document.createAttribute("id");
groupAttr.setValue(String.valueOf(group.groupId));
projectGroup.setAttributeNode(groupAttr);
// bowen define the #train
System.out.println(group.methodList.size());
if (group.methodList.size() < 10) {
return null;
}
int trainNum;
if ((int) (group.methodList.size() * Constants.trainRatio) > 40) {
trainNum = 40;
} else {
trainNum = (int) (group.methodList.size() * Constants.trainRatio);
}
for (int i = 0; i < trainNum; i++) {
System.out.println(group.methodList.get(i).oldProjectName);
Method method = group.methodList.get(i);
method.id = i + 1;
// method element
Element projectMethod = document.createElement("projectMethod");
projectGroup.appendChild(projectMethod);
// set an attribute to staff element
Attr methodAttr = document.createAttribute("id");
methodAttr.setValue(String.valueOf(method.id));
projectMethod.setAttributeNode(methodAttr);
// src element
Element src = document.createElement("src");
src.appendChild(document.createTextNode("SOMETHING"));
projectMethod.appendChild(src);
// oldProjectName element
Element oldProjectName = document.createElement("oldProjectName");
oldProjectName.appendChild(document.createTextNode(method.oldProjectName));
projectMethod.appendChild(oldProjectName);
// newProjectName element
Element newProjectName = document.createElement("newProjectName");
newProjectName.appendChild(document.createTextNode(method.newProjectName));
projectMethod.appendChild(newProjectName);
// oldClassName element
Element oldClassName = document.createElement("oldClassName");
oldClassName.appendChild(document.createTextNode(method.oldClassName));
projectMethod.appendChild(oldClassName);
// newClassName element
Element newClassName = document.createElement("newClassName");
newClassName.appendChild(document.createTextNode(method.newClassName));
projectMethod.appendChild(newClassName);
// oldFilePath element
Element oldFilePath = document.createElement("oldFilePath");
oldFilePath.appendChild(document.createTextNode(method.oldFilePath));
projectMethod.appendChild(oldFilePath);
// newFilePath element
Element newFilePath = document.createElement("newFilePath");
newFilePath.appendChild(document.createTextNode(method.newFilePath));
projectMethod.appendChild(newFilePath);
// oldMethodName element
Element oldMethodName = document.createElement("oldMethodName");
oldMethodName.appendChild(document.createTextNode(method.oldMethodName));
projectMethod.appendChild(oldMethodName);
// newMethodName element
Element newMethodName = document.createElement("newMethodName");
newMethodName.appendChild(document.createTextNode(method.newMethodName));
projectMethod.appendChild(newMethodName);
}
// candidate element
int candidateNum = 1;
Element candidateProjects = document.createElement("candidateProjects");
projectGroup.appendChild(candidateProjects);
for (int i = trainNum; i < group.methodList.size(); i++) {
Method method = group.methodList.get(i);
// newMethodName element
Element candidateProject = document.createElement("candidateProject");
// set an attribute to staff element
Attr candidateProjectAttr = document.createAttribute("id");
candidateProjectAttr.setValue(String.valueOf(candidateNum));
candidateProject.setAttributeNode(candidateProjectAttr);
candidateNum++;
candidateProject.appendChild(document.createTextNode(method.oldProjectName));
candidateProjects.appendChild(candidateProject);
}
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
// Output to console for testing
StreamResult resultConsole = new StreamResult(System.out);
transformer.transform(source, resultConsole);
StreamResult resultFile = new StreamResult(new File(xmlPath));
transformer.transform(source, resultFile);
System.out.println("File saved!");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
return true;
}
}