Search in sources :

Example 76 with StringValue

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

the class PermissionsFunctionModeConversionTest method modeToOctal.

/**
 * Test of eval method, of class PermissionsFunctions.
 */
@Test
public void modeToOctal() throws XPathException {
    final XQueryContext mckContext = EasyMock.createMock(XQueryContext.class);
    final PermissionsFunction permissionsFunctions = new PermissionsFunction(mckContext, PermissionsFunction.FNS_MODE_TO_OCTAL);
    Sequence[] args = { new StringValue("rwxr-x---") };
    final Sequence result = permissionsFunctions.eval(args, null);
    assertEquals(1, result.getItemCount());
    assertEquals("0750", result.itemAt(0).toString());
}
Also used : XQueryContext(org.exist.xquery.XQueryContext) Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue) Test(org.junit.Test)

Example 77 with StringValue

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

the class FileReadUnicode method eval.

@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
    if (!context.getSubject().hasDbaRole()) {
        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 inputPath = args[0].getStringValue();
    final Path file = FileModuleHelper.getFile(inputPath);
    final Charset encoding;
    if (args.length == 2) {
        encoding = Charset.forName(args[1].getStringValue());
    } else {
        encoding = StandardCharsets.UTF_8;
    }
    try (final UnicodeReader reader = new UnicodeReader(Files.newInputStream(file), encoding.name());
        final StringWriter sw = new StringWriter()) {
        char[] buf = new char[1024];
        int len;
        while ((len = reader.read(buf)) > 0) {
            sw.write(buf, 0, len);
        }
        return new StringValue(sw.toString());
    } catch (final IOException e) {
        throw new XPathException(this, e);
    }
}
Also used : Path(java.nio.file.Path) StringWriter(java.io.StringWriter) XPathException(org.exist.xquery.XPathException) Charset(java.nio.charset.Charset) IOException(java.io.IOException) StringValue(org.exist.xquery.value.StringValue)

Example 78 with StringValue

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

the class Collations method eval.

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final ValueSequence result = new ValueSequence();
    final Locale[] locales = Collator.getAvailableLocales();
    for (Locale locale : locales) {
        String language = locale.getLanguage();
        if (locale.getCountry().length() > 0) {
            language += '-' + locale.getCountry();
        }
        result.add(new StringValue(language));
    }
    return result;
}
Also used : Locale(java.util.Locale) ValueSequence(org.exist.xquery.value.ValueSequence) StringValue(org.exist.xquery.value.StringValue)

Example 79 with StringValue

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

the class GetSequenceType method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    final Sequence seq = args[0];
    final StringValue stringValue = new StringValue(Type.getTypeName(seq.getItemType()));
    return stringValue;
}
Also used : Sequence(org.exist.xquery.value.Sequence) StringValue(org.exist.xquery.value.StringValue)

Example 80 with StringValue

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

the class CollectionName 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 (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    final Item item = args[0].itemAt(0);
    if (item.getType() == Type.JAVA_OBJECT) {
        final Object o = ((JavaObjectValue) item).getObject();
        if (!(o instanceof Collection)) {
            throw new XPathException(this, "Passed Java object should be of type org.xmldb.api.base.Collection");
        }
        final Collection collection = (Collection) o;
        try {
            return new StringValue(collection.getName());
        } catch (final XMLDBException e) {
            throw new XPathException(this, "Failed to retrieve collection name", e);
        }
    } else if (Type.subTypeOf(item.getType(), Type.STRING)) {
        final String path = item.getStringValue();
        try {
            final XmldbURI uri = XmldbURI.xmldbUriFor(path).removeLastSegment();
            return new StringValue(uri.toString());
        } catch (final URISyntaxException e) {
            throw new XPathException(this, "Illegal URI for resource path: " + path);
        }
    } else if (Type.subTypeOf(item.getType(), Type.NODE)) {
        final NodeValue node = (NodeValue) item;
        if (node.getImplementationType() == NodeValue.PERSISTENT_NODE) {
            final NodeProxy p = (NodeProxy) node;
            // TODO: use xmldbUri
            return new StringValue(p.getOwnerDocument().getCollection().getURI().toString());
        }
    } else {
        throw new XPathException(this, "First argument to util:collection-name should be either " + "a Java object of type org.xmldb.api.base.Collection or a node; got: " + Type.getTypeName(item.getType()));
    }
    return Sequence.EMPTY_SEQUENCE;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Collection(org.xmldb.api.base.Collection) XMLDBException(org.xmldb.api.base.XMLDBException) URISyntaxException(java.net.URISyntaxException) JavaObjectValue(org.exist.xquery.value.JavaObjectValue) StringValue(org.exist.xquery.value.StringValue) NodeProxy(org.exist.dom.persistent.NodeProxy) XmldbURI(org.exist.xmldb.XmldbURI)

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