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