use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class TestCase method loadVarFromString.
public NodeImpl loadVarFromString(XQueryContext context, String source) throws IOException {
SAXAdapter adapter = new SAXAdapter(context);
SAXParserFactory factory = ExistSAXParserFactory.getSAXParserFactory();
factory.setNamespaceAware(true);
XMLReader xr;
try {
SAXParser parser = factory.newSAXParser();
xr = parser.getXMLReader();
xr.setContentHandler(adapter);
xr.setProperty(Namespaces.SAX_LEXICAL_HANDLER, adapter);
} catch (Exception e) {
throw new IOException(e);
}
try {
InputSource src = new InputSource(new StringReader(source));
xr.parse(src);
return (NodeImpl) adapter.getDocument();
} catch (SAXException e) {
throw new IOException(e);
}
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class ParsingFunctions method parse.
private Sequence parse(final String xmlContent, final Sequence[] args) throws XPathException {
final SAXAdapter adapter = new FragmentSAXAdapter(context, isCalledAs("parse-xml-fragment"));
final ValidationReport report = validate(xmlContent, adapter);
if (report.isValid()) {
return adapter.getDocument();
} else {
try {
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder();
final NodeImpl result = Shared.writeReport(report, builder);
throw new XPathException(this, ErrorCodes.FODC0006, ErrorCodes.FODC0006.getDescription() + ": " + report.toString(), result);
} finally {
context.popDocumentContext();
}
}
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class FunResolveQName method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) 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);
}
}
final Sequence qnameSeq = args[0];
if (qnameSeq.isEmpty()) {
return EmptySequence.EMPTY_SEQUENCE;
} else {
context.pushInScopeNamespaces();
final String qnameString = args[0].getStringValue();
if (QName.isQName(qnameString) == VALID.val) {
try {
String prefix = QName.extractPrefix(qnameString);
if (prefix == null) {
prefix = "";
}
String uri = null;
final NodeValue node = (NodeValue) args[1].itemAt(0);
if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
NodeProxy proxy = (NodeProxy) node;
final NodeSet ancestors = proxy.getAncestors(contextId, true);
for (NodeProxy ancestor : ancestors) {
proxy = ancestor;
final ElementImpl e = (ElementImpl) proxy.getNode();
uri = findNamespaceURI(e, prefix);
if (uri != null) {
break;
}
}
} else {
NodeImpl next = (NodeImpl) node;
do {
uri = findNamespaceURI((org.exist.dom.memtree.ElementImpl) next, prefix);
if (uri != null) {
break;
} else {
next = (NodeImpl) next.getParentNode();
}
} while (next != null && next.getNodeType() == Node.ELEMENT_NODE);
}
if (uri == null && prefix != null && !prefix.isEmpty()) {
throw new XPathException(this, ErrorCodes.FONS0004, "No namespace found for prefix. No binding for prefix '" + prefix + "' was found.", args[0]);
}
final String localPart = QName.extractLocalName(qnameString);
final QName qn = new QName(localPart, uri, prefix);
final QNameValue result = new QNameValue(context, qn);
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
context.popInScopeNamespaces();
return result;
} catch (final QName.IllegalQNameException e) {
throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
}
} else {
throw new XPathException(this, ErrorCodes.FOCA0002, "Invalid lexical value. '" + qnameString + "' is not a QName.", args[0]);
}
}
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class FunRoot method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, 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());
}
}
Sequence seq;
Sequence result;
Item item;
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
if (contextSequence == null || contextSequence.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
}
// If we have one argumment, we take it into account
if (getSignature().getArgumentCount() > 0) {
seq = getArgument(0).eval(contextSequence, contextItem);
} else // Otherwise, we take the context sequence and we iterate over it
{
seq = contextSequence;
}
if (seq == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
if (seq.isPersistentSet()) {
result = new ExtArrayNodeSet(seq.getItemCount());
} else {
result = new ValueSequence(seq.getItemCount());
}
for (final SequenceIterator i = seq.iterate(); i.hasNext(); ) {
item = i.nextItem();
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Item is not a node; got '" + item + "'", seq);
}
final Sequence s = item.toSequence();
if (s.isPersistentSet()) {
final NodeProxy p = s.toNodeSet().get(0);
result.add(new NodeProxy(p.getOwnerDocument()));
} else {
if (seq.hasOne() && item.getType() == Type.ATTRIBUTE) {
result.add(item);
} else if (item.getType() == Type.DOCUMENT) {
result.add((DocumentImpl) item);
} else {
result.add(((NodeImpl) item).getOwnerDocument());
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.memtree.NodeImpl in project exist by eXist-db.
the class ValueSequence method expand.
/**
* Scan the sequence and check all in-memory documents.
* They may contains references to nodes stored in the database.
* Expand those references to get a pure in-memory DOM tree.
*/
private void expand() {
final Set<DocumentImpl> docs = new HashSet<>();
for (int i = 0; i <= size; i++) {
final NodeImpl node = (NodeImpl) values[i];
final DocumentImpl ownerDoc = node.getNodeType() == Node.DOCUMENT_NODE ? (DocumentImpl) node : node.getOwnerDocument();
if (ownerDoc.hasReferenceNodes()) {
docs.add(ownerDoc);
}
}
for (final DocumentImpl doc : docs) {
doc.expand();
}
}
Aggregations