-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
195 additions
and
116 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2007-2022 Maxprograms. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 1.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/org/documents/epl-v10.html | ||
* | ||
* Contributors: | ||
* Maxprograms - initial API and implementation | ||
*******************************************************************************/ | ||
|
||
package com.maxprograms.swordfish.xliff; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import com.maxprograms.converters.Utils; | ||
import com.maxprograms.xml.Document; | ||
import com.maxprograms.xml.Element; | ||
import com.maxprograms.xml.SAXBuilder; | ||
import com.maxprograms.xml.XMLOutputter; | ||
|
||
import org.xml.sax.SAXException; | ||
|
||
public class Split { | ||
|
||
private Split() { | ||
// empty for security | ||
} | ||
|
||
public static List<String> split(String xliff, String outputFolder) | ||
throws SAXException, IOException, ParserConfigurationException { | ||
List<String> result = new ArrayList<>(); | ||
SAXBuilder builder = new SAXBuilder(); | ||
Document doc = builder.build(xliff); | ||
Element root = doc.getRootElement(); | ||
if (!"xliff".equals(root.getName())) { | ||
throw new IOException("Selected file is not an XLIFF document"); | ||
} | ||
File folder = new File(outputFolder); | ||
if (!folder.exists()) { | ||
Files.createDirectories(folder.toPath()); | ||
} | ||
String parentFolder = folder.getParentFile().getAbsolutePath(); | ||
Set<String> originals = new HashSet<>(); | ||
List<Element> files = root.getChildren("file"); | ||
Iterator<Element> it = files.iterator(); | ||
while (it.hasNext()) { | ||
String original = it.next().getAttributeValue("original"); | ||
if (original.isEmpty()) { | ||
throw new IOException("<file> without \"original\" attribute"); | ||
} | ||
originals.add(original); | ||
} | ||
Iterator<String> ot = originals.iterator(); | ||
XMLOutputter outputter = new XMLOutputter(); | ||
outputter.preserveSpace(true); | ||
while (ot.hasNext()) { | ||
String original = ot.next(); | ||
Set<String> skeletons = new HashSet<>(); | ||
Document newDoc = new Document(null, "xliff", null); | ||
Element newRoot = newDoc.getRootElement(); | ||
newRoot.setAttributes(root.getAttributes()); | ||
for (int i = 0; i < files.size(); i++) { | ||
Element file = files.get(i); | ||
if (file.getAttributeValue("original").equals(original)) { | ||
Element skeleton = file.getChild("skeleton"); | ||
String href = skeleton.getAttributeValue("href"); | ||
if (!skeletons.contains(href)) { | ||
skeleton.addContent(Utils.encodeFromFile(new File(href).getAbsolutePath())); | ||
skeleton.removeAttribute("href"); | ||
skeletons.add(href); | ||
} else { | ||
skeleton.setAttribute("href",Utils.makeRelativePath(parentFolder, href)); | ||
} | ||
newRoot.addContent("\n"); | ||
newRoot.addContent(file); | ||
} | ||
} | ||
newRoot.addContent("\n"); | ||
File xliffFile = new File(folder, original + ".xlf"); | ||
result.add(xliffFile.getAbsolutePath()); | ||
try (FileOutputStream out = new FileOutputStream(xliffFile)) { | ||
outputter.output(newDoc, out); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.