1 9 package com.vladium.emma.report; 10 11 import java.util.ArrayList ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Properties ; 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 47 public 48 class ReportCfg implements IReportProperties 49 { 50 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 columns) 66 { 67 m_settings.setProperty (m_prefix.concat (COLUMNS), columns); 68 } 69 70 public void setSort (final String sort) 71 { 72 m_settings.setProperty (m_prefix.concat (SORT), sort); 73 } 74 75 public void setMetrics (final String metrics) 76 { 77 m_settings.setProperty (m_prefix.concat (METRICS), metrics); 78 } 79 80 82 88 public void setOutfile (final String fileName) 89 { 90 m_settings.setProperty (m_prefix.concat (OUT_FILE), fileName); 91 } 92 93 public void setEncoding (final String encoding) 94 { 95 m_settings.setProperty (m_prefix.concat (OUT_ENCODING), encoding); 96 } 97 98 100 public PropertyElement createProperty () 101 { 102 104 final PropertyElement property = new PropertyElement (); 105 m_genericSettings.add (property); 106 107 return property; 108 } 109 110 protected abstract String getType (); 111 112 113 Element (final Task task, final IProperties settings) 114 { 115 if (task == null) 116 throw new IllegalArgumentException ("null input: task"); 117 if (settings == null) 118 throw new IllegalArgumentException ("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 (); 126 } 127 128 129 void processGenericSettings () 130 { 131 for (Iterator i = m_genericSettings.iterator (); i.hasNext (); ) 132 { 133 final PropertyElement property = (PropertyElement) i.next (); 134 135 final String name = property.getName (); 136 final String value = property.getValue () != null ? property.getValue () : ""; 137 138 if (name != null) 139 { 140 final String prefixedName = m_prefix.concat (name); 141 142 144 if (! m_settings.isOverridden (prefixedName)) 145 m_settings.setProperty (prefixedName, value); 146 } 147 } 148 } 149 150 151 protected final Task m_task; protected final String m_prefix; protected final IProperties m_settings; protected final List m_genericSettings; 156 } 158 160 public static class Element_HTML extends Element 161 { 162 protected final String 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 TYPE = "html"; 174 175 } 177 179 public static class Element_TXT extends Element 180 { 181 protected final String 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 TYPE = "txt"; 193 194 } 196 198 public static class Element_XML extends Element 199 { 200 protected final String 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 TYPE = "xml"; 212 213 } 215 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 (4); 224 m_cfgList = new ArrayList (4); 225 m_settings = EMMAProperties.wrap (new Properties ()); 226 } 227 228 public Path getSourcepath () 229 { 230 return m_srcpath; 231 } 232 233 public String [] 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 [] result = new String [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 265 for (Iterator 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; } 276 } 277 278 279 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 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 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 columns) 337 { 338 m_settings.setProperty (PREFIX.concat (COLUMNS), columns); 339 } 340 341 public void setSort (final String sort) 342 { 343 m_settings.setProperty (PREFIX.concat (SORT), sort); 344 } 345 346 public void setMetrics (final String metrics) 347 { 348 m_settings.setProperty (PREFIX.concat (METRICS), metrics); 349 } 350 351 353 365 public void setOutfile (final String fileName) 366 { 367 m_settings.setProperty (PREFIX.concat (OUT_FILE), fileName); 368 } 369 370 public void setEncoding (final String encoding) 371 { 372 m_settings.setProperty (PREFIX.concat (OUT_ENCODING), encoding); 373 } 374 375 377 378 protected Element addCfgElement (final String 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 397 399 400 private void setFailure (final BuildException failure) 401 { 402 if (m_settingsFailure == null) m_settingsFailure = failure; } 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 m_reportTypes; private final List m_cfgList; 416 private final IProperties m_settings; 418 private Path m_srcpath; 419 420 private transient BuildException m_settingsFailure; private transient boolean m_processed; 422 423 } | Popular Tags |