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