use of java.lang.System.Logger in project OpenXLIFF by rmraya.
the class Json2Xliff method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
id = 0;
segments = new ArrayList<>();
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 encoding = params.get("srcEncoding");
String paragraph = params.get("paragraph");
String initSegmenter = params.get("srxFile");
String catalog = params.get("catalog");
String tgtLang = "";
if (targetLanguage != null) {
tgtLang = "\" target-language=\"" + targetLanguage;
}
if (paragraph == null) {
paragraphSegmentation = false;
} else {
paragraphSegmentation = paragraph.equals("yes");
}
try {
if (!paragraphSegmentation) {
segmenter = new Segmenter(initSegmenter, sourceLanguage, catalog);
}
JSONObject json = loadFile(inputFile, encoding);
parseJson(json);
if (segments.size() == 0) {
result.add(Constants.ERROR);
result.add("Nothing to translate.");
return result;
}
try (FileOutputStream out = new FileOutputStream(skeletonFile)) {
out.write(json.toString(4).getBytes(StandardCharsets.UTF_8));
}
try (FileOutputStream out = new FileOutputStream(xliffFile)) {
writeString(out, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
writeString(out, "<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(out, "<file original=\"" + inputFile + "\" source-language=\"" + sourceLanguage + tgtLang + "\" tool-id=\"" + Constants.TOOLID + "\" datatype=\"json\">\n");
writeString(out, "<header>\n");
writeString(out, " <skl>\n");
writeString(out, " <external-file href=\"" + Utils.cleanString(skeletonFile) + "\"/>\n");
writeString(out, " </skl>\n");
writeString(out, " <tool tool-version=\"" + Constants.VERSION + " " + Constants.BUILD + "\" tool-id=\"" + Constants.TOOLID + "\" tool-name=\"" + Constants.TOOLNAME + "\"/>\n");
writeString(out, "</header>\n");
writeString(out, "<?encoding " + encoding + "?>\n");
writeString(out, "<body>\n");
for (int i = 0; i < segments.size(); i++) {
writeString(out, " <trans-unit id=\"" + i + "\" xml:space=\"preserve\" approved=\"no\">\n" + " <source xml:lang=\"" + sourceLanguage + "\">" + Utils.cleanString(segments.get(i)) + "</source>\n </trans-unit>\n");
}
writeString(out, "</body>\n");
writeString(out, "</file>\n");
writeString(out, "</xliff>");
}
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(Json2Xliff.class.getName());
logger.log(Level.ERROR, "Error converting JSON 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 MSOffice2Xliff method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
inputFile = params.get("source");
String xliffFile = params.get("xliff");
skeletonFile = params.get("skeleton");
sourceLanguage = params.get("srcLang");
targetLanguage = params.get("tgtLang");
String elementSegmentation = params.get("paragraph");
String initSegmenter = params.get("srxFile");
srcEncoding = params.get("srcEncoding");
String catalog = params.get("catalog");
segnum = 0;
if (elementSegmentation == null) {
segByElement = false;
} else {
if (elementSegmentation.equals("yes")) {
segByElement = true;
} else {
segByElement = false;
}
}
try {
if (!segByElement) {
segmenter = new Segmenter(initSegmenter, sourceLanguage, catalog);
}
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(inputFile);
Element root = doc.getRootElement();
out = new FileOutputStream(xliffFile);
skel = new FileOutputStream(skeletonFile);
writeHeader();
recurse(root);
writeOut(" </body>\n </file>\n</xliff>");
out.close();
skel.close();
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(MSOffice2Xliff.class.getName());
logger.log(Level.ERROR, "Error converting MS Office 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 Xliff2json method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
String sklFile = params.get("skeleton");
String xliffFile = params.get("xliff");
String catalog = params.get("catalog");
String outputFile = params.get("backfile");
try {
loadSegments(xliffFile, catalog);
JSONObject json = Json2Xliff.loadFile(sklFile, encoding);
parseJson(json);
try (FileOutputStream out = new FileOutputStream(outputFile)) {
out.write(json.toString(4).getBytes(StandardCharsets.UTF_8));
}
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(Xliff2json.class.getName());
logger.log(Level.ERROR, "Error merging 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 Xliff2Mif 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");
catalog = params.get("catalog");
try {
File f = new File(params.get("backfile"));
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();
loadCharMap();
try (InputStreamReader input = new InputStreamReader(new FileInputStream(sklFile), StandardCharsets.UTF_8)) {
BufferedReader buffer = new BufferedReader(input);
String line = buffer.readLine();
if (line.indexOf("<MIFFile 2015>") != -1) {
useUnicode = true;
}
while (line != null) {
if (line.startsWith("%%%")) {
//
// translatable text
//
String code = line.substring(3, line.length() - 3);
Element segment = segments.get(code);
if (segment != null) {
if (segment.getAttributeValue("approved", "no").equals("yes")) {
Element target = segment.getChild("target");
if (target != null) {
process(target);
} else {
throw new UnexistentSegmentException("Missing target in segment " + code);
}
} else {
// process source
Element source = segment.getChild("source");
process(source);
}
} else {
throw new UnexistentSegmentException("Missing segment " + code);
}
} else {
//
// non translatable portion
//
writeString(line + "\n");
}
line = buffer.readLine();
}
}
output.close();
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | UnexistentSegmentException | URISyntaxException e) {
Logger logger = System.getLogger(Xliff2Mif.class.getName());
logger.log(Level.ERROR, "Error mering MIF 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 Office2Xliff method run.
public static List<String> run(Map<String, String> params) {
List<String> result = new ArrayList<>();
inputFile = params.get("source");
String xliff = params.get("xliff");
skeleton = params.get("skeleton");
String catalog = params.get("catalog");
try {
Document merged = new Document(null, "xliff", null, null);
mergedRoot = merged.getRootElement();
mergedRoot.setAttribute("version", "1.2");
mergedRoot.setAttribute("xmlns", "urn:oasis:names:tc:xliff:document:1.2");
mergedRoot.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
mergedRoot.setAttribute("xsi:schemaLocation", "urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd");
mergedRoot.addContent("\n");
try {
out = new ZipOutputStream(new FileOutputStream(skeleton));
in = new ZipInputStream(new FileInputStream(inputFile));
} catch (IOException e) {
result.add(Constants.ERROR);
if (params.get("format").equals(FileFormats.OFF)) {
result.add("Selected file is not a Microsoft Office 2007 document.");
} else {
result.add("Wrong document type.");
}
return result;
}
ZipEntry entry = null;
while ((entry = in.getNextEntry()) != null) {
if (entry.getName().matches(".*\\.[xX][mM][lL]") && !(entry.getName().matches(".*slideMaster.*") || entry.getName().matches(".*slideLayout.*") || entry.getName().matches(".*handoutMaster.*") || entry.getName().matches(".*notesMaster.*"))) {
File f = new File(entry.getName());
String name = f.getName();
File tmp = File.createTempFile(name.substring(0, name.lastIndexOf('.')), ".xml");
try (FileOutputStream output = new FileOutputStream(tmp.getAbsolutePath())) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
output.write(buf, 0, len);
}
}
if (name.equals("content.xml")) {
cleanTags(tmp.getAbsolutePath(), catalog);
}
try {
Map<String, String> table = new HashMap<>();
table.put("source", tmp.getAbsolutePath());
table.put("xliff", tmp.getAbsolutePath() + ".xlf");
table.put("skeleton", tmp.getAbsolutePath() + ".skl");
table.put("catalog", params.get("catalog"));
table.put("srcLang", params.get("srcLang"));
String tgtLang = params.get("tgtLang");
if (tgtLang != null) {
table.put("tgtLang", tgtLang);
}
table.put("srcEncoding", params.get("srcEncoding"));
table.put("paragraph", params.get("paragraph"));
table.put("srxFile", params.get("srxFile"));
table.put("format", params.get("format"));
List<String> res = null;
if (params.get("format").equals(FileFormats.OFF)) {
res = MSOffice2Xliff.run(table);
if (tmp.getName().indexOf("slide") != -1) {
isPPTX = true;
}
} else {
res = Xml2Xliff.run(table);
}
if (Constants.SUCCESS.equals(res.get(0))) {
if (countSegments(tmp.getAbsolutePath() + ".xlf") > 0) {
updateXliff(tmp.getAbsolutePath() + ".xlf", entry.getName());
addFile(tmp.getAbsolutePath() + ".xlf");
ZipEntry content = new ZipEntry(entry.getName() + ".skl");
content.setMethod(ZipEntry.DEFLATED);
out.putNextEntry(content);
try (FileInputStream input = new FileInputStream(tmp.getAbsolutePath() + ".skl")) {
byte[] array = new byte[1024];
int len;
while ((len = input.read(array)) > 0) {
out.write(array, 0, len);
}
out.closeEntry();
}
} else {
saveEntry(entry, tmp.getAbsolutePath());
}
File skl = new File(tmp.getAbsolutePath() + ".skl");
Files.delete(Paths.get(skl.toURI()));
File xlf = new File(tmp.getAbsolutePath() + ".xlf");
Files.delete(Paths.get(xlf.toURI()));
} else {
saveEntry(entry, tmp.getAbsolutePath());
}
} catch (IOException e) {
// do nothing
saveEntry(entry, tmp.getAbsolutePath());
}
Files.delete(Paths.get(tmp.toURI()));
} else {
// not an XML file
File tmp = File.createTempFile("zip", ".tmp");
try (FileOutputStream output = new FileOutputStream(tmp.getAbsolutePath())) {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
output.write(buf, 0, len);
}
}
saveEntry(entry, tmp.getAbsolutePath());
Files.delete(Paths.get(tmp.toURI()));
}
}
try {
in.close();
out.close();
} catch (IOException e) {
result.add(Constants.ERROR);
if (params.get("format").equals(FileFormats.OFF)) {
result.add("Selected file is not a Microsoft Office 2007 document.");
} else {
result.add("Wrong document type.");
}
return result;
}
if (params.get("format").equals(FileFormats.OFF) && isPPTX) {
sortSlides();
}
// output final XLIFF
XMLOutputter outputter = new XMLOutputter();
mergedRoot.addContent("\n");
outputter.preserveSpace(true);
try (FileOutputStream output = new FileOutputStream(xliff)) {
outputter.output(merged, output);
}
result.add(Constants.SUCCESS);
} catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
Logger logger = System.getLogger(Office2Xliff.class.getName());
logger.log(Level.ERROR, "Error converting Office file", e);
result.add(Constants.ERROR);
result.add(e.getMessage());
}
return result;
}
Aggregations