use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class UserXQueryJob method executeXQuery.
private void executeXQuery(final BrokerPool pool, final DBBroker broker, final Source source, final Properties params) throws PermissionDeniedException, XPathException, JobExecutionException {
XQueryPool xqPool = null;
CompiledXQuery compiled = null;
XQueryContext context = null;
try {
// execute the xquery
final XQuery xquery = pool.getXQueryService();
xqPool = pool.getXQueryPool();
// try and get a pre-compiled query from the pool
compiled = xqPool.borrowCompiledXQuery(broker, source);
if (compiled == null) {
context = new XQueryContext(pool);
} else {
context = compiled.getContext();
context.prepareForReuse();
}
if (source instanceof DBSource) {
final XmldbURI collectionUri = ((DBSource) source).getDocumentPath().removeLastSegment();
context.setModuleLoadPath(XmldbURI.EMBEDDED_SERVER_URI.append(collectionUri.getCollectionPath()).toString());
context.setStaticallyKnownDocuments(new XmldbURI[] { collectionUri });
}
if (compiled == null) {
try {
compiled = xquery.compile(context, source);
} catch (final IOException e) {
abort("Failed to read query from " + xqueryResource);
}
}
// declare any parameters as external variables
if (params != null) {
String bindingPrefix = params.getProperty("bindingPrefix");
if (bindingPrefix == null) {
bindingPrefix = "local";
}
for (final Entry param : params.entrySet()) {
final String key = (String) param.getKey();
final String value = (String) param.getValue();
context.declareVariable(bindingPrefix + ":" + key, new StringValue(value));
}
}
xquery.execute(broker, compiled, null);
} finally {
if (context != null) {
context.runCleanupTasks();
}
// return the compiled query to the pool
if (xqPool != null && source != null && compiled != null) {
xqPool.returnCompiledXQuery(source, compiled);
}
}
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class FunctionFactory method equals.
/**
* equals(node-set, string)
*/
private static GeneralComparison equals(XQueryContext context, XQueryAST ast, PathExpr parent, List<Expression> params) throws XPathException {
if (params.size() < 2) {
throw new XPathException(ast.getLine(), ast.getColumn(), ErrorCodes.XPST0017, "Function equals() requires two or three arguments");
}
if (params.size() > 3) {
throw new XPathException(ast.getLine(), ast.getColumn(), ErrorCodes.XPST0017, "Function equals() requires two or three arguments");
}
final PathExpr p0 = (PathExpr) params.get(0);
final PathExpr p1 = (PathExpr) params.get(1);
if (p1.getLength() == 0) {
throw new XPathException(ast.getLine(), ast.getColumn(), "Second argument of equals() is empty");
}
final GeneralComparison op = new GeneralComparison(context, p0, p1, Comparison.EQ, StringTruncationOperator.EQUALS);
// TODO : not sure for parent -pb
context.getProfiler().message(parent, Profiler.OPTIMIZATIONS, "OPTIMIZATION", "Rewritten contains() as a general comparison with no truncations");
op.setLocation(ast.getLine(), ast.getColumn());
if (params.size() == 3) {
op.setCollation((Expression) params.get(2));
} else {
op.setCollation(new StringValue("?strength=identical"));
}
return op;
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class FunLocalName 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();
}
final Item item;
// the context sequence
if (getArgumentCount() > 0) {
final Sequence seq = getArgument(0).eval(contextSequence);
if (!seq.isEmpty()) {
item = seq.itemAt(0);
} else {
item = null;
}
} else {
if (contextSequence == null) {
throw new XPathException(this, ErrorCodes.XPDY0002, "Undefined context item");
}
item = contextSequence.itemAt(0);
}
final Sequence result;
if (item == null) {
result = StringValue.EMPTY_STRING;
} else {
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
final String localName = n.getLocalName();
if (localName != null) {
result = new StringValue(localName);
} else {
result = StringValue.EMPTY_STRING;
}
}
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 FunName 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 = StringValue.EMPTY_STRING;
} else {
final Item item = seq.itemAt(0);
if (!Type.subTypeOf(item.getType(), Type.NODE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "item is not a node; got '" + Type.getTypeName(item.getType()) + "'");
}
// TODO : how to improve performance ?
final Node n = ((NodeValue) item).getNode();
if (n instanceof INode) {
result = new StringValue(((INode) n).getQName().getStringValue());
} else {
result = StringValue.EMPTY_STRING;
}
}
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 FunNormalizeUnicode method eval.
public Sequence eval(Sequence contextSequence, 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();
}
Sequence result;
final Sequence s1 = getArgument(0).eval(contextSequence);
if (s1.isEmpty()) {
result = StringValue.EMPTY_STRING;
} else {
String newNormalizationForm = "NFC";
if (getArgumentCount() > 1) {
newNormalizationForm = getArgument(1).eval(contextSequence).getStringValue().toUpperCase().trim();
}
// TODO : handle the "FULLY-NORMALIZED" string...
if (newNormalizationForm.isEmpty()) {
result = new StringValue(s1.getStringValue());
} else {
try {
Normalizer.Form form = Normalizer.Form.valueOf(newNormalizationForm);
result = new StringValue(Normalizer.normalize(s1.getStringValue(), form));
} catch (IllegalArgumentException e) {
throw new XPathException(this, ErrorCodes.FOCH0003, "Unknown normalization form: " + newNormalizationForm);
}
}
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
Aggregations