Skip to content

Commit

Permalink
Improved conversion to XLIFF 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rmraya committed Aug 25, 2020
1 parent 817356a commit d00f62e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 15 deletions.
Binary file modified lib/openxliff.jar
Binary file not shown.
98 changes: 83 additions & 15 deletions src/com/maxprograms/xliff2/ToXliff2.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.xml.parsers.ParserConfigurationException;

import com.maxprograms.converters.Constants;
import com.maxprograms.xml.Attribute;
import com.maxprograms.xml.Catalog;
import com.maxprograms.xml.Document;
import com.maxprograms.xml.Element;
Expand Down Expand Up @@ -257,7 +258,6 @@ private static void recurse(Element source, Element target) {
space.addContent("keep");
metaGroup.addContent(space);
}

}
target.addContent(group);
target = group;
Expand Down Expand Up @@ -287,20 +287,38 @@ private static void recurse(Element source, Element target) {
unit.addContent(originalData);

Element src = source.getChild("source");
List<Element> tags = src.getChildren("ph");
List<Element> tags = src.getChildren();
Set<String> tagSet = new TreeSet<>();
for (int i = 0; i < tags.size(); i++) {
Element ph = tags.get(i);
String id = "ph" + ph.getAttributeValue("id");
if (!tagSet.contains(id)) {
Element data = new Element("data");
data.setAttribute("id", id);
data.setContent(ph.getContent());
originalData.addContent(data);
tagSet.add("ph" + ph.getAttributeValue("id"));
Element tag = tags.get(i);
if ("ph".equals(tag.getName())) {
String id = "ph" + tag.getAttributeValue("id");
if (!tagSet.contains(id)) {
Element data = new Element("data");
data.setAttribute("id", id);
data.setContent(tag.getContent());
originalData.addContent(data);
tagSet.add("ph" + tag.getAttributeValue("id"));
}
}
if ("g".equals(tag.getName())) {
String id = "g" + tag.getAttributeValue("id");
if (!tagSet.contains(id)) {
Element head = new Element("data");
head.setAttribute("id", id);
head.setText(getHead(tag));
originalData.addContent(head);
tagSet.add("g" + tag.getAttributeValue("id"));

Element tail = new Element("data");
tail.setAttribute("id", "/g" + tag.getAttributeValue("id"));
tail.setText("</g>");
originalData.addContent(tail);
tagSet.add("/g" + tag.getAttributeValue("id"));
}
}
}

Element segment = new Element("segment");
segment.setAttribute("id", source.getAttributeValue("id"));
unit.addContent(segment);
Expand Down Expand Up @@ -328,6 +346,17 @@ private static void recurse(Element source, Element target) {
ph.setAttribute("dataRef", "ph" + tag.getAttributeValue("id"));
src2.addContent(ph);
}
if (tag.getName().equals("g")) {
Element head = new Element("ph");
head.setAttribute("id", "g" + tag.getAttributeValue("id"));
head.setAttribute("dataRef", "g" + tag.getAttributeValue("id"));
src2.addContent(head);
src2.addContent(tag.getText());
Element tail = new Element("ph");
tail.setAttribute("id", "/g" + tag.getAttributeValue("id"));
tail.setAttribute("dataRef", "/g" + tag.getAttributeValue("id"));
src2.addContent(tail);
}
if (tag.getName().equals("mrk")) {
Element mrk = new Element("mrk");
mrk.setAttribute("id", "mrk" + tag.getAttributeValue("mid"));
Expand Down Expand Up @@ -373,6 +402,31 @@ private static void recurse(Element source, Element target) {
ph.setAttribute("dataRef", id);
tgt2.addContent(ph);
}
if (tag.getName().equals("g")) {
String id = "g" + tag.getAttributeValue("id");
if (!tagSet.contains(id)) {
Element head = new Element("data");
head.setAttribute("id", id);
head.setText(getHead(tag));
originalData.addContent(head);
tagSet.add("g" + tag.getAttributeValue("id"));

Element tail = new Element("data");
tail.setAttribute("id", "/g" + tag.getAttributeValue("id"));
tail.setText("</g>");
originalData.addContent(tail);
tagSet.add("/g" + tag.getAttributeValue("id"));
}
Element head = new Element("ph");
head.setAttribute("id", id);
head.setAttribute("dataRef", id);
tgt2.addContent(head);
tgt2.addContent(tag.getText());
Element tail = new Element("ph");
tail.setAttribute("id", "/g" + tag.getAttributeValue("id"));
tail.setAttribute("dataRef", "/g" + tag.getAttributeValue("id"));
tgt2.addContent(tail);
}
if (tag.getName().equals("mrk")) {
Element mrk = new Element("mrk");
mrk.setAttribute("id", "mrk" + tag.getAttributeValue("mid"));
Expand Down Expand Up @@ -421,8 +475,8 @@ private static void recurse(Element source, Element target) {
Element tag = (Element) node;
if (tag.getName().equals("ph")) {
Element ph = new Element("ph");
String id = "ph" + tag.getAttributeValue("id");
ph.setAttribute("id",id);
String id = "ph" + tag.getAttributeValue("id");
ph.setAttribute("id", id);
if (!tagSet.contains(id)) {
Element data = new Element("data");
data.setAttribute("id", id);
Expand Down Expand Up @@ -457,8 +511,8 @@ private static void recurse(Element source, Element target) {
Element tag = (Element) node;
if (tag.getName().equals("ph")) {
Element ph = new Element("ph");
String id = "ph" + tag.getAttributeValue("id");
ph.setAttribute("id",id);
String id = "ph" + tag.getAttributeValue("id");
ph.setAttribute("id", id);
if (!tagSet.contains(id)) {
Element data = new Element("data");
data.setAttribute("id", id);
Expand Down Expand Up @@ -496,4 +550,18 @@ private static void recurse(Element source, Element target) {
}
}

private static String getHead(Element e) {
StringBuilder builder = new StringBuilder();
builder.append('<');
builder.append(e.getName());
List<Attribute> atts = e.getAttributes();
Iterator<Attribute> it = atts.iterator();
while (it.hasNext()) {
Attribute a = it.next();
builder.append(' ');
builder.append(a.toString());
}
builder.append('>');
return builder.toString();
}
}

0 comments on commit d00f62e

Please sign in to comment.