001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar 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 * Sonar 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
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.api.profiles;
021
022import org.apache.commons.lang.StringEscapeUtils;
023import org.apache.commons.lang.StringUtils;
024import org.sonar.api.ServerComponent;
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 */
035public final class XMLProfileSerializer implements ServerComponent {
036
037  public void write(RulesProfile profile, Writer writer) {
038    try {
039      appendHeader(profile, writer);
040      appendRules(profile, writer);
041      appendAlerts(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 void appendHeader(RulesProfile profile, Writer writer) throws IOException {
050    writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
051        + "<!-- Generated by Sonar -->"
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 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 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 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 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 void appendAlerts(RulesProfile profile, Writer writer) throws IOException {
106    if (!profile.getAlerts().isEmpty()) {
107      writer.append("<alerts>");
108      for (Alert alert : profile.getAlerts()) {
109        appendAlert(alert, writer);
110      }
111      writer.append("</alerts>");
112    }
113  }
114
115  private void appendAlert(Alert alert, Writer writer) throws IOException {
116    writer.append("<alert><metric>");
117    StringEscapeUtils.escapeXml(writer, alert.getMetric().getKey());
118    writer.append("</metric>");
119    if (alert.getPeriod() !=null) {
120      writer.append("<period>");
121      StringEscapeUtils.escapeXml(writer, Integer.toString(alert.getPeriod()));
122      writer.append("</period>");
123    }
124    writer.append("<operator>");
125    StringEscapeUtils.escapeXml(writer, alert.getOperator());
126    writer.append("</operator>");
127    writer.append("<warning>");
128    StringEscapeUtils.escapeXml(writer, alert.getValueWarning());
129    writer.append("</warning>");
130    writer.append("<error>");
131    StringEscapeUtils.escapeXml(writer, alert.getValueError());
132    writer.append("</error></alert>");
133  }
134
135  private void appendFooter(Writer writer) throws IOException {
136    writer.append("</profile>");
137  }
138}