Search in sources :

Example 11 with TranslationException

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

the class XMLStreamReader method getEscChar.

private int getEscChar() throws TranslationException {
    // look for amp, lt, gt, apos, quot and &#
    clearCache();
    int cp = getNextCharCache();
    StringBuilder val = new StringBuilder();
    boolean hex = false;
    if (cp == '#') {
        // char code
        cp = getNextCharCache();
        if (cp == 'x' || cp == 'X') {
            cp = getNextCharCache();
            hex = true;
        }
    } else if (cp == ' ') {
        // an ampersand occured by itself - illegal format, but accept
        // anyways
        revertToCached();
        return 0;
    }
    int ctr = 0;
    while (cp != ';') {
        val.appendCodePoint(cp);
        if (cp == 0) {
            throw new TranslationException(OStrings.getString("XSR_ERROR_UNTERMINATED_ESCAPE_CHAR"));
        }
        cp = getNextCharCache();
        if (ctr++ > 13) {
            // appears to be literal char because close for escape
            // sequence not found
            // be lenient and accept the literal '&'
            // rewind stream
            revertToCached();
            return 0;
        }
    }
    // didn't detect an error so assume everything is OK
    clearCache();
    String valString = val.toString();
    if (valString.equals("amp")) {
        return '&';
    } else if (valString.equals("lt")) {
        return '<';
    } else if (valString.equals("gt")) {
        return '>';
    } else if (valString.equals("apos")) {
        return '\'';
    } else if (valString.equals("quot")) {
        return '"';
    } else if (entityFilter != null) {
        return entityFilter.convertToSymbol(val.toString());
    }
    // else, binary data
    if (hex) {
        try {
            cp = Integer.valueOf(valString, 16);
        } catch (NumberFormatException ex) {
            throw new TranslationException(StringUtil.format(OStrings.getString("XSR_ERROR_BAD_BINARY_CHAR"), val), ex);
        }
        if (!StringUtil.isValidXMLChar(cp)) {
            throw new TranslationException(StringUtil.format(OStrings.getString("XSR_ERROR_BAD_BINARY_CHAR"), val));
        }
    } else {
        try {
            cp = Integer.valueOf(valString, 10);
        } catch (NumberFormatException ex) {
            throw new TranslationException(StringUtil.format(OStrings.getString("XSR_ERROR_BAD_DECIMAL_CHAR"), val), ex);
        }
        if (!StringUtil.isValidXMLChar(cp)) {
            throw new TranslationException(StringUtil.format(OStrings.getString("XSR_ERROR_BAD_DECIMAL_CHAR"), val));
        }
    }
    return cp;
}
Also used : TranslationException(org.omegat.filters2.TranslationException)

Example 12 with TranslationException

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

the class ResourceBundleFilterTest method testBadUnicodeLiterals.

@Test
public void testBadUnicodeLiterals() throws Exception {
    String base = "test/data/filters/resourceBundle/";
    ResourceBundleFilter filter = new ResourceBundleFilter();
    try {
        loadSourceFiles(filter, base + "file-ResourceBundleFilter-BadLiteral1.properties");
        fail("Failed to catch invalid Unicode literal: too short");
    } catch (TranslationException ex) {
    }
    try {
        loadSourceFiles(filter, base + "file-ResourceBundleFilter-BadLiteral2.properties");
    } catch (TranslationException ex) {
        fail("Actual Java ResourceBundle loader doesn't prevent you from including characters " + "for which Character.isDefined() returns false.");
    }
    try {
        loadSourceFiles(filter, base + "file-ResourceBundleFilter-BadLiteral3.properties");
        fail("Failed to catch invalid Unicode literal: not hex code");
    } catch (TranslationException ex) {
    }
}
Also used : TranslationException(org.omegat.filters2.TranslationException) ResourceBundleFilter(org.omegat.filters2.text.bundles.ResourceBundleFilter) Test(org.junit.Test)

