Search in sources :

Example 6 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class ParseCQL method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    Sequence ret = Sequence.EMPTY_SEQUENCE;
    if (args[0].isEmpty())
        return Sequence.EMPTY_SEQUENCE;
    String query = args[0].getStringValue();
    String output = "XCQL";
    if (!args[1].isEmpty())
        output = args[1].getStringValue();
    try {
        CQLParser parser = new CQLParser(CQLParser.V1POINT2);
        // String local_full_query_string = query;
        // local_full_query_string = local_full_query_string.replace("-", "%2D");
        CQLNode query_cql = parser.parse(query);
        if (output.equals(OutputTypeXCQL)) {
            String xmlContent = query_cql.toXCQL();
            if (xmlContent.isEmpty()) {
                return Sequence.EMPTY_SEQUENCE;
            }
            StringReader reader = new StringReader(xmlContent);
            SAXAdapter adapter = new SAXAdapter(context);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setNamespaceAware(true);
            InputSource src = new InputSource(reader);
            SAXParser saxParser = factory.newSAXParser();
            XMLReader xr = saxParser.getXMLReader();
            xr.setContentHandler(adapter);
            xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
            xr.parse(src);
            ret = (DocumentImpl) adapter.getDocument();
        } else if (output.equals(OutputTypeString)) {
            ret = new StringValue(query_cql.toString());
        } else {
            ret = new StringValue(query_cql.toCQL());
        }
        return ret;
    } catch (CQLParseException e) {
        throw new XPathException(this, "An error occurred while parsing the query expression (CQLParseException): " + e.getMessage(), e);
    } catch (SAXException e) {
        throw new XPathException(this, "Error while parsing XML: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new XPathException(this, "An error occurred while parsing the query expression (IOException): " + e.getMessage(), e);
    } catch (ParserConfigurationException e) {
        throw new XPathException(this, "Error while constructing XML parser: " + e.getMessage(), e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathException(org.exist.xquery.XPathException) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) CQLParser(org.z3950.zing.cql.CQLParser) CQLNode(org.z3950.zing.cql.CQLNode) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StringValue(org.exist.xquery.value.StringValue) CQLParseException(org.z3950.zing.cql.CQLParseException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 7 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class DocTest method asInMemoryDocument.

private Either<DocumentImpl, org.exist.dom.persistent.DocumentImpl> asInMemoryDocument(final String doc) throws XPathException {
    try {
        final SAXAdapter saxAdapter = new SAXAdapter();
        final SAXParser saxParser = saxParserFactory.newSAXParser();
        final XMLReader xmlReader = saxParser.getXMLReader();
        xmlReader.setContentHandler(saxAdapter);
        xmlReader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, saxAdapter);
        try (final Reader reader = new StringReader(doc)) {
            xmlReader.parse(new InputSource(reader));
        }
        return Left(saxAdapter.getDocument());
    } catch (final ParserConfigurationException | SAXException | IOException e) {
        throw new XPathException("Unable to parse document", e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) XPathException(org.exist.xquery.XPathException) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) SAXParser(javax.xml.parsers.SAXParser) XMLReader(org.xml.sax.XMLReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 8 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class RewriteConfig method parseConfig.

private Document parseConfig(final Path file) throws ParserConfigurationException, SAXException, IOException {
    try (final InputStream is = new BufferedInputStream(Files.newInputStream(file))) {
        final InputSource src = new InputSource(is);
        final XMLReaderPool parserPool = urlRewrite.getBrokerPool().getParserPool();
        XMLReader xr = null;
        try {
            xr = parserPool.borrowXMLReader();
            final SAXAdapter adapter = new SAXAdapter();
            xr.setContentHandler(adapter);
            xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
            xr.parse(src);
            return adapter.getDocument();
        } finally {
            if (xr != null) {
                parserPool.returnXMLReader(xr);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SAXAdapter(org.exist.dom.memtree.SAXAdapter) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader)

Example 9 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class XIncludeFilter method parseExternal.

private Either<ResourceError, org.exist.dom.memtree.DocumentImpl> parseExternal(final URI externalUri) throws ParserConfigurationException, SAXException {
    try {
        final URLConnection con = externalUri.toURL().openConnection();
        if (con instanceof HttpURLConnection) {
            final HttpURLConnection httpConnection = (HttpURLConnection) con;
            if (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return Either.Left(new ResourceError("XInclude: unable to retrieve from URI: " + externalUri.toString() + ", server returned response code: " + httpConnection.getResponseCode()));
            }
        }
        // we use eXist's in-memory DOM implementation
        final XMLReaderPool parserPool = serializer.broker.getBrokerPool().getParserPool();
        XMLReader reader = null;
        try (final InputStream is = con.getInputStream()) {
            final InputSource src = new InputSource(is);
            reader = parserPool.borrowXMLReader();
            final SAXAdapter adapter = new SAXAdapter();
            reader.setContentHandler(adapter);
            reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
            reader.parse(src);
            final org.exist.dom.memtree.DocumentImpl doc = adapter.getDocument();
            doc.setDocumentURI(externalUri.toString());
            return Either.Right(doc);
        } finally {
            if (reader != null) {
                parserPool.returnXMLReader(reader);
            }
        }
    } catch (final IOException e) {
        return Either.Left(new ResourceError("XInclude: unable to retrieve and parse document from URI: " + externalUri.toString(), e));
    }
}
Also used : InputSource(org.xml.sax.InputSource) HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) SAXAdapter(org.exist.dom.memtree.SAXAdapter) IOException(java.io.IOException) XMLReaderPool(org.exist.util.XMLReaderPool) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) XMLReader(org.xml.sax.XMLReader)

Example 10 with SAXAdapter

use of org.exist.dom.memtree.SAXAdapter in project exist by eXist-db.

the class XMLTestRunner method parse.

private Document parse(final Path path) throws ParserConfigurationException, IOException, SAXException {
    final InputSource src = new InputSource(path.toUri().toASCIIString());
    final SAXParser parser = SAX_PARSER_FACTORY.newSAXParser();
    final XMLReader xr = parser.getXMLReader();
    xr.setFeature("http://xml.org/sax/features/external-general-entities", false);
    xr.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    xr.setFeature(FEATURE_SECURE_PROCESSING, true);
    // we have to use eXist-db's SAXAdapter, otherwise un-referenced namespaces as used by xpath assertions may be stripped by Xerces.
    final SAXAdapter adapter = new SAXAdapter();
    xr.setContentHandler(adapter);
    xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
    xr.parse(src);
    return adapter.getDocument();
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) SAXAdapter(org.exist.dom.memtree.SAXAdapter) XMLReader(org.xml.sax.XMLReader)

Aggregations

SAXAdapter (org.exist.dom.memtree.SAXAdapter)22 InputSource (org.xml.sax.InputSource)18 XMLReader (org.xml.sax.XMLReader)17 IOException (java.io.IOException)13 SAXException (org.xml.sax.SAXException)12 StringReader (java.io.StringReader)10 SAXParser (javax.xml.parsers.SAXParser)10 SAXParserFactory (javax.xml.parsers.SAXParserFactory)7 NodeImpl (org.exist.dom.memtree.NodeImpl)6 ExistSAXParserFactory (org.exist.util.ExistSAXParserFactory)6 XMLReaderPool (org.exist.util.XMLReaderPool)6 XPathException (org.exist.xquery.XPathException)6 InputStream (java.io.InputStream)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 Document (org.w3c.dom.Document)4 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 Either (com.evolvedbinary.j8fu.Either)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 DocumentImpl (org.exist.dom.memtree.DocumentImpl)2