Search in sources :

Example 6 with StringValue

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

the class NodeId method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final NodeValue docNode = (NodeValue) args[0].itemAt(0);
    org.exist.numbering.NodeId nodeId;
    if (docNode.getImplementationType() == NodeValue.IN_MEMORY_NODE) {
        nodeId = ((NodeImpl) docNode).getNodeId();
    } else {
        nodeId = ((NodeProxy) docNode).getNodeId();
    }
    return new StringValue(nodeId.toString());
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) StringValue(org.exist.xquery.value.StringValue)

Example 7 with StringValue

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

the class ModuleInfo method eval.

/* (non-Javadoc)
	 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
	 */
@SuppressWarnings("unchecked")
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if ("get-module-description".equals(getSignature().getName().getLocalPart())) {
        final String uri = args[0].getStringValue();
        final Module[] modules = context.getModules(uri);
        if (isEmpty(modules)) {
            throw new XPathException(this, "No module found matching namespace URI: " + uri);
        }
        final Sequence result = new ValueSequence();
        for (final Module module : modules) {
            result.add(new StringValue(module.getDescription()));
        }
        return result;
    } else if ("is-module-registered".equals(getSignature().getName().getLocalPart())) {
        final String uri = args[0].getStringValue();
        final Module[] modules = context.getModules(uri);
        return new BooleanValue(modules != null && modules.length > 0);
    } else if ("mapped-modules".equals(getSignature().getName().getLocalPart())) {
        final ValueSequence resultSeq = new ValueSequence();
        for (final Iterator<String> i = context.getMappedModuleURIs(); i.hasNext(); ) {
            resultSeq.add(new StringValue(i.next()));
        }
        return resultSeq;
    } else if ("is-module-mapped".equals(getSignature().getName().getLocalPart())) {
        final String uri = args[0].getStringValue();
        return new BooleanValue(((Map<String, String>) context.getBroker().getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP)).get(uri) != null);
    } else if ("map-module".equals(getSignature().getName().getLocalPart())) {
        if (!context.getSubject().hasDbaRole()) {
            final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
            logger.error("Invalid user", xPathException);
            throw xPathException;
        }
        final String namespace = args[0].getStringValue();
        final String location = args[1].getStringValue();
        final Map<String, String> moduleMap = (Map<String, String>) context.getBroker().getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP);
        moduleMap.put(namespace, location);
        return Sequence.EMPTY_SEQUENCE;
    } else if ("unmap-module".equals(getSignature().getName().getLocalPart())) {
        if (!context.getSubject().hasDbaRole()) {
            final XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
            logger.error("Invalid user", xPathException);
            throw xPathException;
        }
        final String namespace = args[0].getStringValue();
        final Map<String, String> moduleMap = (Map<String, String>) context.getBroker().getConfiguration().getProperty(XQueryContext.PROPERTY_STATIC_MODULE_MAP);
        moduleMap.remove(namespace);
        return Sequence.EMPTY_SEQUENCE;
    } else if ("get-module-info".equals(getSignature().getName().getLocalPart())) {
        context.pushDocumentContext();
        try {
            final MemTreeBuilder builder = context.getDocumentBuilder();
            builder.startElement(MODULES_QNAME, null);
            if (getArgumentCount() == 1) {
                final Module[] modules = context.getModules(args[0].getStringValue());
                if (modules != null) {
                    outputModules(builder, modules);
                }
            } else {
                for (final Iterator<Module> i = context.getRootModules(); i.hasNext(); ) {
                    final Module module = i.next();
                    outputModule(builder, module);
                }
            }
            return builder.getDocument().getNode(1);
        } finally {
            context.popDocumentContext();
        }
    } else {
        final ValueSequence resultSeq = new ValueSequence();
        final XQueryContext tempContext = new XQueryContext(context.getBroker().getBrokerPool());
        for (final Iterator<Module> i = tempContext.getRootModules(); i.hasNext(); ) {
            final Module module = i.next();
            resultSeq.add(new StringValue(module.getNamespaceURI()));
        }
        if (tempContext.getRepository().isPresent()) {
            for (final URI uri : tempContext.getRepository().get().getJavaModules()) {
                resultSeq.add(new StringValue(uri.toString()));
            }
        }
        return resultSeq;
    }
}
Also used : XPathException(org.exist.xquery.XPathException) XQueryContext(org.exist.xquery.XQueryContext) ValueSequence(org.exist.xquery.value.ValueSequence) Sequence(org.exist.xquery.value.Sequence) URI(java.net.URI) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) BooleanValue(org.exist.xquery.value.BooleanValue) ValueSequence(org.exist.xquery.value.ValueSequence) Module(org.exist.xquery.Module) ExternalModule(org.exist.xquery.ExternalModule) StringValue(org.exist.xquery.value.StringValue) Map(java.util.Map)

Example 8 with StringValue

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

the class GetFragmentBetween method eval.

