Search in sources :

Example 1 with XMLStreamReader

use of org.omegat.util.xml.XMLStreamReader in project omegat by omegat-org.

the class PreferencesXML method load.

/**
 * Loads the preferences from disk, from the specified file or, if the
 * file is null, it attempts to load from a prefs file bundled inside
 * the JAR (not supplied by default).
 */
@Override
public void load(List<String> keys, List<String> values) {
    try (XMLStreamReader xml = new XMLStreamReader()) {
        xml.killEmptyBlocks();
        if (loadFile == null) {
            // defaults. Useful for e.g. Web Start.
            try (InputStream is = getClass().getResourceAsStream(Preferences.FILE_PREFERENCES)) {
                if (is != null) {
                    xml.setStream(is);
                    readXmlPrefs(xml, keys, values);
                }
            }
        } else {
            xml.setStream(loadFile);
            readXmlPrefs(xml, keys, values);
        }
    } catch (TranslationException te) {
        // error loading preference file - keep whatever was
        // loaded then return gracefully to calling function
        // print an error to the console as an FYI
        Log.logWarningRB("PM_WARNING_PARSEERROR_ON_READ");
        Log.log(te);
        makeBackup(loadFile);
    } catch (IndexOutOfBoundsException e3) {
        // error loading preference file - keep whatever was
        // loaded then return gracefully to calling function
        // print an error to the console as an FYI
        Log.logWarningRB("PM_WARNING_PARSEERROR_ON_READ");
        Log.log(e3);
        makeBackup(loadFile);
    } catch (UnsupportedEncodingException e3) {
        // unsupported encoding - forget about it
        Log.logErrorRB(e3, "PM_UNSUPPORTED_ENCODING");
        makeBackup(loadFile);
    } catch (IOException e4) {
        // can't read file - forget about it and move on
        Log.logErrorRB(e4, "PM_ERROR_READING_FILE");
        makeBackup(loadFile);
    }
}
Also used : XMLStreamReader(org.omegat.util.xml.XMLStreamReader) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 2 with XMLStreamReader

use of org.omegat.util.xml.XMLStreamReader 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 3 with XMLStreamReader

use of org.omegat.util.xml.XMLStreamReader in project omegat by omegat-org.

the class XMLStreamReaderTest method testLoadXML.

@Test
public void testLoadXML() throws Exception {
    XMLStreamReader xml = new XMLStreamReader();
    xml.killEmptyBlocks();
    xml.setStream(new File("test/data/xml/test.xml"));
    XMLBlock blk;
    List<XMLBlock> lst;
    assertNotNull(xml.advanceToTag("root"));
    assertNotNull(blk = xml.advanceToTag("body"));
    assertEquals("foo", blk.getAttribute("attr"));
    assertNotNull(lst = xml.closeBlock(blk));
    assertEquals(25, lst.size());
    assertOpenTag(lst.get(0), "ascii");
    assertText(lst.get(1), "bar");
    assertCloseTag(lst.get(2), "ascii");
    assertOpenTag(lst.get(3), "bmp");
    // SNOWMAN
    assertText(lst.get(4), "\u2603");
    assertCloseTag(lst.get(5), "bmp");
    assertOpenTag(lst.get(6), "dec");
    // SNOWMAN
    assertText(lst.get(7), "\u2603");
    assertCloseTag(lst.get(8), "dec");
    assertOpenTag(lst.get(9), "hex");
    // SNOWMAN
    assertText(lst.get(10), "\u2603");
    assertCloseTag(lst.get(11), "hex");
    assertOpenTag(lst.get(12), "astral");
    // PLAYING CARD RED JOKER
    assertText(lst.get(13), "\uD83C\uDCBF");
    assertCloseTag(lst.get(14), "astral");
    assertOpenTag(lst.get(15), "a-dec");
    // PLAYING CARD RED JOKER
    assertText(lst.get(16), "\uD83C\uDCBF");
    assertCloseTag(lst.get(17), "a-dec");
    assertOpenTag(lst.get(18), "a-hex");
    // PLAYING CARD RED JOKER
    assertText(lst.get(19), "\uD83C\uDCBF");
    assertCloseTag(lst.get(20), "a-hex");
    assertOpenTag(lst.get(21), "named");
    assertText(lst.get(22), "&<>'\"");
    assertCloseTag(lst.get(23), "named");
    assertStandaloneTag(lst.get(24), "standalone");
    xml.close();
}
Also used : XMLStreamReader(org.omegat.util.xml.XMLStreamReader) XMLBlock(org.omegat.util.xml.XMLBlock) File(java.io.File) Test(org.junit.Test)

Aggregations

XMLStreamReader (org.omegat.util.xml.XMLStreamReader)3 File (java.io.File)2 Test (org.junit.Test)2 TranslationException (org.omegat.filters2.TranslationException)2 XMLBlock (org.omegat.util.xml.XMLBlock)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1