写附件和xml文件



import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class RefreshResearchReportJob {

	private static final Logger logger = Logger
			.getLogger(RefreshResearchReportJob.class);

	public static final String ANNEX_FOLDER = "annexFolder";

	// private String startDateStr;
	private ResearchReportCenterDbService researchReportService;
	private ResearchReportCache researchReportCache;

	// public String getStartDateStr() {
	// return startDateStr;
	// }
	//
	// public void setStartDateStr(String startDateStr) {
	// this.startDateStr = startDateStr;
	// }

	public ResearchReportCenterDbService getResearchReportService() {
		return researchReportService;
	}

	public void setResearchReportService(
			ResearchReportCenterDbService researchReportService) {
		this.researchReportService = researchReportService;
	}

	public ResearchReportCache getResearchReportCache() {
		return researchReportCache;
	}

	public void setResearchReportCache(ResearchReportCache researchReportCache) {
		this.researchReportCache = researchReportCache;
	}

	public void runJob() {
		logger.info("增量更新附件启动----日期xxxx");
		
		//获取项目根目录的绝对路径
		String rootPath = "";
		String classPath = RefreshResearchReportJob.class.getResource("/../..")+"";
		rootPath = classPath.substring(classPath.indexOf("D:"));
		rootPath = rootPath.replace("/", "\\");
		
		long startMillis = System.currentTimeMillis();

		String currentTime = CalendarUtil.getCurrentTime();
		long lastReportId = 0;

		// 首先判断datas.xml文件是否存在,不存在则创建
		String xmlFilePath = rootPath + "\\reports" + "\\" + ANNEX_FOLDER;
		FileUtil.createFolder(xmlFilePath);
		String filePath = xmlFilePath + "\\" + ResearchReportAnnex.DATAS_XML;

		SAXBuilder builder = new SAXBuilder(false);
		Document document = null;
		Element rootElement = null;
		try {
			File xmlFile = new File(filePath);
			if (xmlFile.exists()) {
				document = builder.build(filePath);
				// 读取datas.xml文件,取出last-report-id作为参数去执行第一次查询
				rootElement = document.getRootElement();
				Element headElement = rootElement.getChild("head");
				lastReportId = Long.parseLong(headElement.getChild(
						"last-report-id").getText());
			} else {
				document = createDocument();
				XMLOutputter XMLOut = new XMLOutputter(FormatXML());
				try {
					XMLOut.output(document, new FileOutputStream(filePath));
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		Date startDate = CalendarUtil.transToDate("2011-01-05");
		Date endDate = CalendarUtil.transToDate("2011-01-05");
		// 第一次查询出数据量较小的字段,放到内存中
		List<ResearchReport> reportWithAnnexList = researchReportService
				.getReportsWithAnnexes(startDate, endDate, lastReportId);
		for (int i = 0; i < reportWithAnnexList.size(); i++) {
			ResearchReport researchReport = reportWithAnnexList.get(i);

			// 放入缓存中
			researchReportCache.addResearchReport(researchReport);

			// 如果有附件,就创建相应的文件夹
			long reportId = researchReport.getId();
			String pubDateStr = CalendarUtil.getDateStr(researchReport
					.getPubDate());
			String annexPath = rootPath + "\\" + ANNEX_FOLDER+ "\\"
					+ pubDateStr + "\\" + reportId;
			List<ResearchReportBlob> reportBlobList = researchReportService
					.getReportBlobs(reportId);
			for (int j = 0; j < reportBlobList.size(); j++) {
				ResearchReportBlob reportBlob = reportBlobList.get(j);
				FileUtil.createFolder(annexPath);
				createAnnexFile(reportBlob, annexPath);

				// 如果有研究报告摘要就创建html文件展示
				createHtmlFile(reportBlob, annexPath);

				// 把记录存到datas.xml文件中
				writeDatasXml(researchReport, document, filePath);
			}
		}

		System.out.println((System.currentTimeMillis() - startMillis) / 1000
				+ "s");

	}

	public Format FormatXML() {
		// 格式化生成的xml文件,如果不进行格式化的话,生成的xml文件将会是很长的一行...
		Format format = Format.getCompactFormat();
		format.setEncoding("GBK");
		format.setIndent("    ");
		return format;
	}

	public void writeDatasXml(ResearchReport researchReport, Document document,
			String filePath) {
		String currentTime = CalendarUtil.getCurrentTime();
		long reportId = researchReport.getId();

		FileOutputStream fo = null;
		try {
			Element rootElement = document.getRootElement();

			// 操作reports的子标签
			Element reportsElement = rootElement.getChild("reports");
			List<Element> reportList = reportsElement.getChildren();
			for (int i = 0; i < reportList.size(); i++) {
				Element reportElement = reportList.get(i);
				Element reportIdElement = reportElement.getChild("id");
				String existReportId = reportIdElement.getText(); 
				if(existReportId.equals(reportId+"")){
					return;
				}
			}
			Element reportElement = addReportElement(reportsElement,
					researchReport);
			
			// 操作head的子标签
			Element headElement = rootElement.getChild("head");
			setHeadElement(headElement, currentTime, reportId);

			XMLOutputter outp = new XMLOutputter(FormatXML());
			fo = new FileOutputStream(filePath);
			outp.output(document, fo);

		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			if(fo != null){
				try {
					fo.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private Document createDocument() {
		Element rootElement = new Element("report-catalog");
		Document document = new Document(rootElement);
		addHeadElement(rootElement);
		XmlUtil.addChildElement(rootElement, "reports");
		return document;
	}

	private Element addHeadElement(Element rootElement) {
		Element headElement = XmlUtil.addChildElement(rootElement, "head");
		String currentTime = CalendarUtil.getCurrentTime();
		XmlUtil.addChildElement(headElement, "count", "0");
		XmlUtil.addChildElement(headElement, "last-report-id", "0");
		XmlUtil.addChildElement(headElement, "create-time", currentTime);
		XmlUtil.addChildElement(headElement, "update-time", currentTime);
		return headElement;
	}

	private void setHeadElement(Element headElement, String currentTime,
			long reportId) {
		Element lastReportId = headElement.getChild("last-report-id");
		lastReportId.setText(reportId + "");
		Element count = headElement.getChild("count");
		int countValue = Integer.parseInt(count.getText()) + 1;
		count.setText(countValue + "");
		Element updateTime = headElement.getChild("update-time");
		updateTime.setText(currentTime);
	}

	private Element addReportElement(Element reportsElement,
			ResearchReport report) {
		Element reportElement = XmlUtil.addChildElement(reportsElement,
				"report");
		XmlUtil.addChildElement(reportElement, "id", report.getId().toString());
		XmlUtil.addChildElement(reportElement, "title", report.getReportTitle());
		XmlUtil.addChildElement(reportElement, "authors", report.getAuthors());
		XmlUtil.addChildElement(reportElement, "level", report.getReportLevel()
				.toString());
		XmlUtil.addChildElement(reportElement, "pub-date",
				CalendarUtil.getDateStr(report.getPubDate()));
		XmlUtil.addChildElement(reportElement, "writing-date",
				CalendarUtil.getDateStr(report.getWritingDate()));
		if (report.getAnnexes() != null && report.getAnnexes().size() > 0) {
			Element annexesElement = XmlUtil.addChildElement(reportElement,
					"annexes");
			for (ResearchReportAnnex annex : report.getAnnexes()) {
				addAnnexElement(annexesElement, annex);
			}
		}
		return reportElement;
	}

	private void addAnnexElement(Element annexesElement,
			ResearchReportAnnex annex) {
		Element annexElement = XmlUtil.addChildElement(annexesElement, "annex");
		XmlUtil.addChildElement(annexElement, "id", annex.getId() + "");
		XmlUtil.addChildElement(annexElement, "name", annex.getAnnexName());
	}

	public void createAnnexFile(ResearchReportBlob reportBlob, String annxePath) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(annxePath + "\\" + reportBlob.getId()
					+ reportBlob.getAnnexFormat());
			fos.write(reportBlob.getAnnexContent());
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void createHtmlFile(ResearchReportBlob reportBlob, String path) {
		String reportAbstract = reportBlob.getAbstractContent();
		if (reportAbstract != null && !"".equals(reportAbstract)) {
			FileUtil.createHtmlFile(path, reportBlob.getId() + "",
					reportAbstract);
		}
	}

}

你可能感兴趣的:(xml)