Search in sources :

Example 41 with Logger

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;
}
Also used : XMLOutputter(com.maxprograms.xml.XMLOutputter) Element(com.maxprograms.xml.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Logger(java.lang.System.Logger) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnexistentSegmentException(com.maxprograms.converters.UnexistentSegmentException) File(java.io.File)

Example 42 with Logger

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) Logger(java.lang.System.Logger)

Example 43 with Logger

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;
}
Also used : XMLOutputter(com.maxprograms.xml.XMLOutputter) SAXBuilder(com.maxprograms.xml.SAXBuilder) Element(com.maxprograms.xml.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Document(com.maxprograms.xml.Document) Logger(java.lang.System.Logger) Catalog(com.maxprograms.xml.Catalog) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 44 with Logger

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;
}
Also used : XMLOutputter(com.maxprograms.xml.XMLOutputter) Element(com.maxprograms.xml.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Logger(java.lang.System.Logger) Catalog(com.maxprograms.xml.Catalog) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 45 with Logger

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;
}
Also used : Element(com.maxprograms.xml.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Logger(java.lang.System.Logger) Catalog(com.maxprograms.xml.Catalog) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Aggregations

Logger (java.lang.System.Logger)58 IOException (java.io.IOException)39 ArrayList (java.util.ArrayList)38 FileOutputStream (java.io.FileOutputStream)36 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)34 SAXException (org.xml.sax.SAXException)34 URISyntaxException (java.net.URISyntaxException)28 File (java.io.File)23 Element (com.maxprograms.xml.Element)22 Document (com.maxprograms.xml.Document)17 FileInputStream (java.io.FileInputStream)16 PlatformLogger (sun.util.logging.PlatformLogger)16 SAXBuilder (com.maxprograms.xml.SAXBuilder)14 XMLOutputter (com.maxprograms.xml.XMLOutputter)14 Catalog (com.maxprograms.xml.Catalog)13 BufferedReader (java.io.BufferedReader)13 InputStreamReader (java.io.InputStreamReader)11 Segmenter (com.maxprograms.segmenter.Segmenter)7 Level (java.lang.System.Logger.Level)7 HashMap (java.util.HashMap)5