Search in sources :

Example 11 with NodeValue

use of org.exist.xquery.value.NodeValue in project exist by eXist-db.

the class JNDIModule method parseAttributes.

/**
 * Parses attributes into a JNDI BasicAttributes object
 *
 * @param arg				The attributes as a sequence of nodes
 * @return 					The BasicAttributes object
 */
protected static BasicAttributes parseAttributes(Sequence arg) {
    BasicAttributes attributes = new BasicAttributes();
    if (!(arg.isEmpty())) {
        Node container = ((NodeValue) arg.itemAt(0)).getNode();
        if (container != null && container.getNodeType() == Node.ELEMENT_NODE) {
            NodeList attrs = ((Element) container).getElementsByTagName("attribute");
            for (int i = 0; i < attrs.getLength(); i++) {
                Element attr = ((Element) attrs.item(i));
                String name = attr.getAttribute("name");
                String value = attr.getAttribute("value");
                String ordered = attr.getAttribute("ordered");
                if (name != null && value != null) {
                    Attribute existingAttr = attributes.get(name);
                    if (existingAttr != null) {
                        existingAttr.add(value);
                    } else {
                        attributes.put(new BasicAttribute(name, value, ordered != null && ordered.equalsIgnoreCase("true")));
                    }
                } else {
                    LOG.warn("Name or value attribute missing for attribute");
                }
            }
        }
    }
    return (attributes);
}
Also used : BasicAttribute(javax.naming.directory.BasicAttribute) BasicAttributes(javax.naming.directory.BasicAttributes) NodeValue(org.exist.xquery.value.NodeValue) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element)

Example 12 with NodeValue

use of org.exist.xquery.value.NodeValue in project exist by eXist-db.

the class FunNamespaceURI 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 = AnyURIValue.EMPTY_URI;
    } else {
        final Item item = seq.itemAt(0);
        if (!Type.subTypeOf(item.getType(), Type.NODE)) {
            throw new XPathException(this, ErrorCodes.XPTY0004, "Context item is not a node; got: " + Type.getTypeName(item.getType()));
        }
        // TODO : how to improve performance ?
        final Node n = ((NodeValue) item).getNode();
        String ns = n.getNamespaceURI();
        if (ns == null) {
            ns = XMLConstants.NULL_NS_URI;
        }
        result = new AnyURIValue(ns);
    }
    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) XPathException(org.exist.xquery.XPathException) Node(org.w3c.dom.Node) AnyURIValue(org.exist.xquery.value.AnyURIValue) Sequence(org.exist.xquery.value.Sequence)

Example 13 with NodeValue

use of org.exist.xquery.value.NodeValue 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]);
        }
    }
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) NodeValue(org.exist.xquery.value.NodeValue) NodeImpl(org.exist.dom.memtree.NodeImpl) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) QNameValue(org.exist.xquery.value.QNameValue) EmptySequence(org.exist.xquery.value.EmptySequence) Sequence(org.exist.xquery.value.Sequence) NodeProxy(org.exist.dom.persistent.NodeProxy) ElementImpl(org.exist.dom.persistent.ElementImpl)

Example 14 with NodeValue

use of org.exist.xquery.value.NodeValue in project exist by eXist-db.

