Skip to content

Commit

Permalink
Move body from getNodes() to getTransUnitsMap()
Browse files Browse the repository at this point in the history
  • Loading branch information
André Laugks committed Oct 17, 2024
1 parent 829e366 commit a765f3a
Showing 1 changed file with 20 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,29 @@ public XliffDocument(Document document) {
}

public Map<String, String> getTransUnitsMap(String transUnitName, List<String> transUnitIdentifiers) {
Map<String, String> transUnitMap = new HashMap<>();

if (this.isXliffDocument()) {
return this.getNodes(
this.root.getElementsByTagName(transUnitName),
transUnitIdentifiers
);
NodeList nodeList = this.root.getElementsByTagName(transUnitName);

for (int item = 0; item < nodeList.getLength(); item++) {
Element node = (Element) nodeList.item(item);
Arrays.stream(transUnitIdentifiers.toArray())
.map(attributeName -> this.getAttributeValue(
node.getAttributes().getNamedItem(attributeName.toString())
))
.filter(code -> (code != null && !code.isEmpty()))
.findFirst()
.ifPresent(code -> transUnitMap.put(
code,
this.getCharacterDataFromElement(
node.getElementsByTagName("target").item(0).getFirstChild()
)
));
}
}

return new HashMap<>();
return transUnitMap;
}

public String getXliffVersion() {
Expand All @@ -48,28 +63,6 @@ private boolean isXliffDocument() {
return root.getNodeName().equals("xliff");
}

private Map<String, String> getNodes(NodeList nodeList, List<String> transUnitIdentifiers) {
Map<String, String> transUnitMap = new HashMap<>();

for (int item = 0; item < nodeList.getLength(); item++) {
Element node = (Element) nodeList.item(item);
Arrays.stream(transUnitIdentifiers.toArray())
.map(attributeName -> this.getAttributeValue(
node.getAttributes().getNamedItem(attributeName.toString())
))
.filter(code -> (code != null && !code.isEmpty()))
.findFirst()
.ifPresent(code -> transUnitMap.put(
code,
this.getCharacterDataFromElement(
node.getElementsByTagName("target").item(0).getFirstChild()
)
));
}

return transUnitMap;
}

private String getCharacterDataFromElement(Node child) {
if (child.getNextSibling() != null) {
return child.getNextSibling().getTextContent().trim();
Expand Down

0 comments on commit a765f3a

Please sign in to comment.