001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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.java.ast.visitor;
021
022 import com.puppycrawl.tools.checkstyle.api.DetailAST;
023 import com.puppycrawl.tools.checkstyle.api.FullIdent;
024 import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025 import org.apache.commons.lang.StringUtils;
026 import org.sonar.api.resources.InputFileUtils;
027 import org.sonar.squid.api.AnalysisException;
028 import org.sonar.squid.api.SourceCode;
029 import org.sonar.squid.api.SourcePackage;
030 import org.sonar.squid.indexer.SquidIndex;
031 import org.sonar.squid.measures.Metric;
032
033 public class PackageVisitor extends JavaAstVisitor {
034
035 private static final String ROOT_PACKAGE = "";
036
037 private SquidIndex indexer;
038
039 public PackageVisitor(SquidIndex indexer) {
040 this.indexer = indexer;
041 }
042
043 @Override
044 public void visitFile(DetailAST ast) {
045 SourceCode packageRes;
046 if (ast == null) {
047 // ast can be null for empty files (all the file is commented-out)
048 packageRes = guessPackage();
049 } else {
050 packageRes = createSourcePackage(ast);
051 }
052 if (peekSourceCode().hasChild(packageRes)) {
053 packageRes = indexer.search(packageRes.getKey());
054 }
055 packageRes.setMeasure(Metric.PACKAGES, 1);
056 addSourceCode(packageRes);
057 }
058
059 private SourcePackage guessPackage() {
060 String directory = InputFileUtils.getRelativeDirectory(getInputFile());
061 return new SourcePackage(directory);
062 }
063
064 @Override
065 public void leaveFile(DetailAST ast) {
066 popSourceCode();
067 }
068
069 private SourcePackage createSourcePackage(DetailAST ast) {
070 String key = ROOT_PACKAGE;
071 if (ast.getType() == TokenTypes.PACKAGE_DEF) {
072 String packageName = FullIdent.createFullIdent(ast.getLastChild().getPreviousSibling()).getText();
073 key = packageName.replace('.', '/');
074 }
075 checkPhysicalDirectory(key);
076 return new SourcePackage(key);
077 }
078
079 /**
080 * Check that package declaration is consistent with the physical location of Java file.
081 * It aims to detect two cases :
082 * - wrong package declaration : "package org.foo" stored in the directory "org/bar"
083 * - source directory badly configured : src/ instead of src/main/java/
084 *
085 * @since 2.8
086 */
087 private void checkPhysicalDirectory(String key) {
088 String relativeDirectory = InputFileUtils.getRelativeDirectory(getInputFile());
089 // both relativeDirectory and key use slash '/' as separator
090 if (!StringUtils.equals(relativeDirectory, key)) {
091 String packageName = StringUtils.replace(key, "/", ".");
092 if (StringUtils.contains(relativeDirectory, key) || StringUtils.contains(key, relativeDirectory)) {
093 throw new AnalysisException(String.format("The source directory does not correspond to the package declaration %s", packageName));
094 }
095 throw new AnalysisException(String.format("The package declaration %s does not correspond to the file path %s",
096 packageName, getInputFile().getRelativePath()));
097 }
098 }
099 }