Example 13 with TranslationException

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

the class XLIFFFilterTest method testInvalidXMLOnWeirdPath.

/*
     * Issue reported by Jean-Christophe Helary: When a file with invalid
     * content is on a path that contains both spaces and "non-path" characters,
     * a URISyntaxException was reported about the path instead of the
     * SAXParseException about the file content.
     *
     * This may only fail with a particular underlying parser implementation, as
     * it depends on a particular codepath in
     * com.sun.org.apache.xerces.internal.impl.XMLEntityManager and
     * com.sun.org.apache.xerces.internal.util.URI where it tries to be lenient
     * in its acceptance of not-quite-valid URIs as system IDs.
     */
@Test
public void testInvalidXMLOnWeirdPath() throws Exception {
    String f = "test/data/filters/xliff/file-XLIFFFilter-invalid-content.xlf";
    File tmpDir = Files.createTempDirectory("omegat").toFile();
    assertTrue(tmpDir.isDirectory());
    // U+2603 SNOWMAN
    File weirdDir = new File(tmpDir, "a b\u2603");
    File testFile = new File(weirdDir, "file-XLIFFFilter-invalid-content.xlf");
    FileUtils.copyFile(new File(f), testFile);
    assertTrue(testFile.isFile());
    try {
        loadSourceFiles(filter, testFile.getAbsolutePath());
        fail("Should have died due to invalid XML character");
    } catch (TranslationException ex) {
        assertTrue(wasCausedBy(ex, SAXException.class));
        assertFalse(wasCausedBy(ex, URISyntaxException.class));
    }
    FileUtils.deleteDirectory(tmpDir);
}
Also used : TranslationException(org.omegat.filters2.TranslationException) File(java.io.File) Test(org.junit.Test)

Example 14 with TranslationException

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

the class XMLStreamReaderTest method testBadEntity.

@Test
public void testBadEntity() throws Exception {
    try (XMLStreamReader xml = new XMLStreamReader()) {
        xml.killEmptyBlocks();
        XMLBlock blk;
        xml.setStream(new File("test/data/xml/test-badDecimalEntity.xml"));
        assertNotNull(xml.advanceToTag("root"));
        assertNotNull(blk = xml.advanceToTag("body"));
        try {
            assertNotNull(xml.closeBlock(blk));
            fail("XML parsing should fail on bad decimal entity");
        } catch (TranslationException ex) {
        }
        xml.setStream(new File("test/data/xml/test-badHexEntity.xml"));
        assertNotNull(xml.advanceToTag("root"));
        assertNotNull(blk = xml.advanceToTag("body"));
        try {
            assertNotNull(xml.closeBlock(blk));
            fail("XML parsing should fail on bad hex entity");
        } catch (TranslationException ex) {
        }
    }
}
Also used : XMLStreamReader(org.omegat.util.xml.XMLStreamReader) XMLBlock(org.omegat.util.xml.XMLBlock) TranslationException(org.omegat.filters2.TranslationException) File(java.io.File) Test(org.junit.Test)

Example 15 with TranslationException

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

the class PdfFilter method createReader.

@Override
public BufferedReader createReader(File infile, String encoding) throws IOException, TranslationException {
    PDFTextStripper stripper;
    stripper = new PDFTextStripper();
    stripper.setLineSeparator("\n");
    stripper.setSortByPosition(true);
    try (PDDocument document = PDDocument.load(infile)) {
        String text = stripper.getText(document);
        return new BufferedReader(new StringReader(text));
    } catch (InvalidPasswordException ex) {
        Logger.getLogger(getClass().getName()).log(Level.WARNING, OStrings.getString("PDFFILTER_ENCRYPTED_FILE"), infile);
        throw new TranslationException(ex);
    }
}
Also used : PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) InvalidPasswordException(org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException) TranslationException(org.omegat.filters2.TranslationException) PDFTextStripper(org.apache.pdfbox.text.PDFTextStripper)

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