Search in sources :

Example 6 with XMLReaderPool

use of org.exist.util.XMLReaderPool 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 7 with XMLReaderPool

use of org.exist.util.XMLReaderPool in project exist by eXist-db.

the class CollectionConfigurationManager method testConfiguration.

/**
 * Check the passed collection configuration. Throws an exception if errors
 * are detected in the configuration document. Note: some configuration
 * settings depend on the current environment, in particular the
 * availability of trigger or index classes.
 *
 * @param broker DBBroker
 * @param config the configuration to test
 * @throws CollectionConfigurationException if errors were detected
 */
public void testConfiguration(DBBroker broker, String config) throws CollectionConfigurationException {
    try {
        final SAXAdapter adapter = new SAXAdapter();
        final InputSource src = new InputSource(new StringReader(config));
        final XMLReaderPool parserPool = broker.getBrokerPool().getParserPool();
        XMLReader reader = null;
        try {
            reader = parserPool.borrowXMLReader();
            reader.setContentHandler(adapter);
            reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
            reader.parse(src);
        } finally {
            if (reader != null) {
                parserPool.returnXMLReader(reader);
            }
        }
        final Document doc = adapter.getDocument();
        final CollectionConfiguration conf = new CollectionConfiguration(broker.getBrokerPool());
        conf.read(broker, doc, true, null, null);
    } catch (final Exception e) {
        throw new CollectionConfigurationException(e);
    }
}
Also used : StringInputSource(org.exist.util.StringInputSource) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXAdapter(org.exist.dom.memtree.SAXAdapter) Document(org.w3c.dom.Document) BinaryDocument(org.exist.dom.persistent.BinaryDocument) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) PermissionDeniedException(org.exist.security.PermissionDeniedException) LockException(org.exist.util.LockException) EXistException(org.exist.EXistException) IOException(java.io.IOException) TriggerException(org.exist.collections.triggers.TriggerException)

Example 8 with XMLReaderPool

use of org.exist.util.XMLReaderPool in project exist by eXist-db.

the class ParsingFunctions method validate.

private ValidationReport validate(final String xmlContent, final SAXAdapter saxAdapter) throws XPathException {
    final String xml;
    if (isCalledAs("parse-xml-fragment")) {
        xml = "<" + FRAGMENT_WRAPPER_NAME + ">" + xmlContent + "</" + FRAGMENT_WRAPPER_NAME + ">";
    } else {
        xml = xmlContent;
    }
    final ValidationReport report = new ValidationReport();
    try (final StringReader reader = new StringReader(xml)) {
        final InputSource src = new InputSource(reader);
        final XMLReaderPool parserPool = context.getBroker().getBrokerPool().getParserPool();
        XMLReader xr = null;
        try {
            xr = parserPool.borrowXMLReader();
            xr.setErrorHandler(report);
            xr.setContentHandler(saxAdapter);
            xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, saxAdapter);
            xr.parse(src);
        } catch (final SAXException e) {
            logger.debug("Error while parsing XML: {}", e.getMessage(), e);
        } catch (final IOException e) {
            throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + e.getMessage(), new StringValue(xml), e);
        } finally {
            if (xr != null) {
                parserPool.returnXMLReader(xr);
            }
        }
    }
    return report;
}
Also used : InputSource(org.xml.sax.InputSource) ValidationReport(org.exist.validation.ValidationReport) StringReader(java.io.StringReader) IOException(java.io.IOException) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 9 with XMLReaderPool

use of org.exist.util.XMLReaderPool in project exist by eXist-db.

the class SystemImport method restore.

public void restore(final String username, final Object credentials, @Nullable final String newCredentials, final Path f, final RestoreListener listener) throws IOException, SAXException, AuthenticationException, ConfigurationException, PermissionDeniedException, TransactionException {
    // login
    try (final DBBroker broker = db.authenticate(username, credentials);
        final Txn transaction = broker.continueOrBeginTransaction()) {
        // set the new password
        if (newCredentials != null) {
            setAdminCredentials(broker, newCredentials);
        }
        // get the backup descriptors, can be more than one if it was an incremental backup
        final Deque<BackupDescriptor> descriptors = getBackupDescriptors(f);
        final XMLReaderPool parserPool = broker.getBrokerPool().getParserPool();
        XMLReader reader = null;
        try {
            reader = parserPool.borrowXMLReader();
            listener.started(0);
            while (!descriptors.isEmpty()) {
                final BackupDescriptor descriptor = descriptors.pop();
                final EXistInputSource is = descriptor.getInputSource();
                is.setEncoding("UTF-8");
                final SystemImportHandler handler = new SystemImportHandler(broker, transaction, descriptor, listener);
                reader.setContentHandler(handler);
                reader.parse(is);
            }
        } finally {
            listener.finished();
            if (reader != null) {
                parserPool.returnXMLReader(reader);
            }
        }
        transaction.commit();
    }
}
Also used : DBBroker(org.exist.storage.DBBroker) SystemImportHandler(org.exist.backup.restore.SystemImportHandler) EXistInputSource(org.exist.util.EXistInputSource) Txn(org.exist.storage.txn.Txn) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader)

Example 10 with XMLReaderPool

use of org.exist.util.XMLReaderPool in project exist by eXist-db.

the class DocUtils method parse.

/**
 * Utility function to parse an input stream into an in-memory DOM document.
 *
 * @param pool    The broker pool
 * @param context The XQuery context
 * @param is      The input stream to parse from
 * @return document The document that was parsed
 * @throws XPathException in case of dynamic error
 */
public static org.exist.dom.memtree.DocumentImpl parse(final BrokerPool pool, final XQueryContext context, final InputStream is) throws XPathException {
    // we use eXist's in-memory DOM implementation
    final XMLReaderPool parserPool = pool.getParserPool();
    XMLReader reader = null;
    try {
        reader = pool.getParserPool().borrowXMLReader();
        final InputSource src = new InputSource(is);
        final SAXAdapter adapter = new SAXAdapter(context);
        reader.setContentHandler(adapter);
        try {
            reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
            reader.parse(src);
        } catch (final SAXNotRecognizedException | SAXNotSupportedException e) {
            throw new XPathException("Error creating XML parser: " + e.getMessage(), e);
        } catch (final IOException | SAXException e) {
            throw new XPathException("Error while parsing XML: " + e.getMessage(), e);
        }
        return adapter.getDocument();
    } finally {
        if (reader != null) {
            parserPool.returnXMLReader(reader);
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) XPathException(org.exist.xquery.XPathException) SAXAdapter(org.exist.dom.memtree.SAXAdapter) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) XMLReaderPool(org.exist.util.XMLReaderPool) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Aggregations

XMLReaderPool (org.exist.util.XMLReaderPool)11 XMLReader (org.xml.sax.XMLReader)10 InputSource (org.xml.sax.InputSource)8 IOException (java.io.IOException)7 SAXAdapter (org.exist.dom.memtree.SAXAdapter)6 SAXException (org.xml.sax.SAXException)4 StringReader (java.io.StringReader)3 InputStream (java.io.InputStream)2 EXistInputSource (org.exist.util.EXistInputSource)2 BufferedInputStream (java.io.BufferedInputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ResultSet (java.sql.ResultSet)1 ResultSetMetaData (java.sql.ResultSetMetaData)1 SQLException (java.sql.SQLException)1 SQLRecoverableException (java.sql.SQLRecoverableException)1 SQLXML (java.sql.SQLXML)1