Search in sources :

Example 96 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project SmartAndroidSource by jaychou2012.

the class SaxAsyncHttpResponseHandler method getResponseData.

/**
     * Deconstructs response into given content handler
     *
     * @param entity returned HttpEntity
     * @return deconstructed response
     * @throws java.io.IOException
     * @see org.apache.http.HttpEntity
     */
@Override
protected byte[] getResponseData(HttpEntity entity) throws IOException {
    if (entity != null) {
        InputStream instream = entity.getContent();
        InputStreamReader inputStreamReader = null;
        if (instream != null) {
            try {
                SAXParserFactory sfactory = SAXParserFactory.newInstance();
                SAXParser sparser = sfactory.newSAXParser();
                XMLReader rssReader = sparser.getXMLReader();
                rssReader.setContentHandler(handler);
                inputStreamReader = new InputStreamReader(instream, DEFAULT_CHARSET);
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                Log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                Log.e(LOG_TAG, "getResponseData exception", e);
            } finally {
                AsyncHttpClient.silentCloseInputStream(instream);
                if (inputStreamReader != null) {
                    try {
                        inputStreamReader.close();
                    } catch (IOException e) {
                    /*ignore*/
                    }
                }
            }
        }
    }
    return null;
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 97 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project jersey by jersey.

the class AbstractJaxbProviderTest method abstractJaxbProviderDoesNotReadExternalDtds.

@Test
public void abstractJaxbProviderDoesNotReadExternalDtds() throws Exception {
    SAXParserFactory spf = injectionManager.getInstance(SAXParserFactory.class);
    String url = "file:///no-such-file";
    String s = "<!DOCTYPE x SYSTEM '" + url + "'><x/>";
    SAXSource saxSource = AbstractJaxbProvider.getSAXSource(spf, new ByteArrayInputStream(s.getBytes("us-ascii")));
    TransformerFactory.newInstance().newTransformer().transform(saxSource, new StreamResult(new ByteArrayOutputStream()));
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 98 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project jersey by jersey.

the class SaxParserFactoryInjectionProvider method get.

@Override
public SAXParserFactory get() {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    if (!isXmlSecurityDisabled()) {
        factory = new SecureSaxParserFactory(factory);
    }
    return factory;
}
Also used : SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 99 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project languagetool by languagetool-org.

the class WikipediaQuickCheck method getRevisionContent.

private MediaWikiContent getRevisionContent(String completeWikiContent) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser;
    RevisionContentHandler handler = new RevisionContentHandler();
    try {
        saxParser = factory.newSAXParser();
        saxParser.parse(new InputSource(new StringReader(completeWikiContent)), handler);
    } catch (Exception e) {
        throw new RuntimeException("Could not parse XML: " + completeWikiContent, e);
    }
    return new MediaWikiContent(handler.getRevisionContent(), handler.getTimestamp());
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 100 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project languagetool by languagetool-org.

the class DisambiguationRuleLoader method getRules.

public final List<DisambiguationPatternRule> getRules(InputStream stream) throws ParserConfigurationException, SAXException, IOException {
    DisambiguationRuleHandler handler = new DisambiguationRuleHandler();
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    Tools.setPasswordAuthenticator();
    saxParser.parse(stream, handler);
    return handler.getDisambRules();
}
Also used : SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SAXParserFactory (javax.xml.parsers.SAXParserFactory)183 SAXParser (javax.xml.parsers.SAXParser)141 InputSource (org.xml.sax.InputSource)76 SAXException (org.xml.sax.SAXException)75 IOException (java.io.IOException)62 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 XMLReader (org.xml.sax.XMLReader)37 DefaultHandler (org.xml.sax.helpers.DefaultHandler)27 InputStream (java.io.InputStream)22 File (java.io.File)21 SAXSource (javax.xml.transform.sax.SAXSource)21 ByteArrayInputStream (java.io.ByteArrayInputStream)16 StringReader (java.io.StringReader)15 Unmarshaller (javax.xml.bind.Unmarshaller)13 Attributes (org.xml.sax.Attributes)13 JAXBContext (javax.xml.bind.JAXBContext)12 SAXParseException (org.xml.sax.SAXParseException)10 InputStreamReader (java.io.InputStreamReader)9 ArrayList (java.util.ArrayList)9 ValidationEvent (javax.xml.bind.ValidationEvent)9