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