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

Commit

Permalink
rhbz1139950 - read config and add trans file resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Sep 15, 2014
1 parent 21938bf commit 023bea9
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
@@ -1,9 +1,11 @@
package org.zanata.client.commands;

import java.io.File;
import java.util.List;


import org.kohsuke.args4j.Option;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleList;
import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -93,4 +95,8 @@ public interface ConfigurableProjectOptions extends ConfigurableOptions {
usage = "Wildcard pattern to exclude files and directories. Usage\n"
+ "--excludes=\"Pattern1,Pattern2,Pattern3\"")
void setExcludes(String excludes);

void setFileMappingRules(List<FileMappingRule> rules);

List<FileMappingRule> getFileMappingRules();
}
Expand Up @@ -21,8 +21,13 @@
package org.zanata.client.commands;

import java.io.File;
import java.util.Collections;
import java.util.List;

import javax.annotation.Nullable;

import org.kohsuke.args4j.Option;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleList;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -55,6 +60,7 @@ public abstract class ConfigurableProjectOptionsImpl extends
private ImmutableList<String> excludes = ImmutableList.of();
private Splitter splitter = Splitter.on(",").omitEmptyStrings()
.trimResults();
private List<FileMappingRule> rules;

@Override
public String getProj() {
Expand Down Expand Up @@ -201,4 +207,18 @@ void setExcludes(String excludes) {
this.excludes = ImmutableList.of();
}
}

@Override
public void setFileMappingRules(List<FileMappingRule> rules) {
this.rules = rules;
}

@Override
public List<FileMappingRule> getFileMappingRules() {
if (rules == null) {
return Collections.emptyList();
}
return ImmutableList.copyOf(rules);
}

}
Expand Up @@ -99,6 +99,7 @@ private static void applyProjectConfig(ConfigurableProjectOptions opts,
if (opts.getCommandHooks().isEmpty() && config.getHooks() != null) {
opts.setCommandHooks(config.getHooks());
}
opts.setFileMappingRules(config.getRules());
}


Expand Down
@@ -0,0 +1,56 @@
/*
* Copyright 2014, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.zanata.client.commands;

import com.google.common.base.Optional;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;

import java.io.File;
import java.util.List;

/**
* @author Patrick Huang
* <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
public class TransFileResolver {
private final ConfigurableProjectOptions opts;

public TransFileResolver(ConfigurableProjectOptions opts) {
this.opts = opts;
}

public Optional<File> getTransFile(String sourceDocWithExtension,
LocaleMapping localeMapping) {
List<FileMappingRule> fileMappingRules = opts.getFileMappingRules();
for (FileMappingRule rule : fileMappingRules) {
FileMappingRuleParser parser = new FileMappingRuleParser(rule);
if (parser.isApplicable(sourceDocWithExtension)) {
String relativePath = parser
.getRelativePathFromRule(sourceDocWithExtension,
localeMapping);
return Optional.of(new File(opts.getTransDir(), relativePath));
}
}
return Optional.absent();
}
}
@@ -0,0 +1,53 @@
package org.zanata.client.commands;


import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.zanata.client.commands.init.InitOptionsImpl;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;

import java.io.File;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

public class TransFileResolverTest {

private TransFileResolver resolver;
private ConfigurableProjectOptions opts;

@Before
public void setUp() {
// we choose init option as the implementation just because it's simple
opts = new InitOptionsImpl();
resolver = new TransFileResolver(opts);
}

@Test
public void canGetTransFileUsingRule() {
opts.setTransDir(new File("."));
opts.setFileMappingRules(Lists.newArrayList(
new FileMappingRule("**/*.pot",
"{path}/{locale_with_underscore}.po"),
new FileMappingRule("**/*.properties",
"{path}/{filename}_{locale_with_underscore}.{extension}")));
Optional<File> gettext =
resolver.getTransFile("gcc/po/gcc.pot", new LocaleMapping("de-DE"));

assertThat(gettext.get().getPath(), equalTo("./gcc/po/de_DE.po"));

Optional<File> prop = resolver
.getTransFile("src/main/resources/messages.properties",
new LocaleMapping("zh"));
assertThat(prop.get().getPath(), equalTo(
"./src/main/resources/messages_zh.properties"));

Optional<File> noMatching = resolver
.getTransFile("doc/marketting.odt", new LocaleMapping("ja"));
assertThat(noMatching.isPresent(), equalTo(false));
}

}
@@ -1,9 +1,12 @@
package org.zanata.maven;

import java.io.File;
import java.util.Collections;
import java.util.List;

import org.zanata.client.commands.ConfigurableOptions;
import org.zanata.client.commands.ConfigurableProjectOptions;
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleList;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -92,6 +95,7 @@ public abstract class ConfigurableProjectMojo<O extends ConfigurableOptions>
private String excludes;
private Splitter splitter = Splitter.on(",").omitEmptyStrings()
.trimResults();
private List<FileMappingRule> rules;

public ConfigurableProjectMojo() {
super();
Expand Down Expand Up @@ -192,4 +196,17 @@ public void setIncludes(String includes) {
public void setExcludes(String excludes) {
this.excludes = excludes;
}

@Override
public void setFileMappingRules(List<FileMappingRule> rules) {
this.rules = rules;
}

@Override
public List<FileMappingRule> getFileMappingRules() {
if (rules == null) {
return Collections.emptyList();
}
return ImmutableList.copyOf(rules);
}
}

0 comments on commit 023bea9

Please sign in to comment.