123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- /*
- * SonarQube
- * Copyright (C) 2009-2021 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- package org.sonar.api.rules;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.LinkedHashSet;
- import java.util.List;
- import java.util.Set;
- import javax.annotation.CheckForNull;
- import javax.annotation.Nullable;
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.builder.EqualsBuilder;
- import org.apache.commons.lang.builder.HashCodeBuilder;
- import org.apache.commons.lang.builder.ToStringBuilder;
- import org.apache.commons.lang.builder.ToStringStyle;
- import org.sonar.api.rule.RuleKey;
- import org.sonar.check.Cardinality;
-
- import static java.util.Arrays.asList;
- import static java.util.Collections.unmodifiableSet;
-
- public class Rule {
-
- /**
- * @since 3.6
- */
- public static final String STATUS_BETA = "BETA";
- /**
- * @since 3.6
- */
- public static final String STATUS_DEPRECATED = "DEPRECATED";
- /**
- * @since 3.6
- */
- public static final String STATUS_READY = "READY";
-
- /**
- * For internal use only.
- *
- * @since 3.6
- */
- public static final String STATUS_REMOVED = "REMOVED";
-
- /**
- * List of available status
- *
- * @since 3.6
- */
- private static final Set<String> STATUS_LIST = unmodifiableSet(new LinkedHashSet<>(asList(STATUS_READY, STATUS_BETA, STATUS_DEPRECATED, STATUS_REMOVED)));
-
- /**
- * The default priority given to a rule if not explicitly set
- */
- public static final RulePriority DEFAULT_PRIORITY = RulePriority.MAJOR;
-
- private String name;
- private String key;
- private String configKey;
- private RulePriority priority = DEFAULT_PRIORITY;
- private String description;
- private String pluginName;
- private boolean isTemplate = false;
- private String status = STATUS_READY;
- private String language;
- private Rule template = null;
- private List<RuleParam> params = new ArrayList<>();
- private Date createdAt;
- private Date updatedAt;
- private String tags;
- private String systemTags;
-
- /**
- * @deprecated since 2.3. Use the factory method {@link #create()}
- */
- @Deprecated
- public Rule() {
- }
-
- /**
- * Creates rule with minimum set of info
- *
- * @param pluginName the plugin name indicates which plugin the rule belongs to
- * @param key the key should be unique within a plugin, but it is even more careful for the time being that it is unique across the
- * application
- * @deprecated since 2.3. Use the factory method {@link #create()}
- */
- @Deprecated
- public Rule(String pluginName, String key) {
- this.pluginName = pluginName;
- this.key = key;
- this.configKey = key;
- }
-
- /**
- * @deprecated since 8.4. Use {@link #ruleKey()} instead
- */
- @Deprecated
- public Integer getId() {
- throw new UnsupportedOperationException("This method is no longer supported");
- }
-
- public String getName() {
- return name;
- }
-
- /**
- * Sets the rule name
- */
- public Rule setName(String name) {
- this.name = removeNewLineCharacters(name);
- return this;
- }
-
- public String getKey() {
- return key;
- }
-
- /**
- * Sets the rule key
- */
- public Rule setKey(String key) {
- this.key = key;
- return this;
- }
-
- public String getConfigKey() {
- return configKey;
- }
-
- /**
- * Sets the configuration key
- */
- public Rule setConfigKey(String configKey) {
- this.configKey = configKey;
- return this;
- }
-
- public String getDescription() {
- return description;
- }
-
- /**
- * Sets the rule description
- */
- public Rule setDescription(String description) {
- this.description = StringUtils.strip(description);
- return this;
- }
-
- public Boolean isEnabled() {
- return !STATUS_REMOVED.equals(status);
- }
-
- public List<RuleParam> getParams() {
- return params;
- }
-
- public RuleParam getParam(String key) {
- for (RuleParam param : params) {
- if (StringUtils.equals(key, param.getKey())) {
- return param;
- }
- }
- return null;
- }
-
- /**
- * Sets the rule parameters
- */
- public Rule setParams(List<RuleParam> params) {
- this.params.clear();
- for (RuleParam param : params) {
- param.setRule(this);
- this.params.add(param);
- }
- return this;
- }
-
- public RuleParam createParameter() {
- RuleParam parameter = new RuleParam()
- .setRule(this);
- params.add(parameter);
- return parameter;
- }
-
- public RuleParam createParameter(String key) {
- RuleParam parameter = new RuleParam()
- .setKey(key)
- .setRule(this);
- params.add(parameter);
- return parameter;
- }
-
- /**
- * @since 2.5
- */
- public RulePriority getSeverity() {
- return priority;
- }
-
- /**
- * @param severity severity to set, if null, uses the default priority.
- * @since 2.5
- */
- public Rule setSeverity(@Nullable RulePriority severity) {
- if (severity == null) {
- this.priority = DEFAULT_PRIORITY;
- } else {
- this.priority = severity;
- }
- return this;
- }
-
- public String getRepositoryKey() {
- return pluginName;
- }
-
- public Rule setRepositoryKey(String s) {
- this.pluginName = s;
- return this;
- }
-
- public Rule setUniqueKey(String repositoryKey, String key) {
- return setRepositoryKey(repositoryKey).setKey(key).setConfigKey(key);
- }
-
- /**
- * @since 4.4
- */
- public boolean isTemplate() {
- return isTemplate;
- }
-
- /**
- * @since 4.4
- */
- public Rule setIsTemplate(boolean isTemplate) {
- this.isTemplate = isTemplate;
- return this;
- }
-
- /**
- * @deprecated since 4.4, use {@link #isTemplate()}
- */
- @Deprecated
- public Cardinality getCardinality() {
- return isTemplate ? Cardinality.MULTIPLE : Cardinality.SINGLE;
- }
-
- /**
- * @deprecated since 4.4, use {@link #setIsTemplate(boolean)}
- */
- @Deprecated
- public Rule setCardinality(Cardinality c) {
- this.isTemplate = Cardinality.MULTIPLE.equals(c);
- return this;
- }
-
- /**
- * @deprecated since 4.4, use {@link #getTemplate()}
- */
- @Deprecated
- public Rule getParent() {
- return getTemplate();
- }
-
- /**
- * @deprecated since 4.4, use {@link #setTemplate(Rule)}}
- */
- @Deprecated
- public Rule setParent(Rule parent) {
- return setTemplate(parent);
- }
-
- /**
- * @since 4.4
- */
- public Rule getTemplate() {
- return template;
- }
-
- /**
- * @since 4.4
- */
- public Rule setTemplate(Rule template) {
- this.template = template;
- return this;
- }
-
- /**
- * @since 3.6
- */
- public String getStatus() {
- return status;
- }
-
- /**
- * @since 3.6
- */
- public Rule setStatus(String status) {
- if (!STATUS_LIST.contains(status)) {
- throw new IllegalStateException("The status of a rule can only contain : " + String.join(", ", STATUS_LIST));
- }
- this.status = status;
- return this;
- }
-
- /**
- * @since 3.6
- */
- public Date getCreatedAt() {
- return createdAt;
- }
-
- /**
- * @since 3.6
- */
- public Rule setCreatedAt(Date d) {
- this.createdAt = d;
- return this;
- }
-
- /**
- * @since 3.6
- */
- public Date getUpdatedAt() {
- return updatedAt;
- }
-
- /**
- * @since 3.6
- */
- public Rule setUpdatedAt(Date updatedAt) {
- this.updatedAt = updatedAt;
- return this;
- }
-
- /**
- * @since 3.6
- */
- public String getLanguage() {
- return language;
- }
-
- /**
- * For internal use only.
- *
- * @since 3.6
- */
- public Rule setLanguage(String language) {
- this.language = language;
- return this;
- }
-
- /**
- * For definition of rule only
- */
- public String[] getTags() {
- return tags == null ? new String[0] : StringUtils.split(tags, ',');
- }
-
- /**
- * For definition of rule only
- */
- public Rule setTags(String[] tags) {
- this.tags = tags == null ? null : StringUtils.join(tags, ',');
- return this;
- }
-
- /**
- * For internal use
- */
- public String[] getSystemTags() {
- return systemTags == null ? new String[0] : StringUtils.split(systemTags, ',');
- }
-
- public Rule setSystemTags(String[] tags) {
- this.systemTags = tags == null ? null : StringUtils.join(tags, ',');
- return this;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (!(obj instanceof Rule)) {
- return false;
- }
- if (this == obj) {
- return true;
- }
- Rule other = (Rule) obj;
- return new EqualsBuilder()
- .append(pluginName, other.getRepositoryKey())
- .append(key, other.getKey())
- .isEquals();
- }
-
- @Override
- public int hashCode() {
- return new HashCodeBuilder(17, 37)
- .append(pluginName)
- .append(key)
- .toHashCode();
- }
-
- @Override
- public String toString() {
- // Note that ReflectionToStringBuilder will not work here - see SONAR-3077
- return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
- .append("name", name)
- .append("key", key)
- .append("configKey", configKey)
- .append("plugin", pluginName)
- .append("severity", priority)
- .append("isTemplate", isTemplate())
- .append("status", status)
- .append("language", language)
- .append("template", template)
- .toString();
- }
-
- @CheckForNull
- private static String removeNewLineCharacters(@Nullable String text) {
- String removedCRLF = StringUtils.remove(text, "\n");
- removedCRLF = StringUtils.remove(removedCRLF, "\r");
- removedCRLF = StringUtils.remove(removedCRLF, "\n\r");
- removedCRLF = StringUtils.remove(removedCRLF, "\r\n");
- return removedCRLF;
- }
-
- public static Rule create() {
- return new Rule();
- }
-
- /**
- * Create with all required fields
- */
- public static Rule create(String repositoryKey, String key, String name) {
- return new Rule().setUniqueKey(repositoryKey, key).setName(name);
- }
-
- /**
- * Create with all required fields
- *
- * @since 2.10
- */
- public static Rule create(String repositoryKey, String key) {
- return new Rule().setUniqueKey(repositoryKey, key);
- }
-
- /**
- * @since 3.6
- */
- public RuleKey ruleKey() {
- return RuleKey.of(getRepositoryKey(), getKey());
- }
-
- }
|