com.technophobia.substeps.glossary.FileBasedGlossaryPublisher Maven / Gradle / Ivy
package com.technophobia.substeps.glossary;
import com.google.common.collect.Ordering;
import com.google.common.collect.TreeMultimap;
import com.google.common.io.Files;
import com.technophobia.substeps.model.exception.SubstepsRuntimeException;
import org.apache.maven.plugin.MojoExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* Created by ian on 25/02/16.
*/
public abstract class FileBasedGlossaryPublisher implements GlossaryPublisher {
private static final Logger log = LoggerFactory.getLogger(FileBasedGlossaryPublisher.class);
/**
* @parameter
*/
private File outputFile;
@Override
public void publish(final List stepimplementationDescriptors) {
final Map> sectionSorted = sortStepDescriptions(stepimplementationDescriptors);
final String output = buildFileContents(sectionSorted);
writeOutputFile(output, outputFile != null ? outputFile : new File(getDefaultFileName()));
}
public abstract String buildFileContents(final Map> sectionSorted);
public abstract String getDefaultFileName();
private Comparator expressionComparator = (s1, s2) -> s1.getExpression().compareTo(s2.getExpression());
private String getSection(StepDescriptor stepTag) {
boolean noTag = stepTag.getSection() == null || stepTag.getSection().isEmpty();
return noTag ? "Miscellaneous" : stepTag.getSection();
}
private Map> sortStepDescriptions(List stepimplementationDescriptors) {
TreeMultimap sections = TreeMultimap.create(Ordering.natural(), expressionComparator);
for (final StepImplementationsDescriptor descriptor : stepimplementationDescriptors) {
for (final StepDescriptor step : descriptor.getExpressions()) {
sections.put(getSection(step), step);
}
}
return sections.asMap();
}
private void writeOutputFile(String content, File outputFile) {
if (outputFile.exists() && !outputFile.delete()){
throw new SubstepsRuntimeException("failed to delete output file: " + outputFile.getAbsolutePath());
}
// write out
try {
if (outputFile.createNewFile()) {
Files.write(content, outputFile, Charset.defaultCharset());
} else {
log.error("unable to create new file: " + outputFile.getAbsolutePath());
}
} catch (final IOException e) {
log.error("IOException writing file", e);
}
}
}