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;
}
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);
}
}
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);
}
}
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
}
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;
}
Aggregations