1   package net.sf.statcvs.pages.xml;
2   
3   
4   
5   
6   
7   
8   
9   
10  import java.io.FileOutputStream;
11  import java.io.IOException;
12  import java.util.Calendar;
13  import java.util.Iterator;
14  
15  import net.sf.statcvs.Messages;
16  import net.sf.statcvs.model.Repository;
17  import net.sf.statcvs.model.VersionedFile;
18  import net.sf.statcvs.output.ConfigurationOptions;
19  import net.sf.statcvs.output.ReportConfig;
20  import net.sf.statcvs.output.WebRepositoryIntegration;
21  import net.sf.statcvs.reports.TopDevelopersTableReport;
22  import net.sf.statcvs.weblinks.bugs.BugTracker;
23  
24  import org.jdom.Comment;
25  import org.jdom.Document;
26  import org.jdom.Element;
27  import org.jdom.output.XMLOutputter;
28  
29  public class GenerateXml {
30      private static final String TAG_REPO_STATISTICS = "RepoStatistics";
31      private final ReportConfig config;
32      private final Repository repository;
33  
34      
35  
36  
37      public GenerateXml(final ReportConfig config) {
38          this.config = config;
39          this.repository = config.getRepository();
40      }
41  
42      
43  
44  
45      public void generate() {
46          final TopDevelopersTableReport topDevelopers = new TopDevelopersTableReport(this.config);
47          final String gen_time = Calendar.getInstance().getTime().toString();
48          final String period = getReportPeriod();
49          final String title = Messages.getString("INDEX_TITLE") + " " + this.config.getProjectName();
50          final Element root = new Element(TAG_REPO_STATISTICS);
51          root.setAttribute("date", gen_time.toLowerCase());
52          root.setAttribute("period", period.toLowerCase());
53          root.setAttribute("numberfiles", Integer.toString(getCurrentFileCount()));
54          root.setAttribute("totalloc", Integer.toString(this.repository.getCurrentLOC()));
55          root.setAttribute("numberdevelopers", Integer.toString(topDevelopers.getDeveloperCount()));
56  
57          final Document myXML = new Document(root);
58          myXML.addContent(new Comment(title));
59  
60          final Element el2 = new Element(XmlTags.TAG_CHECKED_OUT_DIR);
61          el2.setAttribute("path", ConfigurationOptions.getCheckedOutDirectory());
62          root.addContent(el2);
63  
64          if (ConfigurationOptions.getProjectName() != null) {
65              final Element el3 = new Element(XmlTags.TAG_PROJECT);
66              el3.setAttribute("path", ConfigurationOptions.getCheckedOutDirectory());
67              root.addContent(el3);
68          }
69          
70  
71  
72  
73  
74  
75  
76  
77          final BugTracker bt = ConfigurationOptions.getWebBugtracker();
78          if (bt != null) {
79              final Element el = new Element(XmlTags.TAG_BUG_TRACKER);
80              el.setAttribute("baseurl", bt.baseURL());
81              el.setAttribute("name", bt.getName());
82              root.addContent(el);
83          }
84  
85          final WebRepositoryIntegration wi = ConfigurationOptions.getWebRepository();
86          if (wi != null) {
87              final Element el = new Element(XmlTags.TAG_WEB_REPOSITORY);
88              el.setAttribute("baseurl", wi.getBaseUrl());
89              el.setAttribute("name", wi.getName());
90              root.addContent(el);
91          }
92  
93          
94  
95  
96          final AllDevelopersXml adxml = new AllDevelopersXml(config);
97          final Element child1 = adxml.toFile();
98          root.addContent(child1);
99  
100         
101 
102 
103         final DirectoriesXml dxml = new DirectoriesXml(config);
104         final Element child2 = dxml.toFile();
105         root.addContent(child2);
106 
107         
108 
109 
110         final LogXml log = new LogXml(config);
111         final Element child3 = log.toFile();
112         root.addContent(child3);
113 
114         
115 
116 
117         final FilesXml fl = new FilesXml(config);
118         final Element child4 = fl.toFile();
119         root.addContent(child4);
120 
121         
122 
123 
124 
125 
126 
127 
128         
129         try {
130             final String file = ConfigurationOptions.getOutputDir() + "repo-statistics.xml";
131             final FileOutputStream out = new FileOutputStream(file);
132             final XMLOutputter serializer = new XMLOutputter();
133             serializer.output(myXML, out);
134             out.flush();
135             out.close();
136         } catch (final IOException e) {
137             System.err.println(e);
138         }
139     }
140 
141     
142 
143 
144 
145 
146     private String getReportPeriod() {
147         return XML.getDate(this.repository.getFirstDate()) + " to " + XML.getDate(this.repository.getLastDate());
148     }
149 
150     
151 
152 
153 
154 
155     private int getCurrentFileCount() {
156         int result = 0;
157         final Iterator it = this.repository.getFiles().iterator();
158         while (it.hasNext()) {
159             final VersionedFile file = (VersionedFile) it.next();
160             if (!file.isDead()) {
161                 result++;
162             }
163         }
164         return result;
165     }
166 }