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

Commit

Permalink
Browse files Browse the repository at this point in the history
Make encoding of tabs optional; pass options to PullStrategy construc…
…tors
  • Loading branch information
seanf committed Dec 11, 2012
1 parent c69453f commit f462371
Show file tree
Hide file tree
Showing 21 changed files with 153 additions and 85 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -20,7 +20,7 @@

<properties>
<zanata.api.version>2.0.0</zanata.api.version>
<zanata.common.version>1.8.5</zanata.common.version>
<zanata.common.version>1.8.6-SNAPSHOT</zanata.common.version>
</properties>

<dependencyManagement>
Expand Down
@@ -0,0 +1,42 @@
package org.zanata.client.commands;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;

/**
* Boolean {@link OptionHandler}.
*
* @author Kohsuke Kawaguchi
*/
public class BooleanValueHandler extends OptionHandler<Boolean> {
private static final List<String> ACCEPTABLE_VALUES = Arrays.asList(new String[] { "true", "on", "yes", "1",
"false", "off", "no", "0" });

public BooleanValueHandler(CmdLineParser parser, OptionDef option, Setter<? super Boolean> setter) {
super(parser, option, setter);
}

@Override
public int parseArguments(Parameters params) throws CmdLineException {
String valueStr = params.getParameter(0).toLowerCase();
int index = ACCEPTABLE_VALUES.indexOf(valueStr);
if (index == -1) {
throw new CmdLineException(MessageFormat.format("\"{0}\" is not a legal boolean value", valueStr));
}
setter.addValue(index < ACCEPTABLE_VALUES.size() / 2);
return 1;
}

@Override
public String getDefaultMetaVariable() {
return "BOOL";
}
}
Expand Up @@ -32,7 +32,7 @@ public String getCommandName()
@Override
public String getCommandDescription()
{
return "Pulls translated text from Zanata.";
return "Pulls translated text from Zanata. DEPRECATED: use 'pull' with projectType 'podir'";
}

@Option(aliases = { "-d" }, name = "--dst", metaVar = "DIR", required = true, usage = "Base directory for publican files (with subdirectory \"pot\" and locale directories)")
Expand Down
Expand Up @@ -37,7 +37,7 @@ public String getCommandName()
@Override
public String getCommandDescription()
{
return "Publishes publican source text to Zanata so that it can be translated.";
return "Publishes publican source text to Zanata so that it can be translated. DEPRECATED: use 'push' with projectType 'podir'";
}

