Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
use temp file for all translation file uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Aug 30, 2012
1 parent b1f4f1f commit aa0b781
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 65 deletions.
Expand Up @@ -20,7 +20,6 @@
*/
package org.zanata.adapter;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
Expand All @@ -39,8 +38,6 @@
*/
public interface FileFormatAdapter
{


/**
* Extract source strings from the given document content.
*
Expand All @@ -61,7 +58,7 @@ public interface FileFormatAdapter
* @throws FileFormatAdapterException if the document cannot be parsed
* @throws IllegalArgumentException if translatedDocumentContent or localeId is null
*/
TranslationsResource parseTranslationFile(InputStream translatedDocumentContent, String localeId) throws FileFormatAdapterException, IllegalArgumentException;
TranslationsResource parseTranslationFile(URI fileUri, String localeId) throws FileFormatAdapterException, IllegalArgumentException;

/**
* Write translated file to the given output, using the given list of translations.
Expand Down
Expand Up @@ -22,9 +22,7 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.List;
Expand Down Expand Up @@ -60,7 +58,6 @@
*/
public class GenericOkapiFilterAdapter implements FileFormatAdapter
{

private Logger log;

/**
Expand All @@ -75,7 +72,6 @@ public enum IdSource {

private final IFilter filter;
private final IdSource idSource;
private boolean requireUriRawDoc;
private boolean requireFileOutput;

/**
Expand All @@ -96,7 +92,7 @@ public GenericOkapiFilterAdapter(IFilter filter)
*/
public GenericOkapiFilterAdapter(IFilter filter, IdSource idSource)
{
this(filter, idSource, false, false);
this(filter, idSource, false);
}

/**
Expand All @@ -105,11 +101,10 @@ public GenericOkapiFilterAdapter(IFilter filter, IdSource idSource)
* @param filter {@link IFilter} used to parse the document
* @param idSource determines how ids are assigned to TextFlows
*/
public GenericOkapiFilterAdapter(IFilter filter, IdSource idSource, boolean requireUriRawDoc, boolean requireFileOutput)
public GenericOkapiFilterAdapter(IFilter filter, IdSource idSource, boolean requireFileOutput)
{
this.filter = filter;
this.idSource = idSource;
this.requireUriRawDoc = requireUriRawDoc;
this.requireFileOutput = requireFileOutput;

log = LoggerFactory.getLogger(GenericOkapiFilterAdapter.class);
Expand Down Expand Up @@ -180,26 +175,15 @@ private String stripPath(String name)
}

@Override
public TranslationsResource parseTranslationFile(InputStream fileContents, String localeId) throws FileFormatAdapterException, IllegalArgumentException
public TranslationsResource parseTranslationFile(URI fileUri, String localeId) throws FileFormatAdapterException, IllegalArgumentException
{
if (fileContents == null)
{
throw new IllegalArgumentException("File contents cannot be null");
}
if (localeId == null || localeId.isEmpty())
{
throw new IllegalArgumentException("locale id string cannot be null or empty");
}

if (requireUriRawDoc)
{
return parseTranslationFileWithUrlRawDoc(fileContents);
}
else
{
RawDocument rawDoc = new RawDocument(fileContents, "UTF-8", net.sf.okapi.common.LocaleId.fromString(localeId));
return parseTranslationFile(rawDoc);
}
RawDocument rawDoc = new RawDocument(fileUri, "UTF-8", net.sf.okapi.common.LocaleId.fromString("en"));
return parseTranslationFile(rawDoc);
}

private TranslationsResource parseTranslationFile(RawDocument rawDoc)
Expand Down Expand Up @@ -242,44 +226,6 @@ else if (event.getEventType() == EventType.TEXT_UNIT)
return transRes;
}

private TranslationsResource parseTranslationFileWithUrlRawDoc(InputStream fileContents)
{
File tempFile = null;
try
{
tempFile = File.createTempFile("filename", "extension");

byte[] buffer = new byte[4096]; // To hold file contents
int bytesRead;
FileOutputStream output;

output = new FileOutputStream(tempFile);
while ((bytesRead = fileContents.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);
}
output.close();
}
catch (IOException e)
{
throw new FileFormatAdapterException("Error while writing translation file to temporary location", e);
}

RawDocument rawDoc = new RawDocument(tempFile.toURI(), "UTF-8", net.sf.okapi.common.LocaleId.fromString("en"));
TranslationsResource transRes = parseTranslationFile(rawDoc);

if (tempFile != null)
{
if (!tempFile.delete())
{
log.warn("unable to remove temporary file {}, marked for delete on exit", tempFile.getAbsolutePath());
tempFile.deleteOnExit();
}
}
return transRes;
}


@Override
public void writeTranslatedFile(OutputStream output, URI originalFile, Map<String, TextFlowTarget> translations, String locale)
{
Expand Down
Expand Up @@ -32,7 +32,7 @@ public class OpenOfficeAdapter extends GenericOkapiFilterAdapter
{
public OpenOfficeAdapter()
{
super(prepareFilter(), IdSource.subDocNameAndTextUnitId, true, true);
super(prepareFilter(), IdSource.subDocNameAndTextUnitId, true);
}

private static OpenOfficeFilter prepareFilter()
Expand Down
Expand Up @@ -94,14 +94,18 @@ public TranslationsResource parseTranslationFile(InputStream fileContents, Strin
}
else if (hasAdapterFor(fileName))
{
File tempFile = persistToTempFile(fileContents);
TranslationsResource transRes;
try
{
return getAdapterFor(fileName).parseTranslationFile(fileContents, localeId);
transRes = getAdapterFor(fileName).parseTranslationFile(tempFile.toURI(), localeId);
}
catch (FileFormatAdapterException e)
{
throw new ZanataServiceException("Error parsing translation file: " + fileName, e);
}
removeTempFile(tempFile);
return transRes;
}
else
{
Expand Down

0 comments on commit aa0b781

Please sign in to comment.