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 */
020package org.sonar.api.batch;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.resources.JavaFile;
024import org.sonar.api.resources.JavaPackage;
025
026public final class SquidUtils {
027
028  private SquidUtils() {
029    // only static methods
030  }
031
032  public static JavaFile convertJavaFileKeyFromSquidFormat(String key) {
033    boolean isJavaFile = key.endsWith(".java");
034    if (isJavaFile) {
035      key = key.substring(0, key.length() - ".java".length());
036    }
037
038    String convertedKey = key.replace('/', '.');
039    if (convertedKey.indexOf('.') == -1 && !"".equals(convertedKey)) {
040      convertedKey = "[default]." + convertedKey;
041
042    } else if (convertedKey.indexOf('.') == -1) {
043      convertedKey = "[default]";
044    }
045
046    return new JavaFile(convertedKey);
047  }
048
049  public static JavaPackage convertJavaPackageKeyFromSquidFormat(String key) {
050    String convertedKey = key.replace('/', '.');
051    return new JavaPackage(convertedKey);
052  }
053
054  public static String convertToSquidKeyFormat(JavaFile file) {
055    String key = file.getKey();
056    if (file.getParent()==null || file.getParent().isDefault()) {
057      key = StringUtils.substringAfterLast(file.getKey(), ".");
058    } else {
059      key = StringUtils.replace(key, ".", "/");
060    }
061    return  key + ".java";
062  }
063}