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 package org.sonar.batch.util;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.sonar.api.resources.Directory;
024 import org.sonar.api.resources.JavaPackage;
025
026 public class DeprecatedKeyUtils {
027
028 private DeprecatedKeyUtils() {
029 // Utility class
030 }
031
032 /**
033 * Return the parent directory deprecated key for a given deprecated Java file key.
034 * "com.foo.Bar" -> "com/foo"
035 * "[root].Bar" -> "[root]"
036 * "Bar" -> "[root]"
037 */
038 public static String getJavaFileParentDeprecatedKey(String deprecatedJavaFileKey) {
039 String packageFullyQualifiedName;
040 String realKey = StringUtils.trim(deprecatedJavaFileKey);
041 if (realKey.contains(".")) {
042 packageFullyQualifiedName = StringUtils.substringBeforeLast(realKey, ".");
043 String deprecatedDirectoryKey = StringUtils.trimToEmpty(packageFullyQualifiedName);
044 if (JavaPackage.DEFAULT_PACKAGE_NAME.equals(deprecatedDirectoryKey)) {
045 return Directory.ROOT;
046 }
047 deprecatedDirectoryKey = deprecatedDirectoryKey.replaceAll("\\.", Directory.SEPARATOR);
048 return StringUtils.defaultIfEmpty(deprecatedDirectoryKey, Directory.ROOT);
049 } else {
050 return Directory.ROOT;
051 }
052 }
053
054 /**
055 * Return the deprecated key of a Java file given its path relative to source directory.
056 */
057 public static String getJavaFileDeprecatedKey(String sourceRelativePath) {
058 String pacname = null;
059 String classname = sourceRelativePath;
060
061 if (sourceRelativePath.indexOf('/') >= 0) {
062 pacname = StringUtils.substringBeforeLast(sourceRelativePath, "/");
063 pacname = StringUtils.replace(pacname, "/", ".");
064 classname = StringUtils.substringAfterLast(sourceRelativePath, "/");
065 }
066 classname = StringUtils.substringBeforeLast(classname, ".");
067 if (StringUtils.isBlank(pacname)) {
068 return new StringBuilder().append(JavaPackage.DEFAULT_PACKAGE_NAME).append(".").append(classname).toString();
069 } else {
070 return new StringBuilder().append(pacname.trim()).append(".").append(classname).toString();
071 }
072 }
073
074 }