Search in sources :

Example 11 with Logger

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

the class Xliff2Html 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");
    encoding = params.get("encoding");
    try {
        catalog = new Catalog(params.get("catalog"));
        loadEntities();
        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 (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("%%%\n") + 4);
                        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);
                        }
                    }
                // end while
                } else {
                    // 
                    // non translatable portion
                    // 
                    writeString(line);
                }
            }
            output.close();
        }
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
        Logger logger = System.getLogger(Xliff2Html.class.getName());
        logger.log(Level.ERROR, "Error merging HTML 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) Catalog(com.maxprograms.xml.Catalog) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 12 with Logger

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

the class Properties2Xliff 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 elementSegmentation = params.get("paragraph");
    String catalog = params.get("catalog");
    if (elementSegmentation == null) {
        segByElement = false;
    } else {
        if (elementSegmentation.equals("yes")) {
            segByElement = true;
        } else {
            segByElement = false;
        }
    }
    source = "";
    try {
        if (!segByElement) {
            String initSegmenter = params.get("srxFile");
            segmenter = new Segmenter(initSegmenter, sourceLanguage, catalog);
        }
        FileInputStream stream = new FileInputStream(inputFile);
        try (InputStreamReader input = new InputStreamReader(stream, srcEncoding)) {
            BufferedReader buffer = new BufferedReader(input);
            output = new FileOutputStream(xliffFile);
            String tgtLang = "";
            if (targetLanguage != null) {
                tgtLang = "\" target-language=\"" + targetLanguage;
            }
            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=\"javapropertyresourcebundle\">\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");
            skeleton = new FileOutputStream(skeletonFile);
            String line;
            while ((line = buffer.readLine()) != null) {
                if (line.isBlank()) {
                    // no text in this line
                    // segment separator
                    writeSkeleton(line + "\n");
                } else if (line.trim().startsWith("#")) {
                    // this line is a comment
                    // send to skeleton
                    writeSkeleton(line + "\n");
                } else {
                    String tmp = line;
                    if (line.endsWith("\\")) {
                        do {
                            line = buffer.readLine();
                            tmp += "\n" + line;
                        } while (line != null && line.endsWith("\\"));
                    }
                    int index = tmp.indexOf('=');
                    if (index != -1) {
                        String key = tmp.substring(0, index + 1);
                        writeSkeleton(key);
                        source = tmp.substring(index + 1);
                        writeSegment();
                        writeSkeleton("\n");
                    } else {
                        // this line may be wrong, send to skeleton
                        // and continue
                        writeSkeleton(tmp);
                    }
                }
            }
            skeleton.close();
            writeString("</body>\n");
            writeString("</file>\n");
            writeString("</xliff>");
        }
        output.close();
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
        Logger logger = System.getLogger(Properties2Xliff.class.getName());
        logger.log(Level.ERROR, "Error converting .properties file", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) Segmenter(com.maxprograms.segmenter.Segmenter) 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) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 13 with Logger

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

the class Xliff2Properties 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");
    encoding = params.get("encoding");
    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 (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) {
                            Element target = segment.getChild("target");
                            Element source = segment.getChild("source");
                            if (target != null) {
                                if (segment.getAttributeValue("approved", "no").equalsIgnoreCase("yes")) {
                                    writeString(extractText(target));
                                } else {
                                    writeString(extractText(source));
                                }
                            } else {
                                writeString(extractText(source));
                            }
                        } else {
                            result.add(Constants.ERROR);
                            MessageFormat mf = new MessageFormat("Segment {0} not found.");
                            result.add(mf.format(new Object[] { code }));
                            return result;
                        }
                        index = line.indexOf("%%%");
                        if (index == -1) {
                            writeString(line);
                        }
                    }
                // end while
                } else {
                    // 
                    // non translatable portion
                    // 
                    writeString(line);
                }
            }
        }
        output.close();
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
        Logger logger = System.getLogger(Xliff2Properties.class.getName());
        logger.log(Level.ERROR, "Error merging .properties file.", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) MessageFormat(java.text.MessageFormat) 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) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File)

Example 14 with Logger

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

