Skip to content

Commit b1420be

Browse files
committed
Fix Bundle logic to output proper XLIFF 1.2 . Fixes #5
1 parent 0528e99 commit b1420be

7 files changed

Lines changed: 86 additions & 80 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
eclipse.preferences.version=1
22
encoding//gp-res-filter/src/test/resource/resfilter/ios/write-output.strings=UTF-8
3+
encoding//gp-res-filter/src/test/resource/resfilter/xliff/input.xlf=UTF-8
4+
encoding//gp-res-filter/src/test/resource/resfilter/xliff/write-output.xlf=UTF-8
35
encoding//src/main/java=UTF-8
46
encoding//src/main/resources=UTF-8

gp-cli/src/main/java/com/ibm/g11n/pipeline/tools/cli/ExportCmd.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2015, 2016
2+
* Copyright IBM Corp. 2015, 2017
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -82,16 +82,17 @@ protected void _execute() {
8282
ResourceEntryData data = entry.getValue();
8383
String resVal = data.getValue();
8484
Integer seqNum = data.getSequenceNumber();
85+
String srcVal = data.getSourceValue();
8586
if (resVal == null && fallback) {
86-
resVal = data.getSourceValue();
87+
resVal = srcVal;
8788
}
8889
if (resVal != null) {
8990
int sequenceNumber = -1;
9091
if (seqNum != null) {
9192
sequenceNumber = seqNum.intValue();
9293
}
9394
ResourceString resString = new ResourceString(key, resVal,
94-
sequenceNumber, data.getNotes());
95+
sequenceNumber, data.getNotes(), srcVal);
9596
bundle.addResourceString(resString);
9697
}
9798
}

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/Bundle.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2016
2+
* Copyright IBM Corp. 2016, 2017
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,6 +54,10 @@ public void addResourceString(String key, String value, int sequenceNumber, List
5454
addResourceString(new ResourceString(key, value, sequenceNumber, notes));
5555
}
5656

