use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class FunTranslate method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
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 seq = getArgument(0).eval(contextSequence);
if (seq.isEmpty()) {
result = StringValue.EMPTY_STRING;
} else {
final ValueSequence arg = FunStringToCodepoints.getCodePoints(seq.getStringValue());
final ValueSequence mapStr = FunStringToCodepoints.getCodePoints(getArgument(1).eval(contextSequence).getStringValue());
final ValueSequence transStr = FunStringToCodepoints.getCodePoints(getArgument(2).eval(contextSequence).getStringValue());
int p;
IntegerValue ch;
final StringBuilder buf = new StringBuilder(arg.getItemCount());
for (int i = 0; i < arg.getItemCount(); i++) {
ch = (IntegerValue) arg.itemAt(i);
p = FunStringToCodepoints.indexOf(mapStr, ch);
if (p == Constants.STRING_NOT_FOUND) {
buf.append(FunStringToCodepoints.codePointToString(ch));
} else {
if (p < transStr.getItemCount()) {
buf.append(FunStringToCodepoints.codePointToString((IntegerValue) transStr.itemAt(p)));
}
}
}
result = new StringValue(buf.toString());
}
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 FunUpperOrLowerCase method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(org.exist.dom.persistent.DocumentSet, org.exist.xquery.value.Sequence, org.exist.xquery.value.Item)
*/
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 seq = getArgument(0).eval(contextSequence);
if (seq.isEmpty()) {
result = StringValue.EMPTY_STRING;
} else {
final String value = seq.getStringValue();
if (isCalledAs("upper-case")) {
result = new StringValue(value.toUpperCase());
} else {
result = new StringValue(value.toLowerCase());
}
}
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 FunReplace method eval.
@Override
public Sequence eval(final 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());
}
}
final Sequence result;
final Sequence stringArg = getArgument(0).eval(contextSequence, contextItem);
if (stringArg.isEmpty()) {
result = StringValue.EMPTY_STRING;
} else {
final String flags;
if (getSignature().getArgumentCount() == 4) {
flags = getArgument(3).eval(contextSequence, contextItem).getStringValue();
} else {
flags = "";
}
final String string = stringArg.getStringValue();
final Sequence patternSeq = getArgument(1).eval(contextSequence, contextItem);
final String pattern = patternSeq.getStringValue();
final Sequence replaceSeq = getArgument(2).eval(contextSequence, contextItem);
final String replace = replaceSeq.getStringValue();
final Configuration config = context.getBroker().getBrokerPool().getSaxonConfiguration();
final List<String> warnings = new ArrayList<>(1);
try {
final RegularExpression regularExpression = config.compileRegularExpression(pattern, flags, "XP30", warnings);
if (!hasLiteral(flags)) {
final String msg = Replace.checkReplacement(replace);
if (msg != null) {
throw new XPathException(this, ErrorCodes.FORX0004, msg);
}
}
final CharSequence res = regularExpression.replace(string, replace);
result = new StringValue(res.toString());
} catch (final net.sf.saxon.trans.XPathException e) {
switch(e.getErrorCodeLocalPart()) {
case "FORX0001":
throw new XPathException(this, ErrorCodes.FORX0001, e.getMessage());
case "FORX0002":
throw new XPathException(this, ErrorCodes.FORX0002, e.getMessage());
case "FORX0003":
throw new XPathException(this, ErrorCodes.FORX0003, e.getMessage());
case "FORX0004":
throw new XPathException(this, ErrorCodes.FORX0004, e.getMessage());
default:
throw new XPathException(this, e.getMessage());
}
}
}
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 GetHeaderNames method eval.
@Override
public Sequence eval(final Sequence[] args, @Nonnull final RequestWrapper request) throws XPathException {
final Enumeration<String> headerNames = request.getHeaderNames();
if (!headerNames.hasMoreElements()) {
return Sequence.EMPTY_SEQUENCE;
}
final ValueSequence result = new ValueSequence();
while (headerNames.hasMoreElements()) {
final String headerName = headerNames.nextElement();
result.add(new StringValue(headerName));
}
return result;
}
use of org.exist.xquery.value.StringValue in project exist by eXist-db.
the class ExtTestFailureFunction method actualToString.
private String actualToString(final MapType actual) throws XPathException, SAXException, IOException {
final Sequence seqActualError = actual.get(new StringValue("error"));
if (!seqActualError.isEmpty()) {
return errorMapToString(seqActualError);
}
final Sequence seqActualResult = actual.get(new StringValue("result"));
if (!seqActualResult.isEmpty()) {
return seqToString(seqActualResult);
} else {
// empty-sequence()
return "";
}
}
Aggregations