use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class DynamicCommentConstructor method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
if (newDocumentContext) {
context.pushDocumentContext();
}
Sequence result;
try {
final Sequence contentSeq = content.eval(contextSequence, contextItem);
if (contentSeq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final MemTreeBuilder builder = context.getDocumentBuilder();
context.proceed(this, builder);
final StringBuilder buf = new StringBuilder();
for (final SequenceIterator i = Atomize.atomize(contentSeq).iterate(); i.hasNext(); ) {
context.proceed(this, builder);
final Item next = i.nextItem();
if (buf.length() > 0) {
buf.append(' ');
}
buf.append(next.toString());
}
if (buf.indexOf("--") != Constants.STRING_NOT_FOUND || buf.toString().endsWith("-")) {
throw new XPathException(this, ErrorCodes.XQDY0072, "'" + buf.toString() + "' is not a valid comment");
}
final int nodeNr = builder.comment(buf.toString());
result = builder.getDocument().getNode(nodeNr);
}
} finally {
if (newDocumentContext) {
context.popDocumentContext();
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class DynamicTextConstructor method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
if (context.getProfiler().isEnabled()) {
context.getProfiler().start(this);
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
if (contextSequence != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
}
if (contextItem != null) {
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
}
}
if (newDocumentContext) {
context.pushDocumentContext();
}
Sequence result;
try {
final Sequence contentSeq = content.eval(contextSequence, contextItem);
if (contentSeq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final MemTreeBuilder builder = context.getDocumentBuilder();
context.proceed(this, builder);
final StringBuilder buf = new StringBuilder();
for (final SequenceIterator i = Atomize.atomize(contentSeq).iterate(); i.hasNext(); ) {
context.proceed(this, builder);
final Item next = i.nextItem();
if (buf.length() > 0) {
buf.append(' ');
}
buf.append(next.toString());
}
// such a text node will be deleted or merged with another text node.
if (!newDocumentContext && buf.length() == 0) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final int nodeNr = builder.characters(buf);
result = builder.getDocument().getNode(nodeNr);
}
}
} finally {
if (newDocumentContext) {
context.popDocumentContext();
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class ModuleUtils method inputSourceToXML.
/**
* Takes a InputSource of XML and Creates an XML Node from it using SAX in the
* context of the query
*
* @param context
* The Context of the calling XQuery
* @param src
* The InputSource of XML
*
* @throws SAXException in case of a SAX error
* @throws IOException in case of error reading input source
* @return The NodeValue of XML
*/
private static NodeValue inputSourceToXML(XQueryContext context, SAXSource src) throws SAXException, IOException {
if (src.getXMLReader() == null) {
throw new SAXException("No XML Reader specified.");
}
final XMLReader reader = src.getXMLReader();
context.pushDocumentContext();
try {
// try and construct xml document from input stream, we use eXist's
// in-memory DOM implementation
// TODO : we should be able to cope with context.getBaseURI()
final MemTreeBuilder builder = context.getDocumentBuilder();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
reader.setContentHandler(receiver);
reader.parse(src.getInputSource());
final Document doc = receiver.getDocument();
return ((NodeValue) doc);
} finally {
context.popDocumentContext();
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class ModuleUtils method inputSourceToXML.
/**
* Takes a InputSource of XML and Creates an XML Node from it using SAX in the
* context of the query
*
* @param context
* The Context of the calling XQuery
* @param inputSource
* The InputSource of XML
*
* @return The NodeValue of XML
* @throws SAXException in case of a SAX error
* @throws IOException in case of error reading input source
*/
public static NodeValue inputSourceToXML(XQueryContext context, InputSource inputSource) throws SAXException, IOException {
context.pushDocumentContext();
XMLReader reader = null;
try {
// try and construct xml document from input stream, we use eXist's
// in-memory DOM implementation
reader = context.getBroker().getBrokerPool().getParserPool().borrowXMLReader();
LOG.debug("Parsing XML response ...");
// TODO : we should be able to cope with context.getBaseURI()
final MemTreeBuilder builder = context.getDocumentBuilder();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
reader.setContentHandler(receiver);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", receiver);
reader.parse(inputSource);
final Document doc = receiver.getDocument();
// return (NodeValue)doc.getDocumentElement();
return ((NodeValue) doc);
} finally {
context.popDocumentContext();
if (reader != null) {
context.getBroker().getBrokerPool().getParserPool().returnXMLReader(reader);
}
}
}
use of org.exist.dom.memtree.MemTreeBuilder in project exist by eXist-db.
the class RestXqServiceImpl method parseAsXml.
private DocumentImpl parseAsXml(final InputStream is) {
DocumentImpl result = null;
XMLReader reader = null;
try {
// try and construct xml document from input stream, we use eXist's in-memory DOM implementation
// we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread
final InputSource src = new InputSource(new CloseShieldInputStream(is));
reader = getBrokerPool().getParserPool().borrowXMLReader();
final MemTreeBuilder builder = new MemTreeBuilder();
builder.startDocument();
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
reader.setContentHandler(receiver);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", receiver);
reader.parse(src);
builder.endDocument();
final Document doc = receiver.getDocument();
result = (DocumentImpl) doc;
} catch (final SAXException | IOException saxe) {
// do nothing, we will default to trying to return a string below
} finally {
if (reader != null) {
getBrokerPool().getParserPool().returnXMLReader(reader);
}
}
return result;
}
Aggregations