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 */
020package org.sonar.api.profiles;
021
022import org.apache.commons.lang.StringEscapeUtils;
023import org.apache.commons.lang.StringUtils;
024import org.sonar.api.server.ServerSide;
025import org.sonar.api.rules.ActiveRule;
026import org.sonar.api.rules.ActiveRuleParam;
027import org.sonar.api.utils.SonarException;
028
029import java.io.IOException;
030import java.io.Writer;
031
032/**
033 * @since 2.3
034 */
035@ServerSide
036public class XMLProfileSerializer {
037
038  public void write(RulesProfile profile, Writer writer) {
039    try {
040      appendHeader(profile, writer);
041      appendRules(profile, writer);
042      appendFooter(writer);
043
044    } catch (IOException e) {
045      throw new SonarException("Fail to export the profile " + profile, e);
046    }
047  }
048
049  private static void appendHeader(RulesProfile profile, Writer writer) throws IOException {
050    writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
051      + "<!-- Generated by SonarQube -->"
052      + "<profile><name>");
053    StringEscapeUtils.escapeXml(writer, profile.getName());
054    writer.append("</name><language>");
055    StringEscapeUtils.escapeXml(writer, profile.getLanguage());
056    writer.append("</language>");
057  }
058
059  private static void appendRules(RulesProfile profile, Writer writer) throws IOException {
060    if (!profile.getActiveRules().isEmpty()) {
061      writer.append("<rules>");
062      for (ActiveRule activeRule : profile.getActiveRules()) {
063        appendRule(activeRule, writer);
064      }
065      writer.append("</rules>");
066    }
067  }
068
069  private static void appendRule(ActiveRule activeRule, Writer writer) throws IOException {
070    writer.append("<rule><repositoryKey>");
071    writer.append(activeRule.getRepositoryKey());
072    writer.append("</repositoryKey><key>");
073    StringEscapeUtils.escapeXml(writer, activeRule.getRuleKey());
074    writer.append("</key>");
075    if (activeRule.getSeverity() != null) {
076      writer.append("<priority>");
077      writer.append(activeRule.getSeverity().name());
078      writer.append("</priority>");
079    }
080    appendRuleParameters(activeRule, writer);
081    writer.append("</rule>");
082  }
083
084  private static void appendRuleParameters(ActiveRule activeRule, Writer writer) throws IOException {
085    if (activeRule.getActiveRuleParams() != null && !activeRule.getActiveRuleParams().isEmpty()) {
086      writer.append("<parameters>");
087      for (ActiveRuleParam activeRuleParam : activeRule.getActiveRuleParams()) {
088        appendRuleParameter(writer, activeRuleParam);
089      }
090      writer.append("</parameters>");
091    }
092  }
093
094  private static void appendRuleParameter(Writer writer, ActiveRuleParam activeRuleParam) throws IOException {
095    if (StringUtils.isNotBlank(activeRuleParam.getValue())) {
096      writer.append("<parameter><key>");
097      StringEscapeUtils.escapeXml(writer, activeRuleParam.getKey());
098      writer.append("</key><value>");
099      StringEscapeUtils.escapeXml(writer, activeRuleParam.getValue());
100      writer.append("</value>");
101      writer.append("</parameter>");
102    }
103  }
104
105  private static void appendFooter(Writer writer) throws IOException {
106    writer.append("</profile>");
107  }
108}