use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class GrammarTooling method eval.
/**
* @see org.exist.xquery.BasicFunction#eval(Sequence[], Sequence)
*/
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final GrammarPool grammarpool = (GrammarPool) config.getProperty(XMLReaderObjectFactory.GRAMMAR_POOL);
if (isCalledAs("clear-grammar-cache")) {
final Sequence result = new ValueSequence();
final int before = countTotalNumberOfGrammar(grammarpool);
LOG.debug("Clearing {} grammars", before);
clearGrammarPool(grammarpool);
final int after = countTotalNumberOfGrammar(grammarpool);
LOG.debug("Remained {} grammars", after);
final int delta = before - after;
result.add(new IntegerValue(delta));
return result;
} else if (isCalledAs("show-grammar-cache")) {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
final NodeImpl result = writeReport(grammarpool, builder);
return result;
} finally {
context.popDocumentContext();
}
} else if (isCalledAs("pre-parse-grammar")) {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
// Setup for XML schema support only
final XMLGrammarPreparser parser = new XMLGrammarPreparser();
parser.registerPreparser(TYPE_XSD, null);
final List<Grammar> allGrammars = new ArrayList<>();
// iterate through the argument sequence and parse url
for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
String url = i.nextItem().getStringValue();
// Fix database urls
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
LOG.debug("Parsing {}", url);
// parse XSD grammar
try {
if (url.endsWith(".xsd")) {
final InputStream is = new URL(url).openStream();
final XMLInputSource xis = new XMLInputSource(null, url, url, is, null);
final Grammar schema = parser.preparseGrammar(TYPE_XSD, xis);
is.close();
allGrammars.add(schema);
} else {
throw new XPathException(this, "Only XMLSchemas can be preparsed.");
}
} catch (final Exception ex) {
LOG.debug(ex);
throw new XPathException(this, ex);
}
}
LOG.debug("Successfully parsed {} grammars.", allGrammars.size());
// Send all XSD grammars to grammarpool
Grammar[] grammars = new Grammar[allGrammars.size()];
grammars = allGrammars.toArray(grammars);
grammarpool.cacheGrammars(TYPE_XSD, grammars);
// Construct result to end user
final ValueSequence result = new ValueSequence();
for (final Grammar one : grammars) {
result.add(new StringValue(one.getGrammarDescription().getNamespace()));
}
return result;
} else {
// oh oh
LOG.error("function not found error");
throw new XPathException(this, "function not found");
}
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class Profile method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
final Profiler profiler = context.getProfiler();
if (isCalledAs("enable-profiling")) {
final int verbosity = ((IntegerValue) args[0].itemAt(0)).getInt();
profiler.setEnabled(true);
profiler.setVerbosity(verbosity);
} else {
profiler.setEnabled(false);
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class XMLDBDefragment 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[] args, Sequence contextSequence) throws XPathException {
// Get nodes
final NodeSet nodes = args[0].toNodeSet();
final DocumentSet docs = nodes.getDocumentSet();
try {
if (args.length > 1) {
// Use supplied parameter
final int splitCount = ((IntegerValue) args[1].itemAt(0)).getInt();
Modification.checkFragmentation(context, docs, splitCount);
} else {
// Use conf.xml configured value or -1 if not existent
Modification.checkFragmentation(context, docs);
}
} catch (final LockException | EXistException e) {
logger.error("An error occurred while defragmenting documents: {}", e.getMessage());
throw new XPathException(this, "An error occurred while defragmenting documents: " + e.getMessage(), e);
}
return Sequence.EMPTY_SEQUENCE;
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class XMLDBXUpdate method evalWithCollection.
/* (non-Javadoc)
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
*/
public Sequence evalWithCollection(Collection c, Sequence[] args, Sequence contextSequence) throws XPathException {
final NodeValue data = (NodeValue) args[1].itemAt(0);
final StringWriter writer = new StringWriter();
final Properties properties = new Properties();
properties.setProperty(OutputKeys.INDENT, "yes");
final DOMSerializer serializer = new ExtendedDOMSerializer(context.getBroker(), writer, properties);
try {
serializer.serialize(data.getNode());
} catch (final TransformerException e) {
logger.debug("Exception while serializing XUpdate document", e);
throw new XPathException(this, "Exception while serializing XUpdate document: " + e.getMessage(), e);
}
final String xupdate = writer.toString();
long modifications = 0;
try {
final XUpdateQueryService service = (XUpdateQueryService) c.getService("XUpdateQueryService", "1.0");
logger.debug("Processing XUpdate request: {}", xupdate);
modifications = service.update(xupdate);
} catch (final XMLDBException e) {
throw new XPathException(this, "Exception while processing xupdate: " + e.getMessage(), e);
}
context.getRootExpression().resetState(false);
return new IntegerValue(modifications);
}
use of org.exist.xquery.value.IntegerValue in project exist by eXist-db.
the class ExecuteFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args.length == 5 || args.length == 6) {
// was a connection and PL/SQL statement specified?
if (args[0].isEmpty() || args[1].isEmpty()) {
return (Sequence.EMPTY_SEQUENCE);
}
// get the Connection
long connectionUID = ((IntegerValue) args[0].itemAt(0)).getLong();
Connection connection = SQLModule.retrieveConnection(context, connectionUID);
if (connection == null) {
return (Sequence.EMPTY_SEQUENCE);
}
// get the PL/SQL statement
String plSql = args[1].getStringValue();
// get the input parameters (if any)
Element parameters = null;
if (!args[2].isEmpty()) {
parameters = (Element) args[2].itemAt(0);
}
// was a result set position specified?
int resultSetPos = 0;
if (!args[3].isEmpty()) {
resultSetPos = ((IntegerValue) args[3].itemAt(0)).getInt();
}
boolean haveReturnCode = false;
// default value of 1 for success
int plSqlSuccess = 1;
if (args.length == 6) {
// a return code is expected so what is the value indicating success?
plSqlSuccess = ((IntegerValue) args[5].itemAt(0)).getInt();
haveReturnCode = true;
}
CallableStatement statement = null;
ResultSet resultSet = null;
try {
MemTreeBuilder builder = context.getDocumentBuilder();
int iRow = 0;
statement = connection.prepareCall(plSql);
if (haveReturnCode) {
statement.registerOutParameter(1, Types.NUMERIC);
}
if (resultSetPos != 0) {
statement.registerOutParameter(resultSetPos, OracleTypes.CURSOR);
}
if (!args[2].isEmpty()) {
setParametersOnPreparedStatement(statement, parameters);
}
statement.execute();
if (haveReturnCode) {
int returnCode = statement.getInt(1);
if (returnCode != plSqlSuccess) {
LOG.error(plSql + " failed [" + returnCode + "]");
return (Sequence.EMPTY_SEQUENCE);
}
}
if (resultSetPos != 0) {
// iterate through the result set building an XML document
builder.startDocument();
builder.startElement(new QName("result", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
builder.addAttribute(new QName("count", null, null), String.valueOf(-1));
resultSet = (ResultSet) statement.getObject(resultSetPos);
ResultSetMetaData rsmd = resultSet.getMetaData();
int iColumns = rsmd.getColumnCount();
while (resultSet.next()) {
builder.startElement(new QName("row", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
builder.addAttribute(new QName("index", null, null), String.valueOf(resultSet.getRow()));
// get each tuple in the row
for (int i = 0; i < iColumns; i++) {
String columnName = rsmd.getColumnLabel(i + 1);
if (columnName != null) {
String colValue = resultSet.getString(i + 1);
String colElement = "field";
if (((BooleanValue) args[4].itemAt(0)).effectiveBooleanValue() && columnName.length() > 0) {
// use column names as the XML node
/**
* Spaces in column names are replaced with
* underscore's
*/
colElement = SQLUtils.escapeXmlAttr(columnName.replace(' ', '_'));
}
builder.startElement(new QName(colElement, OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
if (!((BooleanValue) args[4].itemAt(0)).effectiveBooleanValue() || columnName.length() <= 0) {
String name;
if (columnName.length() > 0) {
name = SQLUtils.escapeXmlAttr(columnName);
} else {
name = "Column: " + String.valueOf(i + 1);
}
builder.addAttribute(new QName("name", null, null), name);
}
builder.addAttribute(new QName("type", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), rsmd.getColumnTypeName(i + 1));
builder.addAttribute(new QName("type", Namespaces.SCHEMA_NS, "xs"), Type.getTypeName(SQLUtils.sqlTypeToXMLType(rsmd.getColumnType(i + 1))));
if (resultSet.wasNull()) {
// Add a null indicator attribute if the value was SQL Null
builder.addAttribute(new QName("null", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), "true");
}
if (colValue != null) {
builder.characters(SQLUtils.escapeXmlText(colValue));
}
builder.endElement();
}
}
builder.endElement();
iRow++;
}
builder.endElement();
// Change the root element count attribute to have the correct value
NodeValue node = (NodeValue) builder.getDocument().getDocumentElement();
Node count = node.getNode().getAttributes().getNamedItem("count");
if (count != null) {
count.setNodeValue(String.valueOf(iRow));
}
builder.endDocument();
// return the XML result set
return (node);
} else {
// there was no result set so just return an empty sequence
return (Sequence.EMPTY_SEQUENCE);
}
} catch (SQLException sqle) {
LOG.error("oracle:execute() Caught SQLException \"" + sqle.getMessage() + "\" for PL/SQL: \"" + plSql + "\"", sqle);
// return details about the SQLException
MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("exception", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
boolean recoverable = false;
if (sqle instanceof SQLRecoverableException) {
recoverable = true;
}
builder.addAttribute(new QName("recoverable", null, null), String.valueOf(recoverable));
builder.startElement(new QName("state", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
String sqlState = sqle.getSQLState();
if (sqlState != null) {
builder.characters(sqle.getSQLState());
} else {
builder.characters("null");
}
builder.endElement();
builder.startElement(new QName("message", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
builder.characters(sqle.getMessage());
builder.endElement();
builder.startElement(new QName("stack-trace", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
ByteArrayOutputStream bufStackTrace = new ByteArrayOutputStream();
sqle.printStackTrace(new PrintStream(bufStackTrace));
builder.characters(new String(bufStackTrace.toByteArray()));
builder.endElement();
builder.startElement(new QName("oracle", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
builder.characters(SQLUtils.escapeXmlText(plSql));
builder.endElement();
int line = getLine();
int column = getColumn();
builder.startElement(new QName("xquery", OracleModule.NAMESPACE_URI, OracleModule.PREFIX), null);
builder.addAttribute(new QName("line", null, null), String.valueOf(line));
builder.addAttribute(new QName("column", null, null), String.valueOf(column));
builder.endElement();
builder.endElement();
builder.endDocument();
return (NodeValue) builder.getDocument().getDocumentElement();
} finally {
release(connection, statement, resultSet);
}
} else {
throw new XPathException("Invalid number of arguments [" + args.length + "]");
}
}
Aggregations