Search in sources :

Example 21 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project android-async-http by loopj.

the class SaxAsyncHttpResponseHandler method getResponseData.

/**
     * Deconstructs response into given content handler
     *
     * @param entity returned HttpEntity
     * @return deconstructed response
     * @throws java.io.IOException if there is problem assembling SAX response from stream
     * @see cz.msebera.android.httpclient.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, getCharset());
                rssReader.parse(new InputSource(inputStreamReader));
            } catch (SAXException e) {
                AsyncHttpClient.log.e(LOG_TAG, "getResponseData exception", e);
            } catch (ParserConfigurationException e) {
                AsyncHttpClient.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 22 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project siena by mandubian.

the class SelectHandler method request.

private <T extends Response> T request(TreeMap<String, String> parameters, BasicHandler<T> handler) {
    signParams(METHOD, HOST, PATH, awsSecretAccessKey, parameters);
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(false);
        SAXParser parser = factory.newSAXParser();
        URL url = new URL(PROTOCOL + "://" + HOST + PATH);
        URLConnection connection = url.openConnection();
        connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded;charset=" + ENCODING);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        OutputStream os = connection.getOutputStream();
        // System.out.println(PROTOCOL+"://"+HOST+PATH+"?"+query(parameters));
        os.write(query(parameters).getBytes(ENCODING));
        os.close();
        parser.parse(connection.getInputStream(), handler);
        return handler.response;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : OutputStream(java.io.OutputStream) SAXParser(javax.xml.parsers.SAXParser) URL(java.net.URL) URLConnection(java.net.URLConnection) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 23 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project spring-security-oauth by spring-projects.

the class GoogleServiceImpl method getLastTenPicasaPictureURLs.

public List<String> getLastTenPicasaPictureURLs() {
    //    byte[] bytes = getGoogleRestTemplate().getForObject(URI.create("https://picasaweb.google.com/data/feed/api/user/default"), byte[].class);
    byte[] bytes = getGoogleRestTemplate().getForObject(URI.create("https://picasaweb.google.com/data/feed/api/user/default?kind=photo&max-results=10"), byte[].class);
    InputStream photosXML = new ByteArrayInputStream(bytes);
    final List<String> photoUrls = new ArrayList<String>();
    SAXParserFactory parserFactory = SAXParserFactory.newInstance();
    parserFactory.setValidating(false);
    parserFactory.setXIncludeAware(false);
    parserFactory.setNamespaceAware(true);
    try {
        SAXParser parser = parserFactory.newSAXParser();
        parser.parse(photosXML, new DefaultHandler() {

            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                if ("http://search.yahoo.com/mrss/".equals(uri) && "thumbnail".equalsIgnoreCase(localName)) {
                    int width = 0;
                    try {
                        width = Integer.parseInt(attributes.getValue("width"));
                        if (width > 100 && width < 200) {
                            //just do the thumbnails that are between 100 and 200 px...
                            photoUrls.add(attributes.getValue("url"));
                        }
                    } catch (NumberFormatException e) {
                    //fall through...
                    }
                }
            }
        });
        return photoUrls;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e);
    } catch (SAXException e) {
        throw new IllegalStateException(e);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 24 with SAXParserFactory

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

the class MultiCipherStreamsTest method testSaxParserWithMultiCipherTransformer.

public void testSaxParserWithMultiCipherTransformer(List<CipherSpec> cipherSuites) throws Exception {
    String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<database version=\"1\">\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "	<databaseVersions>\n" + "		<databaseVersion>\n" + "		</databaseVersion>\n" + "	</databaseVersions>\n" + "</database>";
    Transformer cipherTransformer = new CipherTransformer(cipherSuites, masterKey);
    // Test encrypt
    byte[] encryptedData = doEncrypt(StringUtil.toBytesUTF8(xmlStr), cipherTransformer);
    // Test decrypt with SAX parser	
    InputStream is = cipherTransformer.createInputStream(new ByteArrayInputStream(encryptedData));
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    saxParser.parse(is, new DefaultHandler());
// Success if it does not throw an exception
// Regular CipherInputStream does NOT work with GCM mode
// GcmCompatibleCipherInputStream fixes this!
// See http://bouncy-castle.1462172.n4.nabble.com/Using-AES-GCM-NoPadding-with-javax-crypto-CipherInputStream-td4655271.html
// and http://bouncy-castle.1462172.n4.nabble.com/using-GCMBlockCipher-with-CipherInputStream-td4655147.html
}
Also used : CipherTransformer(org.syncany.chunk.CipherTransformer) Transformer(org.syncany.chunk.Transformer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CipherTransformer(org.syncany.chunk.CipherTransformer) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 25 with SAXParserFactory

use of javax.xml.parsers.SAXParserFactory in project asciidoctor-fopub by asciidoctor.

the class InputHandler method getXMLReader.

private XMLReader getXMLReader() throws ParserConfigurationException, SAXException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://xml.org/sax/features/namespaces", true);
    spf.setFeature("http://apache.org/xml/features/xinclude", true);
    XMLReader xr = spf.newSAXParser().getXMLReader();
    return xr;
}
Also used : XMLReader(org.xml.sax.XMLReader) 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