Skip to content
Snippets Groups Projects
Commit 68a92ee3 authored by Thorsten Vitt's avatar Thorsten Vitt
Browse files

Made CorpusBasedExporter's buffer configurable

parent 67769c21
No related branches found
No related tags found
No related merge requests found
......@@ -8,8 +8,12 @@
import info.textgrid.namespaces.middleware.tgcrud.services.tgcrudservice.ProtocolNotImplementedFault;
import info.textgrid.services.aggregator.teicorpus.TEICorpusSerializer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
......@@ -22,19 +26,61 @@
import com.google.common.io.ByteStreams;
import com.google.common.io.FileBackedOutputStream;
import com.google.common.io.Files;
import com.google.common.io.InputSupplier;
public abstract class CorpusBasedExporter extends AbstractExporter {
private static final Logger logger = Logger.getLogger(CorpusBasedExporter.class.getCanonicalName());
protected boolean readStylesheetPI = false;
private InputSupplier<InputStream> sourceBuffer = null;
private InputSupplier<? extends InputStream> sourceBuffer = null;
private IBufferFactory bufferFactory = new FBBAOSBufferFactory();
public CorpusBasedExporter(final ITextGridRep repository,
final Request request, final String uriList) {
super(repository, request, uriList);
}
public static interface IBufferFactory {
public OutputStream getBufferSink();
public InputSupplier<? extends InputStream> getBufferContents();
}
public static final class FBBAOSBufferFactory implements IBufferFactory {
private FileBackedOutputStream sink;
@Override
public OutputStream getBufferSink() {
this.sink = new FileBackedOutputStream(1024 * 1024, true);
return sink;
}
@Override
public InputSupplier<InputStream> getBufferContents() {
return sink.getSupplier();
}
}
public static final class FileBufferFactory implements IBufferFactory {
private final File file;
private FileOutputStream sink;
public FileBufferFactory(final File file) throws FileNotFoundException {
this.file = file;
this.sink = new FileOutputStream(file);
}
@Override
public InputSupplier<? extends InputStream> getBufferContents() {
return Files.newInputStreamSupplier(file);
}
@Override
public OutputStream getBufferSink() {
return sink;
}
}
/**
* Loads a source for the given document.
*
......@@ -83,17 +129,15 @@ protected Source loadSource(final boolean bufferRequired)
|| sourceType == SourceType.BASKET) {
final TEICorpusSerializer corpusSerializer =
new TEICorpusSerializer(getRootObjects(), false, getSid().orNull());
final FileBackedOutputStream corpusBuffer = new FileBackedOutputStream(
1024 * 1024, true);
final OutputStream corpusBuffer = getBufferFactory().getBufferSink();
corpusSerializer.write(corpusBuffer);
corpusBuffer.close();
sourceBuffer = corpusBuffer.getSupplier();
sourceBuffer = getBufferFactory().getBufferContents();
source = new StreamSource(sourceBuffer.getInput());
} else if (sourceType == SourceType.XML && bufferRequired) {
final FileBackedOutputStream xmlBuffer = new FileBackedOutputStream(
1024 * 1024, true);
final OutputStream xmlBuffer = getBufferFactory().getBufferSink();
ByteStreams.copy(getContentSimple(), xmlBuffer);
sourceBuffer = xmlBuffer.getSupplier();
sourceBuffer = getBufferFactory().getBufferContents();
source = new StreamSource(sourceBuffer.getInput());
} else {
source = new StreamSource(getContentSimple().getInput(),
......@@ -106,4 +150,12 @@ protected Source loadSource(final boolean bufferRequired)
return source;
}
private IBufferFactory getBufferFactory() {
return bufferFactory;
}
protected void setBufferFactory(final IBufferFactory bufferFactory) {
this.bufferFactory = bufferFactory;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment