net.sf.saxon.functions.NormalizeSpace_0 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of saxon-he Show documentation
Show all versions of saxon-he Show documentation
An OSGi bundle for Saxon-HE
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013 Saxonica Limited.
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.functions;
import net.sf.saxon.expr.Callable;
import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.StaticProperty;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.expr.parser.ExpressionVisitor;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.StringValue;
import net.sf.saxon.value.Whitespace;
/**
* Implement the XPath normalize-space() function with zero arguments
*/
public class NormalizeSpace_0 extends SystemFunctionCall implements Callable {
/**
* Determine the intrinsic dependencies of an expression, that is, those which are not derived
* from the dependencies of its subexpressions. For example, position() has an intrinsic dependency
* on the context position, while (position()+1) does not. The default implementation
* of the method returns 0, indicating "no dependencies".
*
* @return a set of bit-significant flags identifying the "intrinsic"
* dependencies. The flags are documented in class net.sf.saxon.value.StaticProperty
*/
public int getIntrinsicDependencies() {
return StaticProperty.DEPENDS_ON_CONTEXT_ITEM;
}
/*@NotNull*/
public Expression typeCheck(ExpressionVisitor visitor, /*@Nullable*/ ExpressionVisitor.ContextItemType contextItemType) throws XPathException {
if (contextItemType == null) {
XPathException err = new XPathException("The context item for normalize-space() is absent");
err.setErrorCode("XPDY0002");
err.setIsTypeError(true);
err.setLocator(this);
throw err;
}
return super.typeCheck(visitor, contextItemType);
}
/**
* Pre-evaluate a function at compile time. Functions that do not allow
* pre-evaluation, or that need access to context information, can override this method.
* @param visitor an expression visitor
*/
public Expression preEvaluate(ExpressionVisitor visitor) throws XPathException {
return this;
}
/**
* Evaluate in a general context
*/
public StringValue evaluateItem(XPathContext c) throws XPathException {
Item item = c.getContextItem();
if (item == null) {
dynamicError("Context item for normalize-space() is absent", "XPDY0002", c);
return null;
}
return StringValue.makeStringValue(
Whitespace.collapseWhitespace(item.getStringValueCS()));
}
/**
* Get the effective boolean value of the expression. This returns false if the value
* is the empty sequence, a zero-length string, a number equal to zero, or the boolean
* false. Otherwise it returns true.
*
* This method is implemented for normalize-space() because it is quite often used in a
* boolean context to test whether a value exists and is non-white, and because testing for the
* presence of non-white characters is a lot more efficient than constructing the normalized
* string, especially because of early-exit.
*
* @param c The context in which the expression is to be evaluated
* @return the effective boolean value
* @throws net.sf.saxon.trans.XPathException
* if any dynamic error occurs evaluating the expression
*/
public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
Item item = c.getContextItem();
if (item == null) {
dynamicError("Context item for normalize-space() is absent", "XPDY0002", c);
return false;
}
return !Whitespace.isWhite(item.getStringValueCS());
}
/**
* Evaluate the expression
*
* @param context the dynamic evaluation context
* @param arguments the values of the arguments, supplied as Sequences
* @return the result of the evaluation, in the form of a Sequence
* @throws net.sf.saxon.trans.XPathException
* if a dynamic error occurs during the evaluation of the expression
*/
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
return evaluateItem(context);
}
}