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 #1 from zanata/rhbz873489
Browse files Browse the repository at this point in the history
rhbz873489 XLIFF/Properties strategies should calculate and set sourceHash for safety
  • Loading branch information
davidmason committed Apr 8, 2014
2 parents 5a7bb85 + fadda87 commit 98e86d4
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,6 +1,6 @@
language: java
install: true
script: mvn --settings settings.xml test -Dgwt.validateOnly -Darquillian.jboss.home=/dev/null
script: mvn --batch-mode --settings settings.xml test -Dgwt.validateOnly -Darquillian.jboss.home=/dev/null
jdk:
- openjdk6
- openjdk7
Expand Down
Expand Up @@ -110,14 +110,8 @@ public TranslationsResource extractTarget(InputSource inputSource) {
// add the target content (msgstr)
TextFlowTarget tfTarget = new TextFlowTarget();
tfTarget.setResId(id);
String sourceContent;
if (message.isPlural()) {
sourceContent =
message.getMsgid() + '|' + message.getMsgidPlural();
} else {
sourceContent = message.getMsgid();
}
tfTarget.setSourceHash(HashUtil.generateHash(sourceContent));
List<String> sourceContents = getSourceContents(message);
tfTarget.setSourceHash(HashUtil.sourceHash(sourceContents));
tfTarget.setDescription(ShortString.shorten(message.getMsgid()));
tfTarget.setContents(getContents(message));
tfTarget.setState(getContentState(message));
Expand Down Expand Up @@ -298,6 +292,16 @@ else if (inputSource.getByteStream() != null) {
return messageParser;
}

private List<String> getSourceContents(Message message) {
List<String> sourceContents;
if (message.isPlural()) {
sourceContents = Arrays.asList(message.getMsgid(), message.getMsgidPlural());
} else {
sourceContents = Arrays.asList(message.getMsgid());
}
return sourceContents;
}

/**
* Returns the contents of the Message (msgstr for singular, msgstr_plural
* for plural) Also ensures at least one entry.
Expand Down
5 changes: 5 additions & 0 deletions zanata-adapter-properties/pom.xml
Expand Up @@ -28,6 +28,11 @@
<artifactId>zanata-common-util</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
Expand Up @@ -4,8 +4,10 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Collections;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Map;

import org.fedorahosted.openprops.Properties;
import org.zanata.common.ContentState;
Expand All @@ -15,6 +17,9 @@
import org.zanata.rest.dto.resource.TextFlow;
import org.zanata.rest.dto.resource.TextFlowTarget;
import org.zanata.rest.dto.resource.TranslationsResource;
import org.zanata.util.HashUtil;

import com.google.common.collect.Maps;

/**
* A Properties reader with support for skipping NON-TRANSLATABLE keys. NOT
Expand Down Expand Up @@ -54,20 +59,38 @@ public PropReader() {
}

// pre: template already extracted
@Deprecated
public void extractTarget(TranslationsResource doc, InputStream in)
throws IOException, RuntimeException {
extractTarget(doc, in, Collections.<String, List<String>>emptyMap());
}

public void extractTarget(TranslationsResource doc, InputStream in,
Resource srcDoc) throws IOException, RuntimeException {
Map<String, List<String>> keyToSourceContentsMap = Maps.newHashMap();
for (TextFlow tf: srcDoc.getTextFlows()) {
keyToSourceContentsMap.put(tf.getId(), tf.getContents());
}
extractTarget(doc, in, keyToSourceContentsMap);
}

private void extractTarget(TranslationsResource doc, InputStream in,
Map<String, List<String>> keyToSourceContentsMap) throws IOException, RuntimeException {
Properties props = loadProps(in);
for (String key : props.keySet()) {
addPropEntryToDoc(doc, props, key, contentState);
List<String> sourceContents = keyToSourceContentsMap.get(key);
String sourceHash = sourceContents == null ? null : HashUtil.sourceHash(sourceContents);
addPropEntryToDoc(doc, props, key, contentState, sourceHash);
}
}

private void addPropEntryToDoc(TranslationsResource doc, Properties props,
String key, ContentState contentState) {
String key, ContentState contentState, String sourceHash) {
String content = props.getProperty(key);
if (content == null)
return;
TextFlowTarget textFlowTarget = new TextFlowTarget(key);
textFlowTarget.setSourceHash(sourceHash);
textFlowTarget.setContents(content);
if (!content.isEmpty()) {
textFlowTarget.setState(contentState);
Expand Down
@@ -1,2 +1,3 @@
# a source comment
HELLO=Hello World
ENCODED_NEWLINE=This string has an \n encoded newline.
10 changes: 5 additions & 5 deletions zanata-adapter-xliff/pom.xml
Expand Up @@ -22,6 +22,11 @@
<groupId>org.zanata</groupId>
<artifactId>zanata-common-util</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand All @@ -34,10 +39,5 @@
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
</dependency>

<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
</dependencies>
</project>

0 comments on commit 98e86d4

Please sign in to comment.