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