KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > vladium > emma > report > ReportCfg


1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: ReportCfg.java,v 1.1.1.1.2.1 2004/07/08 10:52:11 vlad_r Exp $
8  */

9 package com.vladium.emma.report;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Properties JavaDoc;
15
16 import com.vladium.util.IConstants;
17 import com.vladium.util.IProperties;
18 import com.vladium.emma.EMMAProperties;
19 import com.vladium.emma.ant.PropertyElement;
20 import com.vladium.emma.ant.SuppressableTask;
21 import com.vladium.emma.report.IReportEnums.DepthAttribute;
22 import com.vladium.emma.report.IReportEnums.UnitsTypeAttribute;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.types.Path;
28 import org.apache.tools.ant.types.Reference;
29
30 // ----------------------------------------------------------------------------
31
/**
32  * ReportCfg is a container for report type {@link ReportCfg.Element}s that are
33  * in turn containers for all properties that could be set on a <report>
34  * report type configurator (<txt>, <html>, etc). The elements provide
35  * the ability for report properties to be set either via the generic <property>
36  * nested elements or dedicated attributes. Potential conflicts between the same
37  * conceptual property being set via an attribute and a nested element are resolved
38  * by making dedicated attributes higher priority.<P>
39  *
40  * Note that ReportCfg does not handle any non-report related properties.
41  * This can be done via {@link com.vladium.emma.ant.GenericCfg}. It is also the
42  * parent's responsibility to merge any inherited report properties with
43  * ReportCfg settings.
44  *
45  * @author Vlad Roubtsov, (C) 2003
46  */

