Search in sources :

Example 26 with SAXParser

use of javax.xml.parsers.SAXParser 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 27 with SAXParser

use of javax.xml.parsers.SAXParser 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 28 with SAXParser

use of javax.xml.parsers.SAXParser 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 29 with SAXParser

use of javax.xml.parsers.SAXParser in project buck by facebook.

the class PositionXmlParser method parse.

@NonNull
private static Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom, boolean checkDtd) throws ParserConfigurationException, SAXException, IOException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        if (checkDtd) {
            factory.setFeature(NAMESPACE_FEATURE, true);
            factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
            factory.setFeature(PROVIDE_XMLNS_URIS, true);
        } else {
            factory.setFeature(LOAD_EXTERNAL_DTD, false);
        }
        SAXParser parser = factory.newSAXParser();
        DomBuilder handler = new DomBuilder(xml);
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        parser.parse(input, handler);
        return handler.getDocument();
    } catch (SAXException e) {
        if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
            // Byte order mark in the string? Skip it. There are many markers
            // (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
            // just skip those up to the XML prolog beginning character, <
            //$NON-NLS-1$ //$NON-NLS-2$
            xml = xml.replaceFirst("^([\\W]+)<", "<");
            return parse(xml, new InputSource(new StringReader(xml)), false, checkDtd);
        }
        throw e;
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException) NonNull(com.android.annotations.NonNull)

Example 30 with SAXParser

use of javax.xml.parsers.SAXParser in project gocd by gocd.

the class CompositeExtractorTest method testShouldCallRespondingCallbacksWhenParseRealXml.

public void testShouldCallRespondingCallbacksWhenParseRealXml() throws Exception {
    File logXml = DataUtils.getFailedBuildLbuildAsFile().getFile();
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    com.thoughtworks.go.legacywrapper.CompositeExtractor extractor = new com.thoughtworks.go.legacywrapper.CompositeExtractor(Arrays.asList(new SAXBasedExtractor[] { new ExtractorStub() }));
    parser.parse(logXml, extractor);
    Map result = new HashMap();
    extractor.report(result);
    assertTrue(((Boolean) result.get("allCallbackWereCalled")).booleanValue());
}
Also used : HashMap(java.util.HashMap) SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9