=Context-Awareness=
{{Mark|Introduced with Version 7.2.1:}}
If an instantiated class inherits the abstract [https://github.com/BaseXdb/basex/blob/master/src/main/java/org/basex/query/QueryModule.java QueryModule] class of BaseX, it will be enriched with two internal, context-dependent variables:
* {{Mono|context}} is an instance of the [https://github.com/BaseXdb/basex/blob/master/src/main/java/org/basex/query/QueryContext.java QueryContext] class. It provides access to all static and dynamic properties of the current query.
* {{Mono|input}} is an instance of [https://github.com/BaseXdb/basex/blob/master/src/main/java/org/basex/util/InputInfo.java InputInfo]. It contains line/column and the original query and is mainly used for providing better error messages.
Below, two Java methods are shown that take advantage of the variables:
<pre class="brush:java">
import org.basex.query.*;
import org.basex.query.item.*;
/**
* This example class contains two methods for demonstrating the use of the
* {@link QueryModule} class.
*/
public class QueryModuleExample extends org.basex.query.QueryModule {
/**
* Returns the root expression of this query as string.
* @return root expression
*/
public String rootExpression() {
return context.root.toString();
}
/**
* Converts the specified string to an integer.
* @param value string representation
* @return integer
* @throws QueryException query exception
*/
public int toInt(final String value) throws QueryException {
try {
return Integer.parseInt(value);
} catch(NumberFormatException ex) {
throw new QueryException(input, new QNm("FORM0001"), ex.getMessage());
}
}
}
</pre>
<pre class="brush:java">
import module namespace context = 'java:org.basex.examples.query.ContextModule';
<context>{
context:function-namespace()
}</context>,
<error-code>{
try {
context:to-int('abc')
} catch * {
$err:code
}
}</error-code>
</pre>
=Changelog=
===Version 7.2.1===
* Added: import of Java modules, context awareness
[[Category:XQuery]]
[[Category:API]]