001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program 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 * This program 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 */
020package org.sonar.api.profiles;
021
022import java.io.Reader;
023import org.apache.commons.lang.builder.ToStringBuilder;
024import org.apache.commons.lang.builder.ToStringStyle;
025import org.sonar.api.ExtensionPoint;
026import org.sonar.api.server.ServerSide;
027import org.sonar.api.utils.ValidationMessages;
028
029/**
030 * Create a quality profile from an external rules file.
031 *
032 * @since 2.3
033 */
034@ServerSide
035@ExtensionPoint
036public abstract class ProfileImporter {
037
038  private String[] supportedLanguages = new String[0];
039  private String importerKey;
040  private String importerName;
041
042  protected ProfileImporter(String key, String name) {
043    this.importerKey = key;
044    this.importerName = name;
045  }
046
047  /**
048   * Import the profile from a reader.
049   *
050   * {@link ValidationMessages#warnings} can be used to return some warnings to the user, for instance when some rules doesn't exist.
051   * {@link ValidationMessages#errors} can be used when an unrecoverable error is generating during import. No quality profile will be created.
052   */
053  public abstract RulesProfile importProfile(Reader reader, ValidationMessages messages);
054
055  public String getKey() {
056    return importerKey;
057  }
058
059  public final ProfileImporter setKey(String s) {
060    this.importerKey = s;
061    return this;
062  }
063
064  public final String getName() {
065    return importerName;
066  }
067
068  public final ProfileImporter setName(String s) {
069    this.importerName = s;
070    return this;
071  }
072
073  /**
074   * Set the list of languages supported
075   * An empty value means that it will be available for every languages.
076   */
077  protected final ProfileImporter setSupportedLanguages(String... languages) {
078    supportedLanguages = (languages != null) ? languages : new String[0];
079    return this;
080  }
081
082  /**
083   * @return if empty, then any languages are supported.
084   */
085  public String[] getSupportedLanguages() {
086    return supportedLanguages;
087  }
088
089  @Override
090  public final boolean equals(Object o) {
091    if (this == o) {
092      return true;
093    }
094    if (o == null || getClass() != o.getClass()) {
095      return false;
096    }
097    ProfileImporter that = (ProfileImporter) o;
098    return !((importerKey != null) ? !importerKey.equals(that.importerKey) : (that.importerKey != null));
099  }
100
101  @Override
102  public final int hashCode() {
103    return (importerKey != null) ? importerKey.hashCode() : 0;
104  }
105
106  @Override
107  public String toString() {
108    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
109      .append("key", importerKey)
110      .append("name", importerName)
111      .append("languages", supportedLanguages)
112      .toString();
113  }
114}