Search in sources :

Example 96 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jdk8u_jdk by JetBrains.

the class SupplementaryChars method testInvalid.

@Test(dataProvider = "unsupported", expectedExceptions = SAXParseException.class)
public void testInvalid(String xml) throws Exception {
    ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    getParser().parse(stream, new DefaultHandler());
    stream.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.testng.annotations.Test)

Example 97 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jdk8u_jdk by JetBrains.

the class TestCase method main.

public static void main(String[] args) throws Exception {
    File testcases = new File(System.getProperty("test.src", "."), "testcases");
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    DefaultHandler dh = new VmIdentifierTestHandler();
    sp.parse(testcases, dh);
}
Also used : DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 98 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jabref by JabRef.

the class OAI2Fetcher method importOai2Entry.

/**
     * Import an entry from an OAI2 archive. The BibEntry provided has to
     * have the field OAI2_IDENTIFIER_FIELD set to the search string.
     *
     * @param key
     *            The OAI2 key to fetch from ArXiv.
     * @return The imported BibEntry or null if none.
     */
protected BibEntry importOai2Entry(String key) throws IOException, SAXException {
    /**
         * Fix for problem reported in mailing-list:
         *   https://sourceforge.net/forum/message.php?msg_id=4087158
         */
    String fixedKey = OAI2Fetcher.fixKey(key);
    String url = constructUrl(fixedKey);
    URL oai2Url = new URL(url);
    HttpURLConnection oai2Connection = (HttpURLConnection) oai2Url.openConnection();
    oai2Connection.setRequestProperty("User-Agent", "JabRef");
    /* create an empty BibEntry and set the oai2identifier field */
    BibEntry entry = new BibEntry("article");
    entry.setField(OAI2Fetcher.OAI2_IDENTIFIER_FIELD, fixedKey);
    DefaultHandler handlerBase = new OAI2Handler(entry);
    try (InputStream inputStream = oai2Connection.getInputStream()) {
        /* parse the result */
        saxParser.parse(inputStream, handlerBase);
        /* Correct line breaks and spacing */
        for (String name : entry.getFieldNames()) {
            entry.getField(name).ifPresent(content -> entry.setField(name, OAI2Handler.correctLineBreaks(content)));
        }
        if (fixedKey.matches("\\d\\d\\d\\d\\..*")) {
            entry.setField(FieldName.YEAR, "20" + fixedKey.substring(0, 2));
            int monthNumber = Integer.parseInt(fixedKey.substring(2, 4));
            Optional<Month> month = Month.getMonthByNumber(monthNumber);
            month.ifPresent(entry::setMonth);
        }
    }
    return entry;
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) Month(org.jabref.model.entry.Month) HttpURLConnection(java.net.HttpURLConnection) OAI2Handler(org.jabref.logic.importer.util.OAI2Handler) InputStream(java.io.InputStream) URL(java.net.URL) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 99 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project jabref by JabRef.

the class MrDLibImporter method isRecognizedFormat.

@Override
public boolean isRecognizedFormat(BufferedReader input) throws IOException {
    String recommendationsAsString = convertToString(input);
    // check for valid format
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        DefaultHandler handler = new DefaultHandler() {
        };
        try (InputStream stream = new ByteArrayInputStream(recommendationsAsString.getBytes())) {
            saxParser.parse(stream, handler);
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return false;
        }
    } catch (ParserConfigurationException | SAXException e) {
        LOGGER.error(e.getMessage(), e);
        return false;
    }
    return true;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 100 with DefaultHandler

use of org.xml.sax.helpers.DefaultHandler in project android_frameworks_base by ResurrectionRemix.

the class ExpatPerformanceTest method runSax.

private void runSax() throws IOException, SAXException {
    long start = System.currentTimeMillis();
    Xml.parse(newInputStream(), Xml.Encoding.UTF_8, new DefaultHandler());
    long elapsed = System.currentTimeMillis() - start;
    Log.i(TAG, "expat SAX: " + elapsed + "ms");
}
Also used : DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

DefaultHandler (org.xml.sax.helpers.DefaultHandler)139 InputStream (java.io.InputStream)61 Metadata (org.apache.tika.metadata.Metadata)59 ParseContext (org.apache.tika.parser.ParseContext)52 Test (org.junit.Test)43 Attributes (org.xml.sax.Attributes)39 SAXParser (javax.xml.parsers.SAXParser)35 SAXException (org.xml.sax.SAXException)34 ByteArrayInputStream (java.io.ByteArrayInputStream)30 SAXParserFactory (javax.xml.parsers.SAXParserFactory)25 IOException (java.io.IOException)24 Parser (org.apache.tika.parser.Parser)22 InputSource (org.xml.sax.InputSource)21 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)20 TikaInputStream (org.apache.tika.io.TikaInputStream)20 ContentHandler (org.xml.sax.ContentHandler)20 File (java.io.File)17 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)17 BodyContentHandler (org.apache.tika.sax.BodyContentHandler)16 FileInputStream (java.io.FileInputStream)14