001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020
021 package org.sonar.batch.language;
022
023 import org.sonar.api.batch.Decorator;
024 import org.sonar.api.batch.DecoratorContext;
025 import org.sonar.api.batch.DependedUpon;
026 import org.sonar.api.batch.DependsUpon;
027 import org.sonar.api.measures.CoreMetrics;
028 import org.sonar.api.measures.CountDistributionBuilder;
029 import org.sonar.api.measures.Measure;
030 import org.sonar.api.measures.Metric;
031 import org.sonar.api.resources.Language;
032 import org.sonar.api.resources.Project;
033 import org.sonar.api.resources.Resource;
034 import org.sonar.api.resources.ResourceUtils;
035
036 public class LanguageDistributionDecorator implements Decorator {
037
038 public boolean shouldExecuteOnProject(Project project) {
039 return true;
040 }
041
042 @DependsUpon
043 public Metric dependsUponMetric() {
044 return CoreMetrics.LINES;
045 }
046
047 @DependedUpon
048 public Metric generatesMetric() {
049 return CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION;
050 }
051
052 public void decorate(Resource resource, DecoratorContext context) {
053 CountDistributionBuilder nclocDistribution = new CountDistributionBuilder(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION);
054 if (ResourceUtils.isFile(resource)) {
055 Language language = resource.getLanguage();
056 Measure ncloc = context.getMeasure(CoreMetrics.NCLOC);
057 if (language != null && ncloc != null) {
058 nclocDistribution.add(language.getKey(), ncloc.getIntValue());
059 }
060 } else {
061 for (Measure measure : context.getChildrenMeasures(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION)) {
062 nclocDistribution.add(measure);
063 }
064 }
065 Measure measure = nclocDistribution.build(false);
066 if (measure != null) {
067 context.saveMeasure(measure);
068 }
069 }
070
071 }