Search in sources :

Example 1 with INode

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;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) INode(org.exist.dom.INode) XPathException(org.exist.xquery.XPathException) INode(org.exist.dom.INode) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 2 with INode

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;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) INode(org.exist.dom.INode) Node(org.w3c.dom.Node)

Example 3 with INode

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;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) INode(org.exist.dom.INode) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) INode(org.exist.dom.INode) Node(org.w3c.dom.Node) QNameValue(org.exist.xquery.value.QNameValue) Sequence(org.exist.xquery.value.Sequence)

Aggregations

INode (org.exist.dom.INode)3 Node (org.w3c.dom.Node)3 XPathException (org.exist.xquery.XPathException)2 Item (org.exist.xquery.value.Item)2 NodeValue (org.exist.xquery.value.NodeValue)2 Sequence (org.exist.xquery.value.Sequence)2 QName (org.exist.dom.QName)1 QNameValue (org.exist.xquery.value.QNameValue)1 StringValue (org.exist.xquery.value.StringValue)1 Element (org.w3c.dom.Element)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1