Here you can find the source of createFullReportLog(String sessionLogDir)
Parameter | Description |
---|---|
sessionLogDir | considered log directory |
Parameter | Description |
---|---|
Exception | an exception |
public static void createFullReportLog(String sessionLogDir) throws Exception
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileFilter; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; 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; public class Main { /**/*ww w. j a v a 2 s. c om*/ * Generate a file in logDir refererring all logfiles. Create a file called * "report_logs.xml" in the log folder that includes all logs listed inside * the directory. * * @param sessionLogDir * considered log directory * @throws Exception * @author F.Vitale vitale@imaa.cnr.it */ public static void createFullReportLog(String sessionLogDir) throws Exception { File xml_logs_report_file = new File(sessionLogDir + File.separator + "report_logs.xml"); if (xml_logs_report_file.exists()) { xml_logs_report_file.delete(); xml_logs_report_file.createNewFile(); } xml_logs_report_file = new File(sessionLogDir + File.separator + "report_logs.xml"); OutputStream report_logs = new FileOutputStream(xml_logs_report_file); List<File> files = null; Document result = null; files = getFileListing(new File(sessionLogDir)); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = factory.newDocumentBuilder(); // Create the document Document doc = builder.newDocument(); // Fill the document Element execution = doc.createElement("execution"); execution.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xi", "http://www.w3.org/2001/XInclude"); doc.appendChild(execution); for (File file : files) { // all files are Sorted with CompareTO Element include = doc.createElementNS("http://www.w3.org/2001/XInclude", "xi:include"); include.setAttribute("href", file.getAbsolutePath()); execution.appendChild(include); } // Serialize the document into System.out TransformerFactory xformFactory = TransformerFactory.newInstance(); Transformer idTransform = xformFactory.newTransformer(); Source input = new DOMSource(doc); Result output = new StreamResult(report_logs); idTransform.transform(input, output); result = doc; // actually we do not needs results } /** * Recursively walk a directory tree and return a List of all log files * found. * * * @param logDir * die to walk * @return * @throws Exception */ private static List<File> getFileListing(File logDir) throws Exception { List<File> result = getFileListingLogs(logDir); return result; } /** * Get all log files and directories and make recursive call. * * @param aStartingDir * @return * @throws Exception */ static private List<File> getFileListingLogs(File aStartingDir) throws Exception { List<File> result = new ArrayList<File>(); File[] logfiles = aStartingDir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isFile(); } }); List<File> logFilesList = Arrays.asList(logfiles); File[] allDirs = aStartingDir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); for (File file : logFilesList) { if (file.getName().equals("log.xml")) { result.add(file); } } List<File> allDirsList = Arrays.asList(allDirs); Collections.sort(allDirsList, new Comparator<File>() { public int compare(File o1, File o2) { if (o1.lastModified() > o2.lastModified()) { return +1; } else if (o1.lastModified() < o2.lastModified()) { return -1; } else { return 0; } } }); for (File file : allDirsList) { if (!file.isFile()) { List<File> deeperList = getFileListingLogs(file); result.addAll(deeperList); } } return result; } }