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.Writer;
023import org.apache.commons.lang.StringUtils;
024import org.apache.commons.lang.builder.ToStringBuilder;
025import org.apache.commons.lang.builder.ToStringStyle;
026import org.sonar.api.ExtensionPoint;
027import org.sonar.api.batch.ScannerSide;
028import org.sonar.api.server.ServerSide;
029
030/**
031 * Export quality profile rules to a file
032 *
033 * @since 2.3
034 */
035@ScannerSide
036@ServerSide
037@ExtensionPoint
038public abstract class ProfileExporter {
039
040  private String[] supportedLanguages = new String[0];
041  private String key;
042  private String name;
043  private String mimeType = "text/plain";
044
045  protected ProfileExporter(String key, String name) {
046    this.key = key;
047    this.name = name;
048  }
049
050  /**
051   * Export activated rule from a quality profile to a writer
052   *
053   * Note that the quality profile can contain some rules from other plugins. It should not fail in this case.
054   */
055  public abstract void exportProfile(RulesProfile profile, Writer writer);
056
057  public String getKey() {
058    return key;
059  }
060
061  public final ProfileExporter setKey(String s) {
062    this.key = s;
063    return this;
064  }
065
066  public final String getName() {
067    return name;
068  }
069
070  public final ProfileExporter setName(String s) {
071    this.name = s;
072    return this;
073  }
074
075  /**
076   * Set the list of languages supported
077   * An empty value means that it will be available for every languages.
078   */
079  protected final ProfileExporter setSupportedLanguages(String... languages) {
080    supportedLanguages = (languages != null) ? languages : new String[0];
081    return this;
082  }
083
084  public String getMimeType() {
085    return mimeType;
086  }
087
088  /**
089   * Set the mime type of the exported file
090   */
091  public final ProfileExporter setMimeType(String s) {
092    if (StringUtils.isNotBlank(s)) {
093      this.mimeType = s;
094    }
095    return this;
096  }
097
098  /**
099   * @return if empty, then any languages are supported.
100   */
101  public String[] getSupportedLanguages() {
102    return supportedLanguages;
103  }
104
105  @Override
106  public final boolean equals(Object o) {
107    if (this == o) {
108      return true;
109    }
110    if (o == null || getClass() != o.getClass()) {
111      return false;
112    }
113    ProfileExporter that = (ProfileExporter) o;
114    return !((key != null) ? !key.equals(that.key) : (that.key != null));
115  }
116
117  @Override
118  public final int hashCode() {
119    return key != null ? key.hashCode() : 0;
120  }
121
122  @Override
123  public String toString() {
124    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
125      .append("key", key)
126      .append("name", name)
127      .append("mimeType", mimeType)
128      .append("languages", supportedLanguages)
129      .toString();
130  }
131}