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

Commit

Permalink
Simplify code a little
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jul 3, 2013
1 parent 4f1dfe3 commit d88072c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 53 deletions.
Expand Up @@ -42,12 +42,12 @@ public class SimpleSourceContents implements SourceContents
{
private final String qualifiedId;
private final Map<LocaleId, TargetContents> targets;
private final LocaleId locale;
private final @Nonnull LocaleId locale;
private List<String> contents;

public SimpleSourceContents(String qualifiedId,
Map<LocaleId, TargetContents> targets,
LocaleId locale,
@Nonnull LocaleId locale,
List<String> contents)
{
this.qualifiedId = qualifiedId;
Expand All @@ -57,7 +57,7 @@ public SimpleSourceContents(String qualifiedId,
}

public SimpleSourceContents(String qualifiedId,
LocaleId locale,
@Nonnull LocaleId locale,
@Nonnull String content0,
Map<LocaleId, TargetContents> targets)
{
Expand All @@ -66,7 +66,7 @@ public SimpleSourceContents(String qualifiedId,

public SimpleSourceContents(String qualifiedId,
Map<LocaleId, TargetContents> targets,
LocaleId locale,
@Nonnull LocaleId locale,
@Nonnull String... contents)
{
this(qualifiedId, targets, locale, ImmutableList.copyOf(contents));
Expand Down
11 changes: 7 additions & 4 deletions zanata-model/src/main/java/org/zanata/model/SourceContents.java
Expand Up @@ -21,6 +21,9 @@

package org.zanata.model;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.zanata.common.HasContents;
import org.zanata.common.LocaleId;

Expand All @@ -30,18 +33,18 @@
*/
public interface SourceContents extends HasContents
{
public LocaleId getLocale();
public String getQualifiedId();
public @Nonnull LocaleId getLocale();
public @Nonnull String getQualifiedId();
/**
* Gets the TargetContents for a single locale.
* Note that default implementation in HTextFlow requires a lot of database I/O
* @param localeId
* @return
*/
public TargetContents getTargetContents(LocaleId localeId);
public @Nullable TargetContents getTargetContents(@Nonnull LocaleId localeId);
/**
* Gets the TargetContents for all available locales.
* @return
*/
public Iterable<TargetContents> getAllTargetContents();
public @Nonnull Iterable<TargetContents> getAllTargetContents();
}
Expand Up @@ -38,9 +38,13 @@

import org.zanata.common.LocaleId;
import org.zanata.model.SourceContents;
import org.zanata.util.NullCloseable;
import org.zanata.util.OkapiUtil;
import org.zanata.util.VersionUtility;

import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;

/**
* Exports a collection of NamedDocument (ie a project iteration) to an
* OutputStream in TMX format.
Expand All @@ -53,65 +57,53 @@ public class TMXStreamingOutput implements StreamingOutput
private static final String creationToolVersion =
VersionUtility.getVersionInfo(TMXStreamingOutput.class).getVersionNo();
private final @Nonnull Iterator<? extends SourceContents> tuIter;
private final @Nullable LocaleId targetLocale;
private final ExportTUStrategy exportTUStrategy;

public TMXStreamingOutput(@Nonnull Iterator<? extends SourceContents> tuIter,
@Nullable LocaleId targetLocale)
{
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);
}

@Override
public void write(OutputStream output) throws IOException, WebApplicationException
{
try
{
@Cleanup
Writer writer = new PrintWriter(output);
@Cleanup
XMLWriter xmlWriter = new XMLWriter(writer);
@Cleanup
ZanataTMXWriter tmxWriter = new ZanataTMXWriter(xmlWriter);
String segType = "block"; // TODO other segmentation types
String dataType = "unknown"; // TODO track data type metadata throughout the system

net.sf.okapi.common.LocaleId allLocale = new net.sf.okapi.common.LocaleId("*all*", false);

tmxWriter.writeStartDocument(
allLocale,
toOkapiLocaleOrEmpty(targetLocale),
creationTool, creationToolVersion,
segType, null, dataType);

while (tuIter.hasNext())
{
SourceContents tu = tuIter.next();
exportTUStrategy.exportTranslationUnit(tmxWriter, tu, toOkapiLocaleOrEmpty(tu.getLocale()));
}
tmxWriter.writeEndDocument();
}
finally
@Cleanup
Closeable closeable = (Closeable) (tuIter instanceof Closeable ? tuIter : NullCloseable.INSTANCE);
@SuppressWarnings("null")
PeekingIterator<SourceContents> iter = Iterators.peekingIterator(tuIter);
// Fetch the first result, so that we can fail fast, before
// writing any output. This should enable RESTEasy to return an
// error instead of simply aborting the output stream.
if (iter.hasNext()) iter.peek();

@Cleanup
Writer writer = new PrintWriter(output);
@Cleanup
XMLWriter xmlWriter = new XMLWriter(writer);
@Cleanup
ZanataTMXWriter tmxWriter = new ZanataTMXWriter(xmlWriter);
String segType = "block"; // TODO other segmentation types
String dataType = "unknown"; // TODO track data type metadata throughout the system

net.sf.okapi.common.LocaleId allLocale = new net.sf.okapi.common.LocaleId("*all*", false);

tmxWriter.writeStartDocument(
allLocale,
// TMXWriter demands a non-null target locale, but if you write
// your TUs with writeTUFull(), it is never used.
net.sf.okapi.common.LocaleId.EMPTY,
creationTool, creationToolVersion,
segType, null, dataType);

while (iter.hasNext())
{
if (tuIter instanceof Closeable)
{
((Closeable) tuIter).close();
}
SourceContents tu = iter.next();
net.sf.okapi.common.LocaleId sourceLocale = OkapiUtil.toOkapiLocale(tu.getLocale());
exportTUStrategy.exportTranslationUnit(tmxWriter, tu, sourceLocale);
}
tmxWriter.writeEndDocument();
}

}
13 changes: 13 additions & 0 deletions zanata-war/src/main/java/org/zanata/util/NullCloseable.java
@@ -0,0 +1,13 @@
package org.zanata.util;

import java.io.Closeable;
import java.io.IOException;

public class NullCloseable implements Closeable
{
public static NullCloseable INSTANCE = new NullCloseable();
@Override
public void close() throws IOException
{
}
}

0 comments on commit d88072c

Please sign in to comment.