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

Commit

Permalink
Merge pull request #112 from zanata/ZNTA-1028-refactor-QualifiedSrcDo…
Browse files Browse the repository at this point in the history
…cName

Rename QualifiedSrcDocName and UnqualifiedSrcDocName
  • Loading branch information
seanf committed Apr 19, 2016
2 parents d97b285 + 101f74d commit b62679b
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 87 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ cache:

language: java

before_install:
- wget https://archive.apache.org/dist/maven/maven-3/3.3.9/binaries/apache-maven-3.3.9-bin.zip
- unzip -qq apache-maven-3.3.9-bin.zip
- export M2_HOME=$PWD/apache-maven-3.3.9
- export PATH=$M2_HOME/bin:$PATH

# Use the 'true' command to avoid up-front dependency fetching, for faster builds
# See http://docs.travis-ci.com/user/languages/java/#Dependency-Management
install: /bin/true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@
import com.google.common.base.Strings;

/**
* Represents document name with extension.
* Represents a source document name with extension.
*/
public class QualifiedSrcDocName {
public class DocNameWithExt {
private final String fullName;
private final String extension;

QualifiedSrcDocName(String fullName) {
DocNameWithExt(String fullName) {
this.fullName = fullName;
extension = FilenameUtils.getExtension(fullName).toLowerCase();
}
public static QualifiedSrcDocName from(String qualifiedName) {
String extension = FilenameUtils.getExtension(qualifiedName);
public static DocNameWithExt from(String filename) {
String extension = FilenameUtils.getExtension(filename);
Preconditions.checkArgument(!Strings.isNullOrEmpty(extension),
"expect a qualified document name (with extension)");
return new QualifiedSrcDocName(qualifiedName);
"expected a full filename (with extension)");
return new DocNameWithExt(filename);
}
public static QualifiedSrcDocName from(String unqualifiedName, String extension) {
return new QualifiedSrcDocName(unqualifiedName + "." + extension);
public static DocNameWithExt from(String docNameWithoutExt, String extension) {
return new DocNameWithExt(docNameWithoutExt + "." + extension);
}

public String getFullName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@
import org.zanata.common.ProjectType;

/**
* Represents document name without extension.
* Represents a source document name without extension.
*/
public class UnqualifiedSrcDocName {
public class DocNameWithoutExt {
private final String name;
UnqualifiedSrcDocName(String name) {
DocNameWithoutExt(String name) {
this.name = name;
}

public static UnqualifiedSrcDocName from(String docName) {
return new UnqualifiedSrcDocName(docName);
public static DocNameWithoutExt from(String docName) {
return new DocNameWithoutExt(docName);
}

public QualifiedSrcDocName toQualifiedDocName(ProjectType projectType) {
public DocNameWithExt toDocNameWithExt(ProjectType projectType) {
switch (projectType) {
case Utf8Properties:
case Properties:
return QualifiedSrcDocName
return DocNameWithExt
.from(name, "properties");
case Gettext:
case Podir:
return QualifiedSrcDocName.from(name, "pot");
return DocNameWithExt.from(name, "pot");
case Xliff:
case Xml:
return QualifiedSrcDocName.from(name, "xml");
return DocNameWithExt.from(name, "xml");
case File:
throw new IllegalArgumentException("You can not using unqualified document name in file type project");
throw new IllegalArgumentException("You cannot use document name without extension with FILE project type");
}
throw new IllegalStateException("Can not convert unqualified document name for this project type: " + projectType);
throw new IllegalStateException("Cannot determine file extension for this project type: " + projectType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,32 +75,32 @@ public static boolean isRuleValid(String rule) {
/**
* Check whether the parsed rule is applicable to a source document.
*
* @param qualifiedSrcDocName
* @param docNameWithExt
* source document name with extension
* @return true if this parsed rule is applicable
*/
public boolean isApplicable(QualifiedSrcDocName qualifiedSrcDocName) {
public boolean isApplicable(DocNameWithExt docNameWithExt) {
if (Strings.isNullOrEmpty(mappingRule.getPattern())) {
return matchFileExtensionWithProjectType(qualifiedSrcDocName);
return matchFileExtensionWithProjectType(docNameWithExt);
}
PathMatcher matcher =
FileSystems.getDefault().getPathMatcher("glob:" + mappingRule.getPattern());
// this will help when qualifiedSrcDocName has just file name i.e.
// this will help when docNameWithExt has just file name i.e.
// test.odt whereas pattern is defined as **/*.odt
File srcFile =
new File(opts.getSrcDir(),
qualifiedSrcDocName.getFullName());
docNameWithExt.getFullName());
log.debug("trying to match pattern: {} to file: {}",
mappingRule.getPattern(), srcFile.getPath());
return matcher.matches(Paths.get(srcFile.getPath()));
}

private boolean matchFileExtensionWithProjectType(
QualifiedSrcDocName qualifiedSrcDocName) {
DocNameWithExt docNameWithExt) {
List<DocumentType> documentTypes = projectType.getSourceFileTypes();
for (DocumentType docType: documentTypes) {
if (docType.getSourceExtensions().contains(
qualifiedSrcDocName.getExtension())) {
docNameWithExt.getExtension())) {
return true;
}
}
Expand All @@ -110,17 +110,17 @@ private boolean matchFileExtensionWithProjectType(
/**
* Apply the rule and return relative path of the translation file.
*
* @param qualifiedSrcDocName
* @param docNameWithExt
* source document name with extension
* @param localeMapping
* locale mapping
* @return relative path (relative to trans-dir) for the translation file
*/
public String getRelativeTransFilePathForSourceDoc(
QualifiedSrcDocName qualifiedSrcDocName,
DocNameWithExt docNameWithExt,
@Nonnull LocaleMapping localeMapping, Optional<String> translationFileExtension) {
EnumMap<Placeholders, String> map =
parseToMap(qualifiedSrcDocName.getFullName(), localeMapping, translationFileExtension);
parseToMap(docNameWithExt.getFullName(), localeMapping, translationFileExtension);

String transFilePath = mappingRule.getRule();
for (Map.Entry<Placeholders, String> entry : map.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,22 @@ public TransFileResolver(ConfigurableProjectOptions opts) {
* Determines where to store the translation file for a given source
* document and locale mapping.
*
* @param qualifiedSrcDocName
* @param docNameWithExt
* source document name with extension
* @param localeMapping
* locale mapping
* @return translation destination
*/
public File resolveTransFile(QualifiedSrcDocName qualifiedSrcDocName,
public File resolveTransFile(DocNameWithExt docNameWithExt,
LocaleMapping localeMapping, Optional<String> translationFileExtension) {
Optional<File> fileOptional =
tryGetTransFileFromProjectMappingRules(qualifiedSrcDocName,
tryGetTransFileFromProjectMappingRules(docNameWithExt,
localeMapping, translationFileExtension);
if (fileOptional.isPresent()) {
return fileOptional.get();
} else {
ProjectType projectType = getProjectType();
return getDefaultTransFileFromProjectType(qualifiedSrcDocName,
return getDefaultTransFileFromProjectType(docNameWithExt,
localeMapping, projectType, translationFileExtension);
}
}
Expand All @@ -101,17 +101,17 @@ public File resolveTransFile(QualifiedSrcDocName qualifiedSrcDocName,
* Determines where to store the translation file for a given source
* document and locale mapping.
*
* @param unqualifiedSrcDocName
* @param docNameWithoutExt
* source document name without extension
* @param localeMapping
* locale mapping
* @return translation destination
*/
public File getTransFile(UnqualifiedSrcDocName unqualifiedSrcDocName,
public File getTransFile(DocNameWithoutExt docNameWithoutExt,
LocaleMapping localeMapping) {
QualifiedSrcDocName qualifiedSrcDocName =
unqualifiedSrcDocName.toQualifiedDocName(getProjectType());
return resolveTransFile(qualifiedSrcDocName, localeMapping,
DocNameWithExt docNameWithExt =
docNameWithoutExt.toDocNameWithExt(getProjectType());
return resolveTransFile(docNameWithExt, localeMapping,
Optional.<String>absent());
}

Expand All @@ -125,36 +125,36 @@ private ProjectType getProjectType() {
}

private File getDefaultTransFileFromProjectType(
QualifiedSrcDocName qualifiedSrcDocName, LocaleMapping localeMapping,
DocNameWithExt docNameWithExt, LocaleMapping localeMapping,
ProjectType projectType, Optional<String> translationFileExtension) {
FileMappingRule rule = PROJECT_TYPE_FILE_MAPPING_RULES.get(projectType);
checkState(rule != null, get("no.default.mapping"), projectType);
String relativePath = new FileMappingRuleHandler(rule, projectType, opts)
.getRelativeTransFilePathForSourceDoc(qualifiedSrcDocName,
.getRelativeTransFilePathForSourceDoc(docNameWithExt,
localeMapping, translationFileExtension);
return new File(opts.getTransDir(), relativePath);
}

private Optional<File> tryGetTransFileFromProjectMappingRules(
QualifiedSrcDocName qualifiedSrcDocName, LocaleMapping localeMapping,
DocNameWithExt docNameWithExt, LocaleMapping localeMapping,
Optional<String> translationFileExtension) {
List<FileMappingRule> fileMappingRules = opts.getFileMappingRules();
// TODO may need to sort the rules. put rules without pattern to last
for (FileMappingRule rule : fileMappingRules) {
FileMappingRuleHandler handler = new FileMappingRuleHandler(rule,
getProjectType(), opts);
if (handler.isApplicable(qualifiedSrcDocName)) {
if (handler.isApplicable(docNameWithExt)) {
String relativePath = handler
.getRelativeTransFilePathForSourceDoc(
qualifiedSrcDocName,
docNameWithExt,
localeMapping, translationFileExtension);
return Optional.of(new File(opts.getTransDir(), relativePath));
}
}
if (fileMappingRules.size() > 0) {
log.warn(
"None of the file mapping rule is applicable for {}. Please make sure your mapping is correct.",
qualifiedSrcDocName.getFullName());
docNameWithExt.getFullName());
}
return Optional.absent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
import org.apache.commons.io.FilenameUtils;
import org.zanata.client.commands.ConfigurableProjectOptions;
import org.zanata.client.commands.ConsoleInteractor;
import org.zanata.client.commands.OptionsUtil;
import org.zanata.client.commands.QualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithExt;
import org.zanata.client.commands.TransFileResolver;
import org.zanata.client.commands.UnqualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithoutExt;
import org.zanata.client.commands.pull.PullOptions;
import org.zanata.client.commands.pull.PullOptionsImpl;
import org.zanata.client.config.LocaleList;
Expand Down Expand Up @@ -168,7 +167,7 @@ public String getTransFileToWrite(String srcDoc,
Optional<String> translationFileExtension =
Optional.fromNullable(targetFileExt);
File file = transFileResolver.resolveTransFile(
QualifiedSrcDocName.from(srcDoc),
DocNameWithExt.from(srcDoc),
localeMapping, translationFileExtension);
return file.getPath();
}
Expand All @@ -185,7 +184,7 @@ static class OtherTransFilePathFinder implements TransFilePathFinder {
public String getTransFileToWrite(String srcDoc,
LocaleMapping localeMapping) {
File transFile = transFileResolver.getTransFile(
UnqualifiedSrcDocName.from(srcDoc), localeMapping);
DocNameWithoutExt.from(srcDoc), localeMapping);
return transFile.getPath();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.io.File;

import org.zanata.client.commands.UnqualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithoutExt;

public abstract class AbstractPullStrategy implements PullStrategy {
private final PullOptions opts;
Expand All @@ -27,6 +27,6 @@ public boolean isTransOnly() {
public File getTransFileToWrite(String docName,
LocaleMapping localeMapping) {
return new TransFileResolver(getOpts()).getTransFile(
UnqualifiedSrcDocName.from(docName), localeMapping);
DocNameWithoutExt.from(docName), localeMapping);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.QualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithExt;
import org.zanata.client.commands.TransFileResolver;
import org.zanata.client.config.LocaleMapping;
import org.zanata.util.PathUtil;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void writeTransFile(String localDocName,
+ localDocName);
}
File file = new TransFileResolver(opts).resolveTransFile(
QualifiedSrcDocName.from(localDocName),
DocNameWithExt.from(localDocName),
localeMapping, translationFileExtension);
logAndStreamToFile(transFile, file);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.xml.sax.InputSource;
import org.zanata.adapter.po.PoReader2;
import org.zanata.client.commands.TransFileResolver;
import org.zanata.client.commands.UnqualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithoutExt;
import org.zanata.client.commands.push.PushCommand.TranslationResourcesVisitor;
import org.zanata.client.config.LocaleMapping;
import org.zanata.common.LocaleId;
Expand Down Expand Up @@ -110,7 +110,7 @@ protected Set<String> getSrcDocNames() {

protected File getTransFile(LocaleMapping locale, String docName) {
File transFile = new TransFileResolver(getOpts()).getTransFile(
UnqualifiedSrcDocName.from(docName), locale);
DocNameWithoutExt.from(docName), locale);
return transFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.ConsoleInteractorImpl;
import org.zanata.client.commands.TransFileResolver;
import org.zanata.client.commands.UnqualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithoutExt;
import org.zanata.client.config.LocaleList;
import org.zanata.client.config.LocaleMapping;

Expand Down Expand Up @@ -72,7 +72,7 @@ List<LocaleMapping> findLocales(String srcDocName) {
private boolean hasTranslationFileForLocale(LocaleMapping loc,
String srcDocName) {
File transFile = new TransFileResolver(getOpts()).getTransFile(
UnqualifiedSrcDocName.from(srcDocName), loc);
DocNameWithoutExt.from(srcDocName), loc);
return transFile.exists();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zanata.client.commands.TransFileResolver;
import org.zanata.client.commands.UnqualifiedSrcDocName;
import org.zanata.client.commands.DocNameWithoutExt;
import org.zanata.client.config.LocaleList;
import org.zanata.client.config.LocaleMapping;

Expand All @@ -34,11 +34,11 @@ List<LocaleMapping> findLocales(String srcDocName) {
return Collections.emptyList();
}

final UnqualifiedSrcDocName unqualifiedSrcDocName =
UnqualifiedSrcDocName.from(srcDocName);
final DocNameWithoutExt docNameWithoutExt =
DocNameWithoutExt.from(srcDocName);
List<File> transFilesDestinations =
Lists.transform(localeListInConfig,
new LocaleMappingToTransFile(unqualifiedSrcDocName,
new LocaleMappingToTransFile(docNameWithoutExt,
getOpts()));
// we remove all the ones that WILL be mapped and treated as
// translation files
Expand All @@ -54,18 +54,18 @@ List<LocaleMapping> findLocales(String srcDocName) {

private static class LocaleMappingToTransFile implements
Function<LocaleMapping, File> {
private final UnqualifiedSrcDocName unqualifiedSrcDocName;
private final DocNameWithoutExt docNameWithoutExt;
private TransFileResolver transFileResolver;

public LocaleMappingToTransFile(
UnqualifiedSrcDocName unqualifiedSrcDocName, PushOptions opts) {
this.unqualifiedSrcDocName = unqualifiedSrcDocName;
DocNameWithoutExt docNameWithoutExt, PushOptions opts) {
this.docNameWithoutExt = docNameWithoutExt;
transFileResolver = new TransFileResolver(opts);
}

@Override
public File apply(LocaleMapping localeMapping) {
return transFileResolver.getTransFile(unqualifiedSrcDocName,
return transFileResolver.getTransFile(docNameWithoutExt,
localeMapping);
}
}
Expand Down
Loading

0 comments on commit b62679b

Please sign in to comment.