use of org.exist.xquery.value.Item in project exist by eXist-db.
the class AttributeConstructor method evalEnclosedExpr.
private void evalEnclosedExpr(Sequence seq, StringBuilder buf) throws XPathException {
Item item;
AtomicValue atomic;
for (final SequenceIterator i = Atomize.atomize(seq).iterate(); i.hasNext(); ) {
item = i.nextItem();
buf.append(item.getStringValue());
if (i.hasNext()) {
buf.append(' ');
}
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class DynamicFunctionCall method eval.
@Override
public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
context.proceed(this);
final Sequence funcSeq = functionExpr.eval(contextSequence, contextItem);
if (funcSeq.getCardinality() != Cardinality.EXACTLY_ONE) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Expected exactly one item for the function to be called, got " + funcSeq.getItemCount() + ". Expression: " + ExpressionDumper.dump(functionExpr));
}
final Item item0 = funcSeq.itemAt(0);
if (!Type.subTypeOf(item0.getType(), Type.FUNCTION_REFERENCE)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "Type error: expected function, got " + Type.getTypeName(item0.getType()));
}
final FunctionReference ref = (FunctionReference) item0;
// if the call is a partial application, create a new function
if (isPartial) {
try {
final FunctionCall call = ref.getCall();
call.setArguments(arguments);
final PartialFunctionApplication partialApp = new PartialFunctionApplication(context, call);
partialApp.analyze(new AnalyzeContextInfo(cachedContextInfo));
return partialApp.eval(contextSequence, contextItem);
} catch (final XPathException e) {
e.setLocation(line, column, getSource());
throw e;
}
} else {
ref.setArguments(arguments);
// need to create a new AnalyzeContextInfo to avoid memory leak
// cachedContextInfo will stay in memory
ref.analyze(new AnalyzeContextInfo(cachedContextInfo));
// Evaluate the function
try {
return ref.eval(contextSequence);
} catch (XPathException e) {
if (e.getLine() <= 0) {
e.setLocation(getLine(), getColumn(), getSource());
}
throw e;
} finally {
ref.close();
}
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class DynamicPIConstructor method eval.
/* (non-Javadoc)
* @see org.exist.xquery.Expression#eval(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 (newDocumentContext) {
context.pushDocumentContext();
}
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
context.proceed(this, builder);
final Sequence nameSeq = name.eval(contextSequence, contextItem);
// TODO : get rid of getLength()
if (!nameSeq.hasOne()) {
throw new XPathException(this, ErrorCodes.XPTY0004, "The name expression should evaluate to a single value");
}
final Item nameItem = nameSeq.itemAt(0);
if (!(nameItem.getType() == Type.STRING || nameItem.getType() == Type.NCNAME || nameItem.getType() == Type.UNTYPED_ATOMIC)) {
throw new XPathException(this, ErrorCodes.XPTY0004, "The name expression should evaluate to a " + Type.getTypeName(Type.STRING) + " or a " + Type.getTypeName(Type.NCNAME) + " or a " + Type.getTypeName(Type.UNTYPED_ATOMIC) + ". Got: " + Type.getTypeName(nameItem.getType()));
}
if (!XMLNames.isNCName(nameSeq.getStringValue())) {
throw new XPathException(this, ErrorCodes.XQDY0041, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);
}
if (nameSeq.getStringValue().equalsIgnoreCase("XML")) {
throw new XPathException(this, ErrorCodes.XQDY0064, nameSeq.getStringValue() + "' is not a valid processing instruction name", nameSeq);
}
String contentString;
final Sequence contentSeq = content.eval(contextSequence, contextItem);
if (contentSeq.isEmpty()) {
contentString = "";
} else {
final StringBuilder buf = new StringBuilder();
for (final SequenceIterator i = Atomize.atomize(contentSeq).iterate(); i.hasNext(); ) {
context.proceed(this, builder);
final Item next = i.nextItem();
if (buf.length() > 0) {
buf.append(' ');
}
buf.append(next.getStringValue());
}
while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) buf.deleteCharAt(0);
contentString = buf.toString();
}
if (contentString.contains("?>")) {
throw new XPathException(this, ErrorCodes.XQDY0026, contentString + "' is not a valid processing intruction content", contentSeq);
}
final int nodeNo = builder.processingInstruction(nameSeq.getStringValue(), contentString);
final Sequence result = ((DocumentImpl) builder.getDocument()).getNode(nodeNo);
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
} finally {
if (newDocumentContext) {
context.popDocumentContext();
}
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class FunAbs 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());
}
}
Sequence result;
final Sequence seq = getArgument(0).eval(contextSequence, contextItem);
if (seq.isEmpty()) {
result = Sequence.EMPTY_SEQUENCE;
} else {
final Item item = seq.itemAt(0);
NumericValue value;
if (item instanceof NumericValue) {
value = (NumericValue) item;
} else {
value = (NumericValue) item.convertTo(Type.NUMBER);
}
result = value.abs();
}
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", result);
}
return result;
}
use of org.exist.xquery.value.Item 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;
}
Aggregations