KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > japex > report > ParseReports


1 /*
2  * ParseReport.java
3  *
4  * Created on April 8, 2005, 10:19 PM
5  */

6
7 package com.sun.japex.report;
8 import org.xml.sax.*;
9 import org.xml.sax.helpers.DefaultHandler JavaDoc;
10 import javax.xml.parsers.SAXParserFactory JavaDoc;
11 import javax.xml.parsers.ParserConfigurationException JavaDoc;
12 import javax.xml.parsers.SAXParser JavaDoc;
13
14 import java.io.File JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Date JavaDoc;
18 import java.util.Calendar JavaDoc;
19 import java.util.GregorianCalendar JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 public class ParseReports {
24     ArrayList JavaDoc _reports = new ArrayList JavaDoc();
25     ArrayList JavaDoc _dates = new ArrayList JavaDoc();
26     boolean hasReport = false;
27     
28     /** Creates a new instance of ParseReport */
29     public ParseReports(TrendReportParams params) {
30         File JavaDoc cwd = new File JavaDoc(params.reportPath());
31         ReportFilter filter =new ReportFilter(params.dateFrom(), params.dateTo());
32         File JavaDoc[] reportDirs = cwd.listFiles(filter);
33         Arrays.sort(reportDirs, new DateComparator());
34         
35         String JavaDoc separator = System.getProperty("file.separator");
36         ReportDataParser handler = null;
37         // Use the default (non-validating) parser
38
SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
39         try {
40             // Parse the input
41
SAXParser JavaDoc saxParser = factory.newSAXParser();
42             GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
43             int lastDay=0, lastMonth=0, lastYear=0;
44         
45             for (int i = 0; i < reportDirs.length; i++) {
46                 File JavaDoc file = new File JavaDoc(reportDirs[i].getAbsolutePath()+separator+"report.xml");
47                 if (file.exists()) {
48                     Date JavaDoc date = new Date JavaDoc(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                     //add one report per day
54
if (day==lastDay && month==lastMonth && year==lastYear) {
55                         //skip
56
} else {
57                         handler = new ReportDataParser(params);
58                         saxParser.parse(file, handler);
59                         Map JavaDoc report = (Map JavaDoc)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 JavaDoc t) {
73             t.printStackTrace();
74         }
75         
76     }
77     
78     public Map JavaDoc[] getReports() {
79         if (!hasReport) return null;
80         Map JavaDoc[] reports = new HashMap JavaDoc[_reports.size()];
81         reports = (Map JavaDoc[])_reports.toArray(reports);
82         return reports;
83     }
84     
85     public Date JavaDoc[] getDates() {
86         if (!hasReport) return null;
87         Date JavaDoc[] dates = new Date JavaDoc[_reports.size()];
88         dates = (Date JavaDoc[])_dates.toArray(dates);
89         return dates;
90     }
91 }
92
Popular Tags