View Javadoc

1   package net.sf.statcvs.output;
2   
3   import java.awt.Dimension;
4   import java.io.File;
5   import java.io.IOException;
6   import java.io.InputStream;
7   import java.net.URL;
8   import java.util.Collection;
9   import java.util.Collections;
10  
11  import net.sf.statcvs.charts.ChartImage;
12  import net.sf.statcvs.model.Author;
13  import net.sf.statcvs.model.Repository;
14  import net.sf.statcvs.pages.MarkupHTML;
15  import net.sf.statcvs.pages.MarkupSyntax;
16  import net.sf.statcvs.pages.MarkupXDoc;
17  import net.sf.statcvs.pages.Page;
18  import net.sf.statcvs.pages.xml.MarkupXML;
19  import net.sf.statcvs.util.FileUtils;
20  import net.sf.statcvs.weblinks.bugs.BugTracker;
21  
22  import org.jfree.chart.JFreeChart;
23  
24  /**
25   * A configuration object that controls several aspects of 
26   * report creation, such as the output directory and chart
27   * sizes. A single instance is passed around to all objects
28   * involved in report creation.
29   *
30   * @author Richard Cyganiak (richard@cyganiak.de)
31   * @version $Id: ReportConfig.java,v 1.12 2009/06/02 13:28:53 benoitx Exp $
32   */
33  public class ReportConfig {
34      public static final MarkupSyntax XDOC = MarkupXDoc.getInstance();
35      public static final MarkupSyntax HTML = MarkupHTML.getInstance();
36      public static final MarkupSyntax XML = MarkupXML.getInstance();
37      private static final Dimension SMALL_CHART_SIZE = new Dimension(512, 320);
38      private static final Dimension LARGE_CHART_SIZE = new Dimension(800, 500);
39  
40      private final Repository repository;
41      private final String projectName;
42      private final String rootDirectory;
43      private final MarkupSyntax markup;
44      private final CssHandler cssHandler;
45      private Dimension smallChartSize;
46      private Dimension largeChartSize;
47      private WebRepositoryIntegration webRepository = null;
48      private BugTracker webBugtracker = null;
49      private Collection nonDeveloperLogins = Collections.EMPTY_LIST;
50      private final String charSet;
51  
52      public ReportConfig(final Repository repository, final String projectName, final String rootDirectory, final MarkupSyntax syntax,
53              final CssHandler cssHandler, final String charSet) {
54          this.repository = repository;
55          this.projectName = projectName;
56          this.rootDirectory = rootDirectory;
57          this.markup = syntax;
58          this.cssHandler = cssHandler;
59          this.smallChartSize = SMALL_CHART_SIZE;
60          this.largeChartSize = LARGE_CHART_SIZE;
61          this.charSet = charSet;
62      }
63  
64      public void setSmallChartSize(final Dimension newSize) {
65          this.smallChartSize = newSize;
66      }
67  
68      public void setLargeChartSize(final Dimension newSize) {
69          this.largeChartSize = newSize;
70      }
71  
72      public void setWebRepository(final WebRepositoryIntegration webRepository) {
73          this.webRepository = webRepository;
74      }
75  
76      public void setWebBugtracker(final BugTracker webBugtracker) {
77          this.webBugtracker = webBugtracker;
78      }
79  
80      public void setNonDeveloperLogins(final Collection names) {
81          this.nonDeveloperLogins = names;
82      }
83  
84      public String getRootDirectory() {
85          return this.rootDirectory;
86      }
87  
88      public Repository getRepository() {
89          return this.repository;
90      }
91  
92      public String getProjectName() {
93          return this.projectName;
94      }
95  
96      public Dimension getSmallChartSize() {
97          return this.smallChartSize;
98      }
99  
100     public Dimension getLargeChartSize() {
101         return this.largeChartSize;
102     }
103 
104     public MarkupSyntax getMarkup() {
105         return this.markup;
106     }
107 
108     public CssHandler getCssHandler() {
109         return this.cssHandler;
110     }
111 
112     public WebRepositoryIntegration getWebRepository() {
113         return this.webRepository;
114     }
115 
116     public BugTracker getWebBugtracker() {
117         return this.webBugtracker;
118     }
119 
120     /**
121      * Creates an empty report page.
122      * @param fileName The page's file name, relative to the root, 
123      * 		<em>without</em> file extension
124      * @param shortTitle A short title for use in navigation links
125      * @param fullTitle The full title for the headline
126      * @return An empty page according to the specifications
127      */
128     public Page createPage(final String fileName, final String shortTitle, final String fullTitle) {
129         return new Page(this, fileName, shortTitle, fullTitle);
130     }
131 
132     /**
133      * Writes a chart image file.
134      * @param fileName The file's name, relative to the root.
135      * @param title The chart's title
136      * @param chart The JFreeChart representation
137      * @param size Width and height in pixels
138      * @return An object representing the file
139      */
140     public ChartImage createChartImage(final String fileName, final String title, final JFreeChart chart, final Dimension size) {
141         final ChartImage img = new ChartImage(this.rootDirectory, fileName, title, chart, size);
142         img.write();
143         return img;
144     }
145 
146     /**
147      * Copies a file from a URL into the report.
148      * @param source The source file
149      * @param destinationFilename The destination, relative to the 
150      * 		report root, without initial slash.
151      */
152     public void copyFileIntoReport(final URL source, final String destinationFilename) {
153         if (source == null) {
154             throw new NullPointerException("Source was null");
155         }
156         InputStream stream = null;
157         try {
158             stream = source.openStream();
159             FileUtils.copyFile(stream, new File(this.rootDirectory + destinationFilename));
160             stream.close();
161         } catch (final IOException ex) {
162             throw new RuntimeException(ex);
163         } finally {
164             if (stream != null) {
165                 try {
166                     stream.close();
167                 } catch (IOException e) {
168                     throw new RuntimeException(e);
169                 }
170             }
171         }
172     }
173 
174     public boolean isDeveloper(final Author author) {
175         return !this.nonDeveloperLogins.contains(author.getName());
176     }
177 
178     public String getCharSet() {
179         return charSet;
180     }
181 }