您的位置:首页 > 其它

多叉树遍历生成文件提供下载

2011-08-30 13:23 323 查看
树节点的结构和构造函数

package com.domain;

import java.io.Serializable;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import com.help.XmlFunc;
import com.service.TestCaseService;
import com.service.TestGroupService;

public class GroupNode implements Serializable {

private static final long serialVersionUID = 1L;
private int parentId;//parentgroupid
private int selfId;//groupid
private String nodeName;//groupname
private GroupNode gn;//
private List<TestGroup> childList;//childgrouplist
private List<TestCase> tclist;//caselist

//构造函数
public GroupNode(int groupid){
TestGroupService tgs = new TestGroupService();
TestCaseService tcs = new TestCaseService();
this.selfId = groupid;
this.parentId = tgs.getParentGroupid(groupid);
//this.gn = tgs.getGroupInfo(groupid);
this.nodeName = tgs.getGroupInfo(groupid).getGroupname();
this.childList = tgs.getChildGroupList(groupid);
this.tclist = tcs.getCaseList(groupid);
}

/**
* 按层递归遍历,每次将node下面的tclist到处到文件
*/
public void traverse(String projectname,String dir) throws ParserConfigurationException{
//将group下面的用例列表导出到文件
XmlFunc.exportcasefile(this.tclist,this.nodeName,this.selfId,projectname,dir);

if(this.selfId==0){
return ;
}
if(childList==null || childList.isEmpty()){
return ;
}
int childNumber=childList.size();
for(int i= 0 ; i < childNumber;i++)
{
TestGroup child=childList.get(i);
int childgroupid = child.getGroupid();
GroupNode gn = new GroupNode(childgroupid);
gn.traverse(projectname,dir);
}
}

public boolean isLeaf()
{
if(childList==null){
return true;
}
else{
if(childList.isEmpty()){
return true;
}else{
return false;
}
}
}

public int getParentId() {
return parentId;
}

public int getSelfId() {
return selfId;
}

public String getNodeName() {
return nodeName;
}

public List<TestGroup> getChildList() {
return childList;
}

public List<TestCase> getTclist() {
return tclist;
}

}


exportcasefile 的实现:

public static int exportcasefile(List<TestCase> tclist,String groupname,int groupid,String projectname,String dir) throws ParserConfigurationException {
//创建xml对象,将从数据库中读出的数据写到xml中
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setIgnoringElementContentWhitespace(true);
DocumentBuilder db=factory.newDocumentBuilder();
Document xmldoc=db.newDocument();
Element testgroup = xmldoc.createElement("testgroup");
TestGroupService tgs = new TestGroupService();
String prefix = tgs.getAbsolutelyGroupname(groupid);
testgroup.setAttribute("module", prefix+groupname);
testgroup.setAttribute("projectname",projectname);
for(int i = 0;i<tclist.size();i++){
Element testcase = xmldoc.createElement("testcase");
TestCase tc = tclist.get(i);
String casecode = tc.getTestcasename();
String description = tc.getDescription();
String classname = tc.getClassname();
String caselevel = tc.getCaselevel();
int caseid = tc.getCaseid();
//String creator = tc.getCreator();
testcase.setAttribute("casecode", casecode);
testcase.setAttribute("description", description);
testcase.setAttribute("classname", classname);
testcase.setAttribute("caselevel", caselevel);
TestStepService tss = new TestStepService();
List<TestStep> tslist = tss.getStepList(caseid);
for(int j = 0;j<tslist.size();j++){
Element teststep = xmldoc.createElement("step");
TestStep ts = tslist.get(j);
String type = ts.getSteptype();
String method = ts.getStepmethod();
String stepdescription = ts.getDescription();
String stepmodule = ts.getModule();
String stepclassname = ts.getClassname();
int stepid = ts.getId();
teststep.setAttribute("type", type);
teststep.setAttribute("method", method);
teststep.setAttribute("description", stepdescription);
if((stepmodule !=null)&& (stepmodule !="")){
teststep.setAttribute("module", stepmodule);
}
if((stepclassname !=null)&& (stepclassname !="")){
teststep.setAttribute("classname", stepclassname);
}
TestDataService tds = new TestDataService();
List<TestData> tdlist = tds.getTestDataList(stepid);
for(int k = 0;k<tdlist.size();k++){
Element testdata = xmldoc.createElement("inputdata");
TestData td = tdlist.get(k);
String key = td.getK_key();
String value = td.getValue();
String datatype = td.getType();
String datadescription = td.getDescription();

testdata.setAttribute("key", key);
testdata.setAttribute("value", value);
testdata.setAttribute("description", datadescription);
testdata.setAttribute("datatype", datatype);
teststep.appendChild(testdata);
}

testcase.appendChild(teststep);
}
testgroup.appendChild(testcase);
}
//将写完的xml对象输出到文件
saveXml(dir+"//"+groupname+".xml",testgroup);
return 0;

}
savexml实现:

public static void saveXml(String fileName, Node node) {//将Document输出到文件
TransformerFactory transFactory=TransformerFactory.newInstance();
try {
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
DOMSource source=new DOMSource();
source.setNode(node);
StreamResult result=new StreamResult();
result.setOutputStream(new FileOutputStream(fileName));

transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
下载文件的servlet的代码:

GroupNode gn = new GroupNode(groupid);
try{
//生成时间戳
long timestamp = System.currentTimeMillis();
timestamp = timestamp/1000;
String dir = tempFile+timestamp;
//如果不存在,则创建这个目录
File f=new File(dir);
if(!f.exists())
f.mkdirs();
gn.traverse(projectname,dir);
//对导出的文件进行压缩
CompressFile bean = new CompressFile();
String zipfilename = zipFile+timestamp+".zip";
try{
bean.zip(dir, zipfilename);
//删除导出的临时文件
DeleteFileUtil.delete(dir);
response = download(zipfilename,response);
//删除生成的zip压缩文件
DeleteFileUtil.delete(zipfilename);
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}


download下载函数实现:

public HttpServletResponse download(String path, HttpServletResponse response) {
try {
// path是指欲下载的文件的路径。
File file = new File(path);
// 取得文件名。
String filename = file.getName();
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();

// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return response;
}


前台一句话实现下载:

function exportcasefile(groupid){

window.open('DownloadCasefile?groupid='+groupid,'#');

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: