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