the class Index method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    try {
        // Retrieve Lucene
        LuceneIndexWorker index = (LuceneIndexWorker) context.getBroker().getIndexController().getWorkerByIndexId(LuceneIndex.ID);
        if (isCalledAs("index")) {
            // Get first parameter, this is the document
            String path = args[0].itemAt(0).getStringValue();
            // Retrieve document from database
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(XmldbURI.xmldbUriFor(path), LockMode.READ_LOCK)) {
                // Verify the document actually exists
                final DocumentImpl doc = lockedDoc == null ? null : lockedDoc.getDocument();
                if (doc == null) {
                    throw new XPathException(this, "Document " + path + " does not exist.");
                }
                boolean flush = args.length == 2 || args[2].effectiveBooleanValue();
                // Note: code order is important here,
                index.setDocument(doc, ReindexMode.STORE);
                index.setMode(ReindexMode.STORE);
                // Get 'solr' node from second parameter
                NodeValue descriptor = (NodeValue) args[1].itemAt(0);
                // Pas document and index instructions to indexer
                index.indexNonXML(descriptor);
                if (flush) {
                    // Make sure things are written
                    index.writeNonXML();
                }
            }
        } else {
            // "close"
            index.writeNonXML();
        }
    } catch (Exception ex) {
        // PermissionDeniedException
        logger.error(ex.getMessage(), ex);
        throw new XPathException(this, ex);
    }
    // Return nothing [status would be nice]
    return Sequence.EMPTY_SEQUENCE;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) LockedDocument(org.exist.dom.persistent.LockedDocument) DocumentImpl(org.exist.dom.persistent.DocumentImpl) LuceneIndexWorker(org.exist.indexing.lucene.LuceneIndexWorker) XPathException(org.exist.xquery.XPathException)

Example 15 with NodeValue

use of org.exist.xquery.value.NodeValue in project exist by eXist-db.

the class AddMatch method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return args[0];
    }
    final NodeValue nv = (NodeValue) args[0].itemAt(0);
    if (!nv.isPersistentSet()) {
        return nv;
    }
    final NodeProxy node = (NodeProxy) nv;
    final int thisLevel = node.getNodeId().getTreeLevel();
    String matchStr = null;
    NodeId nodeId = null;
    try {
        for (final XMLStreamReader reader = context.getBroker().getXMLStreamReader(node, true); reader.hasNext(); ) {
            final int status = reader.next();
            if (status == XMLStreamConstants.CHARACTERS) {
                matchStr = reader.getText();
                nodeId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
                break;
            }
            final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
            final int otherLevel = otherId.getTreeLevel();
            if (status == XMLStreamConstants.END_ELEMENT && otherLevel == thisLevel) {
                // exit-for
                break;
            }
        }
    } catch (IOException | XMLStreamException e) {
        throw new XPathException(this, ErrorCodes.FOER0000, "Exception caught while reading document");
    }
    if (nodeId != null) {
        Match match = new NGramMatch(getContextId(), node.getNodeId(), matchStr);
        match.addOffset(0, matchStr.length());
        node.addMatch(match);
    }
    return node;
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XMLStreamReader(javax.xml.stream.XMLStreamReader) ExtendedXMLStreamReader(org.exist.stax.ExtendedXMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) XPathException(org.exist.xquery.XPathException) NodeId(org.exist.numbering.NodeId) NGramMatch(org.exist.indexing.ngram.NGramMatch) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) Match(org.exist.dom.persistent.Match) NGramMatch(org.exist.indexing.ngram.NGramMatch)

Aggregations

NodeValue (org.exist.xquery.value.NodeValue)52 XPathException (org.exist.xquery.XPathException)35 Sequence (org.exist.xquery.value.Sequence)21 Item (org.exist.xquery.value.Item)17 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)16 Node (org.w3c.dom.Node)15 NodeProxy (org.exist.dom.persistent.NodeProxy)11 IOException (java.io.IOException)10 QName (org.exist.dom.QName)10 Element (org.w3c.dom.Element)7 SAXException (org.xml.sax.SAXException)7 Properties (java.util.Properties)6 SequenceIterator (org.exist.xquery.value.SequenceIterator)6 DocumentBuilderReceiver (org.exist.dom.memtree.DocumentBuilderReceiver)5 BrokerPool (org.exist.storage.BrokerPool)5 DBBroker (org.exist.storage.DBBroker)5 StringValue (org.exist.xquery.value.StringValue)5 Path (java.nio.file.Path)4 NodeImpl (org.exist.dom.memtree.NodeImpl)4 Serializer (org.exist.storage.serializers.Serializer)4