Search in sources :

Example 21 with Logger

use of java.lang.System.Logger in project OpenXLIFF by rmraya.

the class Jscript2xliff 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");
    String targetLanguage = params.get("tgtLang");
    String encoding = params.get("srcEncoding");
    String tgtLang = "";
    if (targetLanguage != null) {
        tgtLang = "\" target-language=\"" + targetLanguage;
    }
    try {
        try (FileInputStream stream = new FileInputStream(inputFile)) {
            try (InputStreamReader input = new InputStreamReader(stream, encoding)) {
                BufferedReader buffer = new BufferedReader(input);
                output = new FileOutputStream(xliffFile);
                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=\"javascript\">\n");
                writeString("<header>\n");
                writeString("   <skl>\n");
                writeString("      <external-file href=\"" + Utils.cleanString(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 " + encoding + "?>\n");
                writeString("<body>\n");
                skeleton = new FileOutputStream(skeletonFile);
                String line;
                String comment = "";
                while ((line = buffer.readLine()) != null) {
                    line = line + "\n";
                    comment = findComment(line);
                    if (!comment.isEmpty()) {
                        line = line.substring(0, line.indexOf(comment));
                    }
                    if (line.indexOf('\"') == -1 && line.indexOf('\'') == -1) {
                        // no text in this line
                        writeSkeleton(line + comment);
                    } else {
                        // check for strings to extract
                        int number = countQuotes(line, '\"') + countQuotes(line, '\'');
                        if (number > 0 && number % 2 == 0) {
                            // all strings closed in the same line
                            extractStrings(line);
                            writeSkeleton(comment);
                        } else {
                            // check if the line ends with "/"
                            if (line.trim().endsWith("/") && !line.trim().endsWith("//")) {
                                String nextLine = buffer.readLine();
                                if (nextLine == null) {
                                    result.add(Constants.ERROR);
                                    result.add("Unexpected end of file.");
                                    return result;
                                }
                                comment = findComment(nextLine);
                                if (!comment.isEmpty()) {
                                    nextLine = nextLine.substring(0, nextLine.indexOf(comment));
                                }
                                line = line + nextLine;
                                continue;
                            }
                            result.add(Constants.ERROR);
                            result.add("Found a string that is not properly closed.");
                            return result;
                        }
                    }
                }
                skeleton.close();
                writeString("</body>\n");
                writeString("</file>\n");
                writeString("</xliff>");
            }
        }
        output.close();
        result.add(Constants.SUCCESS);
    } catch (IOException e) {
        Logger logger = System.getLogger(Jscript2xliff.class.getName());
        logger.log(Level.ERROR, "Error converting JavaScript file.", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Logger(java.lang.System.Logger) FileInputStream(java.io.FileInputStream)

Example 22 with Logger

use of java.lang.System.Logger in project OpenXLIFF by rmraya.

the class Xliff2Wpml 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);
        recurseSkeleton(skeleton.getRootElement());
        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();
            outputter.preserveSpace(true);
            outputter.output(skeleton, out);
        }
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
        Logger logger = System.getLogger(Xliff2Wpml.class.getName());
        logger.log(Level.ERROR, "Error merging WPML file.", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : XMLOutputter(com.maxprograms.xml.XMLOutputter) 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 23 with Logger

use of java.lang.System.Logger in project OpenXLIFF by rmraya.

the class Xliff2Ts method run.

public static List<String> run(Map<String, String> params) {
    List<String> result = new ArrayList<>();
    try {
        xliffFile = params.get("xliff");
        loadSegments();
        String sklFile = params.get("skeleton");
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(sklFile);
        recurseSkl(doc.getRootElement());
        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()));
        }
        try (FileOutputStream output = new FileOutputStream(f)) {
            XMLOutputter outputter = new XMLOutputter();
            outputter.setEncoding(StandardCharsets.UTF_8);
            outputter.preserveSpace(true);
            outputter.escapeQuotes(true);
            outputter.setEmptyDoctype(true);
            outputter.output(doc, output);
        }
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException e) {
        Logger logger = System.getLogger(Xliff2Ts.class.getName());
        logger.log(Level.ERROR, "Error merging TS file.", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : XMLOutputter(com.maxprograms.xml.XMLOutputter) SAXBuilder(com.maxprograms.xml.SAXBuilder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(com.maxprograms.xml.Document) Logger(java.lang.System.Logger) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 24 with Logger

use of java.lang.System.Logger in project OpenXLIFF by rmraya.

the class Xliff2Txml 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");
    String encoding = params.get("encoding");
    String outputFile = params.get("backfile");
    try {
        loadSegments();
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(sklFile);
        Element root = doc.getRootElement();
        replaceTargets(root);
        XMLOutputter outputter = new XMLOutputter();
        outputter.preserveSpace(true);
        outputter.setEncoding(Charset.forName(encoding));
        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 output = new FileOutputStream(f)) {
            outputter.output(doc, output);
        }
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
        Logger logger = System.getLogger(Xliff2Txml.class.getName());
        logger.log(Level.ERROR, "Error merging 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) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 25 with Logger

use of java.lang.System.Logger in project OpenXLIFF by rmraya.

the class Xliff2Rc method run.

public static List<String> run(Map<String, String> params) {
    List<String> result = new ArrayList<>();
    dlgText = new HashMap<>();
    sklFile = params.get("skeleton");
    xliffFile = params.get("xliff");
    catalog = params.get("catalog");
    encoding = params.get("encoding");
    try {
        File tempFile = File.createTempFile("tempRC", ".temp");
        destTemp = tempFile.getAbsolutePath();
        output = new FileOutputStream(destTemp);
        loadSegments();
        dlgInitExists(params);
        try (InputStreamReader input = new InputStreamReader(new FileInputStream(sklFile), StandardCharsets.UTF_8)) {
            BufferedReader buffer = new BufferedReader(input);
            String line;
            while ((line = buffer.readLine()) != null) {
                line = line + "\n";
                if (line.indexOf("%%%") != -1) {
                    // 
                    // contains translatable text
                    // 
                    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) {
                            if (segment.getAttributeValue("approved", "no").equalsIgnoreCase("Yes")) {
                                Element target = segment.getChild("target");
                                if (target != null) {
                                    writeString(converDlgInit(target.getText(), code));
                                } else {
                                    throw new UnexistentSegmentException();
                                }
                            } else {
                                // process source
                                Element source = segment.getChild("source");
                                writeString(converDlgInit(source.getText(), code));
                            }
                        } else {
                            throw new UnexistentSegmentException();
                        }
                        index = line.indexOf("%%%");
                        if (index == -1) {
                            writeString(line);
                        }
                    }
                // end while
                } else {
                    writeString(line);
                }
            }
        }
        output.close();
        dlgInitLengths(params);
        Files.delete(Paths.get(tempFile.toURI()));
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | UnexistentSegmentException | URISyntaxException e) {
        Logger logger = System.getLogger(Xliff2Rc.class.getName());
        logger.log(Level.ERROR, "Error merging RC file", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) Element(com.maxprograms.xml.Element) ArrayList(java.util.ArrayList) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Logger(java.lang.System.Logger) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) UnexistentSegmentException(com.maxprograms.converters.UnexistentSegmentException) 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