Search in sources :

Example 56 with XMLReader

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

the class FromHowTo method processFirstSheet.

public void processFirstSheet(String filename) throws Exception {
    OPCPackage pkg = OPCPackage.open(filename, PackageAccess.READ);
    try {
        XSSFReader r = new XSSFReader(pkg);
        SharedStringsTable sst = r.getSharedStringsTable();
        XMLReader parser = fetchSheetParser(sst);
        // process the first sheet
        InputStream sheet2 = r.getSheetsData().next();
        InputSource sheetSource = new InputSource(sheet2);
        parser.parse(sheetSource);
        sheet2.close();
    } finally {
        pkg.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SharedStringsTable(org.apache.poi.xssf.model.SharedStringsTable) InputStream(java.io.InputStream) OPCPackage(org.apache.poi.openxml4j.opc.OPCPackage) XMLReader(org.xml.sax.XMLReader) XSSFReader(org.apache.poi.xssf.eventusermodel.XSSFReader)

Example 57 with XMLReader

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

the class TestSAXHelper method testXMLReader.

@Test
public void testXMLReader() throws Exception {
    XMLReader reader = SAXHelper.newXMLReader();
    assertNotSame(reader, SAXHelper.newXMLReader());
    assertTrue(reader.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING));
    assertEquals(SAXHelper.IGNORING_ENTITY_RESOLVER, reader.getEntityResolver());
    assertNotNull(reader.getProperty("http://apache.org/xml/properties/security-manager"));
    reader.parse(new InputSource(new ByteArrayInputStream("<xml></xml>".getBytes("UTF-8"))));
}
Also used : InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 58 with XMLReader

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

the class SerializationTest method createXMLReader.

/**
     * Creates an XMLReader for the given content handler.
     *
     * @param handler the content handler.
     * @return an XMLReader for the given content handler.
     * @throws SAXException if the reader cannot be created.
     */
private XMLReader createXMLReader(ContentHandler handler) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setFeature("http://xml.org/sax/features/namespaces", true);
    reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
    reader.setContentHandler(handler);
    reader.setErrorHandler(new DefaultHandler());
    return reader;
}
Also used : XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 59 with XMLReader

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

the class DefaultSamplerCreator method isPotentialXml.

/**
     * Tries parsing to see if content is xml
     * @param postData String
     * @return boolean
     */
private static boolean isPotentialXml(String postData) {
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        ErrorDetectionHandler detectionHandler = new ErrorDetectionHandler();
        xmlReader.setContentHandler(detectionHandler);
        xmlReader.setErrorHandler(detectionHandler);
        xmlReader.parse(new InputSource(new StringReader(postData)));
        return !detectionHandler.isErrorDetected();
    } catch (ParserConfigurationException | SAXException | IOException e) {
        return false;
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 60 with XMLReader

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

the class XMLAssertion method getResult.

/**
     * Returns the result of the Assertion. 
     * Here it checks whether the Sample data is XML. 
     * If so an AssertionResult containing a FailureMessage will be returned. 
     * Otherwise the returned AssertionResult will reflect the success of the Sample.
     */
@Override
public AssertionResult getResult(SampleResult response) {
    // no error as default
    AssertionResult result = new AssertionResult(getName());
    String resultData = response.getResponseDataAsString();
    if (resultData.length() == 0) {
        return result.setResultForNull();
    }
    result.setFailure(false);
    XMLReader builder = XML_READER.get();
    if (builder != null) {
        try {
            builder.setErrorHandler(new LogErrorHandler());
            builder.parse(new InputSource(new StringReader(resultData)));
        } catch (SAXException | IOException e) {
            result.setError(true);
            result.setFailure(true);
            result.setFailureMessage(e.getMessage());
        }
    } else {
        result.setError(true);
        result.setFailureMessage("Cannot initialize XMLReader in element:" + getName() + ", check jmeter.log file");
    }
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) IOException(java.io.IOException) 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