57+
public void addResourceString(String key, String value, int sequenceNumber, List<String> notes, String srcValue) {
58+
addResourceString(new ResourceString(key, value, sequenceNumber, notes, srcValue));
59+
}
60+
5761
public void addNote(String note) {
5862
if (notes == null) {
5963
notes = new ArrayList<>();

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/ResourceString.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2016
2+
* Copyright IBM Corp. 2016, 2017
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,12 +31,18 @@ public final class ResourceString {
3131
private final String value;
3232
private int sequenceNumber;
3333
private List<String> notes;
34+
private final String srcValue;
3435

3536
public ResourceString(String key, String value, int sequenceNumber, List<String> notes ) {
37+
this(key, value, sequenceNumber, notes, null);
38+
}
39+
40+
public ResourceString(String key, String value, int sequenceNumber, List<String> notes, String srcValue ) {
3641
this.key = key;
3742
this.value = value;
3843
this.sequenceNumber = sequenceNumber;
3944
this.notes = notes == null ? null : new ArrayList<>(notes);
45+
this.srcValue = srcValue;
4046
}
4147

4248
public ResourceString(String key, String value, int sequenceNumber) {
@@ -77,6 +83,10 @@ public int getSequenceNumber() {
7783
return sequenceNumber;
7884
}
7985

86+
public String getSrcValue() {
87+
return srcValue;
88+
}
89+
8090
@Override
8191
public boolean equals(Object obj) {
8292
if (!(obj instanceof ResourceString)) {

gp-res-filter/src/main/java/com/ibm/g11n/pipeline/resfilter/XLIFFResource.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2015, 2016
2+
* Copyright IBM Corp. 2015, 2017
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,7 +35,6 @@
3535
import javax.xml.transform.dom.DOMSource;
3636
import javax.xml.transform.stream.StreamResult;
3737

38-
import org.w3c.dom.Attr;
3938
import org.w3c.dom.Document;
4039
import org.w3c.dom.Element;
4140
import org.w3c.dom.Node;
@@ -45,16 +44,25 @@
4544
public class XLIFFResource implements ResourceFilter {
4645

4746
private static final String VERSION_STRING = "version";
48-
private static final String VERSION_NUMBER_STRING = "2.0";
49-
private static final String UNIT_STRING = "unit";
47+
private static final String VERSION_NUMBER_STRING = "1.2";
48+
private static final String XMLNS_STRING = "xmlns:xsi";
49+
private static final String XMLNS_VALUE_STRING = "http://www.w3.org/2001/XMLSchema-instance";
50+
private static final String XSI_STRING = "xsi:schemaLocation";
51+
private static final String XSI_VALUE_STRING = "urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd";
52+
53+
private static final String UNIT_STRING = "trans-unit";
5054
private static final String ID_STRING = "id";
5155
private static final String SOURCE_STRING = "source";
56+
private static final String TARGET_STRING = "target";
5257
private static final String XLIFF_STRING = "xliff";
5358
private static final String FILE_STRING = "file";
5459
private static final String ORIGINAL_STRING = "original";
55-
private static final String GLOBAL_STRING = "global";
60+
private static final String GLOBAL_STRING = "g11n-pipeline";
5661
private static final String DATATYPE_STRING = "datatype";
5762
private static final String PLAINTEXT_STRING = "plaintext";
63+
private static final String SOURCE_LANGUAGE_STRING = "source-language";
64+
private static final String ENGLISH = "en";
65+
private static final String TARGET_LANGUAGE_STRING = "target-language";
5866
private static final String BODY_STRING = "body";
5967

6068
@Override
@@ -106,13 +114,6 @@ private int collectResourceStrings(NodeList nodeList, int startSeqNum, Bundle bu
106114
}
107115

108116
@Override
109-
@Deprecated
110-
/*
111-
* This method is incomplete and may not produce a valid XLIFF output file.
112-
*
113-
* (non-Javadoc)
114-
* @see com.ibm.g11n.pipeline.resfilter.ResourceFilter#write(java.io.OutputStream, java.lang.String, java.util.Collection)
115-
*/
116117
public void write(OutputStream os, String language, Bundle bundle) {
117118
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
118119
DocumentBuilder docBuilder = null;
@@ -127,18 +128,15 @@ public void write(OutputStream os, String language, Bundle bundle) {
127128
Document doc = docBuilder.newDocument();
128129

129130
Element xliff = doc.createElement(XLIFF_STRING);
130-
Attr attr = doc.createAttribute(VERSION_STRING);
131-
attr.setValue(VERSION_NUMBER_STRING);
132-
133-
Attr lang = doc.createAttribute("srcLang");
134-
lang.setValue(language);
135-
136-
xliff.setAttributeNode(attr);
137-
xliff.setAttributeNode(lang);
131+
xliff.setAttribute(VERSION_STRING, VERSION_NUMBER_STRING);
132+
xliff.setAttribute(XMLNS_STRING,XMLNS_VALUE_STRING);
133+
xliff.setAttribute(XSI_STRING,XSI_VALUE_STRING);
138134

139135
Element file = doc.createElement(FILE_STRING);
140136
file.setAttribute(ORIGINAL_STRING, GLOBAL_STRING);
141137
file.setAttribute(DATATYPE_STRING, PLAINTEXT_STRING);
138+
file.setAttribute(SOURCE_LANGUAGE_STRING, ENGLISH);
139+
file.setAttribute(TARGET_LANGUAGE_STRING, language);
142140
xliff.appendChild(file);
143141

144142
Element body = doc.createElement(BODY_STRING);
@@ -148,8 +146,11 @@ public void write(OutputStream os, String language, Bundle bundle) {
148146
Element trans_unit = doc.createElement(UNIT_STRING);
149147
trans_unit.setAttribute(ID_STRING, key.getKey());
150148
Element source = doc.createElement(SOURCE_STRING);
151-
source.setTextContent(key.getValue());
149+
source.setTextContent(key.getSrcValue());
152150
trans_unit.appendChild(source);
151+
Element target = doc.createElement(TARGET_STRING);
152+
target.setTextContent(key.getValue());
153+
trans_unit.appendChild(target);
153154
body.appendChild(trans_unit);
154155
}
155156

gp-res-filter/src/test/java/com/ibm/g11n/pipeline/resfilter/XLIFFResourceTest.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corp. 2016
2+
* Copyright IBM Corp. 2016, 2017
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616
package com.ibm.g11n.pipeline.resfilter;
1717

18+
import static org.junit.Assert.assertEquals;
1819
import static org.junit.Assert.assertTrue;
1920

2021
import java.io.File;
@@ -23,19 +24,23 @@
2324
import java.io.IOException;
2425
import java.io.InputStream;
2526
import java.io.OutputStream;
27+
import java.util.ArrayList;
2628
import java.util.Collection;
29+
import java.util.Collections;
2730
import java.util.LinkedList;
31+
import java.util.List;
2832

2933
import org.junit.Test;
3034

35+
import com.ibm.g11n.pipeline.resfilter.ResourceString.ResourceStringComparator;
36+
3137
/**
32-
* @author farhan
38+
* @author farhan, jcemmons
3339
*
3440
*/
3541
public class XLIFFResourceTest {
3642
private static final File INPUT_FILE = new File("src/test/resource/resfilter/xliff/input.xlf");
3743

38-
@SuppressWarnings("unused")
3944
private static final File EXPECTED_WRITE_FILE = new File("src/test/resource/resfilter/xliff/write-output.xlf");
4045

4146
private static final File MERGE_INPUT_1_FILE = new File("src/test/resource/resfilter/xliff/merge-input-1.xlf");
@@ -45,25 +50,27 @@ public class XLIFFResourceTest {
4550
@SuppressWarnings("unused")
4651
private static final File EXPECTED_MERGE_2_FILE = new File("src/test/resource/resfilter/xliff/merge-output-2.xlf");
4752

48-
private static Collection<ResourceString> EXPECTED_INPUT_RES_LIST;
53+
private static final Collection<ResourceString> EXPECTED_INPUT_RES_LIST;
4954

5055
static {
51-
EXPECTED_INPUT_RES_LIST = new LinkedList<ResourceString>();
56+
List<ResourceString> lst = new LinkedList<>();
57+
58+
lst.add(new ResourceString("1", "Quetzal", 1));
59+
lst.add(new ResourceString("3", "An application to manipulate and process XLIFF documents", 2));
60+
lst.add(new ResourceString("4", "XLIFF Data Manager", 3));
61+
Collections.sort(lst, new ResourceStringComparator());
62+
EXPECTED_INPUT_RES_LIST = lst;
5263

53-
EXPECTED_INPUT_RES_LIST.add(new ResourceString("1", "Quetzal", 1));
54-
EXPECTED_INPUT_RES_LIST
55-
.add(new ResourceString("3", "An application to manipulate and process XLIFF documents", 2));
56-
EXPECTED_INPUT_RES_LIST.add(new ResourceString("4", "XLIFF Data Manager", 3));
5764
}
5865

5966
private static Bundle WRITE_BUNDLE;
6067

6168
static {
6269
WRITE_BUNDLE = new Bundle();
6370

64-
WRITE_BUNDLE.addResourceString("3", "An application to manipulate and process XLIFF documents", 2);
65-
WRITE_BUNDLE.addResourceString("4", "XLIFF Data Manager", 3);
66-
WRITE_BUNDLE.addResourceString("1", "Quetzal", 1);
71+
WRITE_BUNDLE.addResourceString("3", "XLIFF 文書を編集、または処理 するアプリケーションです。", 2, null, "An application to manipulate and process XLIFF documents");
72+
WRITE_BUNDLE.addResourceString("4", "XLIFF データ・マネージャ", 3, null, "XLIFF Data Manager");
73+
WRITE_BUNDLE.addResourceString("1", "Quetzal", 1, null, "Quetzal");
6774
}
6875

6976
private static Bundle MERGE_BUNDLE;
@@ -82,24 +89,22 @@ public void testParse() throws IOException {
8289
assertTrue("The input test file <" + INPUT_FILE + "> does not exist.", INPUT_FILE.exists());
8390

8491
try (InputStream is = new FileInputStream(INPUT_FILE)) {
85-
@SuppressWarnings("unused")
8692
Bundle bundle = res.parse(is);
87-
// TODO: Not ready yet
88-
// assertEquals("ResourceStrings did not match.", EXPECTED_INPUT_RES_LIST, bundle.getResourceStrings());
93+
List<ResourceString> resStrList = new ArrayList<>(bundle.getResourceStrings());
94+
Collections.sort(resStrList, new ResourceStringComparator());
95+
assertEquals("ResourceStrings did not match.", EXPECTED_INPUT_RES_LIST, resStrList);
8996
}
9097
}
9198

92-
@SuppressWarnings("deprecation")
9399
@Test
94100
public void testWrite() throws IOException {
95101
File tempFile = File.createTempFile(this.getClass().getSimpleName(), ".xlf");
96102
tempFile.deleteOnExit();
97103

98104
try (OutputStream os = new FileOutputStream(tempFile)) {
99-
res.write(os, null, WRITE_BUNDLE);
105+
res.write(os, "ja", WRITE_BUNDLE);
100106
os.flush();
101-
// TODO: Not ready yet
102-
// assertTrue(ResourceTestUtil.compareFiles(EXPECTED_WRITE_FILE, tempFile));
107+
assertTrue(ResourceTestUtil.compareFiles(EXPECTED_WRITE_FILE, tempFile));
103108
}
104109
}
105110

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
1-
<xliff version="1.2">
2-
<file original="Graphic Example.psd"
3-
source-language="en-US" target-language="ja-JP"
4-
tool="Rainbow" datatype="photoshop">
5-
<header>
6-
<skl>
7-
<external-file uid="3BB236513BB24732" href="Graphic Example.psd.skl"/>
8-
</skl>
9-
<phase-group>
10-
<phase phase-name="extract" process-name="extraction"
11-
tool="Rainbow" date="20010926T152258Z"
12-
company-name="NeverLand Inc." job-id="123"
13-
contact-name="Peter Pan" contact-email="ppan@example.com">
14-
<note>Make sure to use the glossary I sent you yesterday.
15-
Thanks.</note>
16-
</phase>
17-
</phase-group>
18-
</header>
19-
<body>
20-
<trans-unit id="1" maxbytes="14">
21-
<source xml:lang="en-US">Quetzal</source>
22-
<target xml:lang="ja-JP">Quetzal</target>
23-
</trans-unit>
24-
<trans-unit id="3" maxbytes="114">
25-
<source xml:lang="en-US">An application to manipulate and
26-
process XLIFF documents</source>
27-
<target xml:lang="ja-JP">XLIFF 文書を編集、または処理
28-
するアプリケーションです。</target>
29-
</trans-unit>
30-
<trans-unit id="4" maxbytes="36">
31-
<source xml:lang="en-US">XLIFF Data Manager</source>
32-
<target xml:lang="ja-JP">XLIFF データ・マネージャ</target>
33-
</trans-unit>
34-
</body>
35-
</file>
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<xliff xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-strict.xsd">
3+
<file datatype="plaintext" original="g11n-pipeline" source-language="en" target-language="ja">
4+
<body>
5+
<trans-unit id="3">
6+
<source>An application to manipulate and process XLIFF documents</source>
7+
<target>XLIFF 文書を編集、または処理 するアプリケーションです。</target>
8+
</trans-unit>
9+
<trans-unit id="4">
10+
<source>XLIFF Data Manager</source>
11+
<target>XLIFF データ・マネージャ</target>
12+
</trans-unit>
13+
<trans-unit id="1">
14+
<source>Quetzal</source>
15+
<target>Quetzal</target>
16+
</trans-unit>
17+
</body>
18+
</file>
3619
</xliff>

0 commit comments

Comments
 (0)