001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar 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 * Sonar 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 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.squid.text; 021 022 import java.io.Reader; 023 import java.util.ArrayList; 024 import java.util.HashSet; 025 import java.util.List; 026 import java.util.Set; 027 028 import org.sonar.squid.measures.Metric; 029 import org.sonar.squid.recognizer.CodeRecognizer; 030 031 public class Source { 032 033 private List<Line> lines = new ArrayList<Line>(); 034 private CodeRecognizer codeRecognizer; 035 private Set<Integer> noSonarTagLines = new HashSet<Integer>(); 036 037 public Source(Reader reader, CodeRecognizer codeRecognizer) { 038 this(reader, codeRecognizer, null); 039 } 040 041 public Source(Reader reader, CodeRecognizer codeRecognizer, String additionalSingleLineCommentFlag) { 042 this.codeRecognizer = codeRecognizer; 043 LinesFactory linesFactory = new LinesFactory(reader, additionalSingleLineCommentFlag); 044 lines = linesFactory.getLines(); 045 processLines(); 046 } 047 048 public Source(String[] stringLines, CodeRecognizer codeRecognizer) { 049 this(new StringArrayReader(stringLines), codeRecognizer); 050 } 051 052 private void processLines() { 053 for (Line line : lines) { 054 computeBlankLine(line); 055 computeHeaderCommentLine(line); 056 computeCommentLine(line); 057 computeCommentBlankLine(line); 058 computeLineOfCode(line); 059 computeNoSonarTag(line); 060 } 061 } 062 063 private void computeNoSonarTag(Line line) { 064 if (line.isThereNoSonarTag()) { 065 noSonarTagLines.add(line.getLineIndex()); 066 } 067 } 068 069 private void computeLineOfCode(Line line) { 070 if (line.isThereCode()) { 071 line.setMeasure(Metric.LINES_OF_CODE, 1); 072 } 073 } 074 075 private void computeHeaderCommentLine(Line line) { 076 if (line.isThereComment() && !line.isThereBlankComment() && line.isThereLicenseHeaderComment()) { 077 line.setMeasure(Metric.HEADER_COMMENT_LINES, 1); 078 } 079 } 080 081 private void computeCommentLine(Line line) { 082 if (line.isThereComment() && !line.isThereBlankComment()) { 083 if (line.isThereJavadoc() || line.isThereLicenseHeaderComment()) { 084 line.setMeasure(Metric.COMMENT_LINES, 1); 085 return; 086 } 087 088 boolean isCommentedOutCode = codeRecognizer.isLineOfCode(line.getComment()); 089 if (!isCommentedOutCode) { 090 line.setMeasure(Metric.COMMENT_LINES, 1); 091 } else { 092 line.setMeasure(Metric.COMMENTED_OUT_CODE_LINES, 1); 093 } 094 } 095 } 096 097 private void computeBlankLine(Line line) { 098 if (line.isBlank()) { 099 line.setMeasure(Metric.BLANK_LINES, 1); 100 } 101 } 102 103 private void computeCommentBlankLine(Line line) { 104 if (line.isThereBlankComment()) { 105 line.setMeasure(Metric.COMMENT_BLANK_LINES, 1); 106 } 107 } 108 109 public int getMeasure(Metric metric) { 110 return getMeasure(metric, 1, lines.size()); 111 } 112 113 public int getMeasure(Metric metric, int fromLine, int toLine) { 114 if (toLine > lines.size()) { 115 throw new IllegalStateException("There are only " + lines.size() + " lines in the file and you're trying to reach line " + toLine); 116 } 117 if (fromLine < 1) { 118 throw new IllegalStateException("Line index starts from 1 and not from " + fromLine); 119 } 120 121 int measure = 0; 122 for (int index = fromLine; index < toLine + 1; index++) { 123 measure += lines.get(index - 1).getInt(metric); 124 } 125 return measure; 126 } 127 128 public Set<Integer> getNoSonarTagLines() { 129 return noSonarTagLines; 130 } 131 }