You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DuplicationsData.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2008-2011 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.cpd;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Comparator;
  24. import java.util.HashSet;
  25. import java.util.List;
  26. import java.util.Set;
  27. import org.sonar.api.batch.SensorContext;
  28. import org.sonar.api.measures.CoreMetrics;
  29. import org.sonar.api.measures.Measure;
  30. import org.sonar.api.resources.Resource;
  31. public class DuplicationsData {
  32. private Resource resource;
  33. private Set<Integer> duplicatedLines = new HashSet<Integer>();
  34. private double duplicatedBlocks;
  35. private List<XmlEntry> duplicationXMLEntries = new ArrayList<XmlEntry>();
  36. private SensorContext context;
  37. public DuplicationsData(Resource resource, SensorContext context) {
  38. this.resource = resource;
  39. this.context = context;
  40. }
  41. public void cumulate(String targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) {
  42. duplicationXMLEntries.add(new XmlEntry(targetResource, targetDuplicationStartLine, duplicationStartLine, duplicatedLines));
  43. for (int duplicatedLine = duplicationStartLine; duplicatedLine < duplicationStartLine + duplicatedLines; duplicatedLine++) {
  44. this.duplicatedLines.add(duplicatedLine);
  45. }
  46. }
  47. public void cumulate(Resource targetResource, int targetDuplicationStartLine, int duplicationStartLine, int duplicatedLines) {
  48. cumulate(context.saveResource(targetResource), targetDuplicationStartLine, duplicationStartLine, duplicatedLines);
  49. }
  50. public void incrementDuplicatedBlock() {
  51. duplicatedBlocks++;
  52. }
  53. public void save() {
  54. context.saveMeasure(resource, CoreMetrics.DUPLICATED_FILES, 1d);
  55. context.saveMeasure(resource, CoreMetrics.DUPLICATED_LINES, (double) duplicatedLines.size());
  56. context.saveMeasure(resource, CoreMetrics.DUPLICATED_BLOCKS, duplicatedBlocks);
  57. context.saveMeasure(resource, new Measure(CoreMetrics.DUPLICATIONS_DATA, getDuplicationXMLData()));
  58. }
  59. private String getDuplicationXMLData() {
  60. Collections.sort(duplicationXMLEntries, COMPARATOR);
  61. StringBuilder duplicationXML = new StringBuilder("<duplications>");
  62. for (XmlEntry xmlEntry : duplicationXMLEntries) {
  63. duplicationXML.append(xmlEntry.toString());
  64. }
  65. duplicationXML.append("</duplications>");
  66. return duplicationXML.toString();
  67. }
  68. private static final Comparator<XmlEntry> COMPARATOR = new Comparator<XmlEntry>() {
  69. public int compare(XmlEntry o1, XmlEntry o2) {
  70. if (o1.startLine == o2.startLine) {
  71. return o1.lines - o2.lines;
  72. }
  73. return o1.startLine - o2.startLine;
  74. }
  75. };
  76. private static final class XmlEntry {
  77. private String target;
  78. private int targetStartLine;
  79. private int startLine;
  80. private int lines;
  81. private XmlEntry(String target, int targetStartLine, int startLine, int lines) {
  82. this.target = target;
  83. this.targetStartLine = targetStartLine;
  84. this.startLine = startLine;
  85. this.lines = lines;
  86. }
  87. @Override
  88. public String toString() {
  89. return new StringBuilder().append("<duplication lines=\"").append(lines)
  90. .append("\" start=\"").append(startLine)
  91. .append("\" target-start=\"").append(targetStartLine)
  92. .append("\" target-resource=\"").append(target).append("\"/>")
  93. .toString();
  94. }
  95. }
  96. }