use of org.exist.xquery.value.Item in project exist by eXist-db.
the class Atomize method atomize.
public static Sequence atomize(Sequence input) throws XPathException {
if (input.isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
input = ArrayType.flatten(input);
if (input.hasOne()) {
return input.itemAt(0).atomize();
}
Item next;
final ValueSequence result = new ValueSequence();
for (final SequenceIterator i = input.iterate(); i.hasNext(); ) {
next = i.nextItem();
result.add(next.atomize());
}
return result;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class DocumentConstructor 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());
}
}
context.pushDocumentContext();
final Sequence contentSeq = content.eval(contextSequence, contextItem);
context.popDocumentContext();
context.pushDocumentContext();
final MemTreeBuilder builder = context.getDocumentBuilder(true);
final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder);
try {
if (!contentSeq.isEmpty()) {
StringBuilder buf = null;
final SequenceIterator i = contentSeq.iterate();
Item next = i.nextItem();
while (next != null) {
context.proceed(this, builder);
if (next.getType() == Type.ATTRIBUTE || next.getType() == Type.NAMESPACE) {
throw new XPathException(this, "Found a node of type " + Type.getTypeName(next.getType()) + " inside a document constructor");
}
// following atomic values and seperate them by a space.
if (Type.subTypeOf(next.getType(), Type.ATOMIC)) {
if (buf == null) {
buf = new StringBuilder();
} else if (buf.length() > 0) {
buf.append(' ');
}
buf.append(next.getStringValue());
next = i.nextItem();
// if item is a node, flush any collected character data and
// copy the node to the target doc.
} else if (next.getType() == Type.DOCUMENT) {
if (buf != null && buf.length() > 0) {
receiver.characters(buf);
buf.setLength(0);
}
next.copyTo(context.getBroker(), receiver);
next = i.nextItem();
} else if (Type.subTypeOf(next.getType(), Type.NODE)) {
if (buf != null && buf.length() > 0) {
receiver.characters(buf);
buf.setLength(0);
}
next.copyTo(context.getBroker(), receiver);
next = i.nextItem();
}
}
// flush remaining character data
if (buf != null && buf.length() > 0) {
receiver.characters(buf);
buf.setLength(0);
}
}
} catch (final SAXException e) {
throw new XPathException(this, "Encountered SAX exception while processing document constructor: " + ExpressionDumper.dump(this));
}
context.popDocumentContext();
final NodeImpl node = builder.getDocument();
if (context.getProfiler().isEnabled()) {
context.getProfiler().end(this, "", node);
}
return node;
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class RenderFunction method eval.
/*
* Actual implementation of the rendering process. When a function in this
* module is called, this method is executed with the given inputs. @param
* Sequence[] args (XSL-FO, mime-type, parameters) @param Sequence
* contextSequence (default sequence)
*
* @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[],
* org.exist.xquery.value.Sequence)
*/
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
// process
if (args[0].isEmpty()) {
return Sequence.EMPTY_SEQUENCE;
}
final Item inputNode = args[0].itemAt(0);
// get media-type
final String mediaType = args[1].getStringValue();
// get parameters
final Properties parameters;
if (!args[2].isEmpty()) {
parameters = ParametersExtractor.parseParameters(((NodeValue) args[2].itemAt(0)).getNode());
} else {
parameters = new Properties();
}
ProcessorAdapter adapter = null;
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
adapter = ((XSLFOModule) getParentModule()).getProcessorAdapter();
final NodeValue processorConfig = args.length == 4 ? (NodeValue) args[3].itemAt(0) : null;
final ContentHandler contentHandler = adapter.getContentHandler(context.getBroker(), processorConfig, parameters, mediaType, baos);
// process the XSL-FO
contentHandler.startDocument();
inputNode.toSAX(context.getBroker(), contentHandler, new Properties());
contentHandler.endDocument();
// return the result
return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), new ByteArrayInputStream(baos.toByteArray()));
} catch (final IOException | SAXException se) {
throw new XPathException(this, se.getMessage(), se);
} finally {
if (adapter != null) {
adapter.cleanup();
}
}
}
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
*
* @throws SAXException If an error occurs during serialization
*/
public void toSAX(final Sequence seq) throws SAXException {
try {
setStylesheetFromProperties(null);
} catch (final TransformerConfigurationException e) {
throw new SAXException(e.getMessage(), e);
}
setXSLHandler(null, false);
if (!documentStarted) {
receiver.startDocument();
documentStarted = true;
}
try {
final SequenceIterator itSeq = seq.iterate();
while (itSeq.hasNext()) {
final Item item = itSeq.nextItem();
itemToSAX(item, false, false);
}
} catch (final XPathException xpe) {
throw new SAXException(xpe.getMessage(), xpe);
}
receiver.endDocument();
}
use of org.exist.xquery.value.Item in project exist by eXist-db.
the class DiffMatcher method matches.
@Override
public boolean matches(final Object item, final Description mismatch) {
if (item == null) {
mismatch.appendText("null");
return false;
}
final Item actualItem;
if (item instanceof NodeValue) {
actualItem = (NodeValue) item;
} else if (item instanceof Sequence) {
final Sequence actual = ((Sequence) item);
if (actual.getItemCount() != 1) {
mismatch.appendText("Sequence does not contain 1 item");
return false;
}
actualItem = actual.itemAt(0);
if (!(actualItem instanceof NodeValue)) {
mismatch.appendText("Sequence does not contain a Node");
return false;
}
} else {
mismatch.appendText("is not a Node");
return false;
}
final Source actualSource = Input.fromNode((org.w3c.dom.Node) actualItem).build();
DiffBuilder diffBuilder = DiffBuilder.compare(expectedSource).withTest(actualSource);
if (identical) {
diffBuilder = diffBuilder.checkForIdentical();
} else {
diffBuilder = diffBuilder.checkForSimilar();
}
final Diff diff = diffBuilder.build();
if (diff.hasDifferences()) {
mismatch.appendText("differences: " + diff.toString());
return false;
}
return true;
}
Aggregations