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

Commit

Permalink
rhbz965890 allow okapi filter adapter to use parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed May 23, 2013
1 parent e4448d4 commit 83355a3
Showing 1 changed file with 45 additions and 10 deletions.
Expand Up @@ -50,6 +50,8 @@
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.util.HashUtil;

import com.google.common.base.Optional;

/**
* An adapter that uses a provided {@link IFilter} implementation to parse documents.
*
Expand Down Expand Up @@ -119,6 +121,12 @@ public GenericOkapiFilterAdapter(IFilter filter, IdSource idSource, boolean requ

@Override
public Resource parseDocumentFile(URI documentContent, LocaleId sourceLocale) throws FileFormatAdapterException, IllegalArgumentException
{
return parseDocumentFile(documentContent, sourceLocale, Optional.<String>absent());
}

public Resource parseDocumentFile(URI documentContent, LocaleId sourceLocale,
Optional<String> params) throws FileFormatAdapterException, IllegalArgumentException
{
// null documentContent is handled by RawDocument constructor
if (sourceLocale == null)
Expand All @@ -133,7 +141,7 @@ public Resource parseDocumentFile(URI documentContent, LocaleId sourceLocale) th
List<TextFlow> resources = document.getTextFlows();

RawDocument rawDoc = new RawDocument(documentContent, "UTF-8", net.sf.okapi.common.LocaleId.fromString("en"));

updateParams(params);
try
{
filter.open(rawDoc);
Expand Down Expand Up @@ -183,21 +191,27 @@ private String stripPath(String name)

@Override
public TranslationsResource parseTranslationFile(URI fileUri, String localeId) throws FileFormatAdapterException, IllegalArgumentException
{
return parseTranslationFile(fileUri, localeId, Optional.<String>absent());
}

public TranslationsResource parseTranslationFile(URI fileUri, String localeId,
Optional<String> params) throws FileFormatAdapterException, IllegalArgumentException
{
if (localeId == null || localeId.isEmpty())
{
throw new IllegalArgumentException("locale id string cannot be null or empty");
}

RawDocument rawDoc = new RawDocument(fileUri, "UTF-8", net.sf.okapi.common.LocaleId.fromString("en"));
return parseTranslationFile(rawDoc);
return parseTranslationFile(rawDoc, params);
}

private TranslationsResource parseTranslationFile(RawDocument rawDoc)
private TranslationsResource parseTranslationFile(RawDocument rawDoc, Optional<String> params)
{
TranslationsResource transRes = new TranslationsResource();
List<TextFlowTarget> translations = transRes.getTextFlowTargets();

updateParams(params);
try
{
filter.open(rawDoc);
Expand Down Expand Up @@ -235,31 +249,40 @@ else if (event.getEventType() == EventType.TEXT_UNIT)

@Override
public void writeTranslatedFile(OutputStream output, URI originalFile, Map<String, TextFlowTarget> translations, String locale)
{
writeTranslatedFile(output, originalFile, translations, locale, Optional.<String>absent());
}

public void writeTranslatedFile(OutputStream output, URI originalFile,
Map<String, TextFlowTarget> translations, String locale, Optional<String> params)
throws FileFormatAdapterException, IllegalArgumentException
{
net.sf.okapi.common.LocaleId localeId = net.sf.okapi.common.LocaleId.fromString(locale);
IFilterWriter writer = filter.createFilterWriter();
writer.setOptions(localeId, "UTF-8");

if (requireFileOutput)
{
writeTranslatedFileWithFileOutput(output, originalFile, translations, localeId, writer);
writeTranslatedFileWithFileOutput(output, originalFile, translations, localeId, writer, params);
}
else
{
writer.setOutput(output);
generateTranslatedFile(originalFile, translations, localeId, writer);
generateTranslatedFile(originalFile, translations, localeId, writer, params);
}
}

private void writeTranslatedFileWithFileOutput(OutputStream output, URI originalFile, Map<String, TextFlowTarget> translations, net.sf.okapi.common.LocaleId localeId, IFilterWriter writer)
private void writeTranslatedFileWithFileOutput(OutputStream output, URI originalFile,
Map<String, TextFlowTarget> translations, net.sf.okapi.common.LocaleId localeId,
IFilterWriter writer, Optional<String> params)
{
File tempFile = null;

try
{
tempFile = File.createTempFile("filename", "extension");
writer.setOutput(tempFile.getCanonicalPath());
generateTranslatedFile(originalFile, translations, localeId, writer);
generateTranslatedFile(originalFile, translations, localeId, writer, params);

byte[] buffer = new byte[4096]; // To hold file contents
int bytesRead;
Expand Down Expand Up @@ -293,10 +316,11 @@ private void writeTranslatedFileWithFileOutput(OutputStream output, URI original

}

private void generateTranslatedFile(URI originalFile, Map<String, TextFlowTarget> translations, net.sf.okapi.common.LocaleId localeId, IFilterWriter writer)
private void generateTranslatedFile(URI originalFile, Map<String, TextFlowTarget> translations,
net.sf.okapi.common.LocaleId localeId, IFilterWriter writer, Optional<String> params)
{
RawDocument rawDoc = new RawDocument(originalFile, "UTF-8", net.sf.okapi.common.LocaleId.fromString("en"));

updateParams(params);
try
{
filter.open(rawDoc);
Expand Down Expand Up @@ -353,4 +377,15 @@ protected String getIdFor(TextUnit tu, String subDocName)
return tu.getId();
}
}

private void updateParams(Optional<String> params)
{
filter.getParameters().reset();
if (params.isPresent())
{
filter.getParameters().fromString(params.get());
}
log.info("filter parameters: " + filter.getParameters());
}

}

0 comments on commit 83355a3

Please sign in to comment.