001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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 * @since 2.3
031 */
032@ServerSide
033@ExtensionPoint
034public abstract class ProfileImporter {
035
036  private String[] supportedLanguages = new String[0];
037  private String importerKey;
038  private String importerName;
039
040  protected ProfileImporter(String key, String name) {
041    this.importerKey = key;
042    this.importerName = name;
043  }
044
045  public abstract RulesProfile importProfile(Reader reader, ValidationMessages messages);
046
047  public String getKey() {
048    return importerKey;
049  }
050
051  public final ProfileImporter setKey(String s) {
052    this.importerKey = s;
053    return this;
054  }
055
056  public final String getName() {
057    return importerName;
058  }
059
060  public final ProfileImporter setName(String s) {
061    this.importerName = s;
062    return this;
063  }
064
065  protected final ProfileImporter setSupportedLanguages(String... languages) {
066    supportedLanguages = (languages != null) ? languages : new String[0];
067    return this;
068  }
069
070  /**
071   * @return if empty, then any languages are supported.
072   */
073  public String[] getSupportedLanguages() {
074    return supportedLanguages;
075  }
076
077  @Override
078  public final boolean equals(Object o) {
079    if (this == o) {
080      return true;
081    }
082    if (o == null || getClass() != o.getClass()) {
083      return false;
084    }
085    ProfileImporter that = (ProfileImporter) o;
086    return !((importerKey != null) ? !importerKey.equals(that.importerKey) : (that.importerKey != null));
087  }
088
089  @Override
090  public final int hashCode() {
091    return (importerKey != null) ? importerKey.hashCode() : 0;
092  }
093
094  @Override
095  public String toString() {
096    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
097      .append("key", importerKey)
098      .append("name", importerName)
099      .append("languages", supportedLanguages)
100      .toString();
101  }
102}