diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 13:00:25 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-14 13:00:25 +0000 |
commit | ddcc6272a62c245618d7797712062c96ff7ad3d4 (patch) | |
tree | 2465df13f82cbc664a51b4b7439c570b50e2146e /sonar-deprecated/src/main/java | |
parent | 2f54216f77f9580f56891d662e56a5d151ce1a2a (diff) | |
download | sonarqube-ddcc6272a62c245618d7797712062c96ff7ad3d4.tar.gz sonarqube-ddcc6272a62c245618d7797712062c96ff7ad3d4.zip |
move deprecated rule API to sonar-deprecated
Diffstat (limited to 'sonar-deprecated/src/main/java')
13 files changed, 1060 insertions, 0 deletions
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractViolationsStaxParser.java b/sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractViolationsStaxParser.java new file mode 100644 index 00000000000..9a7432f16b1 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractViolationsStaxParser.java @@ -0,0 +1,194 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.batch; + +import static org.apache.commons.lang.StringUtils.isNotBlank; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.text.ParseException; + +import javax.xml.stream.XMLStreamException; + +import org.apache.commons.io.IOUtils; +import org.codehaus.staxmate.in.SMEvent; +import org.codehaus.staxmate.in.SMHierarchicCursor; +import org.codehaus.staxmate.in.SMInputCursor; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Resource; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RulesManager; +import org.sonar.api.rules.Violation; +import org.sonar.api.utils.ParsingUtils; +import org.sonar.api.utils.StaxParser; + +/** + * @since 1.10 + * @deprecated since 2.3. Too much "black-box". + */ +@Deprecated +public abstract class AbstractViolationsStaxParser { + + protected RulesManager rulesManager; + protected SensorContext context; + protected boolean doSaveViolationsOnUnexistedResource = true; + + /** + * @deprecated since 1.11. + */ + @Deprecated + protected AbstractViolationsStaxParser(SensorContext context, RulesManager rulesManager, RulesProfile profile) { + this.rulesManager = rulesManager; + this.context = context; + } + + protected AbstractViolationsStaxParser(SensorContext context, RulesManager rulesManager) { + this.rulesManager = rulesManager; + this.context = context; + } + + /** + * Cursor for child resources to parse, the returned input cursor should be filtered on SMEvent.START_ELEMENT for optimal perfs + * + * @param rootCursor + * the root xml doc cursor + * @return a cursor with child resources elements to parse + */ + protected abstract SMInputCursor cursorForResources(SMInputCursor rootCursor) throws XMLStreamException; + + /** + * Cursor for violations to parse for a given resource, the returned input cursor should be filtered on SMEvent.START_ELEMENT for optimal + * perfs + * + * @param resourcesCursor + * the current resource cursor + * @return a cursor with child violations elements to parse + */ + protected abstract SMInputCursor cursorForViolations(SMInputCursor resourcesCursor) throws XMLStreamException; + + /** + * Transforms a given xml resource to a resource Object + */ + protected abstract Resource toResource(SMInputCursor resourceCursor) throws XMLStreamException; + + protected abstract String messageFor(SMInputCursor violationCursor) throws XMLStreamException; + + protected abstract String ruleKey(SMInputCursor violationCursor) throws XMLStreamException; + + protected abstract String keyForPlugin(); + + protected abstract String lineNumberForViolation(SMInputCursor violationCursor) throws XMLStreamException; + + /** + * Specify if violations must be saved even if when the Resource associated to a violation doesn't yet exist. + * In that case the Resource is automatically created. + * + * @param doSaveViolationsOnUnexistedResource by default, the value is true + */ + public final void setDoSaveViolationsOnUnexistedResource(boolean doSaveViolationsOnUnexistedResource) { + this.doSaveViolationsOnUnexistedResource = doSaveViolationsOnUnexistedResource; + } + + public void parse(File violationsXMLFile) throws XMLStreamException { + if (violationsXMLFile != null && violationsXMLFile.exists()) { + InputStream input = null; + try { + input = new FileInputStream(violationsXMLFile); + parse(input); + + } catch (FileNotFoundException e) { + throw new XMLStreamException(e); + + } finally { + IOUtils.closeQuietly(input); + } + } + } + + public final void parse(InputStream input) throws XMLStreamException { + if (input != null) { + StaxParser parser = new StaxParser(new StaxParser.XmlStreamHandler() { + + public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { + parseResources(rootCursor.advance()); + } + }, true); + parser.parse(input); + } + } + + private void parseResources(SMInputCursor rootCursor) throws XMLStreamException { + SMInputCursor resourcesCursor = cursorForResources(rootCursor); + SMEvent event; + while ((event = resourcesCursor.getNext()) != null) { + if (event.compareTo(SMEvent.START_ELEMENT) == 0) { + parseViolations(resourcesCursor); + } + } + } + + private void parseViolations(SMInputCursor resourcesCursor) throws XMLStreamException { + Resource resource = toResource(resourcesCursor); + if ( !doSaveViolationsOnUnexistedResource && context.getResource(resource) == null) { + return; + } + SMInputCursor violationsCursor = cursorForViolations(resourcesCursor); + SMEvent event; + while ((event = violationsCursor.getNext()) != null) { + if (event.compareTo(SMEvent.START_ELEMENT) == 0) { + createViolationFor(resource, violationsCursor); + } + } + } + + private void createViolationFor(Resource resource, SMInputCursor violationCursor) throws XMLStreamException { + Rule rule = getRule(violationCursor); + Integer line = getLineIndex(violationCursor); + if (rule != null && resource != null) { + Violation violation = new Violation(rule, resource) + .setLineId(line) + .setMessage(messageFor(violationCursor)); + context.saveViolation(violation); + } + } + + private Rule getRule(SMInputCursor violationCursor) throws XMLStreamException { + return rulesManager.getPluginRule(keyForPlugin(), ruleKey(violationCursor)); + } + + private Integer getLineIndex(SMInputCursor violationCursor) throws XMLStreamException { + String line = lineNumberForViolation(violationCursor); + return parseLineIndex(line); + } + + protected static Integer parseLineIndex(String line) { + if ( !isNotBlank(line) || line.indexOf('-') != -1) { + return null; + } + try { + return (int) ParsingUtils.parseNumber(line); + } catch (ParseException ignore) { + return null; + } + } + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/AbstractImportableRulesRepository.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/AbstractImportableRulesRepository.java new file mode 100644 index 00000000000..9926a33081e --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/AbstractImportableRulesRepository.java @@ -0,0 +1,75 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.CharEncoding; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Language; +import org.sonar.api.utils.SonarException; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +@Deprecated +public abstract class AbstractImportableRulesRepository<LANG extends Language, MAPPER extends RulePriorityMapper<?, ?>> extends AbstractRulesRepository<LANG, MAPPER> implements ConfigurationImportable { + + public AbstractImportableRulesRepository(LANG language, MAPPER mapper) { + super(language, mapper); + } + + /** + * A map a of profiles to import, The profile name as key, and the xml profile file name in the classpath + * + * @return + */ + public abstract Map<String, String> getBuiltInProfiles(); + + public final List<RulesProfile> getProvidedProfiles() { + List<RulesProfile> profiles = new ArrayList<RulesProfile>(); + + Map<String, String> defaultProfiles = new TreeMap<String, String>(getBuiltInProfiles()); + for (Map.Entry<String, String> entry : defaultProfiles.entrySet()) { + profiles.add(loadProvidedProfile(entry.getKey(), getCheckResourcesBase() + entry.getValue())); + } + return profiles; + } + + public final RulesProfile loadProvidedProfile(String name, String fileName) { + InputStream input = null; + try { + input = getClass().getResourceAsStream(fileName); + RulesProfile profile = new RulesProfile(name, getLanguage().getKey()); + profile.setActiveRules(importConfiguration(IOUtils.toString(input, CharEncoding.UTF_8), getInitialReferential())); + return profile; + + } catch (IOException e) { + throw new SonarException("Configuration file not found for the profile : " + name, e); + + } finally { + IOUtils.closeQuietly(input); + } + } + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/AbstractRulesRepository.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/AbstractRulesRepository.java new file mode 100644 index 00000000000..ec69f047174 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/AbstractRulesRepository.java @@ -0,0 +1,76 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules; + +import org.apache.commons.io.IOUtils; +import org.sonar.api.resources.Language; +import org.sonar.api.utils.SonarException; + +import java.io.InputStream; +import java.util.List; + +@Deprecated +public abstract class AbstractRulesRepository<LANG extends Language, MAPPER extends RulePriorityMapper<?, ?>> implements RulesRepository<LANG> { + + private MAPPER priorityMapper; + private LANG language; + + public AbstractRulesRepository(LANG language, MAPPER priorityMapper) { + super(); + this.priorityMapper = priorityMapper; + this.language = language; + } + + public LANG getLanguage() { + return language; + } + + public abstract String getRepositoryResourcesBase(); + + public final List<Rule> getInitialReferential() { + String baseCP = getCheckResourcesBase(); + InputStream input = getClass().getResourceAsStream(baseCP + "rules.xml"); + if (input == null) { + throw new SonarException("Resource not found : " + baseCP + "rules.xml"); + } + try { + return new StandardRulesXmlParser().parse(input); + } + finally { + IOUtils.closeQuietly(input); + } + } + + public List<Rule> parseReferential(String fileContent) { + return new StandardRulesXmlParser().parse(fileContent); + } + + public MAPPER getRulePriorityMapper() { + return priorityMapper; + } + + protected String getCheckResourcesBase() { + String base = getRepositoryResourcesBase(); + base = base.startsWith("/") ? base : "/" + base; + base = base.endsWith("/") ? base : base + "/"; + return base; + } + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/ConfigurationExportable.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/ConfigurationExportable.java new file mode 100644 index 00000000000..3a853a6ab3f --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/ConfigurationExportable.java @@ -0,0 +1,29 @@ +/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.rules;
+
+import org.sonar.api.profiles.RulesProfile;
+
+@Deprecated
+public interface ConfigurationExportable {
+
+ String exportConfiguration(RulesProfile profile);
+
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/ConfigurationImportable.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/ConfigurationImportable.java new file mode 100644 index 00000000000..c16337875e3 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/ConfigurationImportable.java @@ -0,0 +1,30 @@ +/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.rules;
+
+import java.util.List;
+import org.sonar.api.rules.Rule;
+
+@Deprecated
+public interface ConfigurationImportable {
+
+ List<ActiveRule> importConfiguration(String configuration, List<Rule> rules);
+
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/RulePriorityMapper.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/RulePriorityMapper.java new file mode 100644 index 00000000000..c8a382ecd89 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/RulePriorityMapper.java @@ -0,0 +1,29 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules; + +@Deprecated +public interface RulePriorityMapper<IN_TYPE, OUT_TYPE> { + + RulePriority from(IN_TYPE priority); + + OUT_TYPE to(RulePriority priority); + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/RulesManager.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/RulesManager.java new file mode 100644 index 00000000000..374f2241cf3 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/RulesManager.java @@ -0,0 +1,96 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules; + +import org.sonar.api.Plugin; +import org.sonar.api.resources.Language; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @deprecated since 2.3 + */ +@Deprecated +public abstract class RulesManager { + + /** + * Returns the list of languages for which there is a rule repository + * + * @return a Set of languages + */ + public abstract Set<Language> getLanguages(); + + /** + * Gets the list of Rules Repositories available for a language + * + * @param language the language + * @return the list of rules repositories + */ + public abstract List<RulesRepository<?>> getRulesRepositories(Language language); + + /** + * Gets the complete list of Rules Repositories in the Sonar instance + * + * @return the list of rules repositories + */ + public abstract List<RulesRepository<?>> getRulesRepositories(); + + /** + * Gets the list of rules plugins for a given language + * @param language the language + * @return the list of plugins + */ + public abstract List<Plugin> getPlugins(Language language); + + /** + * Get the list of rules plugin that implement a mechanism of import for a given language + * + * @param language the language + * @return the list of plugins + */ + public abstract List<Plugin> getImportablePlugins(Language language); + + /** + * Gets a list of rules indexed by their key for a given plugin + * @param pluginKey the plugin key + * @return a Map with the rule key and the rule + */ + public abstract Map<String, Rule> getPluginRulesIndexedByKey(String pluginKey); + + /** + * Gets a collection of rules belonging to a plugin + * + * @param pluginKey the plugin key + * @return the collection of rules + */ + public abstract Collection<Rule> getPluginRules(String pluginKey); + + /** + * Gets a rule belonging to a defined plugin based on its key + * + * @param pluginKey the plugin key + * @param ruleKey the rule key + * @return the rule + */ + public abstract Rule getPluginRule(String pluginKey, String ruleKey); +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/RulesRepository.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/RulesRepository.java new file mode 100644 index 00000000000..8156cac9723 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/RulesRepository.java @@ -0,0 +1,58 @@ +/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.rules;
+
+import org.sonar.api.BatchExtension;
+import org.sonar.api.ServerExtension;
+import org.sonar.api.profiles.RulesProfile;
+import org.sonar.api.resources.Language;
+
+import java.util.List;
+
+/**
+ * @deprecated since 2.3
+ */
+@Deprecated
+public interface RulesRepository<LANG extends Language> extends BatchExtension, ServerExtension {
+
+ /**
+ * @return the language the repository is associated
+ */
+ LANG getLanguage();
+
+ /**
+ * @return the list of rules of the repository
+ */
+ List<Rule> getInitialReferential();
+
+ /**
+ * The method to parse the base referential of rules and return a list of rules
+ *
+ * @param fileContent the initial referential
+ * @return a list of rules
+ */
+ List<Rule> parseReferential(String fileContent);
+
+ /**
+ * @return a list of profiles that are provided with the referential
+ */
+ List<RulesProfile> getProvidedProfiles();
+
+}
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/StandardProfileXmlParser.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/StandardProfileXmlParser.java new file mode 100644 index 00000000000..272b9642889 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/StandardProfileXmlParser.java @@ -0,0 +1,119 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules; + +import com.thoughtworks.xstream.XStream; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.xml.Profile; +import org.sonar.api.rules.xml.Property; +import org.sonar.api.utils.SonarException; + +import java.util.ArrayList; +import java.util.List; + +@Deprecated +public class StandardProfileXmlParser { + + private final List<Rule> rules; + + public StandardProfileXmlParser() { + rules = new ArrayList<Rule>(); + } + + public StandardProfileXmlParser(List<Rule> rules) { + this.rules = rules; + } + + /** + * see the XML format into the unit test src/test/java/.../StandardProfileXmlParserTest + */ + public Profile parse(String xml) { + return (Profile) getXStream().fromXML(xml); + } + + private XStream getXStream() { + XStream xstream = new XStream(); + xstream.processAnnotations(Profile.class); + xstream.processAnnotations(Rule.class); + xstream.processAnnotations(Property.class); + return xstream; + } + + public RulesProfile importConfiguration(String configuration) { + RulesProfile rulesProfile = new RulesProfile(); + List<ActiveRule> activeRules = new ArrayList<ActiveRule>(); + Profile profile = buildProfileFromXml(configuration); + + rulesProfile.setName(profile.getName()); + rulesProfile.setLanguage(profile.getLanguage()); + + if (StringUtils.isBlank(rulesProfile.getName())) { + throw new SonarException("Profile name can't be null or empty"); + } + + buildActiveRulesFromProfile(profile, activeRules); + rulesProfile.setActiveRules(activeRules); + return rulesProfile; + } + + protected Profile buildProfileFromXml(String configuration) { + StandardProfileXmlParser xstream = new StandardProfileXmlParser(); + return xstream.parse(configuration); + } + + protected void buildActiveRulesFromProfile(Profile profile, List<ActiveRule> activeRules) { + if (profile.getRules() != null && !profile.getRules().isEmpty()) { + for (org.sonar.api.rules.xml.Rule module : profile.getRules()) { + String ref = module.getKey(); + for (Rule rule : rules) { + if (rule.getConfigKey().equals(ref)) { + RulePriority rulePriority = getRulePriority(module); + ActiveRule activeRule = new ActiveRule(null, rule, rulePriority); + activeRule.setActiveRuleParams(getActiveRuleParams(module, rule, activeRule)); + activeRules.add(activeRule); + break; + } + } + } + } + } + + private RulePriority getRulePriority(org.sonar.api.rules.xml.Rule module) { + return StringUtils.isBlank(module.getPriority()) ? null : RulePriority.valueOfString(module.getPriority()); + } + + private List<ActiveRuleParam> getActiveRuleParams(org.sonar.api.rules.xml.Rule module, Rule rule, ActiveRule activeRule) { + List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>(); + if (module.getProperties() != null) { + for (Property property : module.getProperties()) { + if (rule.getParams() != null) { + for (RuleParam ruleParam : rule.getParams()) { + if (ruleParam.getKey().equals(property.getName())) { + activeRuleParams.add(new ActiveRuleParam(activeRule, ruleParam, property.getValue())); + } + } + } + } + } + return activeRuleParams; + } + +}
\ No newline at end of file diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/StandardRulesXmlParser.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/StandardRulesXmlParser.java new file mode 100644 index 00000000000..e532df1d040 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/StandardRulesXmlParser.java @@ -0,0 +1,160 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter; +import com.thoughtworks.xstream.core.util.QuickWriter; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; +import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; +import com.thoughtworks.xstream.io.xml.XppDriver; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang.CharEncoding; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.utils.SonarException; + +import java.io.*; +import java.lang.ref.WeakReference; +import java.util.*; + +@Deprecated +public class StandardRulesXmlParser { + + /** + * see the XML format into the unit test src/test/java/.../StandardRulesXmlParserTest + */ + public List<Rule> parse(String xml) { + InputStream inputStream = null; + try { + inputStream = IOUtils.toInputStream(xml, CharEncoding.UTF_8); + return setDefaultRulePriorities((List<Rule>) getXStream().fromXML(inputStream)); + + } catch (IOException e) { + throw new SonarException("Can't parse xml file", e); + + } finally { + IOUtils.closeQuietly(inputStream); + } + } + + public List<Rule> parse(Reader reader) { + return setDefaultRulePriorities((List<Rule>) getXStream().fromXML(reader)); + } + + public List<Rule> parse(InputStream input) { + try { + return setDefaultRulePriorities((List<Rule>) getXStream().fromXML(new InputStreamReader(input, CharEncoding.UTF_8))); + + } catch (UnsupportedEncodingException e) { + throw new SonarException("Can't parse xml file", e); + } + } + + private List<Rule> setDefaultRulePriorities(List<Rule> rules) { + for (Rule rule : rules) { + if (rule.getPriority() == null) { + rule.setPriority(RulePriority.MAJOR); + } + } + return rules; + } + + public String toXml(List<Rule> rules) { + return getXStream().toXML(rules); + } + + private static class CDataXppDriver extends XppDriver { + @Override + public HierarchicalStreamWriter createWriter(Writer out) { + return new XDataPrintWriter(out); + } + } + + private static class XDataPrintWriter extends PrettyPrintWriter { + public XDataPrintWriter(Writer writer) { + super(writer); + } + + @Override + protected void writeText(QuickWriter writer, String text) { + writer.write("<![CDATA["); + writer.write(text); + writer.write("]]>"); + } + } + + private XStream getXStream() { + XStream xstream = new XStream(new CDataXppDriver()); + xstream.registerConverter(new TrimStringConverter()); + xstream.alias("rules", ArrayList.class); + + xstream.alias("categ", RulesCategory.class); + xstream.useAttributeFor(RulesCategory.class, "name"); + xstream.aliasField("category", Rule.class, "rulesCategory"); + + xstream.alias("rule", Rule.class); + xstream.useAttributeFor(Rule.class, "key"); + xstream.useAttributeFor("priority", RulePriority.class); + + xstream.addImplicitCollection(Rule.class, "params"); + + xstream.alias("param", RuleParam.class); + xstream.useAttributeFor(RuleParam.class, "key"); + xstream.useAttributeFor(RuleParam.class, "type"); + + // only for backward compatibility with sonar 1.4. + xstream.omitField(RuleParam.class, "defaultValue"); + return xstream; + } + + /** + * See http://svn.codehaus.org/xstream/trunk/xstream/src/java/com/thoughtworks/xstream/converters/basic/StringConverter.java + */ + public static class TrimStringConverter extends AbstractSingleValueConverter { + + private final Map cache; + + public TrimStringConverter(final Map map) { + cache = map; + } + + public TrimStringConverter() { + this(Collections.synchronizedMap(new WeakHashMap())); + } + + public boolean canConvert(final Class type) { + return type.equals(String.class); + } + + public Object fromString(final String str) { + String trim = StringUtils.trim(str); + final WeakReference ref = (WeakReference) cache.get(trim); + String s = (String) (ref == null ? null : ref.get()); + + if (s == null) { + // fill cache + cache.put(str, new WeakReference(trim)); + s = trim; + } + + return s; + } + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Profile.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Profile.java new file mode 100644 index 00000000000..cd40e8a1f0e --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Profile.java @@ -0,0 +1,65 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar 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. + * + * Sonar 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 Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.rules.xml; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +import java.util.ArrayList; +import java.util.List; + +@Deprecated +@XStreamAlias("profile") +public class Profile { + + @XStreamAsAttribute + private String language; + + @XStreamAsAttribute + private String name; + + @XStreamImplicit + private List<Rule> rules = new ArrayList<Rule>(); + + public List<Rule> getRules() { + return rules; + } + + public void setRules(List<Rule> rules) { + this.rules = rules; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Property.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Property.java new file mode 100644 index 00000000000..8e6dbec7670 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Property.java @@ -0,0 +1,47 @@ +/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.rules.xml;
+
+import com.thoughtworks.xstream.annotations.XStreamAlias;
+import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
+
+@Deprecated
+@XStreamAlias("property")
+public class Property {
+
+ @XStreamAsAttribute
+ private String name;
+
+ @XStreamAsAttribute
+ private String value;
+
+ public Property(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public String getValue() {
+ return value;
+ }
+}
\ No newline at end of file diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Rule.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Rule.java new file mode 100644 index 00000000000..e424d1466a6 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/xml/Rule.java @@ -0,0 +1,82 @@ +/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.api.rules.xml;
+
+import com.thoughtworks.xstream.annotations.XStreamAlias;
+import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
+import com.thoughtworks.xstream.annotations.XStreamImplicit;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Deprecated
+@XStreamAlias("rule")
+public class Rule implements Comparable<String> {
+
+ @XStreamAsAttribute
+ private String key;
+
+ @XStreamAsAttribute
+ private String priority;
+
+ @XStreamImplicit
+ private List<Property> properties;
+
+ public Rule(String ref) {
+ this(ref, null);
+ }
+
+ public Rule(String ref, String priority) {
+ this.key = ref;
+ this.priority = priority;
+ }
+
+ public String getKey() {
+ return key;
+ }
+
+ public void setProperties(List<Property> properties) {
+ this.properties = properties;
+ }
+
+ public List<Property> getProperties() {
+ return properties;
+ }
+
+ public int compareTo(String o) {
+ return o.compareTo(key);
+ }
+
+ public String getPriority() {
+ return priority;
+ }
+
+ public void setPriority(String priority) {
+ this.priority = priority;
+ }
+
+ public void addProperty(Property property) {
+ if (properties == null) {
+ properties = new ArrayList<Property>();
+ }
+ properties.add(property);
+ }
+
+}
\ No newline at end of file |