Search in sources :

Example 46 with Item

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;
}
Also used : Item(org.exist.xquery.value.Item) SequenceIterator(org.exist.xquery.value.SequenceIterator) ValueSequence(org.exist.xquery.value.ValueSequence)

Example 47 with Item

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;
}
Also used : Item(org.exist.xquery.value.Item) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator) NodeImpl(org.exist.dom.memtree.NodeImpl) Sequence(org.exist.xquery.value.Sequence) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) SAXException(org.xml.sax.SAXException)

Example 48 with Item

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();
        }
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) IOException(java.io.IOException) Properties(java.util.Properties) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException) Item(org.exist.xquery.value.Item) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 49 with Item

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();
}
Also used : Item(org.exist.xquery.value.Item) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SequenceIterator(org.exist.xquery.value.SequenceIterator) XPathException(org.exist.xquery.XPathException) SAXException(org.xml.sax.SAXException)

Example 50 with Item

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;
}
Also used : Item(org.exist.xquery.value.Item) NodeValue(org.exist.xquery.value.NodeValue) DiffBuilder(org.xmlunit.builder.DiffBuilder) Diff(org.xmlunit.diff.Diff) Node(org.w3c.dom.Node) Sequence(org.exist.xquery.value.Sequence) Source(javax.xml.transform.Source)

Aggregations

Item (org.exist.xquery.value.Item)88 Sequence (org.exist.xquery.value.Sequence)69 SequenceIterator (org.exist.xquery.value.SequenceIterator)36 XPathException (org.exist.xquery.XPathException)26 DBBroker (org.exist.storage.DBBroker)18 NodeValue (org.exist.xquery.value.NodeValue)17 XQuery (org.exist.xquery.XQuery)16 ValueSequence (org.exist.xquery.value.ValueSequence)16 BrokerPool (org.exist.storage.BrokerPool)14 NumericValue (org.exist.xquery.value.NumericValue)12 SAXException (org.xml.sax.SAXException)11 Properties (java.util.Properties)10 StringValue (org.exist.xquery.value.StringValue)10 Node (org.w3c.dom.Node)10 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)9 AtomicValue (org.exist.xquery.value.AtomicValue)9 Txn (org.exist.storage.txn.Txn)8 IOException (java.io.IOException)7 StringWriter (java.io.StringWriter)7 EXistException (org.exist.EXistException)7