
com.qualinsight.plugins.sonarqube.badges.ws.SVGImageGenerator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qualinsight-plugins-sonarqube-badges Show documentation
Show all versions of qualinsight-plugins-sonarqube-badges Show documentation
This plugin adds a webservice to SonarQube that allows the retrieval of different types of badges that display as a SVG image the quality of a project or a view.
/*
* qualinsight-plugins-sonarqube-badges
* Copyright (c) 2015-2016, QualInsight
* http://www.qualinsight.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, you can retrieve a copy
* from .
*/
package com.qualinsight.plugins.sonarqube.badges.ws;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.server.ServerSide;
import com.qualinsight.plugins.sonarqube.badges.font.FontProvider;
import com.qualinsight.plugins.sonarqube.badges.font.FontProviderLocator;
/**
* Generates SVG images.
*
* @author Michel Pawlak
*/
@ServerSide
public final class SVGImageGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(SVGImageGenerator.class);
private static final String TEMPLATE_PATH = "/com/qualinsight/plugins/sonarqube/badges/ws/badge-template.svg";
private FontProvider fontProvider;
private final String templateString;
/**
* {@link SVGImageGenerator} IoC constructor.
*
* @param fontProviderLocator {@link FontProviderLocator} that will give access to a {@link FontProvider}.
*/
public SVGImageGenerator(final FontProviderLocator fontProviderLocator) throws IOException {
this.fontProvider = fontProviderLocator.fontProvider();
final InputStream resourceStream = getClass().getResourceAsStream(TEMPLATE_PATH);
this.templateString = IOUtils.toString(resourceStream);
LOGGER.info("SVGImageGenerator is now ready.");
}
/**
* Generates a SVG image object from a provided Data object.
*
* @param data Data object holding data required to produce a SVGGraphics2D object
* @return generated InputStream to read SVG image from
*/
public InputStream generateFor(final SVGImageData data) {
final Map replacements = new HashMap<>();
replacements.put("{{fontFamily}}", data.fontFamily());
replacements.put("{{labelText}}", data.labelText());
replacements.put("{{labelBackgroundColor}}", data.labelBackgroundColor()
.hexColor());
replacements.put("{{labelWidth}}", data.labelWidth());
replacements.put("{{labelHalfWidth}}", data.labelHalfWidth());
replacements.put("{{valueText}}", data.valueText());
replacements.put("{{valueBackgroundColor}}", data.valueBackgroundColor()
.hexColor());
replacements.put("{{valueWidth}}", data.valueWidth());
replacements.put("{{valueHalfWidth}}", data.valueHalfWidth());
replacements.put("{{totalWidth}}", data.totalWidth());
return IOUtils.toInputStream(StringUtils.replaceEach(this.templateString, replacements.keySet()
.toArray(new String[0]), replacements.values()
.toArray(new String[0])));
}
/**
* Returns the {@link FontProvider} to be used by badge generators.
*
* @return font
*/
public FontProvider fontProvider() {
return this.fontProvider;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy