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

Commit

Permalink
Use server FileTypeInfo merged with --file-types
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Jul 12, 2016
1 parent 17b3f4a commit a13725d
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 162 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.zanata</groupId>
<artifactId>zanata-parent</artifactId>
<version>28-SNAPSHOT</version>
<version>29-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
Expand All @@ -40,19 +36,19 @@
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import org.zanata.adapter.po.PoWriter2;
import org.zanata.common.ContentState;
import org.zanata.common.FileTypeInfo;
import org.zanata.common.DocumentType;
import org.zanata.common.LocaleId;
import org.zanata.common.ProjectType;
import org.zanata.rest.DocumentFileUploadForm;
import org.zanata.rest.StringSet;
import org.zanata.rest.dto.ChunkUploadResponse;
import org.zanata.rest.dto.Project;
import org.zanata.rest.dto.resource.Resource;
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;

import static org.zanata.common.ProjectType.Podir;
import static org.zanata.common.ProjectType.fileProjectSourceDocTypes;

/**
* @author Patrick Huang <a
Expand All @@ -77,9 +73,19 @@ public Response acceptedFileTypes() {
}

@Override
@Deprecated
public Response acceptedFileTypeList() {
GenericEntity<List<DocumentType>> genericEntity =
new GenericEntity<List<DocumentType>>(ProjectType.fileProjectSourceDocTypes()) {};
new GenericEntity<List<DocumentType>>(fileProjectSourceDocTypes()) {};
return Response.ok(genericEntity).build();
}

@Override
public Response fileTypeInfoList() {
List fileTypeInfoList = (List) fileProjectSourceDocTypes().stream().map(
DocumentType::toFileTypeInfo).collect(Collectors.toList());
GenericEntity<List<FileTypeInfo>> genericEntity =
new GenericEntity<List<FileTypeInfo>>(fileTypeInfoList) {};
return Response.ok(genericEntity).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.zanata.client.config.FileMappingRule;
import org.zanata.client.config.LocaleMapping;
import org.zanata.client.util.FileUtil;
import org.zanata.common.DocumentType;
import org.zanata.common.ProjectType;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -81,7 +80,7 @@ public static boolean isRuleValid(String rule) {
*/
public boolean isApplicable(DocNameWithExt docNameWithExt) {
if (Strings.isNullOrEmpty(mappingRule.getPattern())) {
return matchFileExtensionWithProjectType(docNameWithExt);
return true;
}
PathMatcher matcher =
FileSystems.getDefault().getPathMatcher("glob:" + mappingRule.getPattern());
Expand All @@ -95,18 +94,6 @@ public boolean isApplicable(DocNameWithExt docNameWithExt) {
return matcher.matches(Paths.get(srcFile.getPath()));
}

private boolean matchFileExtensionWithProjectType(
DocNameWithExt docNameWithExt) {
List<DocumentType> documentTypes = projectType.getSourceFileTypes();
for (DocumentType docType: documentTypes) {
if (docType.getSourceExtensions().contains(
docNameWithExt.getExtension())) {
return true;
}
}
return false;
}

/**
* Apply the rule and return relative path of the translation file.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ private Optional<File> tryGetTransFileFromProjectMappingRules(
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
// Rules are applied in order. A rule with no pattern always matches,
// so such a rule should be the last.
for (FileMappingRule rule : fileMappingRules) {
FileMappingRuleHandler handler = new FileMappingRuleHandler(rule,
getProjectType(), opts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.kohsuke.args4j.Option;
Expand All @@ -40,7 +39,7 @@
import org.zanata.client.commands.push.PushOptionsImpl;
import org.zanata.client.commands.push.RawPushCommand;
import org.zanata.client.commands.push.RawPushStrategy;
import org.zanata.common.DocumentType;
import org.zanata.common.FileTypeInfo;
import org.zanata.rest.client.FileResourceClient;
import org.zanata.rest.client.RestClientFactory;

Expand Down Expand Up @@ -256,23 +255,21 @@ private ImmutableList<String> filteredFileExtensions(PushOptions opts) {
new RawPushCommand(opts, clientFactory, console);
FileResourceClient client =
clientFactory.getFileResourceClient();
List<DocumentType> rawDocumentTypes = client.acceptedFileTypes();
Map<DocumentType, Set<String>> filteredDocTypes =
rawPushCommand.validateFileTypes(rawDocumentTypes,
List<FileTypeInfo> serverFileTypes = client.fileTypeInfoList();
ImmutableList<FileTypeInfo> filteredDocTypes =
rawPushCommand.getActualFileTypes(serverFileTypes,
opts.getFileTypes());

if (filteredDocTypes.isEmpty()) {
log.info("no valid types specified; nothing to do");
return ImmutableList.of();
}

ImmutableList.Builder<String> sourceFileExtensionsBuilder =
ImmutableList.Builder<String> sourceFileExtensions =
ImmutableList.builder();
for (Set<String> filteredSourceExtensions : filteredDocTypes
.values()) {
sourceFileExtensionsBuilder.addAll(filteredSourceExtensions);
}
return sourceFileExtensionsBuilder.build();
filteredDocTypes.forEach(type ->
sourceFileExtensions.addAll(type.getSourceExtensions()));
return sourceFileExtensions.build();
}
}

Expand Down
Loading

0 comments on commit a13725d

Please sign in to comment.