use of java.lang.System.Logger in project OpenXLIFF by rmraya.
the class Xliff2Sdl method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
sklFile = params.get("skeleton");
xliffFile = params.get("xliff");
catalog = params.get("catalog");
String outputFile = params.get("backfile");
String type = params.get("type");
if (type == null) {
type = "sdlxliff";
}
try {
loadSegments();
loadSkeleton();
Set<String> keys = segments.keySet();
Iterator<String> it = keys.iterator();
while (it.hasNext()) {
String key = it.next();
Element unit = segments.get(key);
if (type.equals("sdlxliff")) {
if (!unit.getAttributeValue("translate", "yes").equals("no")) {
String fullid = unit.getAttributeValue("id");
char separator = fullid.indexOf('|') != -1 ? '|' : ':';
String id = fullid.substring(0, fullid.lastIndexOf(separator));
String mrk = fullid.substring(fullid.lastIndexOf(separator) + 1);
replaceTarget(id, mrk, unit.getChild("target"), unit.getAttributeValue("approved", "no").equals("yes"));
}
} else {
String fullid = unit.getAttributeValue("id");
if (fullid.indexOf('|') != -1 || fullid.indexOf(':') != -1) {
char separator = fullid.indexOf('|') != -1 ? '|' : ':';
String id = fullid.substring(0, fullid.lastIndexOf(separator));
String mrk = fullid.substring(fullid.lastIndexOf(separator) + 1);
replaceTarget(id, mrk, unit.getChild("target"), false);
}
}
}
XMLOutputter outputter = new XMLOutputter();
outputter.preserveSpace(true);
if (type.equals("sdlxliff")) {
outputter.setSkipLinefeed(true);
outputter.writeBOM(true);
}
File f = new File(outputFile);
File p = f.getParentFile();
if (p == null) {
p = new File(System.getProperty("user.dir"));
}
if (!p.exists()) {
p.mkdirs();
}
try (FileOutputStream out = new FileOutputStream(f)) {
outputter.output(doc, out);
}
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | UnexistentSegmentException | URISyntaxException e) {
Logger logger = System.getLogger(Xliff2Sdl.class.getName());
logger.log(Level.ERROR, "Error merging SDLXLIFF file", e);
result.add(Constants.ERROR);
result.add(e.getMessage());
}
return result;
}
use of java.lang.System.Logger in project OpenXLIFF by rmraya.
the class Srt2Xliff method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
segId = 0;
String inputFile = params.get("source");
String xliffFile = params.get("xliff");
String skeletonFile = params.get("skeleton");
sourceLanguage = params.get("srcLang");
String targetLanguage = params.get("tgtLang");
String srcEncoding = params.get("srcEncoding");
String tgtLang = "";
if (targetLanguage != null) {
tgtLang = "\" target-language=\"" + targetLanguage;
}
try {
output = new FileOutputStream(xliffFile);
skeleton = new FileOutputStream(skeletonFile);
writeString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writeString("<xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\" " + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + "xsi:schemaLocation=\"urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd\">\n");
writeString("<file original=\"" + inputFile + "\" source-language=\"" + sourceLanguage + tgtLang + "\" tool-id=\"" + Constants.TOOLID + "\" datatype=\"x-srt\">\n");
writeString("<header>\n");
writeString(" <skl>\n");
writeString(" <external-file href=\"" + skeletonFile + "\"/>\n");
writeString(" </skl>\n");
writeString(" <tool tool-version=\"" + Constants.VERSION + " " + Constants.BUILD + "\" tool-id=\"" + Constants.TOOLID + "\" tool-name=\"" + Constants.TOOLNAME + "\"/>\n");
writeString("</header>\n");
writeString("<?encoding " + srcEncoding + "?>\n");
writeString("<body>\n");
try (FileReader reader = new FileReader(inputFile, Charset.forName(srcEncoding))) {
try (BufferedReader buffered = new BufferedReader(reader)) {
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = buffered.readLine()) != null) {
if (line.isBlank()) {
writeSkeleton(line + '\n');
if (sb.length() > 0) {
writeSegment(sb.toString());
sb = new StringBuilder();
}
} else {
if (line.trim().matches("[0-9]+")) {
segId = Integer.valueOf(line.trim());
writeSkeleton(line + '\n');
} else if (line.contains(" --> ")) {
segTime = line;
writeSkeleton(line);
} else {
sb.append(line);
sb.append('\n');
}
}
}
if (sb.length() > 0) {
writeSegment(sb.toString());
}
}
}
writeString("</body>\n");
writeString("</file>\n");
writeString("</xliff>");
output.close();
skeleton.close();
result.add(Constants.SUCCESS);
} catch (IOException e) {
Logger logger = System.getLogger(Srt2Xliff.class.getName());
logger.log(Level.ERROR, "Error converting .srt file", e);
result.add(Constants.ERROR);
result.add(e.getMessage());
}
return result;
}
use of java.lang.System.Logger in project OpenXLIFF by rmraya.
the class Txml2Xliff method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
String inputFile = params.get("source");
String xliffFile = params.get("xliff");
String skeletonFile = params.get("skeleton");
sourceLanguage = params.get("srcLang");
targetLanguage = params.get("tgtLang");
String catalog = params.get("catalog");
srcEncoding = params.get("srcEncoding");
try {
SAXBuilder builder = new SAXBuilder();
builder.setEntityResolver(new Catalog(catalog));
Document doc = builder.build(inputFile);
Element root = doc.getRootElement();
output = new FileOutputStream(xliffFile);
writeHeader(inputFile, skeletonFile);
recurse(root);
writeEnd();
output.close();
try (FileOutputStream skl = new FileOutputStream(skeletonFile)) {
XMLOutputter outputter = new XMLOutputter();
outputter.output(doc, skl);
}
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(Txml2Xliff.class.getName());
logger.log(Level.ERROR, "Error converting TXML file", e);
result.add(Constants.ERROR);
if (e.getMessage() != null) {
result.add(e.getMessage());
} else {
result.add("Unknown error");
}
}
return result;
}
use of java.lang.System.Logger in project OpenXLIFF by rmraya.
the class FromOpenXliff method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
String xliffFile = params.get("xliff");
String sklFile = params.get("skeleton");
String outputFile = params.get("backfile");
try {
catalog = new Catalog(params.get("catalog"));
loadXliff(xliffFile);
loadSkeleton(sklFile);
Element root = skeleton.getRootElement();
recurseSkeleton(root);
if (root.getAttributeValue("version").startsWith("1")) {
restoreAttributes(root);
}
File f = new File(outputFile);
File p = f.getParentFile();
if (p == null) {
p = new File(System.getProperty("user.dir"));
}
if (!p.exists()) {
p.mkdirs();
}
if (!f.exists()) {
Files.createFile(Paths.get(f.toURI()));
}
try (FileOutputStream out = new FileOutputStream(outputFile)) {
XMLOutputter outputter = new XMLOutputter();
Indenter.indent(root, 2);
outputter.preserveSpace(true);
outputter.output(skeleton, out);
}
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(FromOpenXliff.class.getName());
logger.log(Level.ERROR, "Error merging XLIFF file.", e);
result.add(Constants.ERROR);
result.add(e.getMessage());
}
return result;
}
use of java.lang.System.Logger in project OpenXLIFF by rmraya.
the class Xliff2Srt method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
String sklFile = params.get("skeleton");
xliffFile = params.get("xliff");
try {
catalog = new Catalog(params.get("catalog"));
String outputFile = params.get("backfile");
File f = new File(outputFile);
File p = f.getParentFile();
if (p == null) {
p = new File(System.getProperty("user.dir"));
}
if (!p.exists()) {
p.mkdirs();
}
if (!f.exists()) {
Files.createFile(Paths.get(f.toURI()));
}
output = new FileOutputStream(f);
loadSegments();
try (FileReader reader = new FileReader(sklFile, StandardCharsets.UTF_8)) {
try (BufferedReader buffer = new BufferedReader(reader)) {
String line;
while ((line = buffer.readLine()) != null) {
line = line + "\n";
if (line.contains("%%%")) {
int index = line.indexOf("%%%");
while (index != -1) {
String start = line.substring(0, index);
writeString(start);
line = line.substring(index + 3);
String code = line.substring(0, line.indexOf("%%%"));
line = line.substring(line.indexOf("%%%") + 3);
Element segment = segments.get(code);
if (segment != null) {
Element target = segment.getChild("target");
Element source = segment.getChild("source");
if (target != null) {
if (segment.getAttributeValue("approved", "no").equals("yes")) {
writeString(extractText(target));
} else {
writeString(extractText(source));
}
} else {
writeString(extractText(source));
}
} else {
result.add(Constants.ERROR);
result.add("segment " + code + " not found");
return result;
}
index = line.indexOf("%%%");
if (index == -1) {
writeString(line);
}
}
} else {
writeString(line);
}
}
}
}
output.close();
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(Xliff2Srt.class.getName());
logger.log(Level.ERROR, "Error merging SRT file", e);
result.add(Constants.ERROR);
result.add(e.getMessage());
}
return result;
}
Aggregations