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.java.api;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.resources.Project;
024
025/**
026 * @since 2.6
027 */
028public final class JavaUtils {
029
030  public static final String PACKAGE_SEPARATOR = ".";
031  public static final String DEFAULT_PACKAGE = "[default]";
032
033  /**
034   * All sensors executed after this barrier are sure that all Java resources are indexed.
035   */
036  public static final String BARRIER_BEFORE_SQUID = "BEFORE_SQUID";
037
038  /**
039   * Sensors executed before this barrier must not rely on index. No Java resources are indexed.
040   * Value is 'squid' in order to be backward-compatible with Sensor.FLAG_SQUID_ANALYSIS.
041   */
042  public static final String BARRIER_AFTER_SQUID = "squid";
043
044  /**
045   * To determine value of this property use {@link #getSourceVersion(Project)}.
046   */
047  public static final String JAVA_SOURCE_PROPERTY = "sonar.java.source";
048
049  /**
050   * Default value for property {@link #JAVA_SOURCE_PROPERTY}.
051   */
052  public static final String JAVA_SOURCE_DEFAULT_VALUE = "1.5";
053
054  /**
055   * To determine value of this property use {@link #getTargetVersion(Project)}.
056   */
057  public static final String JAVA_TARGET_PROPERTY = "sonar.java.target";
058
059  /**
060   * Default value for property {@link #JAVA_TARGET_PROPERTY}.
061   */
062  public static final String JAVA_TARGET_DEFAULT_VALUE = "1.5";
063
064  private JavaUtils() {
065    // only static methods
066  }
067
068  public static String abbreviatePackage(String packageName) {
069    String[] parts = StringUtils.split(packageName, PACKAGE_SEPARATOR);
070    StringBuilder sb = new StringBuilder();
071    if (parts.length >= 1) {
072      sb.append(parts[0]);
073    }
074    for (int index = 1; index < parts.length; index++) {
075      sb.append(PACKAGE_SEPARATOR).append(parts[index].charAt(0));
076    }
077    return sb.toString();
078  }
079
080  public static String getSourceVersion(Project project) {
081    String version = project.getConfiguration() != null ? project.getConfiguration().getString(JAVA_SOURCE_PROPERTY) : null;
082    return StringUtils.isNotBlank(version) ? version : JAVA_SOURCE_DEFAULT_VALUE;
083  }
084
085  public static String getTargetVersion(Project project) {
086    String version = project.getConfiguration() != null ? project.getConfiguration().getString(JAVA_TARGET_PROPERTY) : null;
087    return StringUtils.isNotBlank(version) ? version : JAVA_TARGET_DEFAULT_VALUE;
088  }
089}