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 */
020package org.sonar.api.profiles;
021
022import org.apache.commons.lang.builder.ToStringBuilder;
023import org.apache.commons.lang.builder.ToStringStyle;
024import org.sonar.api.ServerExtension;
025import org.sonar.api.utils.ValidationMessages;
026
027import java.io.Reader;
028
029/**
030 * @since 2.3
031 */
032public abstract class ProfileImporter implements ServerExtension {
033
034  private String[] supportedLanguages = new String[0];
035  private String importerKey;
036  private String importerName;
037
038  protected ProfileImporter(String key, String name) {
039    this.importerKey = key;
040    this.importerName = name;
041  }
042
043  public abstract RulesProfile importProfile(Reader reader, ValidationMessages messages);
044
045  public String getKey() {
046    return importerKey;
047  }
048
049  public final ProfileImporter setKey(String s) {
050    this.importerKey = s;
051    return this;
052  }
053
054  public final String getName() {
055    return importerName;
056  }
057
058  public final ProfileImporter setName(String s) {
059    this.importerName = s;
060    return this;
061  }
062
063  protected final ProfileImporter setSupportedLanguages(String... languages) {
064    supportedLanguages = (languages != null ? languages : new String[0]);
065    return this;
066  }
067
068  /**
069   * @return if empty, then any languages are supported.
070   */
071  public String[] getSupportedLanguages() {
072    return supportedLanguages;
073  }
074
075  @Override
076  public final boolean equals(Object o) {
077    if (this == o) {
078      return true;
079    }
080    if (o == null || getClass() != o.getClass()) {
081      return false;
082    }
083    ProfileImporter that = (ProfileImporter) o;
084    if (importerKey != null ? !importerKey.equals(that.importerKey) : that.importerKey != null) {
085      return false;
086    }
087    return true;
088  }
089
090  @Override
091  public final int hashCode() {
092    return importerKey != null ? importerKey.hashCode() : 0;
093  }
094
095  @Override
096  public String toString() {
097    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
098        .append("key", importerKey)
099        .append("name", importerName)
100        .append("languages", supportedLanguages)
101        .toString();
102  }
103}