org.sonar.plugins.ideainspections.IdeaProfileExporter.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.plugins.ideainspections.IdeaProfileExporter.java

Source

// Copyright 2008-2009 Emilio Lopez-Gabeiras
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
//

/*
 * 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.plugins.ideainspections;

import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.profiles.ProfileExporter;
import org.sonar.api.profiles.RulesProfile;
import org.sonar.api.resources.Java;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleParam;
import org.sonar.api.utils.SonarException;
import org.sonar.plugins.ideainspections.rules.DefaultRules;

import java.io.IOException;
import java.io.Writer;
import java.util.SortedMap;
import java.util.TreeMap;

import static org.sonar.plugins.ideainspections.IdeaConstants.*;

public class IdeaProfileExporter extends ProfileExporter {
    //~ Constructors ...................................................................................................

    @SuppressWarnings("UnusedDeclaration")
    public IdeaProfileExporter(Configuration conf) {
        super(REPOSITORY_KEY, PLUGIN_NAME);
        setSupportedLanguages(Java.KEY);
        setMimeType("application/xml");
    }

    /**
     * for unit tests
     */
    IdeaProfileExporter() {
        this(new BaseConfiguration());
    }

    //~ Methods ........................................................................................................

    public void exportProfile(RulesProfile profile, Writer writer) {
        try {
            appendXmlHeader(writer);

            SortedMap<String, Rule> defaultRules = new TreeMap<String, Rule>(DefaultRules.get());

            for (ActiveRule activeRule : profile.getActiveRulesByRepository(REPOSITORY_KEY)) {
                final Rule rule = activeRule.getRule();
                final boolean hasParameters = !rule.getParams().isEmpty();
                if (defaultRules.remove(rule.getKey()) == null || hasParameters) {
                    appendRule(writer, rule, true, !hasParameters);

                    if (hasParameters) {
                        appendParameters(writer, activeRule);
                    }
                }
            }

            for (Rule rule : defaultRules.values()) {
                appendRule(writer, rule, false, true);
            }

            appendXmlFooter(writer);
        } catch (IOException e) {
            throw new SonarException("Fail to export the profile " + profile, e);
        }
    }

    private void appendXmlHeader(Writer writer) throws IOException {
        writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + //
                "<!-- Generated by Sonar -->\n" + //
                "<inspections version=\"1.0\" is_locked=\"false\">\n");
    }

    private void appendXmlFooter(Writer writer) throws IOException {
        writer.append("</inspections>");
    }

    private void appendRule(Writer writer, Rule rule, boolean enabled, boolean close) throws IOException {
        writer.append("<").append(INSPECTION_TOOL_NODE);
        appendAttribute(writer, INSPECTION_CLASS_ATTR, rule.getConfigKey());
        appendAttribute(writer, INSPECTION_LEVEL_ATTR, IdeaUtils.toSeverity(rule.getPriority()));
        appendAttribute(writer, INSPECTION_ENABLED, Boolean.toString(enabled));
        if (close)
            writer.append("/>\n");
    }

    private void appendParameters(Writer writer, ActiveRule activeRule) throws IOException {
        writer.append(">\n");

        for (RuleParam option : activeRule.getRule().getParams()) {
            String value = activeRule.getParameter(option.getKey());

            if (StringUtils.isNotBlank(value)) {
                writer.append("<").append(INSPECTION_OPTION_NODE);
                appendAttribute(writer, INSPECTION_OPTION_NAME_ATTR, option.getKey());
                appendAttribute(writer, INSPECTION_OPTION_VALUE_ATTR, value);
                writer.append("/>\n");
            }
        }

        writer.append("</").append(INSPECTION_TOOL_NODE).append(">\n");
    }

    private void appendAttribute(Writer writer, String attr, String value) throws IOException {
        writer.append(" ").append(attr).append('=').append('"').append(value).append('"');
    }
}