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));
}
}
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);
}
}
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;
}
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();
}
}
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);
}
}
}
Aggregations