@Override
Expand Down
Expand Up @@ -33,28 +33,22 @@
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public abstract class AbstractGettextPullStrategy implements PullStrategy
public abstract class AbstractGettextPullStrategy extends AbstractPullStrategy
{
private PoWriter2 poWriter = new PoWriter2();
private final PoWriter2 poWriter;
private StringSet extensions = new StringSet("gettext;comment");
private PullOptions opts;

public PullOptions getOpts()
protected AbstractGettextPullStrategy(PullOptions opts)
{
return opts;
super(opts);
poWriter = new PoWriter2(opts.getEncodeTabs());
}

protected PoWriter2 getPoWriter()
{
return poWriter;
}

@Override
public void setPullOptions(PullOptions opts)
{
this.opts = opts;
}

@Override
public StringSet getExtensions()
{
Expand Down
@@ -0,0 +1,17 @@
package org.zanata.client.commands.pull;

public abstract class AbstractPullStrategy implements PullStrategy
{
private final PullOptions opts;

protected AbstractPullStrategy(PullOptions opts)
{
this.opts = opts;
}

public PullOptions getOpts()
{
return opts;
}

}
Expand Up @@ -34,6 +34,10 @@
*/
public class GettextDirStrategy extends AbstractGettextPullStrategy
{
public GettextDirStrategy(PullOptions opts)
{
super(opts);
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping locMapping, TranslationsResource targetDoc) throws IOException
Expand Down
Expand Up @@ -35,6 +35,11 @@
public class GettextPullStrategy extends AbstractGettextPullStrategy
{

public GettextPullStrategy(PullOptions opts)
{
super(opts);
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping locMapping, TranslationsResource targetDoc) throws IOException
{
Expand Down
Expand Up @@ -33,27 +33,13 @@
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class PropertiesStrategy implements PullStrategy
public class PropertiesStrategy extends AbstractPullStrategy
{
StringSet extensions = new StringSet("comment");
private PullOptions pullOptions;

public PropertiesStrategy()
protected PropertiesStrategy(PullOptions opts)
{
}

/**
* @return the opts
*/
protected PullOptions getPullOptions()
{
return pullOptions;
}

@Override
public void setPullOptions(PullOptions opts)
{
this.pullOptions = opts;
super(opts);
}

@Override
Expand All @@ -71,17 +57,17 @@ public boolean needsDocToWriteTrans()
@Override
public void writeSrcFile(Resource doc) throws IOException
{
PropWriter.write(doc, getPullOptions().getSrcDir());
PropWriter.write(doc, getOpts().getSrcDir());
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping localeMapping, TranslationsResource targetDoc) throws IOException
{
boolean createSkeletons = getPullOptions().getCreateSkeletons();
boolean createSkeletons = getOpts().getCreateSkeletons();
if (createSkeletons)
PropWriter.write(doc, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
PropWriter.write(doc, targetDoc, getOpts().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
else
PropWriter.write(null, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
PropWriter.write(null, targetDoc, getOpts().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
}

}
@@ -1,6 +1,8 @@
package org.zanata.client.commands.pull;

import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -35,15 +37,15 @@ public class PullCommand extends PushPullCommand<PullOptions>
{
private static final Logger log = LoggerFactory.getLogger(PullCommand.class);

private static final Map<String, PullStrategy> strategies = new HashMap<String, PullStrategy>();
private static final Map<String, Class<? extends PullStrategy>> strategies = new HashMap<String, Class<? extends PullStrategy>>();

{
strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, new UTF8PropertiesStrategy());
strategies.put(PROJECT_TYPE_PROPERTIES, new PropertiesStrategy());
strategies.put(PROJECT_TYPE_GETTEXT, new GettextPullStrategy());
strategies.put(PROJECT_TYPE_PUBLICAN, new GettextDirStrategy());
strategies.put(PROJECT_TYPE_XLIFF, new XliffStrategy());
strategies.put(PROJECT_TYPE_XML, new XmlStrategy());
strategies.put(PROJECT_TYPE_UTF8_PROPERTIES, UTF8PropertiesStrategy.class);
strategies.put(PROJECT_TYPE_PROPERTIES, PropertiesStrategy.class);
strategies.put(PROJECT_TYPE_GETTEXT, GettextPullStrategy.class);
strategies.put(PROJECT_TYPE_PUBLICAN, GettextDirStrategy.class);
strategies.put(PROJECT_TYPE_XLIFF, XliffStrategy.class);
strategies.put(PROJECT_TYPE_XML, XmlStrategy.class);
}

public PullCommand(PullOptions opts)
Expand All @@ -56,15 +58,17 @@ public PullCommand(PullOptions opts, ZanataProxyFactory factory, ISourceDocResou
super(opts, factory, sourceDocResource, translationResources, uri);
}

private PullStrategy getStrategy(String strategyType)
private PullStrategy createStrategy(String strategyType)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException
{
PullStrategy strat = strategies.get(strategyType);
if (strat == null)
Class<? extends PullStrategy> clazz = strategies.get(strategyType);
if (clazz == null)
{
throw new RuntimeException("unknown project type: " + getOpts().getProjectType());
}
strat.setPullOptions(getOpts());
return strat;
Constructor<? extends PullStrategy> ctor = clazz.getConstructor(PullOptions.class);
assert ctor != null: "strategy must have constructor which accepts PullOptions";
return ctor.newInstance(getOpts());
}

private void logOptions()
Expand Down Expand Up @@ -103,6 +107,7 @@ public static void logOptions(Logger logger, PullOptions opts)
}
}
logger.info("Locales to pull: {}", opts.getLocaleMapList());
logger.info("Encode tab as \\t: {}", opts.getEncodeTabs());
if (opts.getPullType() == PushPullType.Source)
{
logger.info("Pulling source documents only");
Expand All @@ -129,7 +134,7 @@ public void run() throws Exception
LocaleList locales = getOpts().getLocaleMapList();
if (locales == null)
throw new ConfigException("no locales specified");
PullStrategy strat = getStrategy(getOpts().getProjectType());
PullStrategy strat = createStrategy(getOpts().getProjectType());
List<String> docNamesForModule = getQualifiedDocNamesForCurrentModuleFromServer();

// TODO compare docNamesForModule with localDocNames, offer to delete obsolete translations from filesystem
Expand Down
Expand Up @@ -32,4 +32,5 @@ public interface PullOptions extends PushPullOptions
{
PushPullType getPullType();
boolean getCreateSkeletons();
boolean getEncodeTabs();
}
Expand Up @@ -7,15 +7,26 @@
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TranslationsResource;

/**
* Strategy for converting documents from Zanata to a local file type.
* Every implementation must have a public constructor which accepts PullOptions.
*/
public interface PullStrategy
{
void setPullOptions(PullOptions opts);
/**
* Which extensions (eg gettext, comment) does this strategy need to fetch from the server?
* @return
*/
StringSet getExtensions();

/**
* Does this strategy need the source document (Resource) when writing translations?
* @return
*/
boolean needsDocToWriteTrans();

/**
* @param doc
* @param docWithLocalName
* @throws IOException
*/
void writeSrcFile(Resource docWithLocalName) throws IOException;
Expand Down
Expand Up @@ -35,24 +35,25 @@
public class UTF8PropertiesStrategy extends PropertiesStrategy
{

public UTF8PropertiesStrategy()
protected UTF8PropertiesStrategy(PullOptions opts)
{
super(opts);
}

@Override
public void writeSrcFile(Resource doc) throws IOException
{
PropWriter.writeUTF8(doc, getPullOptions().getSrcDir());
PropWriter.writeUTF8(doc, getOpts().getSrcDir());
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping localeMapping, TranslationsResource targetDoc) throws IOException
{
boolean createSkeletons = getPullOptions().getCreateSkeletons();
boolean createSkeletons = getOpts().getCreateSkeletons();
if (createSkeletons)
PropWriter.writeUTF8(doc, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
PropWriter.writeUTF8(doc, targetDoc, getOpts().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
else
PropWriter.writeUTF8(null, targetDoc, getPullOptions().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
PropWriter.writeUTF8(null, targetDoc, getOpts().getTransDir(), docName, localeMapping.getJavaLocale(), createSkeletons);
}

}
Expand Up @@ -33,15 +33,13 @@
* @author Sean Flanigan <a href="mailto:sflaniga@redhat.com">sflaniga@redhat.com</a>
*
*/
public class XliffStrategy implements PullStrategy
public class XliffStrategy extends AbstractPullStrategy
{
StringSet extensions = new StringSet("comment");
private PullOptions opts;

@Override
public void setPullOptions(PullOptions opts)
protected XliffStrategy(PullOptions opts)
{
this.opts = opts;
super(opts);
}

@Override
Expand All @@ -59,13 +57,13 @@ public boolean needsDocToWriteTrans()
@Override
public void writeSrcFile(Resource doc) throws IOException
{
XliffWriter.write(opts.getSrcDir(), doc, "en-US");
XliffWriter.write(getOpts().getSrcDir(), doc, "en-US");
}

@Override
public void writeTransFile(Resource doc, String docName, LocaleMapping localeMapping, TranslationsResource targetDoc) throws IOException
{
XliffWriter.write(opts.getTransDir(), doc, localeMapping.getLocalLocale(), targetDoc, opts.getCreateSkeletons());
XliffWriter.write(getOpts().getTransDir(), doc, localeMapping.getLocalLocale(), targetDoc, getOpts().getCreateSkeletons());
}

}

0 comments on commit f462371

Please sign in to comment.