1   package com.stateofflow.eclipse.metrics.factory;
2   
3   import java.io.File;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import org.eclipse.core.resources.IMarker;
8   import org.eclipse.core.resources.IProject;
9   import org.eclipse.core.runtime.CoreException;
10  import org.eclipse.jdt.core.ICompilationUnit;
11  import org.eclipse.jdt.core.IJavaProject;
12  import org.eclipse.jdt.core.JavaCore;
13  import org.eclipse.ui.texteditor.MarkerUtilities;
14  
15  import com.stateofflow.eclipse.metrics.MetricsBuilder;
16  import com.stateofflow.eclipse.metrics.MetricsPlugin;
17  import com.stateofflow.eclipse.metrics.builder.CompilationUnitListMetricProcessor;
18  import com.stateofflow.eclipse.metrics.builder.EnabledCalculatorMetricProcessorDecorator;
19  import com.stateofflow.eclipse.metrics.builder.ExportingMetricProcessor;
20  import com.stateofflow.eclipse.metrics.builder.MarkerFactory;
21  import com.stateofflow.eclipse.metrics.builder.MarkingMetricProcessor;
22  import com.stateofflow.eclipse.metrics.builder.MetricProcessor;
23  import com.stateofflow.eclipse.metrics.calculators.Calculator;
24  import com.stateofflow.eclipse.metrics.collator.MetricsCollator;
25  import com.stateofflow.eclipse.metrics.configuration.MetricPresentations;
26  import com.stateofflow.eclipse.metrics.configuration.MetricsConfiguration;
27  import com.stateofflow.eclipse.metrics.export.Exporter;
28  import com.stateofflow.eclipse.metrics.export.csv.CsvExporter;
29  import com.stateofflow.eclipse.metrics.export.html.HtmlExportConfiguration;
30  import com.stateofflow.eclipse.metrics.export.html.HtmlExportFactory;
31  import com.stateofflow.eclipse.metrics.location.NamedLineNumber;
32  import com.stateofflow.eclipse.metrics.metric.MetricId;
33  
34  public class DefaultMetricsFactory implements MetricsFactory, MarkerFactory {
35      private final MetricsConfiguration metricsConfiguration;
36      private final MetricPresentations metricPresentations;
37  
38      public DefaultMetricsFactory(final MetricsConfiguration configuration, final MetricPresentations presentations) {
39          this.metricsConfiguration = configuration;
40          this.metricPresentations = presentations;
41      }
42  
43      public CompilationUnitListMetricProcessor createExportingMetricProcessor(final Exporter[] exporters) {
44          return createCompilationUnitListMetricProcessor(new ExportingMetricProcessor(exporters, createMetricsCollator(), createMetricsCollator()));
45      }
46  
47      public CompilationUnitListMetricProcessor createMarkingMetricProcessor() {
48          return createCompilationUnitListMetricProcessor(new MarkingMetricProcessor(metricsConfiguration, this));
49      }
50  
51      private CompilationUnitListMetricProcessor createCompilationUnitListMetricProcessor(final MetricProcessor processor) {
52          return new CompilationUnitListMetricProcessor(new EnabledCalculatorMetricProcessorDecorator(metricsConfiguration, processor), createEnabledCalculatorSet());
53      }
54  
55      public void createMarker(final ICompilationUnit compilationUnit, final NamedLineNumber lineNumberInfo, final MetricId metricKey, final int value) {
56          final Map<String, Comparable< ? >> attributes = new HashMap<String, Comparable< ? >>();
57          attributes.put(IMarker.LINE_NUMBER, new Integer(lineNumberInfo.getLineNumber()));
58          attributes.put(IMarker.MESSAGE, metricPresentations.getViolationMessage(metricKey, value));
59          attributes.put(IMarker.PRIORITY, new Integer(IMarker.PRIORITY_NORMAL));
60          attributes.put(IMarker.SEVERITY, new Integer(IMarker.SEVERITY_WARNING));
61          try {
62              MarkerUtilities.createMarker(compilationUnit.getCorrespondingResource(), attributes, MetricsBuilder.MARKER_ID);
63          } catch (final CoreException cex) {
64              MetricsPlugin.log(cex);
65          }
66      }
67  
68      public Exporter createHtmlExporter(final HtmlExportConfiguration exportConfiguration) {
69          return new HtmlExportFactory(exportConfiguration, metricsConfiguration, metricPresentations).createExporter();
70      }
71  
72      public Exporter createCsvExporter(final File directory) {
73          return new CsvExporter(directory, metricPresentations);
74      }
75  
76      public IJavaProject createJavaProject(final IProject project) {
77          return JavaCore.create(project);
78      }
79  
80      private MetricsCollator createMetricsCollator() {
81          return new MetricsCollator(metricPresentations);
82      }
83  
84      private Calculator[] createEnabledCalculatorSet() {
85          return new EnabledCalculatorSetBuilder().build(metricsConfiguration);
86      }
87  }
88