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

Commit

Permalink
rhbz873489 Add sourceHash to TextFlowTarget in XliffReader
Browse files Browse the repository at this point in the history
  • Loading branch information
seanf committed Mar 26, 2014
1 parent 2895604 commit 1d20fc1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 5 additions & 0 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 Down
Expand Up @@ -12,6 +12,8 @@
import java.io.Reader;
import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
Expand All @@ -36,6 +38,7 @@
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.base.Charsets;

Expand Down Expand Up @@ -104,8 +107,9 @@ public LSInput resolveResource(String type,
}
}

private void extractXliff(File file, Resource document,
TranslationsResource transDoc) throws FileNotFoundException {
private void extractXliff(@Nonnull File file, @Nullable Resource document,
@Nullable TranslationsResource transDoc)
throws FileNotFoundException {

if (validationType == ValidationType.XSD) {
InputSource inputSource =
Expand Down Expand Up @@ -140,17 +144,14 @@ && getLocalName(xmlr).equals(ELE_FILE)) {
&& getLocalName(xmlr).equals(ELE_TRANS_UNIT)) {
if (document != null) {
TextFlow textFlow = extractTransUnit(xmlr);
if (textFlow != null) {
document.getTextFlows().add(textFlow);
}
document.getTextFlows().add(textFlow);
} else {
TextFlowTarget tfTarget = extractTransUnitTarget(xmlr);
List<String> contents = tfTarget.getContents();
boolean targetEmpty =
contents.isEmpty()
|| StringUtils.isEmpty(contents.get(0));
if (!targetEmpty) {
tfTarget.setState(ContentState.Translated);
transDoc.getTextFlowTargets().add(tfTarget);
}
}
Expand Down Expand Up @@ -215,18 +216,26 @@ private TextFlowTarget extractTransUnitTarget(XMLStreamReader xmlr)
if (endElement && localName.equals(ELE_TRANS_UNIT)) {
endTransUnit = true;
} else {
if (xmlr.isStartElement() && localName.equals(ELE_TARGET)) {
boolean startElement = xmlr.isStartElement();
if (startElement && localName.equals(ELE_SOURCE)) {
String content =
getElementValue(xmlr, ELE_SOURCE,
getContentElementList());
String sourceHash = HashUtil.sourceHash(content);
textFlowTarget.setSourceHash(sourceHash);
} else if (startElement && localName.equals(ELE_TARGET)) {
String content =
getElementValue(xmlr, ELE_TARGET,
getContentElementList());
textFlowTarget.setContents(asList(content));
} else if (xmlr.isStartElement()
} else if (startElement
&& localName.equals(ELE_CONTEXT_GROUP)) {
textFlowTarget.getExtensions(true).addAll(
extractContextList(xmlr));
}
}
}
textFlowTarget.setState(ContentState.Translated);
return textFlowTarget;
}

Expand Down
Expand Up @@ -38,6 +38,14 @@ public static String sourceHash(@Nonnull List<String> sourceContents) {
}
}

public static String sourceHash(@Nonnull String sourceContent) {
if (sourceContent.isEmpty()) {
return null;
} else {
return generateHash(sourceContent);
}
}

public static String md5Hex(String message) {
return DigestUtils.md5Hex(message);
}
Expand Down

0 comments on commit 1d20fc1

Please sign in to comment.