47 public
48 class ReportCfg implements IReportProperties
49 {
50     // public: ................................................................
51

52
53     public static abstract class Element implements IReportEnums, IReportProperties
54     {
55         public void setUnits (final UnitsTypeAttribute units)
56         {
57             m_settings.setProperty (m_prefix.concat (UNITS_TYPE), units.getValue ());
58         }
59         
60         public void setDepth (final DepthAttribute depth)
61         {
62             m_settings.setProperty (m_prefix.concat (DEPTH), depth.getValue ());
63         }
64         
65         public void setColumns (final String JavaDoc columns)
66         {
67             m_settings.setProperty (m_prefix.concat (COLUMNS), columns);
68         }
69         
70         public void setSort (final String JavaDoc sort)
71         {
72             m_settings.setProperty (m_prefix.concat (SORT), sort);
73         }
74         
75         public void setMetrics (final String JavaDoc metrics)
76         {
77             m_settings.setProperty (m_prefix.concat (METRICS), metrics);
78         }
79         
80         // not supported anymore:
81

82 // public void setOutdir (final File dir)
83
// {
84
// // TODO: does ANT resolve files relative to current JVM dir or ${basedir}?
85
// m_settings.setProperty (m_prefix.concat (OUT_DIR), dir.getAbsolutePath ());
86
// }
87

88         public void setOutfile (final String JavaDoc fileName)
89         {
90             m_settings.setProperty (m_prefix.concat (OUT_FILE), fileName);
91         }
92         
93         public void setEncoding (final String JavaDoc encoding)
94         {
95             m_settings.setProperty (m_prefix.concat (OUT_ENCODING), encoding);
96         }
97         
98         // generic property element [don't doc this publicly]:
99

100         public PropertyElement createProperty ()
101         {
102             // TODO: error out on conficting duplicate settings
103

104             final PropertyElement property = new PropertyElement ();
105             m_genericSettings.add (property);
106             
107             return property;
108         }
109         
110         protected abstract String JavaDoc getType ();
111         
112
113         Element (final Task task, final IProperties settings)
114         {
115             if (task == null)
116                 throw new IllegalArgumentException JavaDoc ("null input: task");
117             if (settings == null)
118                 throw new IllegalArgumentException JavaDoc ("null input: settings");
119             
120             m_task = task;
121             m_settings = settings;
122             
123             m_prefix = PREFIX.concat (getType ()).concat (".");
124             
125             m_genericSettings = new ArrayList JavaDoc ();
126         }
127         
128         
129         void processGenericSettings ()
130         {
131             for (Iterator JavaDoc i = m_genericSettings.iterator (); i.hasNext (); )
132             {
133                 final PropertyElement property = (PropertyElement) i.next ();
134                 
135                 final String JavaDoc name = property.getName ();
136                 final String JavaDoc value = property.getValue () != null ? property.getValue () : "";
137                 
138                 if (name != null)
139                 {
140                     final String JavaDoc prefixedName = m_prefix.concat (name);
141                     
142                     // generically named settings don't override report named settings:
143

144                     if (! m_settings.isOverridden (prefixedName))
145                         m_settings.setProperty (prefixedName, value);
146                 }
147             }
148         }
149         
150         
151         protected final Task m_task; // never null
152
protected final String JavaDoc m_prefix; // never null
153
protected final IProperties m_settings; // never null
154
protected final List JavaDoc /* PropertyElement */ m_genericSettings; // never null
155

156     } // end of nested class
157

158     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
159

160     public static class Element_HTML extends Element
161     {
162         protected final String JavaDoc getType ()
163         {
164             return TYPE;
165         }
166         
167         Element_HTML (final Task task, final IProperties settings)
168         {
169             super (task, settings);
170         }
171         
172         
173         static final String JavaDoc TYPE = "html";
174         
175     } // end of nested class
176

177     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178

179     public static class Element_TXT extends Element
180     {
181         protected final String JavaDoc getType ()
182         {
183             return TYPE;
184         }
185         
186         Element_TXT (final Task task, final IProperties settings)
187         {
188             super (task, settings);
189         }
190         
191         
192         static final String JavaDoc TYPE = "txt";
193         
194     } // end of nested class
195

196     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197

198     public static class Element_XML extends Element
199     {
200         protected final String JavaDoc getType ()
201         {
202             return TYPE;
203         }
204         
205         Element_XML (final Task task, final IProperties settings)
206         {
207             super (task, settings);
208         }
209         
210         
211         static final String JavaDoc TYPE = "xml";
212         
213     } // end of nested class
214

215     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
216

217     
218     public ReportCfg (final Project project, final Task task)
219     {
220         m_project = project;
221         m_task = task;
222         
223         m_reportTypes = new ArrayList JavaDoc (4);
224         m_cfgList = new ArrayList JavaDoc (4);
225         m_settings = EMMAProperties.wrap (new Properties JavaDoc ());
226     }
227     
228     public Path getSourcepath ()
229     {
230         return m_srcpath;
231     }
232     
233     public String JavaDoc [] getReportTypes ()
234     {
235         final BuildException failure = getFailure ();
236         
237         if (failure != null)
238             throw failure;
239         else
240         {
241             if (m_reportTypes.isEmpty ())
242                 return IConstants.EMPTY_STRING_ARRAY;
243             else
244             {
245                 final String JavaDoc [] result = new String JavaDoc [m_reportTypes.size ()];
246                 m_reportTypes.toArray (result);
247                 
248                 return result;
249             }
250         }
251     }
252     
253     public IProperties getReportSettings ()
254     {
255         final BuildException failure = getFailure ();
256         
257         if (failure != null)
258             throw failure;
259         else
260         {
261             if (! m_processed)
262             {
263                 // collect all nested elements' generic settins into m_settings:
264

265                 for (Iterator JavaDoc i = m_cfgList.iterator (); i.hasNext (); )
266                 {
267                     final Element cfg = (Element) i.next ();
268                     cfg.processGenericSettings ();
269                 }
270                 
271                 m_processed = true;
272             }
273             
274             return m_settings; // no clone
275
}
276     }
277     
278     
279     // sourcepath attribute/element:
280

281     public void setSourcepath (final Path path)
282     {
283         if (m_srcpath == null)
284             m_srcpath = path;
285         else
286             m_srcpath.append (path);
287     }
288     
289     public void setSourcepathRef (final Reference ref)
290     {
291         createSourcepath ().setRefid (ref);
292     }
293     
294     public Path createSourcepath ()
295     {
296         if (m_srcpath == null)
297             m_srcpath = new Path (m_project);
298         
299         return m_srcpath.createPath ();
300     }
301     
302     
303     // generator elements:
304

305     public Element_TXT createTxt ()
306     {
307         return (Element_TXT) addCfgElement (Element_TXT.TYPE,
308                                                      new Element_TXT (m_task, m_settings));
309     }
310     
311     public Element_HTML createHtml ()
312     {
313         return (Element_HTML) addCfgElement (Element_HTML.TYPE,
314                                                       new Element_HTML (m_task, m_settings));
315     }
316     
317     public Element_XML createXml ()
318     {
319         return (Element_XML) addCfgElement (Element_XML.TYPE,
320                                                      new Element_XML (m_task, m_settings));
321     }
322     
323     
324     // report properties [defaults for all report types]:
325

326     public void setUnits (final UnitsTypeAttribute units)
327     {
328         m_settings.setProperty (PREFIX.concat (UNITS_TYPE), units.getValue ());
329     }
330
331     public void setDepth (final DepthAttribute depth)
332     {
333         m_settings.setProperty (PREFIX.concat (DEPTH), depth.getValue ());
334     }
335     
336     public void setColumns (final String JavaDoc columns)
337     {
338         m_settings.setProperty (PREFIX.concat (COLUMNS), columns);
339     }
340     
341     public void setSort (final String JavaDoc sort)
342     {
343         m_settings.setProperty (PREFIX.concat (SORT), sort);
344     }
345     
346     public void setMetrics (final String JavaDoc metrics)
347     {
348         m_settings.setProperty (PREFIX.concat (METRICS), metrics);
349     }
350
351     // not supported anymore:
352

353 // public void setOutdir (final File dir)
354
// {
355
// // TODO: does ANT resolve files relative to current JVM dir or ${basedir}?
356
// m_settings.setProperty (PREFIX.concat (OUT_DIR), dir.getAbsolutePath ());
357
// }
358
//
359
// public void setDestdir (final File dir)
360
// {
361
// // TODO: does ANT resolve files relative to current JVM dir or ${basedir}?
362
// m_settings.setProperty (PREFIX.concat (OUT_DIR), dir.getAbsolutePath ());
363
// }
364

365     public void setOutfile (final String JavaDoc fileName)
366     {
367         m_settings.setProperty (PREFIX.concat (OUT_FILE), fileName);
368     }
369     
370     public void setEncoding (final String JavaDoc encoding)
371     {
372         m_settings.setProperty (PREFIX.concat (OUT_ENCODING), encoding);
373     }
374     
375     // protected: .............................................................
376

377     
378     protected Element addCfgElement (final String JavaDoc type, final Element cfg)
379     {
380         if (m_reportTypes.contains (type))
381         {
382             setFailure ((BuildException) SuppressableTask.newBuildException (m_task.getTaskName ()
383                 + ": duplicate configuration for report type [" + type + "]" ,
384                 m_task.getLocation ()).fillInStackTrace ());
385         }
386         else
387         {
388             m_reportTypes.add (type);
389             m_cfgList.add (cfg);
390         }
391         
392         return cfg;
393     }
394
395     // package: ...............................................................
396

397     // private: ...............................................................
398

399     
400     private void setFailure (final BuildException failure)
401     {
402         if (m_settingsFailure == null) m_settingsFailure = failure; // record the first one only
403
}
404     
405     private BuildException getFailure ()
406     {
407         return m_settingsFailure;
408     }
409     
410     
411     private final Project m_project;
412     private final Task m_task;
413     
414     private final List JavaDoc /* report type:String */ m_reportTypes; // using a list to keep the generation order same as configuration
415
private final List JavaDoc /* Element */ m_cfgList;
416     private final IProperties m_settings; // never null
417

418     private Path m_srcpath;
419     
420     private transient BuildException m_settingsFailure; // can be null
421
private transient boolean m_processed;
422
423 } // end of class
424
// ----------------------------------------------------------------------------
Popular Tags