Java XML String Transform createSettingsXml(Map props)

Here you can find the source of createSettingsXml(Map props)

Description

Creates a settings xml with a sonar profile, containing all the given properties Also adds repox to continue to use QAed artifacts

License

Open Source License

Declaration

public static String createSettingsXml(Map<String, String> props)
        throws Exception 

Method Source Code

//package com.java2s;
/*//w  w  w . ja v a  2s.c  o m
 * SonarSource :: IT :: SonarQube Maven
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    /**
     * Creates a settings xml with a sonar profile, containing all the given properties
     * Also adds repox to continue to use QAed artifacts 
     */
    public static String createSettingsXml(Map<String, String> props)
            throws Exception {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
                .newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        Element settings = doc.createElement("settings");
        Element profiles = doc.createElement("profiles");
        Element profile = doc.createElement("profile");

        Element id = doc.createElement("id");
        id.setTextContent("sonar");

        Element properties = doc.createElement("properties");

        for (Map.Entry<String, String> e : props.entrySet()) {
            Element el = doc.createElement(e.getKey());
            el.setTextContent(e.getValue());
            properties.appendChild(el);
        }

        profile.appendChild(id);
        profile.appendChild(properties);
        profile.appendChild(createRepositories(doc));
        profile.appendChild(createPluginRepositories(doc));

        profiles.appendChild(profile);
        settings.appendChild(profiles);
        doc.appendChild(settings);

        Writer writer = new StringWriter();
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    }

    private static Element createRepositories(Document doc) {
        Element repositories = doc.createElement("repositories");
        Element repository = doc.createElement("repository");
        Element id = doc.createElement("id");
        Element url = doc.createElement("url");

        id.setTextContent("sonarsource");
        url.setTextContent("https://repox.sonarsource.com/sonarsource");

        repositories.appendChild(repository);
        repository.appendChild(id);
        repository.appendChild(url);

        return repositories;
    }

    private static Element createPluginRepositories(Document doc) {
        Element repositories = doc.createElement("pluginRepositories");
        Element repository = doc.createElement("pluginRepository");
        Element id = doc.createElement("id");
        Element url = doc.createElement("url");

        id.setTextContent("sonarsource");
        url.setTextContent("https://repox.sonarsource.com/sonarsource");

        repositories.appendChild(repository);
        repository.appendChild(id);
        repository.appendChild(url);

        return repositories;
    }
}

Related

  1. convertValidatorResult(Result result, StringWriter writer)
  2. createElement(Node node, String name, String value, Map attributes)
  3. createFullReportLog(String sessionLogDir)
  4. createObject(String classname)
  5. createOrUpdateConnection(String fileName, String directory, String communityId, String serviceName, String serviceUrl, String defaultVersion, Logger log)
  6. createSource(final String message)
  7. createSource(String msg)
  8. createString(Element element)
  9. createXml(ArrayList nameList, ArrayList stringList, ArrayList stringArrayNameList, HashMap> stringArrayContentMap)