use of org.exist.dom.INode in project exist by eXist-db.
the class FunName method eval.
@Override
public Sequence eval(Sequence contextSequence, final 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 (contextItem != null) {
contextSequence = contextItem.toSequence();
}
// If we have one argument, we take it into account
final Sequence seq;
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");
}
final Sequence result;
if (seq.isEmpty()) {
result = StringValue.EMPTY_STRING;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
if (n instanceof INode) {
result = new StringValue(((INode) n).getQName().getStringValue());
} else {
result = StringValue.EMPTY_STRING;
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.dom.INode in project exist by eXist-db.
the class NodePath2 method addNode.
public void addNode(final Node node, @Nullable final Attributes saxAttribs) {
assert node instanceof Element;
super.addComponent(((INode) node).getQName());
if (n_pos == attribs.length) {
// extend the array
attribs = Arrays.copyOf(attribs, n_pos + 1);
}
Map<String, String> amap = null;
if (saxAttribs != null && saxAttribs.getLength() > 0) {
amap = new HashMap<>(saxAttribs.getLength());
for (int i = 0; i < saxAttribs.getLength(); ++i) {
amap.put(saxAttribs.getQName(i), saxAttribs.getValue(i));
}
} else {
final NamedNodeMap nnm = node.getAttributes();
if (nnm != null && nnm.getLength() > 0) {
amap = new HashMap<>(nnm.getLength());
for (int i = 0; i < nnm.getLength(); ++i) {
final Node child = nnm.item(i);
if (child.getNodeType() == Node.ATTRIBUTE_NODE) {
amap.put(child.getNodeName(), child.getNodeValue());
}
}
}
}
attribs[n_pos++] = amap;
}
use of org.exist.dom.INode in project exist by eXist-db.
the class FunNodeName method eval.
@Override
public Sequence eval(Sequence contextSequence, final 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 (contextItem != null) {
contextSequence = contextItem.toSequence();
}
// If we have one argument, we take it into account
final Sequence seq;
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");
}
final Sequence result;
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
// Returns an expanded-QName for node kinds that can have names.
if (n instanceof INode) {
final QName qn = ((INode) n).getQName();
if (qn.equals(QName.EMPTY_QNAME)) {
result = Sequence.EMPTY_SEQUENCE;
} else {
result = new QNameValue(context, qn);
}
// For other kinds of nodes it returns the empty sequence.
} else {
result = Sequence.EMPTY_SEQUENCE;
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
Aggregations