
org.sonar.gherkin.checks.RuleDescriptionsGenerator Maven / Gradle / Ivy
/*
* SonarQube Gherkin Analyzer
* Copyright (C) 2016-2016 David RACODON
* [email protected]
*
* 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.
*/
package org.sonar.gherkin.checks;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class RuleDescriptionsGenerator {
private static final String UTF_8 = "UTF-8";
private final Map tags = ImmutableMap.builder()
.put("[[allForbiddenWords]]", generateForbiddenWordsHtmlTable())
.build();
public void generateHtmlRuleDescription(String templatePath, String outputPath) {
try (OutputStream fileOutputStream = new FileOutputStream(outputPath)) {
Writer writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream, UTF_8));
writer.write(replaceTags(FileUtils.readFileToString(new File(templatePath), UTF_8)));
writer.flush();
writer.close();
} catch (IOException e) {
throw new IllegalStateException("Could not generate the HTML description.", e);
}
}
private String generateForbiddenWordsHtmlTable() {
StringBuilder html = new StringBuilder("\n");
List> subLists = Lists.partition(Arrays.stream(WordingBusinessLevelCheck.FORBIDDEN_WORDS).sorted().collect(Collectors.toList()), 3);
for (List subList : subLists) {
html.append("");
for (String word : subList) {
html.append("");
html.append(word);
html.append(" \n");
}
html.append(" ");
}
html.append("
\n");
return html.toString();
}
private String replaceTags(String rawDescription) {
String description = rawDescription;
for (Map.Entry tag : tags.entrySet()) {
description = description.replace(tag.getKey(), tag.getValue());
}
return description;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy