use of org.exist.xquery.value.Item in project exist by eXist-db.
the class ExistXqueryRegistry method extractMissingModuleHint.
private MissingModuleHint extractMissingModuleHint(final RestXqServiceCompilationException e) {
MissingModuleHint missingModuleHint = null;
if (e.getCause() instanceof XPathException) {
final XPathException xpe = (XPathException) e.getCause();
if (xpe.getErrorCode() == ErrorCodes.XQST0059) {
final Sequence errorVals = xpe.getErrorVal();
if (errorVals != null && errorVals.getItemCount() > 0) {
final Item errorVal1 = errorVals.itemAt(0);
if (errorVal1 instanceof StringValue) {
missingModuleHint = new MissingModuleHint();
missingModuleHint.moduleHint = ((StringValue) errorVal1).getStringValue();
}
if (errorVals.getItemCount() == 2) {
final Item errorVal2 = errorVals.itemAt(1);
if (errorVal2 instanceof StringValue) {
if (missingModuleHint == null) {
missingModuleHint = new MissingModuleHint();
}
final String dependantModuleUri = ((StringValue) errorVal2).getStringValue();
// path will be of xmldb:exist:///db/a/c/1.xqm form so change it to /db/a/c/1.xqm form
missingModuleHint.dependantModule = makeDbAbsolutePath(dependantModuleUri);
}
}
}
}
}
return missingModuleHint;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class EXIUtils method getInputStream.
protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
switch(item.getType()) {
case Type.ANY_URI:
LOG.debug("Streaming xs:anyURI");
// anyURI provided
String url = item.getStringValue();
// Fix URL
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
return new URL(url).openStream();
case Type.ELEMENT:
case Type.DOCUMENT:
LOG.debug("Streaming element or document node");
/*
if (item instanceof NodeProxy) {
NodeProxy np = (NodeProxy) item;
String url = "xmldb:exist://" + np.getDocument().getBaseURI();
LOG.debug("Document detected, adding URL " + url);
streamSource.setSystemId(url);
}
*/
// Node provided
final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
final Serializer serializer = context.getBroker().borrowSerializer();
try {
fn.accept(serializer);
} finally {
context.getBroker().returnSerializer(serializer);
}
};
NodeValue node = (NodeValue) item;
return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
default:
LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class EncodeExiFunction method eval.
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
EXISerializer exiSerializer;
if (args.length > 1) {
if (!args[1].isEmpty()) {
Item xsdItem = args[1].itemAt(0);
try (InputStream xsdInputStream = EXIUtils.getInputStream(xsdItem, context)) {
exiSerializer = new EXISerializer(baos, xsdInputStream);
}
} else {
exiSerializer = new EXISerializer(baos);
}
} else {
exiSerializer = new EXISerializer(baos);
}
Item inputNode = args[0].itemAt(0);
exiSerializer.startDocument();
inputNode.toSAX(context.getBroker(), exiSerializer, new Properties());
exiSerializer.endDocument();
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), baos.toInputStream());
} catch (IOException ioex) {
// TODO - test!
throw new XPathException(this, ErrorCodes.FODC0002, ioex.getMessage());
} catch (EXIException | SAXException exie) {
throw new XPathException(this, new JavaErrorCode(exie.getCause()), exie.getMessage());
}
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class Serializer method toSAX.
/**
* Serialize the items in the given sequence to SAX, starting with item start. If parameter
* wrap is set to true, output a wrapper element to enclose the serialized items. The
* wrapper element will be in namespace {@link org.exist.Namespaces#EXIST_NS} and has the following form:
*
* <exist:result hits="sequence length" start="value of start" count="value of count">
*
* @param seq The sequence to serialize
* @param start The position in the sequence to start serialization from
* @param count The number of items from the start position to serialize
* @param wrap Indicates whether the output should be wrapped
* @param typed Indicates whether the output types should be wrapped
* @param compilationTime The time taken to compile the query which produced the sequence
* @param executionTime The time taken to execute the query which produced the sequence
*
* @throws SAXException If an error occurs during serialization
*/
public void toSAX(final Sequence seq, int start, final int count, final boolean wrap, final boolean typed, final long compilationTime, final long executionTime) throws SAXException {
try {
setStylesheetFromProperties(null);
} catch (final TransformerConfigurationException e) {
throw new SAXException(e.getMessage(), e);
}
setXSLHandler(null, false);
final AttrList attrs = new AttrList();
attrs.addAttribute(ATTR_HITS_QNAME, Integer.toString(seq.getItemCount()));
attrs.addAttribute(ATTR_START_QNAME, Integer.toString(start));
attrs.addAttribute(ATTR_COUNT_QNAME, Integer.toString(count));
if (outputProperties.getProperty(PROPERTY_SESSION_ID) != null) {
attrs.addAttribute(ATTR_SESSION_ID, outputProperties.getProperty(PROPERTY_SESSION_ID));
}
attrs.addAttribute(ATTR_COMPILATION_TIME_QNAME, Long.toString(compilationTime));
attrs.addAttribute(ATTR_EXECUTION_TIME_QNAME, Long.toString(compilationTime));
if (!documentStarted) {
receiver.startDocument();
documentStarted = true;
}
if (wrap) {
receiver.startPrefixMapping("exist", Namespaces.EXIST_NS);
receiver.startElement(ELEM_RESULT_QNAME, attrs);
}
for (int i = --start; i < start + count; i++) {
final Item item = seq.itemAt(i);
if (item == null) {
LOG.debug("item {} not found", i);
continue;
}
itemToSAX(item, typed, wrap);
}
if (wrap) {
receiver.endElement(ELEM_RESULT_QNAME);
receiver.endPrefixMapping("exist");
}
receiver.endDocument();
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class ArrowOperator method eval.
@Override
public Sequence eval(Sequence contextSequence, final Item contextItem) throws XPathException {
if (contextItem != null) {
contextSequence = contextItem.toSequence();
}
contextSequence = leftExpr.eval(contextSequence);
final FunctionReference fref;
if (fcall != null) {
fref = new FunctionReference(fcall);
} else {
final Sequence funcSeq = funcSpec.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(funcSpec));
}
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()));
}
fref = (FunctionReference) item0;
}
try {
final List<Expression> fparams = new ArrayList<>(parameters.size() + 1);
fparams.add(new ContextParam(context, contextSequence));
fparams.addAll(parameters);
fref.setArguments(fparams);
// need to create a new AnalyzeContextInfo to avoid memory leak
// cachedContextInfo will stay in memory
fref.analyze(new AnalyzeContextInfo(cachedContextInfo));
// Evaluate the function
return fref.eval(null);
} finally {
fref.close();
}
}
Aggregations