package Testlink;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
//定义操作XML文件的方法
public class XMLUtil {
//定义工作目录
private File workspaceFile;
//定义所有文件对象
private File dirFile;
//定义buildXML文件夹
private File buildXMLDirectory;
//html文件夹
private File htmlFileDirectory;
//工作目录路径
private String workspacePath;
//buildXML文件夹路径
private String buildXMLDirectoryPath;
//定义build.xml文件路ing
private String buildXMLFilePath;
//html文件夹路径
private String htmlFileDirectoryPath;
//tap文件路径
private String tapPath;
//测试结果节点
private List<Node> testResult = new ArrayList();
//样本节点
private List<Integer> sampleNumber = new ArrayList();
//此方法用户改变build.xml文件
public void BuildXMLChange() {
//获取工作目录路径
workspaceFile = new File("");
workspacePath = workspaceFile.getAbsolutePath();
//获取工作目录下所有文件
this.dirFile = new File(workspacePath);
File\[\] files = dirFile.listFiles();
//定义buildXML文件夹路径
buildXMLDirectoryPath = workspacePath + "\\\\BuildXML";
//获取buildXML文件夹所有文件
buildXMLDirectory = new File(buildXMLDirectoryPath);
File\[\] xmlFiles = this.buildXMLDirectory.listFiles();
//获取build.xml文件路径
buildXMLFilePath = buildXMLDirectoryPath + "\\\\build.xml";
if (files.length != 0) {
File\[\] arrayOfFile1;
int j = (arrayOfFile1 = files).length;
for (int i = 0; i < j; i++) {
File file = arrayOfFile1\[i\];
if (file.getName().equals("BuildXML")) {
if (xmlFiles.length != 0) {
File\[\] arrayOfFile2;
int m = (arrayOfFile2 = xmlFiles).length;
for (int k = 0; k < m; k++) {
File xmlFile = arrayOfFile2\[k\];
if (xmlFile.getName().equals("build.xml")) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
//读取xml
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("file:///" + buildXMLFilePath);
NodeList nodeList = document.getElementsByTagName("property");
Node property = nodeList.item(0);
NamedNodeMap nodeMap = property.getAttributes();
Node node = nodeMap.getNamedItem("value");
node.setTextContent(workspacePath);
//写入XML
TransformerFactory factory = TransformerFactory.newInstance();
Transformer former = factory.newTransformer();
former.setOutputProperty("encoding", "GBK");
try {
former.transform(new DOMSource(document),
new StreamResult(new File(buildXMLFilePath)));
} catch (TransformerException e) {
e.printStackTrace();
}
System.out.println("build.xml文件不存在");
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e1) {
e1.printStackTrace();
}
}
}
} else {
System.out.println("BuildXML文件夹为空");
}
}
}
} else {
System.out.println("文件夹内容为空");
}
}
public void XMLToTAP() {
workspaceFile = new File("");
workspacePath = workspaceFile.getAbsolutePath();
dirFile = new File(workspacePath);
File\[\] files = dirFile.listFiles();
htmlFileDirectoryPath = (workspacePath + "\\\\XMLFolder");
htmlFileDirectory = new File(htmlFileDirectoryPath);
File\[\] htmlFiles = htmlFileDirectory.listFiles();
tapPath = (workspacePath + "\\\\TAPFolder");
if (files.length != 0) {
File\[\] arrayOfFile1;
int j = (arrayOfFile1 = files).length;
for (int i = 0; i < j; i++) {
File file = arrayOfFile1\[i\];
if (file.getName().equals("XMLFolder")) {
if (htmlFiles.length != 0) {
File\[\] arrayOfFile2;
int m = (arrayOfFile2 = htmlFiles).length;
for (int k = 0; k < m; k++) {
File xmlFile = arrayOfFile2\[k\];
if (xmlFile.getName().endsWith(".xml")) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFile);
Element rootElement = document.getDocumentElement();
NodeList nodeList = rootElement.getChildNodes();
if (rootElement.hasChildNodes()) {
for (int x = 0; x < nodeList.getLength(); x++) {
if (nodeList.item(x).getNodeType() == 1) {
this.testResult.add(nodeList.item(x));
}
}
} else {
System.out.println("接口测试结果为空");
}
for (int y = 0; y < this.testResult.size(); y++) {
if (((Node) this.testResult.get(y)).getNodeName().equals("sample")) {
this.sampleNumber.add(Integer.valueOf(y));
}
}
for (int z = 0; z < this.sampleNumber.size(); z++) {
NamedNodeMap namedNodeMap = ((Node) this.testResult
.get(((Integer) this.sampleNumber.get(z)).intValue())).getAttributes();
if (namedNodeMap.getNamedItem("s").getNodeValue().equals("false")) {
String lbValue = namedNodeMap.getNamedItem("lb").getNodeValue();
String\[\] fileContent = lbValue.split("-");
String fileNewPath = this.tapPath + "\\\\" + fileContent\[0\] + ".tap";
File fileNew = new File(fileNewPath);
FileWriter fWriter = new FileWriter(fileNew, true);
BufferedWriter bWriter = new BufferedWriter(fWriter);
bWriter.write("TAP version 13");
bWriter.newLine();
bWriter.write("1..1");
bWriter.newLine();
String resultStr = "not ok 1";
bWriter.write(resultStr);
bWriter.flush();
bWriter.close();
fWriter.close();
} else {
String lbValue = namedNodeMap.getNamedItem("lb").getNodeValue();
String\[\] fileContent = lbValue.split("-");
String fileNewPath = this.tapPath + "\\\\" + fileContent\[0\] + ".tap";
File fileNew = new File(fileNewPath);
FileWriter fWriter = new FileWriter(fileNew, true);
BufferedWriter bWriter = new BufferedWriter(fWriter);
bWriter.write("TAP version 13");
bWriter.newLine();
String resultStr = "1..1\\nok 1";
fWriter.write(resultStr);
bWriter.flush();
bWriter.close();
fWriter.close();
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("xml文件不存在");
}
}
} else {
System.out.println("XML文件夹为空");
}
}
}
} else {
System.out.println("文件夹内容为空");
}
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章