Search in sources :

Example 76 with XMLReader

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;
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Attributes(org.xml.sax.Attributes) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLFilterImpl(org.xml.sax.helpers.XMLFilterImpl) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 77 with XMLReader

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));
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) XMLReader(org.xml.sax.XMLReader)

Example 78 with XMLReader

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) StringReader(java.io.StringReader) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader)

Example 79 with XMLReader

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);
}
Also used : InputSource(org.xml.sax.InputSource) ExpatReader(org.apache.harmony.xml.ExpatReader) XMLReader(org.xml.sax.XMLReader)

Example 80 with XMLReader

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) {
    }
}
Also used : InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) MergeContentFilter(aQute.libg.sax.filters.MergeContentFilter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Aggregations

XMLReader (org.xml.sax.XMLReader)234 InputSource (org.xml.sax.InputSource)186 SAXException (org.xml.sax.SAXException)82 IOException (java.io.IOException)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)51 SAXSource (javax.xml.transform.sax.SAXSource)48 SAXParser (javax.xml.parsers.SAXParser)42 StringReader (java.io.StringReader)37 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)35 InputStream (java.io.InputStream)28 ExpatReader (org.apache.harmony.xml.ExpatReader)24 ContentHandler (org.xml.sax.ContentHandler)20 TransformerException (javax.xml.transform.TransformerException)19 DOMSource (javax.xml.transform.dom.DOMSource)18 StreamSource (javax.xml.transform.stream.StreamSource)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileReader (java.io.FileReader)16 InputStreamReader (java.io.InputStreamReader)12 SAXParseException (org.xml.sax.SAXParseException)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10