the class Html2Xliff 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");
    srcEncoding = params.get("srcEncoding");
    catalog = params.get("catalog");
    String paragraphSegmentation = params.get("paragraph");
    if (paragraphSegmentation == null) {
        segByElement = false;
    } else {
        if (paragraphSegmentation.equals("yes")) {
            segByElement = true;
        } else {
            segByElement = false;
        }
    }
    try {
        if (!segByElement) {
            String initSegmenter = params.get("srxFile");
            segmenter = new Segmenter(initSegmenter, sourceLanguage, catalog);
        }
        try (FileInputStream input = new FileInputStream(inputFile)) {
            skeleton = new FileOutputStream(skeletonFile);
            output = new FileOutputStream(xliffFile);
            writeHeader();
            int size = input.available();
            byte[] array = new byte[size];
            if (size != input.read(array)) {
                result.add(Constants.ERROR);
                result.add("Error reading from input file.");
                return result;
            }
            String file = new String(array, srcEncoding);
            buildTables();
            buildList(file);
            processList();
            skeleton.close();
            writeString("</body>\n");
            writeString("</file>\n");
            writeString("</xliff>");
            output.close();
        }
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException | URISyntaxException e) {
        Logger logger = System.getLogger(Html2Xliff.class.getName());
        logger.log(Level.ERROR, "Error converting HTML file", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Segmenter(com.maxprograms.segmenter.Segmenter) 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) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 15 with Logger

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

the class Xliff2Idml method run.

public static List<String> run(Map<String, String> params) {
    List<String> result = new ArrayList<>();
    String xliffFile = params.get("xliff");
    String outputFile = params.get("backfile");
    filesTable = new HashMap<>();
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(xliffFile);
        Element root = doc.getRootElement();
        List<Element> files = root.getChildren("file");
        Iterator<Element> it = files.iterator();
        while (it.hasNext()) {
            saveFile(it.next(), xliffFile);
        }
        String skeleton = params.get("skeleton");
        if (isEmbedded) {
            File t = new File(skeleton);
            t.deleteOnExit();
        }
        try (ZipInputStream in = new ZipInputStream(new FileInputStream(skeleton))) {
            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 (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f))) {
                ZipEntry entry = null;
                while ((entry = in.getNextEntry()) != null) {
                    if (entry.getName().matches(".*Story_.*\\.xml\\.skl")) {
                        String name = entry.getName().substring(0, entry.getName().lastIndexOf(".skl"));
                        File tmp = new File(filesTable.get(name) + ".skl");
                        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);
                            }
                        }
                        Map<String, String> table = new HashMap<>();
                        table.put("xliff", filesTable.get(name));
                        table.put("backfile", filesTable.get(name) + ".xml");
                        table.put("catalog", params.get("catalog"));
                        table.put("skeleton", filesTable.get(name) + ".skl");
                        table.put("encoding", params.get("encoding"));
                        table.put("IDML", "true");
                        List<String> res = Xliff2Xml.run(table);
                        if (!Constants.SUCCESS.equals(res.get(0))) {
                            return res;
                        }
                        ZipEntry content = new ZipEntry(name);
                        content.setMethod(ZipEntry.DEFLATED);
                        out.putNextEntry(content);
                        try (FileInputStream input = new FileInputStream(filesTable.get(name) + ".xml")) {
                            byte[] buf = new byte[1024];
                            int len;
                            while ((len = input.read(buf)) > 0) {
                                out.write(buf, 0, len);
                            }
                            out.closeEntry();
                        }
                        File xml = new File(filesTable.get(name) + ".xml");
                        Files.delete(Paths.get(xml.toURI()));
                        File xlf = new File(filesTable.get(name));
                        Files.delete(Paths.get(xlf.toURI()));
                        tmp.deleteOnExit();
                    } else {
                        File tmp = File.createTempFile("entry", ".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);
                            }
                        }
                        ZipEntry content = new ZipEntry(entry.getName());
                        content.setMethod(ZipEntry.DEFLATED);
                        out.putNextEntry(content);
                        try (FileInputStream input = new FileInputStream(tmp.getAbsolutePath())) {
                            byte[] buf = new byte[1024];
                            int len;
                            while ((len = input.read(buf)) > 0) {
                                out.write(buf, 0, len);
                            }
                            out.closeEntry();
                        }
                        tmp.deleteOnExit();
                    }
                }
            }
        }
        if (isEmbedded) {
            File f1 = new File(skeleton);
            Files.delete(Paths.get(f1.toURI()));
        }
        result.add(Constants.SUCCESS);
    } catch (IOException | SAXException | ParserConfigurationException e) {
        Logger logger = System.getLogger(Xliff2Idml.class.getName());
        logger.log(Level.ERROR, "Error merging IDML file", e);
        result.add(Constants.ERROR);
        result.add(e.getMessage());
    }
    return result;
}
Also used : SAXBuilder(com.maxprograms.xml.SAXBuilder) HashMap(java.util.HashMap) Element(com.maxprograms.xml.Element) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(com.maxprograms.xml.Document) Logger(java.lang.System.Logger) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) ZipInputStream(java.util.zip.ZipInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) 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