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());
}
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;
}
}
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());
}
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;
}
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;
}
Aggregations