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