use of org.xml.sax.XMLReader in project ddf by codice.
the class XacmlClient method addNamespaceAndPrefixes.
/**
* Adds namespaces and namespace prefixes to the XACML response returned by the XACML PDP. The
* XACML PDP returns a response with no namespaces, so we need to add them to unmarshal the
* response.
*
* @param xacmlResponse The XACML response as a string.
* @return DOM representation of the XACML response with namespaces and namespace prefixes.
* @throws PdpException
*/
private DOMResult addNamespaceAndPrefixes(String xacmlResponse) throws PdpException {
XMLReader xmlReader = null;
try {
XMLReader xmlParser = XMLReaderFactory.createXMLReader();
xmlParser.setFeature("http://xml.org/sax/features/external-general-entities", false);
xmlParser.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
xmlReader = new XMLFilterImpl(xmlParser) {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
super.startElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName, attributes);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(XACML30_NAMESPACE, localName, XACML_PREFIX + ":" + qName);
}
};
} catch (SAXException e) {
String message = "Unable to read XACML response:\n" + xacmlResponse;
LOGGER.info(message);
throw new PdpException(message, e);
}
DOMResult domResult;
ClassLoader tccl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(XacmlClient.class.getClassLoader());
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
domResult = new DOMResult();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new SAXSource(xmlReader, new InputSource(new StringReader(xacmlResponse))), domResult);
} catch (TransformerException e) {
String message = "Unable to transform XACML response:\n" + xacmlResponse;
LOGGER.info(message);
throw new PdpException(message, e);
} finally {
Thread.currentThread().setContextClassLoader(tccl);
}
return domResult;
}
use of org.xml.sax.XMLReader in project android_frameworks_base by crdroidandroid.
the class Xml method parse.
/**
* Parses xml from the given reader and fires events on the given SAX
* handler.
*/
public static void parse(Reader in, ContentHandler contentHandler) throws IOException, SAXException {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(in));
}
use of org.xml.sax.XMLReader in project android_frameworks_base by crdroidandroid.
the class Xml method parse.
/**
* Parses the given xml string and fires events on the given SAX handler.
*/
public static void parse(String xml, ContentHandler contentHandler) throws SAXException {
try {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
reader.parse(new InputSource(new StringReader(xml)));
} catch (IOException e) {
throw new AssertionError(e);
}
}
use of org.xml.sax.XMLReader in project android_frameworks_base by crdroidandroid.
the class Xml method parse.
/**
* Parses xml from the given input stream and fires events on the given SAX
* handler.
*/
public static void parse(InputStream in, Encoding encoding, ContentHandler contentHandler) throws IOException, SAXException {
XMLReader reader = new ExpatReader();
reader.setContentHandler(contentHandler);
InputSource source = new InputSource(in);
source.setEncoding(encoding.expatName);
reader.parse(source);
}
use of org.xml.sax.XMLReader in project bnd by bndtools.
the class TestSAXFilters method testMergeInconsistentRoots.
public void testMergeInconsistentRoots() throws Exception {
ByteArrayOutputStream output = new ByteArrayOutputStream();
MergeContentFilter merger = new MergeContentFilter();
XMLReader reader = SAXUtil.buildPipeline(new StreamResult(output), merger);
try {
reader.parse(new InputSource(new ByteArrayInputStream(SAMPLE1.getBytes())));
reader.parse(new InputSource(new ByteArrayInputStream(SAMPLE3.getBytes())));
fail("Should throw exception for inconsistent roots");
} catch (SAXException e) {
}
}
Aggregations