Search in sources :

Example 81 with XMLReader

use of org.xml.sax.XMLReader in project bnd by bndtools.

the class TestSAXFilters method testSelectionFilter.

public void testSelectionFilter() throws Exception {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ElementSelectionFilter filter = new ElementSelectionFilter() {

        @Override
        protected boolean select(int depth, String uri, String localName, String qName, Attributes attribs) {
            return !"e".equals(qName);
        }
    };
    XMLReader reader = SAXUtil.buildPipeline(new StreamResult(output), filter);
    reader.parse(new InputSource(new ByteArrayInputStream(SAMPLE4.getBytes())));
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root c=\"3\"><a><b><c><d/></c></b></a></root>", output.toString());
}
Also used : InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) Attributes(org.xml.sax.Attributes) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ElementSelectionFilter(aQute.libg.sax.filters.ElementSelectionFilter) XMLReader(org.xml.sax.XMLReader)

Example 82 with XMLReader

use of org.xml.sax.XMLReader in project bnd by bndtools.

the class TestSAXFilters method testDontRepeatProcessingInstruction.

public void testDontRepeatProcessingInstruction() throws Exception {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    MergeContentFilter merger = new MergeContentFilter();
    XMLReader reader = SAXUtil.buildPipeline(new StreamResult(output), merger);
    reader.parse(new InputSource(new ByteArrayInputStream(SAMPLE5.getBytes())));
    reader.parse(new InputSource(new ByteArrayInputStream(SAMPLE5.getBytes())));
    merger.closeRootAndDocument();
    String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><?xml-stylesheet type='text/xsl' href='http://www2.osgi.org/www/obr2html.xsl'?><root><a/><a/></root>";
    assertEquals(expected, stripLineBreaks(output.toString()));
}
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)

Example 83 with XMLReader

use of org.xml.sax.XMLReader in project tika by apache.

the class XWPFEventBasedWordExtractor method handlePart.

private void handlePart(PackagePart packagePart, XWPFListManager xwpfListManager, StringBuilder buffer) throws IOException, SAXException {
    Map<String, String> hyperlinks = loadHyperlinkRelationships(packagePart);
    try (InputStream stream = packagePart.getInputStream()) {
        XMLReader reader = SAXHelper.newXMLReader();
        reader.setContentHandler(new OOXMLWordAndPowerPointTextHandler(new XWPFToTextContentHandler(buffer), hyperlinks));
        reader.parse(new InputSource(new CloseShieldInputStream(stream)));
    } catch (ParserConfigurationException e) {
        LOG.warn("Can't configure XMLReader", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) OOXMLWordAndPowerPointTextHandler(org.apache.tika.parser.microsoft.ooxml.OOXMLWordAndPowerPointTextHandler) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream) InputStream(java.io.InputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 84 with XMLReader

use of org.xml.sax.XMLReader in project sling by apache.

the class SlingTransformer method createTransformerHandler.

private TransformerHandler createTransformerHandler() throws Exception {
    SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler();
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setContentHandler(templatesHandler);
    InputSource inputSource = new InputSource(getXsltSource());
    xmlReader.parse(inputSource);
    // Create transformer handler
    final TransformerHandler handler = transformerFactory.newTransformerHandler(templatesHandler.getTemplates());
    return handler;
}
Also used : InputSource(org.xml.sax.InputSource) TransformerHandler(javax.xml.transform.sax.TransformerHandler) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) XMLReader(org.xml.sax.XMLReader)

Example 85 with XMLReader

use of org.xml.sax.XMLReader in project sling by apache.

the class XplBuilder method build.

public Step build(Reader xplReader) throws Exception {
    XMLReader xMLReader = XMLReaderFactory.createXMLReader();
    XplHandler xplHandler = new XplHandler();
    xMLReader.setContentHandler(xplHandler);
    InputSource inputSource = new InputSource(xplReader);
    xMLReader.parse(inputSource);
    return xplHandler.getRootStep();
}
Also used : InputSource(org.xml.sax.InputSource) XMLReader(org.xml.sax.XMLReader)

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