/**
 * Get the fragment between two elements (normally milestone elements) of a document
 *
 * @param contextSequence 1. first node (e.g. pb[10])  2. second node (e.g.: pb[11]) 3. pathCompletion:
 *                        open and closing tags before and after the fragment are appended (Default: true)
 *                        4. Display the namespace of the root node of the fragment (Default: false)
 * @return the fragment between the two nodes
 *
 * @throws XPathException in case of dynamic error
 */
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    final Node ms1Node = ((NodeValue) args[0].itemAt(0)).getNode();
    final Optional<Node> ms2Node;
    if (args[1].isEmpty()) {
        ms2Node = Optional.empty();
    } else {
        ms2Node = Optional.of(((NodeValue) args[1].itemAt(0)).getNode());
    }
    final boolean pathCompletion;
    if (args[2].isEmpty()) {
        // default
        pathCompletion = true;
    } else {
        pathCompletion = args[2].itemAt(0).atomize().effectiveBooleanValue();
    }
    final boolean displayRootNamespace;
    if (args[3].isEmpty()) {
        // default
        displayRootNamespace = false;
    } else {
        displayRootNamespace = args[3].itemAt(0).atomize().effectiveBooleanValue();
    }
    // fetch the fragment between the two milestones
    final StringBuilder fragment = getFragmentBetween(ms1Node, ms2Node);
    if (pathCompletion) {
        final String msFromPathName = getNodeXPath(ms1Node.getParentNode(), displayRootNamespace);
        final String openElementsOfMsFrom = pathName2XmlTags(msFromPathName, "open");
        final String closingElementsOfMsTo;
        if (ms2Node.isPresent()) {
            final String msToPathName = getNodeXPath(ms2Node.get().getParentNode(), displayRootNamespace);
            closingElementsOfMsTo = pathName2XmlTags(msToPathName, "close");
        } else {
            closingElementsOfMsTo = "";
        }
        fragment.insert(0, openElementsOfMsFrom);
        fragment.append(closingElementsOfMsTo);
    }
    return new StringValue(fragment.toString());
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) StoredNode(org.exist.dom.persistent.StoredNode) Node(org.w3c.dom.Node) StringValue(org.exist.xquery.value.StringValue)

Example 9 with StringValue

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

the class IndexType method eval.

/*
     * (non-Javadoc)
     * 
     * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
     *      org.exist.xquery.value.Sequence)
     */
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);
        }
    }
    Sequence result;
    if (args[0].isEmpty()) {
        result = Sequence.EMPTY_SEQUENCE;
    } else {
        final NodeSet nodes = args[0].toNodeSet();
        // Remember it is the default value when no index is defined
        if (nodes.getIndexType() == Type.ANY_TYPE) {
            result = Sequence.EMPTY_SEQUENCE;
        } else {
            result = new StringValue(Type.getTypeName(nodes.getIndexType()));
        }
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : NodeSet(org.exist.dom.persistent.NodeSet) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 10 with StringValue

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

the class XMLDBGetMimeType method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final String path = new AnyURIValue(args[0].itemAt(0).getStringValue()).toString();
    if (path.matches("^[a-z]+://.*")) {
        // external
        final MimeTable mimeTable = MimeTable.getInstance();
        final MimeType mimeType = mimeTable.getContentTypeFor(path);
        if (mimeType != null) {
            return new StringValue(mimeType.getName());
        }
    } else {
        // database
        try {
            XmldbURI pathUri = XmldbURI.xmldbUriFor(path);
            // relative collection Path: add the current base URI
            pathUri = context.getBaseURI().toXmldbURI().resolveCollectionPath(pathUri);
            // try to open the document and acquire a lock
            try (final LockedDocument lockedDoc = context.getBroker().getXMLResource(pathUri, LockMode.READ_LOCK)) {
                if (lockedDoc != null) {
                    return new StringValue(lockedDoc.getDocument().getMimeType());
                }
            }
        } catch (final Exception e) {
            logger.error(e.getMessage());
            throw new XPathException(this, e);
        }
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : MimeTable(org.exist.util.MimeTable) XPathException(org.exist.xquery.XPathException) AnyURIValue(org.exist.xquery.value.AnyURIValue) LockedDocument(org.exist.dom.persistent.LockedDocument) StringValue(org.exist.xquery.value.StringValue) MimeType(org.exist.util.MimeType) XmldbURI(org.exist.xmldb.XmldbURI) XPathException(org.exist.xquery.XPathException)

Aggregations

StringValue (org.exist.xquery.value.StringValue)96 Sequence (org.exist.xquery.value.Sequence)49 XPathException (org.exist.xquery.XPathException)40 ValueSequence (org.exist.xquery.value.ValueSequence)27 IOException (java.io.IOException)11 PermissionDeniedException (org.exist.security.PermissionDeniedException)10 Item (org.exist.xquery.value.Item)10 XQueryContext (org.exist.xquery.XQueryContext)8 Txn (org.exist.storage.txn.Txn)7 AnyURIValue (org.exist.xquery.value.AnyURIValue)7 QName (org.exist.dom.QName)6 LockException (org.exist.util.LockException)6 NodeValue (org.exist.xquery.value.NodeValue)6 Path (java.nio.file.Path)5 EXistException (org.exist.EXistException)5 TriggerException (org.exist.collections.triggers.TriggerException)5 DocumentImpl (org.exist.dom.persistent.DocumentImpl)5 StoredNode (org.exist.dom.persistent.StoredNode)5 NotificationService (org.exist.storage.NotificationService)5 MimeType (org.exist.util.MimeType)5