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

Commit

Permalink
Implement gettext adapter for file project type
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 25, 2015
1 parent 7fbbc0c commit a4df408
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
Expand Up @@ -169,9 +169,12 @@ public void writeTranslatedFile(OutputStream output, Resource resource,
try {
tempFile = File.createTempFile("filename", "extension");
if(charset.equals(ISO_8859_1)) {
PropWriter.write(resource, translationsResource, tempFile, createSkeletons);
PropWriter.writeTranslationsLatinOne(resource,
translationsResource, tempFile, createSkeletons);
} else if (charset.equals(UTF_8)) {
PropWriter.writeUTF8(resource, translationsResource, tempFile, createSkeletons);
PropWriter.writeTranslationsUTF8(resource,
translationsResource,
tempFile, createSkeletons);
}

byte[] buffer = new byte[4096]; // To hold file contents
Expand Down
4 changes: 2 additions & 2 deletions zanata-war/src/main/java/org/zanata/adapter/XliffAdapter.java
Expand Up @@ -121,8 +121,8 @@ public void writeTranslatedFile(OutputStream output, URI originalFile,
File tempFile = null;
try {
tempFile = File.createTempFile("filename", "extension");
XliffWriter.write(resource, translationsResource, tempFile, locale,
createSkeletons);
XliffWriter.writeFile(tempFile, resource, locale,
translationsResource, createSkeletons);

byte[] buffer = new byte[4096]; // To hold file contents
int bytesRead;
Expand Down
Expand Up @@ -157,7 +157,7 @@ public Response tryValidatedUploadSourceFile(GlobalDocumentId id,
.persistTempFileFromUpload(uploadForm));
}
processAdapterFile(tempFile.get(), id, uploadForm);
} else if (DocumentType.getByName(uploadForm.getFileType()) == DocumentType.GETTEXT_PORTABLE_OBJECT_TEMPLATE) {
} else if (DocumentType.getByName(uploadForm.getFileType()) == DocumentType.GETTEXT_PORTABLE_OBJECT) {
InputStream potStream = getInputStream(tempFile, uploadForm);
parsePotFile(potStream, id, uploadForm);
} else {
Expand Down Expand Up @@ -223,7 +223,7 @@ private boolean isSourceDocumentType(DocumentType type) {
}

private boolean isPotType(DocumentType type) {
return type == DocumentType.GETTEXT_PORTABLE_OBJECT_TEMPLATE;
return type == DocumentType.GETTEXT_PORTABLE_OBJECT;
}

private boolean isAdapterType(DocumentType type) {
Expand Down
18 changes: 12 additions & 6 deletions zanata-war/src/main/java/org/zanata/rest/service/FileService.java
Expand Up @@ -42,6 +42,7 @@
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;

import org.apache.commons.io.FilenameUtils;
import org.jboss.resteasy.util.GenericType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
Expand All @@ -57,6 +58,7 @@
import org.zanata.file.SourceDocumentUpload;
import org.zanata.file.TranslationDocumentUpload;
import org.zanata.model.HDocument;
import org.zanata.model.HRawDocument;
import org.zanata.rest.DocumentFileUploadForm;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.resource.Resource;
Expand Down Expand Up @@ -296,24 +298,28 @@ public Response downloadTranslationFile(String projectSlug,
// the generated file should be scanned instead
virusScanner.scan(tempFile, name);
URI uri = tempFile.toURI();
HRawDocument hRawDocument = hDocument.getRawDocument();
FileFormatAdapter adapter =
translationFileServiceImpl.getAdapterFor(hDocument
.getRawDocument().getType());
String rawParamString =
hDocument.getRawDocument().getAdapterParameters();
translationFileServiceImpl.getAdapterFor(hRawDocument.getType());
String rawParamString = hRawDocument.getAdapterParameters();
Optional<String> params =
Optional.<String> fromNullable(Strings
.emptyToNull(rawParamString));
StreamingOutput output =
new FormatAdapterStreamingOutput(uri, res, transRes,
locale, adapter, params);

String srcExt = FilenameUtils.getExtension(document.getName());
String transExt = hRawDocument.getType().getExtensions().get(srcExt);
String transFileName = FilenameUtils.removeExtension(document
.getName()) + "." + transExt;
response =
Response.ok()
.header("Content-Disposition",
"attachment; filename=\""
+ document.getName() + "\"")
+ transFileName + "\"")
.entity(output).build();
// TODO damason: remove more immediately, but make sure response has
// TODO damason: remove more immediately, but make sure response has
// finished with the file
// Note: may not be necessary when file storage is on disk.
tempFile.deleteOnExit();
Expand Down
Expand Up @@ -67,7 +67,6 @@

import static org.jboss.seam.ScopeType.STATELESS;
import static org.zanata.common.DocumentType.GETTEXT_PORTABLE_OBJECT;
import static org.zanata.common.DocumentType.GETTEXT_PORTABLE_OBJECT_TEMPLATE;
import static org.zanata.common.DocumentType.HTML;
import static org.zanata.common.DocumentType.IDML;
import static org.zanata.common.DocumentType.OPEN_DOCUMENT_GRAPHICS;
Expand Down Expand Up @@ -109,7 +108,7 @@ public class TranslationFileServiceImpl implements TranslationFileService {
DOCTYPEMAP.put(PROPERTIES, PropertiesAdapter.class);
DOCTYPEMAP.put(PROPERTIES_UTF8, PropertiesUTF8Adapter.class);
DOCTYPEMAP.put(XLIFF, XliffAdapter.class);
DOCTYPEMAP.put(GETTEXT_PORTABLE_OBJECT_TEMPLATE, GettextAdapter.class);
DOCTYPEMAP.put(GETTEXT_PORTABLE_OBJECT, GettextAdapter.class);
}

private static Set<String> SUPPORTED_EXTENSIONS =
Expand Down Expand Up @@ -416,8 +415,7 @@ public boolean isPoDocument(String projectSlug, String iterationSlug,

// additional check in case we do start storing raw documents for po
DocumentType docType = doc.getRawDocument().getType();
return docType == GETTEXT_PORTABLE_OBJECT
|| docType == GETTEXT_PORTABLE_OBJECT_TEMPLATE;
return docType == GETTEXT_PORTABLE_OBJECT;
}
return false;
}
Expand Down

0 comments on commit a4df408

Please sign in to comment.