Skip to content
Open
1 change: 1 addition & 0 deletions api/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
exports org.itsallcode.openfasttrace.api.exporter;
exports org.itsallcode.openfasttrace.api.report;
// Workaround: Required for the EqualsVerifier
opens org.itsallcode.openfasttrace.api;
opens org.itsallcode.openfasttrace.api.core;

requires java.logging;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package org.itsallcode.openfasttrace.api.exporter;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
import java.util.stream.Stream;

import org.itsallcode.openfasttrace.api.core.Newline;
import org.itsallcode.openfasttrace.api.core.SpecificationItem;

/**
* Base class for {@link ExporterFactory} implementations that share exporter logic.
*/
public abstract class AbstractExporterFactory implements ExporterFactory
{
private static final Logger LOG = Logger.getLogger(AbstractExporterFactory.class.getName());

private final String supportedOutputFormat;

private ExporterContext context;

/**
* Create a new {@link AbstractExporterFactory}.
*
* @param supportedOutputFormat
* the format of the supported output format, e.g.
* {@code "html"}.
*/
protected AbstractExporterFactory(final String supportedOutputFormat)
{
this.supportedOutputFormat = supportedOutputFormat;
}

@Override
public void init(final ExporterContext context)
{
this.context = context;
}

/**
* Get the {@link ExporterContext} set by the {@link #init(ExporterContext)}
* method.
*
* @return the {@link ExporterContext}.
*/
@Override
public ExporterContext getContext()
{
return this.context;
}

@Override
public boolean supportsFormat(final String format)
{
return this.supportedOutputFormat.equals(format);
}

@Override
public Exporter createExporter(final Path file, final String format, final Charset charset,
final Newline newline, final Stream<SpecificationItem> itemStream)
{
if (!supportsFormat(format))
{
throw new ExporterException("Output format '" + format + "' not supported for export");
}
final Writer writer = createWriter(file, charset);
return createExporter(writer, itemStream, newline);
}

private static Writer createWriter(final Path file, final Charset charset)
{
if (file == null)
{
LOG.finest(() -> "Creating exporter for STDOUT using charset " + charset);
return new OutputStreamWriter(getStdOutStream(), charset);
}
LOG.finest(() -> "Creating exporter for file " + file + " using charset " + charset);
return createFileWriter(file, charset);
}

// Using System.out by intention
@SuppressWarnings("squid:S106")
private static PrintStream getStdOutStream()
{
return System.out;
}

private static Writer createFileWriter(final Path file, final Charset charset)
{
try
{
return Files.newBufferedWriter(file, charset);
}
catch (final IOException e)
{
throw new ExporterException("Error creating writer for file " + file, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.itsallcode.openfasttrace.api.core.serviceloader.Initializable;

/**
* Context for {@link ExporterFactory}. Currently only used to satisfy
* {@link Initializable} interface.
* Context for {@link ExporterFactory} implementations. Currently only used to
* satisfy {@link Initializable} interface.
*/
// Class is empty by intention
@SuppressWarnings("squid:S2094")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,25 @@
package org.itsallcode.openfasttrace.api.exporter;
import java.io.*;

import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Logger;
import java.util.stream.Stream;

import org.itsallcode.openfasttrace.api.core.Newline;
import org.itsallcode.openfasttrace.api.core.SpecificationItem;
import org.itsallcode.openfasttrace.api.core.serviceloader.Initializable;

/**
* Super class for factories producing {@link Exporter}s.
* Interface for factories producing {@link Exporter}s.
*/
public abstract class ExporterFactory implements Initializable<ExporterContext>
public interface ExporterFactory extends Initializable<ExporterContext>
{
private static final Logger LOG = Logger.getLogger(ExporterFactory.class.getName());

private final String supportedOutputFormat;

private ExporterContext context;

/**
* Creates a new {@link ExporterFactory}.
*
* @param supportedOutputFormat
* the format of the supported output format, e.g.
* {@code "html"}.
*/
protected ExporterFactory(final String supportedOutputFormat)
{
this.supportedOutputFormat = supportedOutputFormat;
}

@Override
public void init(final ExporterContext context)
{
this.context = context;
}

/**
* Get the {@link ExporterContext} set by the {@link #init(ExporterContext)}
* method.
* Get the {@link ExporterContext}.
*
* @return the {@link ExporterContext}.
*/
public ExporterContext getContext()
{
return this.context;
}
ExporterContext getContext();

/**
* Returns {@code true} if this {@link ExporterFactory} supports
Expand All @@ -58,10 +29,7 @@ public ExporterContext getContext()
* the output type to check.
* @return {@code true} if the given type is supported for exporting.
*/
public boolean supportsFormat(final String format)
{
return this.supportedOutputFormat.equals(format);
}
boolean supportsFormat(final String format);

/**
* Create an exporter that is able to export the given output format.
Expand All @@ -78,46 +46,8 @@ public boolean supportsFormat(final String format)
* the items to export
* @return an exporter instance
*/
public Exporter createExporter(final Path file, final String format, final Charset charset,
final Newline newline, final Stream<SpecificationItem> itemStream)
{
if (!supportsFormat(format))
{
throw new ExporterException("Output format '" + format + "' not supported for export");
}
final Writer writer = createWriter(file, charset);
return createExporter(writer, itemStream, newline);
}

private static Writer createWriter(final Path file, final Charset charset)
{
if (file == null)
{
LOG.finest(() -> "Creating exporter for STDOUT using charset " + charset);
return new OutputStreamWriter(getStdOutStream(), charset);
}
LOG.finest(() -> "Creating exporter for file " + file + " using charset " + charset);
return createFileWriter(file, charset);
}

// Using System.out by intention
@SuppressWarnings("squid:S106")
private static PrintStream getStdOutStream()
{
return System.out;
}

private static Writer createFileWriter(final Path file, final Charset charset)
{
try
{
return Files.newBufferedWriter(file, charset);
}
catch (final IOException e)
{
throw new ExporterException("Error creating writer for file " + file, e);
}
}
Exporter createExporter(final Path file, final String format, final Charset charset,
final Newline newline, final Stream<SpecificationItem> itemStream);

/**
* Create an exporter that is able to write to the given file.
Expand All @@ -130,6 +60,6 @@ private static Writer createFileWriter(final Path file, final Charset charset)
* newline format
* @return an {@link Exporter} instance
*/
protected abstract Exporter createExporter(final Writer writer,
Stream<SpecificationItem> linkedSpecItemStream, final Newline newline);
Exporter createExporter(final Writer writer, Stream<SpecificationItem> linkedSpecItemStream,
final Newline newline);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.itsallcode.openfasttrace.api.importer;

import java.util.Objects;

/**
* Base class for {@link ImporterFactory} implementations that share context handling.
*/
public abstract class AbstractImporterFactory implements ImporterFactory
{
private ImporterContext context;

/**
* Create a new {@link AbstractImporterFactory}.
*/
protected AbstractImporterFactory()
{
// empty by intention
}

@Override
public void init(final ImporterContext context)
{
this.context = context;
}

/**
* Get the {@link ImporterContext} set by the {@link #init(ImporterContext)}
* method.
*
* @return the {@link ImporterContext}.
*/
@Override
public ImporterContext getContext()
{
return Objects.requireNonNull(this.context, "Context was not initialized");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
import org.itsallcode.openfasttrace.api.importer.input.InputFile;

/**
* Base class for {@link ImporterFactory}s that can import files matching a list
* of regexp patterns.
* Base class for {@link ImporterFactory} implementations that can import files
* matching a list of regexp patterns.
*/
public abstract class RegexMatchingImporterFactory extends ImporterFactory
public abstract class AbstractRegexMatchingImporterFactory extends AbstractImporterFactory
{
private static final Logger LOG = Logger
.getLogger(RegexMatchingImporterFactory.class.getName());
.getLogger(AbstractRegexMatchingImporterFactory.class.getName());

private final Set<Pattern> supportedFilenamePatterns;

Expand All @@ -27,7 +27,7 @@ public abstract class RegexMatchingImporterFactory extends ImporterFactory
* @param supportedFilenamePatterns
* the filename patterns supported by the importer.
*/
protected RegexMatchingImporterFactory(final String... supportedFilenamePatterns)
protected AbstractRegexMatchingImporterFactory(final String... supportedFilenamePatterns)
{
this(asList(supportedFilenamePatterns));
}
Expand All @@ -38,7 +38,7 @@ protected RegexMatchingImporterFactory(final String... supportedFilenamePatterns
* @param supportedFilenamePatterns
* the filename patterns supported by the importer.
*/
protected RegexMatchingImporterFactory(final Collection<String> supportedFilenamePatterns)
protected AbstractRegexMatchingImporterFactory(final Collection<String> supportedFilenamePatterns)
{
this.supportedFilenamePatterns = supportedFilenamePatterns.stream() //
.map(Pattern::compile) //
Expand Down
Loading
Loading