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

Commit

Permalink
Partially implement getTranslationMemory (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jul 22, 2013
1 parent f018dcd commit 851f5ee
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 41 deletions.
13 changes: 13 additions & 0 deletions zanata-model/src/main/java/org/zanata/util/OkapiUtil.java
Expand Up @@ -22,6 +22,7 @@

import java.io.ByteArrayInputStream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
Expand Down Expand Up @@ -49,6 +50,18 @@ private OkapiUtil()
return LocaleId.fromBCP47(zanataLocale.getId());
}

@SuppressWarnings("null")
public static @Nonnull LocaleId toOkapiLocaleOrEmpty(@Nullable org.zanata.common.LocaleId locale)
{
if (locale == null)
{
// TMXWriter demands a non-null target locale, but if you write
// your TUs with writeTUFull(), it is never actually used.
return LocaleId.EMPTY;
}
return toOkapiLocale(locale);
}

/**
* Count words using Okapi's WordCounter, which tries to implement the LISA
* standard GMX-V:
Expand Down
Expand Up @@ -21,6 +21,9 @@

package org.zanata.rest.service;

import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;

import lombok.extern.slf4j.Slf4j;
import net.sf.okapi.common.resource.ITextUnit;
import net.sf.okapi.common.resource.TextFragment;
Expand All @@ -37,15 +40,16 @@
*
*/
@Slf4j
public class ExportTUStrategy
@ParametersAreNonnullByDefault
public class ExportTUStrategy<TU extends SourceContents> //implements ExportTUStrategy<SourceContents>
{
private final LocaleId localeId;

/**
* Exports one or all locales. If localeId is null, export all locales.
* @param localeId
*/
public ExportTUStrategy(LocaleId localeId)
public ExportTUStrategy(@Nullable LocaleId localeId)
{
this.localeId = localeId;
}
Expand All @@ -56,8 +60,9 @@ public ExportTUStrategy(LocaleId localeId)
* @param tuidPrefix String to be prepended to all resIds when generating tuids
* @param tf the SourceContents (TextFlow) whose contents and translations are to be exported
*/
public void exportTranslationUnit(ZanataTMXWriter tmxWriter, SourceContents tf, net.sf.okapi.common.LocaleId sourceLocaleId)
public void exportTranslationUnit(ZanataTMXWriter tmxWriter, SourceContents tf)
{
net.sf.okapi.common.LocaleId sourceLocaleId = OkapiUtil.toOkapiLocaleOrEmpty(tf.getLocale());
String tuid = tf.getQualifiedId();
// Perhaps we could encode plurals using TMX attributes?
String srcContent = tf.getContents().get(0);
Expand Down
Expand Up @@ -47,33 +47,23 @@
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class TMXStreamingOutput implements StreamingOutput
public class TMXStreamingOutput<TU extends SourceContents> implements StreamingOutput
{
private static final String creationTool = "Zanata " + TMXStreamingOutput.class.getSimpleName();
private static final String creationToolVersion =
VersionUtility.getVersionInfo(TMXStreamingOutput.class).getVersionNo();
private final @Nonnull Iterator<? extends SourceContents> tuIter;
private final @Nonnull Iterator<TU> tuIter;
private final @Nullable LocaleId targetLocale;
private final ExportTUStrategy exportTUStrategy;
private final ExportTUStrategy<TU> exportTUStrategy;

public TMXStreamingOutput(@Nonnull Iterator<? extends SourceContents> tuIter,
@Nullable LocaleId targetLocale)
public TMXStreamingOutput(
@Nonnull Iterator<TU> tuIter,
@Nullable LocaleId targetLocale,
@Nonnull ExportTUStrategy<TU> exportTUStrategy)
{
this.tuIter = tuIter;
this.targetLocale = targetLocale;
this.exportTUStrategy = new ExportTUStrategy(targetLocale);
}

@SuppressWarnings("null")
private @Nonnull net.sf.okapi.common.LocaleId toOkapiLocaleOrEmpty(@Nullable LocaleId locale)
{
if (locale == null)
{
// TMXWriter demands a non-null target locale, but if you write
// your TUs with writeTUFull(), it is never actually used.
return net.sf.okapi.common.LocaleId.EMPTY;
}
return OkapiUtil.toOkapiLocale(locale);
this.exportTUStrategy = exportTUStrategy;
}

@Override
Expand All @@ -94,14 +84,16 @@ public void write(OutputStream output) throws IOException, WebApplicationExcepti

tmxWriter.writeStartDocument(
allLocale,
toOkapiLocaleOrEmpty(targetLocale),
// TMXWriter demands a non-null target locale, but if you write
// your TUs with writeTUFull(), it is never actually used.
OkapiUtil.toOkapiLocaleOrEmpty(targetLocale),
creationTool, creationToolVersion,
segType, null, dataType);

while (tuIter.hasNext())
{
SourceContents tu = tuIter.next();
exportTUStrategy.exportTranslationUnit(tmxWriter, tu, toOkapiLocaleOrEmpty(tu.getLocale()));
TU tu = tuIter.next();
exportTUStrategy.exportTranslationUnit(tmxWriter, tu);
}
tmxWriter.writeEndDocument();
}
Expand Down
Expand Up @@ -26,20 +26,16 @@
import javax.annotation.Nullable;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;

import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.annotations.Suspend;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
import org.jboss.resteasy.spi.AsynchronousResponse;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.TransactionPropagationType;
Expand All @@ -48,9 +44,12 @@
import org.zanata.common.LocaleId;
import org.zanata.dao.TextFlowStreamDAO;
import org.zanata.dao.TransMemoryDAO;
import org.zanata.exception.ZanataServiceException;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.model.SourceContents;
import org.zanata.model.tm.TMTranslationUnit;
import org.zanata.model.tm.TransMemory;
import org.zanata.service.LocaleService;
import org.zanata.tmx.TMXParser;

Expand Down Expand Up @@ -79,21 +78,22 @@ public class TranslationMemoryResourceService implements TranslationMemoryResour
@Restrict("#{s:hasRole('admin')}")
public Response getAllTranslationMemory(@Nullable LocaleId locale)
{
TranslationMemoryResourceService.log.debug("exporting TM for all projects, locale {}", locale);
log.debug("exporting TMX for all projects, locale {}", locale);
Iterator<? extends SourceContents> tuIter;
if (locale != null)
{
localeServiceImpl.validateSourceLocale(locale);
// TODO findTextFlowsByLocale
}
tuIter = textFlowStreamDAO.findTextFlows();
return buildTMX(tuIter, null, null, locale);
String filename = makeTMXFilename(null, null, locale);
return buildTMX(tuIter, locale, filename);
}

@Override
public Response getProjectTranslationMemory(@Nonnull String projectSlug, @Nullable LocaleId locale)
{
TranslationMemoryResourceService.log.debug("exporting TM for project {}, locale {}", projectSlug, locale);
log.debug("exporting TMX for project {}, locale {}", projectSlug, locale);
Iterator<? extends SourceContents> tuIter;
HProject hProject = restSlugValidator.retrieveAndCheckProject(projectSlug, false);
if (locale != null)
Expand All @@ -102,14 +102,15 @@ public Response getProjectTranslationMemory(@Nonnull String projectSlug, @Nullab
// TODO findTextFlowsByProjectAndLocale
}
tuIter = textFlowStreamDAO.findTextFlowsByProject(hProject);
return buildTMX(tuIter, projectSlug, null, locale);
String filename = makeTMXFilename(projectSlug, null, locale);
return buildTMX(tuIter, locale, filename);
}

@Override
public Response getProjectIterationTranslationMemory(
@Nonnull String projectSlug, @Nonnull String iterationSlug, @Nullable LocaleId locale)
{
TranslationMemoryResourceService.log.debug("exporting TM for project {}, iteration {}, locale {}", projectSlug, iterationSlug, locale);
log.debug("exporting TMX for project {}, iteration {}, locale {}", projectSlug, iterationSlug, locale);
Iterator<? extends SourceContents> tuIter;
HProjectIteration hProjectIteration = restSlugValidator.retrieveAndCheckIteration(projectSlug, iterationSlug, false);
if (locale != null)
Expand All @@ -118,7 +119,25 @@ public Response getProjectIterationTranslationMemory(
// TODO findTextFlowsByProjectIterationAndLocale
}
tuIter = textFlowStreamDAO.findTextFlowsByProjectIteration(hProjectIteration);
return buildTMX(tuIter, projectSlug, iterationSlug, locale);
String filename = makeTMXFilename(projectSlug, iterationSlug, locale);
return buildTMX(tuIter, locale, filename);
}

@Override
public Response getTranslationMemory(@Nonnull String slug, @Nullable String locale)
{
log.debug("exporting TMX for translation memory {}, locale {}", slug, locale);
Iterator<? extends TMTranslationUnit> tuIter;
TransMemory transMemory = transMemoryDAO.getBySlug(slug);
if (transMemory == null)
{
throw new ZanataServiceException("Translation memory " + slug + " was not found.", 404);
}
// TODO check locale?
// tuIter = transMemoryStreamDAO.findTransUnitsByTM(transMemory);
tuIter = null;
String filename = makeTMXFilename(slug, locale);
return buildTMX(tuIter, locale, filename);
}

@Override
Expand All @@ -142,22 +161,41 @@ public Response deleteTranslationUnits(String slug)
return Response.ok().build();
}

private Response buildTMX(@Nonnull Iterator<? extends SourceContents> tuIter, @Nullable String projectSlug, @Nullable String iterationSlug, @Nullable LocaleId locale)
private Response buildTMX(
@Nonnull Iterator<? extends SourceContents> tuIter,
@Nullable LocaleId locale, @Nonnull String filename)
{
StreamingOutput output = new TMXStreamingOutput(tuIter, locale, new ExportTUStrategy(locale));
return okResponse(filename, output);
}

private Response buildTMX(Iterator<? extends TMTranslationUnit> tuIter, String locale, String filename)
{
LocaleId localeId = locale == null ? null : new LocaleId(locale);
StreamingOutput output = new TMXStreamingOutput(tuIter, localeId, new ExportTUStrategy(localeId));
return okResponse(filename, output);
}

private Response okResponse(String filename, StreamingOutput output)
{
StreamingOutput output = new TMXStreamingOutput(tuIter, locale);
String filename = makeTMXFilename(projectSlug, iterationSlug, locale);
return Response.ok()
.header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
.type(PREFERRED_MEDIA_TYPE)
.entity(output).build();
}

private static String makeTMXFilename(@Nullable String projectSlug, @Nullable String iterationSlug, @Nullable LocaleId locale)
private static @Nonnull String makeTMXFilename(@Nullable String projectSlug, @Nullable String iterationSlug, @Nullable LocaleId locale)
{
String p = projectSlug != null ? projectSlug : "allProjects";
String i = iterationSlug != null ? iterationSlug : "allVersions";
String l = locale != null ? locale.getId() : "allLocales";
return "zanata-"+p+"-"+i+"-"+l+".tmx";
}

private static @Nonnull String makeTMXFilename(@Nullable String tmSlug, @Nullable String locale)
{
String l = locale != null ? locale : "allLocales";
return "zanata-"+tmSlug+"-"+l+".tmx";
}

}
Expand Up @@ -51,7 +51,7 @@ public class TMXStreamingOutputTest
public void exportAllLocales() throws Exception
{
LocaleId targetLocale = null;
StreamingOutput output = new TMXStreamingOutput(createTestData(), targetLocale);
StreamingOutput output = new TMXStreamingOutput(createTestData(), targetLocale, new ExportTUStrategy(targetLocale));

Document doc = writeToXmlWithValidation(output);

Expand All @@ -63,7 +63,7 @@ public void exportAllLocales() throws Exception
public void exportFrench() throws Exception
{
LocaleId targetLocale = LocaleId.FR;
StreamingOutput output = new TMXStreamingOutput(createTestData(), targetLocale);
StreamingOutput output = new TMXStreamingOutput(createTestData(), targetLocale, new ExportTUStrategy(targetLocale));

Document doc = writeToXmlWithValidation(output);

Expand All @@ -76,7 +76,7 @@ public void exportFrench() throws Exception
public void exportGerman() throws Exception
{
LocaleId targetLocale = LocaleId.DE;
StreamingOutput output = new TMXStreamingOutput(createTestData(), targetLocale);
StreamingOutput output = new TMXStreamingOutput(createTestData(), targetLocale, new ExportTUStrategy(targetLocale));

Document doc = writeToXmlWithValidation(output);

Expand Down

0 comments on commit 851f5ee

Please sign in to comment.