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

Commit

Permalink
add .odf adapter and improve error handling behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Aug 29, 2012
1 parent 1ea3306 commit 40481c5
Show file tree
Hide file tree
Showing 9 changed files with 483 additions and 105 deletions.
6 changes: 6 additions & 0 deletions zanata-war/pom.xml
Expand Up @@ -932,6 +932,12 @@
<version>0.17</version>
</dependency>

<dependency>
<groupId>net.sf.okapi.filters</groupId>
<artifactId>okapi-filter-openoffice</artifactId>
<version>0.17</version>
</dependency>


<!-- Other -->

Expand Down
Expand Up @@ -21,6 +21,7 @@
package org.zanata.action;

import java.io.InputStream;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -142,17 +143,16 @@ public TranslationStatistics getTransUnitWordsForDocument(HDocument doc)
@Restrict("#{projectIterationFilesAction.fileUploadAllowed}")
public String uploadTranslationFile()
{
TranslationsResource transRes = null;
try
{
// process the file
transRes = this.translationFileServiceImpl.parseTranslationFile(this.translationFileUpload.getFileContents(),
this.translationFileUpload.getFileName());
TranslationsResource transRes = translationFileServiceImpl.parseTranslationFile(translationFileUpload.getFileContents(),
translationFileUpload.getFileName(), localeId);

// translate it
List<String> warnings =
this.translationServiceImpl.translateAllInDoc(this.projectSlug, this.iterationSlug, this.translationFileUpload.getDocId(),
new LocaleId(this.localeId), transRes, new StringSet(ExtensionType.GetText.toString()),
this.translationServiceImpl.translateAllInDoc(projectSlug, iterationSlug, translationFileUpload.getDocId(),
new LocaleId(localeId), transRes, new StringSet(ExtensionType.GetText.toString()),
this.translationFileUpload.getMergeTranslations() ? MergeType.AUTO : MergeType.IMPORT);

StringBuilder facesInfoMssg = new StringBuilder("File {0} uploaded.");
Expand Down Expand Up @@ -180,53 +180,94 @@ public String uploadTranslationFile()
@Restrict("#{projectIterationFilesAction.documentUploadAllowed}")
public String uploadDocumentFile()
{
InputStream fileContents = this.documentFileUpload.getFileContents();

if (translationFileServiceImpl.hasAdapterFor(documentFileUpload.getFileName()))
if (this.documentFileUpload.getFileName().endsWith(".pot"))
{
uploadPotFile();
}
else if (translationFileServiceImpl.hasAdapterFor(documentFileUpload.getFileName()))
{
uploadAdapterFile();
}
else
{
translationFileServiceImpl.persistDocument(fileContents, projectSlug, iterationSlug, documentFileUpload.getDocumentPath(), documentFileUpload.getFileName());
fileContents = translationFileServiceImpl.streamDocument(projectSlug, iterationSlug, documentFileUpload.getDocumentPath(), documentFileUpload.getFileName());
FacesMessages.instance().add(Severity.ERROR, "Unrecognized file extension for {0}.", documentFileUpload.getFileName());
}

// NB This needs to be done as for some reason seam is losing the parameters when redirecting
// This is effectively the same as returning void
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
}

private void showUploadSuccessMessage()
{
// TODO localized string by UI language
FacesMessages.instance().add(Severity.INFO, "Document file {0} uploaded.", documentFileUpload.getFileName());
}

private void uploadPotFile()
{
try
{
Resource doc = this.translationFileServiceImpl.parseDocumentFile(fileContents,
this.documentFileUpload.getDocumentPath(), this.documentFileUpload.getFileName());

doc.setLang( new LocaleId(this.documentFileUpload.getSourceLang()) );
Resource doc = translationFileServiceImpl.parseDocumentFile(documentFileUpload.getFileContents(),
documentFileUpload.getDocumentPath(), documentFileUpload.getFileName());

// Temporary solution to hard-coded GetText extensions.
Set<String> extensions;
if (this.documentFileUpload.getFileName().endsWith(".pot"))
{
extensions = new StringSet(ExtensionType.GetText.toString());
}
else
{
extensions = Collections.<String>emptySet();
}
doc.setLang( new LocaleId(documentFileUpload.getSourceLang()) );

// TODO Copy Trans values
this.documentServiceImpl.saveDocument(this.projectSlug, this.iterationSlug,
doc, extensions,
false);
documentServiceImpl.saveDocument(projectSlug, iterationSlug,
doc, new StringSet(ExtensionType.GetText.toString()), false);

FacesMessages.instance().add(Severity.INFO, "Document file {0} uploaded.", this.documentFileUpload.getFileName());
showUploadSuccessMessage();
}
catch (ZanataServiceException zex)
{
FacesMessages.instance().add(Severity.ERROR, zex.getMessage(), this.documentFileUpload.getFileName());
FacesMessages.instance().add(Severity.ERROR, zex.getMessage(), documentFileUpload.getFileName());
}
catch (InvalidStateException isex)
{
FacesMessages.instance().add(Severity.ERROR, "Invalid arguments");
}
}

// TODO if Resource (document) can't be saved properly, remove the persisted file.
// TODO add logging for disk writing errors
private void uploadAdapterFile()
{
String documentPath = documentFileUpload.getDocumentPath();
String fileName = documentFileUpload.getFileName();

// NB This needs to be done as for some reason seam is losing the parameters when redirecting
// This is efectively the same as returning void
return FacesContext.getCurrentInstance().getViewRoot().getViewId();
try
{
// FIXME uploading a corrupt version of a file will overwrite a non-corrupt file, leaving
// it in a bad state. Change this so that old file is still present after failed upload.
translationFileServiceImpl.persistDocument(documentFileUpload.getFileContents(), projectSlug, iterationSlug, documentPath, fileName);
}
catch (ZanataServiceException e) {
FacesMessages.instance().add(Severity.ERROR, "Error saving uploaded document {0} to server.", documentFileUpload.getFileName());
return;
}

try
{
URI uri = translationFileServiceImpl.getDocumentURI(projectSlug, iterationSlug, documentPath, fileName);
Resource doc = translationFileServiceImpl.parseDocumentFile(uri, documentPath, fileName);

doc.setLang( new LocaleId(documentFileUpload.getSourceLang()) );
Set<String> extensions = Collections.<String>emptySet();

// TODO Copy Trans values
documentServiceImpl.saveDocument(projectSlug, iterationSlug, doc, extensions, false);

showUploadSuccessMessage();
}
catch (SecurityException e)
{
FacesMessages.instance().add(Severity.ERROR, "Error reading uploaded document {0} on server.", documentFileUpload.getFileName());
// TODO try to remove persisted document?
}
catch (ZanataServiceException e) {
FacesMessages.instance().add(Severity.ERROR, "Invalid document format for {0}.", documentFileUpload.getFileName());
// TODO remove persisted document
}
}

public List<HLocale> getAvailableSourceLocales()
Expand Down
19 changes: 13 additions & 6 deletions zanata-war/src/main/java/org/zanata/adapter/FileFormatAdapter.java
Expand Up @@ -20,12 +20,13 @@
*/
package org.zanata.adapter;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;

import org.zanata.common.LocaleId;
import org.zanata.exception.FileFormatAdapterException;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
Expand All @@ -39,23 +40,28 @@
public interface FileFormatAdapter
{


/**
* Extract source strings from the given document content.
*
* @param documentContent
* @param documentUri
* @param sourceLocale
* @return representation of the strings in the document
* @throws IllegalArgumentException if documentUri or sourceLocale is null
* @throws FileFormatAdapterException if the document cannot be parsed
*/
// TODO may want to use a string locale id so it can be used both for Zanata and Okapi locale classes
Resource parseDocumentFile(InputStream documentContent, LocaleId sourceLocale);
Resource parseDocumentFile(URI documentUri, LocaleId sourceLocale) throws FileFormatAdapterException, IllegalArgumentException;

/**
* Extract translation strings from the given translation document.
*
* @param translatedDocumentContent translated document to parse
* @return representation of the translations in the document
* @throws FileFormatAdapterException if the document cannot be parsed
* @throws IllegalArgumentException if translatedDocumentContent or localeId is null
*/
TranslationsResource parseTranslationFile(InputStream translatedDocumentContent);
TranslationsResource parseTranslationFile(InputStream translatedDocumentContent, String localeId) throws FileFormatAdapterException, IllegalArgumentException;

/**
* Write translated file to the given output, using the given list of translations.
Expand All @@ -64,8 +70,9 @@ public interface FileFormatAdapter
* @param original source document
* @param translations to use in generating translated file
* @param locale to use for translated document
* @throws IOException
* @throws FileFormatAdapterException if there is any problem parsing the original file or writing the translated file
* @throws IllegalArgumentException if any parameters are null
*/
void writeTranslatedFile(OutputStream output, InputStream original, Map<String, TextFlowTarget> translations, String locale) throws IOException;
void writeTranslatedFile(OutputStream output, URI originalFile, Map<String, TextFlowTarget> translations, String locale) throws FileFormatAdapterException, IllegalArgumentException;

}

0 comments on commit 40481c5

Please sign in to comment.