1 6 7 package com.sun.japex.report; 8 import org.xml.sax.*; 9 import org.xml.sax.helpers.DefaultHandler ; 10 import javax.xml.parsers.SAXParserFactory ; 11 import javax.xml.parsers.ParserConfigurationException ; 12 import javax.xml.parsers.SAXParser ; 13 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.Date ; 18 import java.util.Calendar ; 19 import java.util.GregorianCalendar ; 20 import java.util.Map ; 21 import java.util.HashMap ; 22 23 public class ParseReports { 24 ArrayList _reports = new ArrayList (); 25 ArrayList _dates = new ArrayList (); 26 boolean hasReport = false; 27 28 29 public ParseReports(TrendReportParams params) { 30 File cwd = new File (params.reportPath()); 31 ReportFilter filter =new ReportFilter(params.dateFrom(), params.dateTo()); 32 File [] reportDirs = cwd.listFiles(filter); 33 Arrays.sort(reportDirs, new DateComparator()); 34 35 String separator = System.getProperty("file.separator"); 36 ReportDataParser handler = null; 37 SAXParserFactory factory = SAXParserFactory.newInstance(); 39 try { 40 SAXParser saxParser = factory.newSAXParser(); 42 GregorianCalendar cal = new GregorianCalendar (); 43 int lastDay=0, lastMonth=0, lastYear=0; 44 45 for (int i = 0; i < reportDirs.length; i++) { 46 File file = new File (reportDirs[i].getAbsolutePath()+separator+"report.xml"); 47 if (file.exists()) { 48 Date date = new Date (reportDirs[i].lastModified()); 49 cal.setTime(date); 50 int day = cal.get(cal.DAY_OF_MONTH); 51 int month = cal.get(cal.MONTH); 52 int year = cal.get(cal.YEAR); 53 if (day==lastDay && month==lastMonth && year==lastYear) { 55 } else { 57 handler = new ReportDataParser(params); 58 saxParser.parse(file, handler); 59 Map report = (Map )handler.getReports(); 60 if (report != null) { 61 _reports.add(report); 62 _dates.add(date); 63 hasReport = true; 64 lastDay = day; 65 lastMonth = month; 66 lastYear = year; 67 } 68 } 69 } 70 } 71 72 } catch (Throwable t) { 73 t.printStackTrace(); 74 } 75 76 } 77 78 public Map [] getReports() { 79 if (!hasReport) return null; 80 Map [] reports = new HashMap [_reports.size()]; 81 reports = (Map [])_reports.toArray(reports); 82 return reports; 83 } 84 85 public Date [] getDates() { 86 if (!hasReport) return null; 87 Date [] dates = new Date [_reports.size()]; 88 dates = (Date [])_dates.toArray(dates); 89 return dates; 90 } 91 } 92 | Popular Tags |