summaryrefslogtreecommitdiff
path: root/ant/ant14/com/vladium/emma/report/ReportCfg.java
blob: 71a01b4c420959fcc92ccc30da3e2589e305cba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
 * 
 * This program and the accompanying materials are made available under
 * the terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * $Id: ReportCfg.java,v 1.1.1.1.2.1 2004/07/08 10:52:11 vlad_r Exp $
 */
package com.vladium.emma.report;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import com.vladium.util.IConstants;
import com.vladium.util.IProperties;
import com.vladium.emma.EMMAProperties;
import com.vladium.emma.ant.PropertyElement;
import com.vladium.emma.ant.SuppressableTask;
import com.vladium.emma.report.IReportEnums.DepthAttribute;
import com.vladium.emma.report.IReportEnums.UnitsTypeAttribute;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;

// ----------------------------------------------------------------------------
/**
 * ReportCfg is a container for report type {@link ReportCfg.Element}s that are
 * in turn containers for all properties that could be set on a <report>
 * report type configurator (<txt>, <html>, etc). The elements provide
 * the ability for report properties to be set either via the generic <property>
 * nested elements or dedicated attributes. Potential conflicts between the same
 * conceptual property being set via an attribute and a nested element are resolved
 * by making dedicated attributes higher priority.<P>
 * 
 * Note that ReportCfg does not handle any non-report related properties.
 * This can be done via {@link com.vladium.emma.ant.GenericCfg}. It is also the
 * parent's responsibility to merge any inherited report properties with
 * ReportCfg settings. 
 * 
 * @author Vlad Roubtsov, (C) 2003
 */
public
class ReportCfg implements IReportProperties
{
    // public: ................................................................


    public static abstract class Element implements IReportEnums, IReportProperties
    {
        public void setUnits (final UnitsTypeAttribute units)
        {
            m_settings.setProperty (m_prefix.concat (UNITS_TYPE), units.getValue ());
        }
        
        public void setDepth (final DepthAttribute depth)
        {
            m_settings.setProperty (m_prefix.concat (DEPTH), depth.getValue ());
        }
        
        public void setColumns (final String columns)
        {
            m_settings.setProperty (m_prefix.concat (COLUMNS), columns);
        }
        
        public void setSort (final String sort)
        {
            m_settings.setProperty (m_prefix.concat (SORT), sort);
        }
        
        public void setMetrics (final String metrics)
        {
            m_settings.setProperty (m_prefix.concat (METRICS), metrics);
        }
        
        // not supported anymore:
        
//        public void setOutdir (final File dir)
//        {
//            // TODO: does ANT resolve files relative to current JVM dir or ${basedir}?
//            m_settings.setProperty (m_prefix.concat (OUT_DIR), dir.getAbsolutePath ());
//        }
        
        public void setOutfile (final String fileName)
        {
            m_settings.setProperty (m_prefix.concat (OUT_FILE), fileName);
        }
        
        public void setEncoding (final String encoding)
        {
            m_settings.setProperty (m_prefix.concat (OUT_ENCODING), encoding);
        }
        
        // generic property element [don't doc this publicly]:
        
        public PropertyElement createProperty ()
        {
            // TODO: error out on conficting duplicate settings
            
            final PropertyElement property = new PropertyElement ();
            m_genericSettings.add (property);
            
            return property;
        }
        
        protected abstract String getType ();
        

        Element (final Task task, final IProperties settings)
        {
            if (task == null)
                throw new IllegalArgumentException ("null input: task");
            if (settings == null)
                throw new IllegalArgumentException ("null input: settings");
            
            m_task = task;
            m_settings = settings;
            
            m_prefix = PREFIX.concat (getType ()).concat (".");
            
            m_genericSettings = new ArrayList ();
        }
        
        
        void processGenericSettings ()
        {
            for (Iterator i = m_genericSettings.iterator (); i.hasNext (); )
            {
                final PropertyElement property = (PropertyElement) i.next ();
                
                final String name = property.getName ();
                final String value = property.getValue () != null ? property.getValue () : "";
                
                if (name != null)
                {
                    final String prefixedName = m_prefix.concat (name);
                    
                    // generically named settings don't override report named settings:
                    
                    if (! m_settings.isOverridden (prefixedName))
                        m_settings.setProperty (prefixedName, value);
                }
            }
        }
        
        
        protected final Task m_task; // never null
        protected final String m_prefix; // never null
        protected final IProperties m_settings; // never null
        protected final List /* PropertyElement */ m_genericSettings; // never null
        
    } // end of nested class

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public static class Element_HTML extends Element
    {
        protected final String getType ()
        {
            return TYPE;
        }
        
        Element_HTML (final Task task, final IProperties settings)
        {
            super (task, settings);
        }
        
        
        static final String TYPE = "html";
        
    } // end of nested class
    
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    public static class Element_TXT extends Element
    {
        protected final String getType ()
        {
            return TYPE;
        }
        
        Element_TXT (final Task task, final IProperties settings)
        {
            super (task, settings);
        }
        
        
        static final String TYPE = "txt";
        
    } // end of nested class
    
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    public static class Element_XML extends Element
    {
        protected final String getType ()
        {
            return TYPE;
        }
        
        Element_XML (final Task task, final IProperties settings)
        {
            super (task, settings);
        }
        
        
        static final String TYPE = "xml";
        
    } // end of nested class
    
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    public ReportCfg (final Project project, final Task task)
    {
        m_project = project;
        m_task = task;
        
        m_reportTypes = new ArrayList (4);
        m_cfgList = new ArrayList (4);
        m_settings = EMMAProperties.wrap (new Properties ());
    }
    
