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;
}
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) {
}
}
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);
}
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) {
}
}
}
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);
}
}
Aggregations