Search in sources :

Example 16 with TranslationException

use of org.omegat.filters2.TranslationException in project omegat by omegat-org.

the class Handler method endDocument.

/**
 * Receive notification of the end of the document.
 */
@Override
public void endDocument() throws SAXException {
    try {
        translateAndFlush();
        if (extWriter != null) {
            extWriter.close();
            extWriter = null;
        }
        translateAndFlush();
        currWriter().close();
    } catch (TranslationException e) {
        throw new SAXException(e);
    } catch (IOException e) {
        throw new SAXException(e);
    }
}
Also used : TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 17 with TranslationException

use of org.omegat.filters2.TranslationException in project omegat by omegat-org.

the class OpenDocFilter method processFile.

/**
 * Processes a single OpenDocument file, which is actually a ZIP file consisting of many XML files, some
 * of which should be translated.
 */
@Override
public void processFile(File inFile, File outFile, FilterContext fc) throws IOException, TranslationException {
    ZipFile zipfile = new ZipFile(inFile);
    ZipOutputStream zipout = null;
    if (outFile != null) {
        zipout = new ZipOutputStream(new FileOutputStream(outFile));
    }
    Enumeration<? extends ZipEntry> zipcontents = zipfile.entries();
    while (zipcontents.hasMoreElements()) {
        ZipEntry zipentry = zipcontents.nextElement();
        String shortname = zipentry.getName();
        if (shortname.lastIndexOf('/') >= 0) {
            shortname = shortname.substring(shortname.lastIndexOf('/') + 1);
        }
        if (TRANSLATABLE.contains(shortname)) {
            File tmpin = tmp();
            FileUtils.copyInputStreamToFile(zipfile.getInputStream(zipentry), tmpin);
            File tmpout = null;
            if (zipout != null) {
                tmpout = tmp();
            }
            try {
                createXMLFilter(processOptions).processFile(tmpin, tmpout, fc);
            } catch (Exception e) {
                zipfile.close();
                throw new TranslationException(e.getLocalizedMessage() + "\n" + OStrings.getString("OpenDoc_ERROR_IN_FILE") + inFile);
            }
            if (zipout != null) {
                ZipEntry outentry = new ZipEntry(zipentry.getName());
                outentry.setMethod(ZipEntry.DEFLATED);
                zipout.putNextEntry(outentry);
                FileUtils.copyFile(tmpout, zipout);
                zipout.closeEntry();
            }
            if (!tmpin.delete()) {
                tmpin.deleteOnExit();
            }
            if (tmpout != null) {
                if (!tmpout.delete()) {
                    tmpout.deleteOnExit();
                }
            }
        } else {
            if (zipout != null) {
                ZipEntry outentry = new ZipEntry(zipentry.getName());
                zipout.putNextEntry(outentry);
                IOUtils.copy(zipfile.getInputStream(zipentry), zipout);
                zipout.closeEntry();
            }
        }
    }
    if (zipout != null) {
        zipout.close();
    }
    zipfile.close();
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) TranslationException(org.omegat.filters2.TranslationException) File(java.io.File) ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) TranslationException(org.omegat.filters2.TranslationException)

Aggregations

TranslationException (org.omegat.filters2.TranslationException)16 IOException (java.io.IOException)7 File (java.io.File)6 Test (org.junit.Test)4 IFilter (org.omegat.filters2.IFilter)3 BufferedReader (java.io.BufferedReader)2 FileOutputStream (java.io.FileOutputStream)2 InputStream (java.io.InputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 XMLStreamReader (org.omegat.util.xml.XMLStreamReader)2 SAXException (org.xml.sax.SAXException)2 Files (gen.core.filters.Files)1 Filter (gen.core.filters.Filter)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Path (java.nio.file.Path)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXParser (javax.xml.parsers.SAXParser)1