Skip to content

Commit b25ec0b

Browse files
committed
add xmlutil
1 parent 2afe75e commit b25ec0b

File tree

3 files changed

+46
-168
lines changed

3 files changed

+46
-168
lines changed

lib/utils/FileUtil.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public static String readFile2Str(String fpath) {
6666

6767
public static Boolean writeStr2File(String wStr, String fPath) {
6868
try {
69+
Path parentDir = Paths.get(fPath).getParent();
70+
if (!Files.exists(parentDir)) {
71+
Files.createDirectories(parentDir);
72+
}
6973
Files.write(Paths.get(fPath), wStr.getBytes());
7074
} catch (IOException e) {
7175
e.printStackTrace();
@@ -76,6 +80,9 @@ public static Boolean writeStr2File(String wStr, String fPath) {
7680

7781
public static Boolean writeStr2File(String wStr, Path fPath) {
7882
try {
83+
Path parentDir = fPath.getParent();
84+
if (!Files.exists(parentDir))
85+
Files.createDirectories(parentDir);
7986
Files.write(fPath, wStr.getBytes());
8087
} catch (IOException e) {
8188
e.printStackTrace();
@@ -86,6 +93,9 @@ public static Boolean writeStr2File(String wStr, Path fPath) {
8693

8794
public static Boolean writeStrList2File(List<String> strList, Path fPath, Boolean ifTrim) {
8895
try {
96+
Path parentDir = fPath.getParent();
97+
if (!Files.exists(parentDir))
98+
Files.createDirectories(parentDir);
8999
String wStr = "";
90100
for (String line : strList) {
91101
if (ifTrim) {

lib/utils/GitUtil.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static Boolean clone(String repoName, String usrName, Path targetDir) {
2929

3030
public static List<String> getAllCommitsSha(Path repoDir) {
3131

32-
String cmd = "timeout 300 git log --pretty=format:\"%H\"";
32+
String cmd = "timeout 60 git log --pretty=format:\"%H\"";
3333

3434
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
3535
if (pr.exitCode == 0) {
@@ -43,7 +43,7 @@ public static List<String> getAllCommitsSha(Path repoDir) {
4343

4444
public static String getCommitMsg(Path repoDir, String com) {
4545

46-
String cmd = "timeout 300 git log --format=%B -n 1 " + com;
46+
String cmd = "timeout 60 git log --format=%B -n 1 " + com;
4747

4848
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
4949
if (pr.exitCode == 0) {
@@ -57,7 +57,7 @@ public static String getCommitMsg(Path repoDir, String com) {
5757

5858
public static String getParentCommit(Path repoDir, String com) {
5959

60-
String cmd = "timeout 300 git log --pretty=%P -n 1 " + com;
60+
String cmd = "timeout 60 git log --pretty=%P -n 1 " + com;
6161

6262
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
6363
if (pr.exitCode == 0) {
@@ -81,10 +81,10 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
8181

8282
String cmd = null;
8383
if (diffMode != null) {
84-
cmd = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString() + " diff "
84+
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString() + " diff "
8585
+ diffMode + " --unified=0 " + oldCom + " " + newCom;
8686
} else {
87-
cmd = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
87+
cmd = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
8888
+ " diff --unified=0 " + oldCom + " " + newCom;
8989
}
9090

@@ -100,7 +100,7 @@ public static String getDiffBetween2Commits(Path repoDir, String oldCom, String
100100

101101
public static List<String> getChangedFileList(Path repoDir, String com) {
102102

103-
String cmd = "timeout 300 git diff-tree --no-commit-id --name-only -r " + com;
103+
String cmd = "timeout 30 git diff-tree --no-commit-id --name-only -r " + com;
104104

105105
ProcessUtil.ProcessReporter pr = ProcessUtil.executeCMD(cmd, null, repoDir);
106106
if (pr.exitCode == 0) {
@@ -117,25 +117,25 @@ public static Boolean checkout(Path repoDir, String com, Boolean ifForce) {
117117
ProcessUtil.ProcessReporter pr = new ProcessUtil.ProcessReporter();
118118

119119
if (ifForce) {
120-
String resetCMD = "timeout 300 git reset --hard";
120+
String resetCMD = "timeout 60 git reset --hard";
121121
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
122122
}
123123

124124
String checkoutCMD = null;
125125
if (com == null) { // checkout latest if null
126-
checkoutCMD = "timeout 300 git checkout master";
126+
checkoutCMD = "timeout 60 git checkout master";
127127
} else {
128-
checkoutCMD = "timeout 300 git checkout " + com;
128+
checkoutCMD = "timeout 60 git checkout " + com;
129129
}
130130
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);
131131

132132
if (pr.exitCode == 0) {
133133
return true;
134134
} else {
135-
String cleanCMD = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
135+
String cleanCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
136136
+ " clean -dfx .";
137137
pr = ProcessUtil.executeCMD(cleanCMD, null, repoDir);
138-
String resetCMD = "timeout 300 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
138+
String resetCMD = "timeout 60 git --git-dir " + repoDir.toString() + " /.git --work-tree " + repoDir.toString()
139139
+ " reset --hard";
140140
pr = ProcessUtil.executeCMD(resetCMD, null, repoDir);
141141
pr = ProcessUtil.executeCMD(checkoutCMD, null, repoDir);

lib/utils/XMLUtil.java

Lines changed: 25 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,157 +1,25 @@
1-
//package javaToolkit.lib.utils;
2-
//
3-
//import java.io.File;
4-
//import javax.xml.transform.*;
5-
//import javax.xml.transform.dom.DOMSource;
6-
//import javax.xml.transform.stream.StreamResult;
7-
//import javax.xml.parsers.*;
8-
//import org.w3c.dom.Attr;
9-
//import org.w3c.dom.Document;
10-
//import org.w3c.dom.Element;
11-
//
12-
//import smu.bowen.ct4j.datapreparation.Group;
13-
//import smu.bowen.ct4j.datapreparation.Constants;
14-
//
15-
//public class XMLUtil {
16-
//
17-
// public static Boolean writeProjectGroup(Group group, String xmlPath) {
18-
//
19-
// try {
20-
// DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
21-
// DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
22-
// Document document = documentBuilder.newDocument();
23-
//
24-
// // root element
25-
// Element root = document.createElement("list");
26-
// document.appendChild(root);
27-
//
28-
// // projectGroup element
29-
// Element projectGroup = document.createElement("projectGroup");
30-
// root.appendChild(projectGroup);
31-
//
32-
// // set an attribute to staff element
33-
// Attr groupAttr = document.createAttribute("id");
34-
// groupAttr.setValue(String.valueOf(group.groupId));
35-
// projectGroup.setAttributeNode(groupAttr);
36-
//
37-
// // bowen define the #train
38-
// System.out.println(group.methodList.size());
39-
// if (group.methodList.size() < 10) {
40-
// return null;
41-
// }
42-
// int trainNum;
43-
// if ((int) (group.methodList.size() * Constants.trainRatio) > 40) {
44-
// trainNum = 40;
45-
// } else {
46-
// trainNum = (int) (group.methodList.size() * Constants.trainRatio);
47-
// }
48-
//
49-
// for (int i = 0; i < trainNum; i++) {
50-
//
51-
// System.out.println(group.methodList.get(i).oldProjectName);
52-
//
53-
// Method method = group.methodList.get(i);
54-
// method.id = i + 1;
55-
// // method element
56-
// Element projectMethod = document.createElement("projectMethod");
57-
// projectGroup.appendChild(projectMethod);
58-
//
59-
// // set an attribute to staff element
60-
// Attr methodAttr = document.createAttribute("id");
61-
// methodAttr.setValue(String.valueOf(method.id));
62-
// projectMethod.setAttributeNode(methodAttr);
63-
//
64-
// // src element
65-
// Element src = document.createElement("src");
66-
// src.appendChild(document.createTextNode("SOMETHING"));
67-
// projectMethod.appendChild(src);
68-
//
69-
// // oldProjectName element
70-
// Element oldProjectName = document.createElement("oldProjectName");
71-
// oldProjectName.appendChild(document.createTextNode(method.oldProjectName));
72-
// projectMethod.appendChild(oldProjectName);
73-
//
74-
// // newProjectName element
75-
// Element newProjectName = document.createElement("newProjectName");
76-
// newProjectName.appendChild(document.createTextNode(method.newProjectName));
77-
// projectMethod.appendChild(newProjectName);
78-
//
79-
// // oldClassName element
80-
// Element oldClassName = document.createElement("oldClassName");
81-
// oldClassName.appendChild(document.createTextNode(method.oldClassName));
82-
// projectMethod.appendChild(oldClassName);
83-
//
84-
// // newClassName element
85-
// Element newClassName = document.createElement("newClassName");
86-
// newClassName.appendChild(document.createTextNode(method.newClassName));
87-
// projectMethod.appendChild(newClassName);
88-
//
89-
// // oldFilePath element
90-
// Element oldFilePath = document.createElement("oldFilePath");
91-
// oldFilePath.appendChild(document.createTextNode(method.oldFilePath));
92-
// projectMethod.appendChild(oldFilePath);
93-
//
94-
// // newFilePath element
95-
// Element newFilePath = document.createElement("newFilePath");
96-
// newFilePath.appendChild(document.createTextNode(method.newFilePath));
97-
// projectMethod.appendChild(newFilePath);
98-
//
99-
// // oldMethodName element
100-
// Element oldMethodName = document.createElement("oldMethodName");
101-
// oldMethodName.appendChild(document.createTextNode(method.oldMethodName));
102-
// projectMethod.appendChild(oldMethodName);
103-
//
104-
// // newMethodName element
105-
// Element newMethodName = document.createElement("newMethodName");
106-
// newMethodName.appendChild(document.createTextNode(method.newMethodName));
107-
// projectMethod.appendChild(newMethodName);
108-
// }
109-
//
110-
// // candidate element
111-
// int candidateNum = 1;
112-
// Element candidateProjects = document.createElement("candidateProjects");
113-
// projectGroup.appendChild(candidateProjects);
114-
//
115-
// for (int i = trainNum; i < group.methodList.size(); i++) {
116-
//
117-
// Method method = group.methodList.get(i);
118-
//
119-
// // newMethodName element
120-
// Element candidateProject = document.createElement("candidateProject");
121-
//
122-
// // set an attribute to staff element
123-
// Attr candidateProjectAttr = document.createAttribute("id");
124-
// candidateProjectAttr.setValue(String.valueOf(candidateNum));
125-
// candidateProject.setAttributeNode(candidateProjectAttr);
126-
//
127-
// candidateNum++;
128-
//
129-
// candidateProject.appendChild(document.createTextNode(method.oldProjectName));
130-
// candidateProjects.appendChild(candidateProject);
131-
// }
132-
//
133-
// // write the content into xml file
134-
// TransformerFactory transformerFactory = TransformerFactory.newInstance();
135-
// Transformer transformer = transformerFactory.newTransformer();
136-
// transformer.setOutputProperty(OutputKeys.INDENT, "yes");
137-
// transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
138-
// DOMSource source = new DOMSource(document);
139-
//
140-
// // Output to console for testing
141-
// StreamResult resultConsole = new StreamResult(System.out);
142-
// transformer.transform(source, resultConsole);
143-
//
144-
// StreamResult resultFile = new StreamResult(new File(xmlPath));
145-
// transformer.transform(source, resultFile);
146-
// System.out.println("File saved!");
147-
//
148-
// } catch (ParserConfigurationException pce) {
149-
// pce.printStackTrace();
150-
// } catch (TransformerException tfe) {
151-
// tfe.printStackTrace();
152-
// }
153-
//
154-
// return true;
155-
// }
156-
//
157-
//}
1+
package javaToolkit.lib.utils;
2+
3+
import java.io.StringWriter;
4+
5+
import javax.xml.transform.*;
6+
import javax.xml.transform.dom.DOMSource;
7+
import javax.xml.transform.stream.StreamResult;
8+
import org.w3c.dom.Node;
9+
10+
public class XMLUtil {
11+
12+
public static String nodeToString(Node node) {
13+
StringWriter sw = new StringWriter();
14+
try {
15+
Transformer t = TransformerFactory.newInstance().newTransformer();
16+
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
17+
t.setOutputProperty(OutputKeys.INDENT, "yes");
18+
t.transform(new DOMSource(node), new StreamResult(sw));
19+
} catch (TransformerException te) {
20+
System.out.println("nodeToString Transformer Exception");
21+
}
22+
return sw.toString();
23+
}
24+
25+
}

0 commit comments

Comments
 (0)