    public Path getSourcepath ()
    {
        return m_srcpath;
    }
    
    public String [] getReportTypes ()
    {
        final BuildException failure = getFailure ();
        
        if (failure != null)
            throw failure;
        else
        {
            if (m_reportTypes.isEmpty ())
                return IConstants.EMPTY_STRING_ARRAY;
            else
            {
                final String [] result = new String [m_reportTypes.size ()];
                m_reportTypes.toArray (result);
                
                return result;
            }
        }
    }
    
    public IProperties getReportSettings ()
    {
        final BuildException failure = getFailure ();
        
        if (failure != null)
            throw failure;
        else
        {
            if (! m_processed)
            {
                // collect all nested elements' generic settins into m_settings:
                
                for (Iterator i = m_cfgList.iterator (); i.hasNext (); )
                {
                    final Element cfg = (Element) i.next ();
                    cfg.processGenericSettings ();
                }
                
                m_processed = true;
            }
            
            return m_settings; // no clone
        }
    }
    
    
    // sourcepath attribute/element:
    
    public void setSourcepath (final Path path)
    {
        if (m_srcpath == null)
            m_srcpath = path;
        else
            m_srcpath.append (path);
    }
    
    public void setSourcepathRef (final Reference ref)
    {
        createSourcepath ().setRefid (ref);
    }
    
    public Path createSourcepath ()
    {
        if (m_srcpath == null)
            m_srcpath = new Path (m_project);
        
        return m_srcpath.createPath ();
    }
    
    
    // generator elements:
    
    public Element_TXT createTxt ()
    {
        return (Element_TXT) addCfgElement (Element_TXT.TYPE,
                                                     new Element_TXT (m_task, m_settings));
    }
    
    public Element_HTML createHtml ()
    {
        return (Element_HTML) addCfgElement (Element_HTML.TYPE,
                                                      new Element_HTML (m_task, m_settings));
    }
    
    public Element_XML createXml ()
    {
        return (Element_XML) addCfgElement (Element_XML.TYPE,
                                                     new Element_XML (m_task, m_settings));
    }
    
    
    // report properties [defaults for all report types]:

    public void setUnits (final UnitsTypeAttribute units)
    {
        m_settings.setProperty (PREFIX.concat (UNITS_TYPE), units.getValue ());
    }    

    public void setDepth (final DepthAttribute depth)
    {
        m_settings.setProperty (PREFIX.concat (DEPTH), depth.getValue ());
    }
    
    public void setColumns (final String columns)
    {
        m_settings.setProperty (PREFIX.concat (COLUMNS), columns);
    }
    
    public void setSort (final String sort)
    {
        m_settings.setProperty (PREFIX.concat (SORT), sort);
    }
    
    public void setMetrics (final String metrics)
    {
        m_settings.setProperty (PREFIX.concat (METRICS), metrics);
    }

    // not supported anymore:
    
//    public void setOutdir (final File dir)
//    {
//        // TODO: does ANT resolve files relative to current JVM dir or ${basedir}?
//        m_settings.setProperty (PREFIX.concat (OUT_DIR), dir.getAbsolutePath ());
//    }
//    
//    public void setDestdir (final File dir)
//    {
//        // TODO: does ANT resolve files relative to current JVM dir or ${basedir}?
//        m_settings.setProperty (PREFIX.concat (OUT_DIR), dir.getAbsolutePath ());
//    }
    
    public void setOutfile (final String fileName)
    {
        m_settings.setProperty (PREFIX.concat (OUT_FILE), fileName);
    }
    
    public void setEncoding (final String encoding)
    {
        m_settings.setProperty (PREFIX.concat (OUT_ENCODING), encoding);
    }
    
    // protected: .............................................................
    
    
    protected Element addCfgElement (final String type, final Element cfg)
    {
        if (m_reportTypes.contains (type))
        {
            setFailure ((BuildException) SuppressableTask.newBuildException (m_task.getTaskName ()
                + ": duplicate configuration for report type [" + type + "]" ,
                m_task.getLocation ()).fillInStackTrace ());
        }
        else
        {
            m_reportTypes.add (type);
            m_cfgList.add (cfg);
        }
        
        return cfg;
    }

    // package: ...............................................................
    
    // private: ...............................................................
    
    
    private void setFailure (final BuildException failure)
    {
        if (m_settingsFailure == null) m_settingsFailure = failure; // record the first one only
    }
    
    private BuildException getFailure ()
    {
        return m_settingsFailure;
    }
    
    
    private final Project m_project;
    private final Task m_task;
    
    private final List /* report type:String */ m_reportTypes; // using a list to keep the generation order same as configuration
    private final List /* Element */ m_cfgList;
    private final IProperties m_settings; // never null
    
    private Path m_srcpath;
    
    private transient BuildException m_settingsFailure; // can be null
    private transient boolean m_processed;

} // end of class
// ----------------------------------------------------------------------------