Search in sources :

Example 41 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class Expand method eval.

public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    if (args[0].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // apply serialization options set on the XQuery context
    final Properties serializeOptions = new Properties();
    serializeOptions.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "yes");
    serializeOptions.setProperty(EXistOutputKeys.HIGHLIGHT_MATCHES, "elements");
    if (getArgumentCount() == 2) {
        final String serOpts = args[1].getStringValue();
        final String[] contents = Option.tokenize(serOpts);
        for (String content : contents) {
            final String[] pair = Option.parseKeyValuePair(content);
            if (pair == null) {
                throw new XPathException(this, "Found invalid serialization option: " + content);
            }
            logger.debug("Setting serialization property: {} = {}", pair[0], pair[1]);
            serializeOptions.setProperty(pair[0], pair[1]);
        }
    } else {
        context.checkOptions(serializeOptions);
    }
    context.pushDocumentContext();
    try {
        final InMemoryNodeSet result = new InMemoryNodeSet();
        final MemTreeBuilder builder = new MemTreeBuilder(getContext());
        final DocumentBuilderReceiver receiver = new DocumentBuilderReceiver(builder, true);
        int attrNr = -1;
        for (final SequenceIterator i = args[0].iterate(); i.hasNext(); ) {
            final NodeValue next = (NodeValue) i.nextItem();
            final short nodeType = ((INodeHandle) next).getNodeType();
            builder.startDocument();
            if (nodeType == Node.ATTRIBUTE_NODE) {
                // NOTE: Attributes nodes need special handling as they cannot be directly serialized via SAX to a ContentHandler
                final Attr attr = (Attr) next.getNode();
                String ns = attr.getNamespaceURI();
                if (ns == null || ns.isEmpty()) {
                    ns = XMLConstants.NULL_NS_URI;
                }
                attrNr = builder.addAttribute(new QName(attr.getLocalName(), ns), attr.getValue());
            } else {
                next.toSAX(context.getBroker(), receiver, serializeOptions);
            }
            builder.endDocument();
            if (Node.DOCUMENT_NODE == nodeType) {
                result.add(builder.getDocument());
            } else if (Node.ATTRIBUTE_NODE == nodeType) {
                result.add(builder.getDocument().getAttribute(attrNr));
            } else {
                result.add(builder.getDocument().getNode(1));
            }
            builder.reset(getContext());
        }
        return result;
    } catch (final SAXException e) {
        throw new XPathException(this, e);
    } finally {
        context.popDocumentContext();
    }
}
Also used : NodeValue(org.exist.xquery.value.NodeValue) XPathException(org.exist.xquery.XPathException) INodeHandle(org.exist.dom.INodeHandle) QName(org.exist.dom.QName) InMemoryNodeSet(org.exist.dom.memtree.InMemoryNodeSet) Properties(java.util.Properties) DocumentBuilderReceiver(org.exist.dom.memtree.DocumentBuilderReceiver) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) SequenceIterator(org.exist.xquery.value.SequenceIterator)

Example 42 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class ResourceFunctionExecutorImpl method findFunction.

/**
 * Lookup a Function in an XQuery given a Function Signature
 *
 * @param xquery The XQuery to interrogate
 * @param functionSignature The Function Signature to use to match a Function
 *
 * @return The Function from the XQuery matching the Function Signature
 */
private UserDefinedFunction findFunction(final CompiledXQuery xquery, final FunctionSignature functionSignature) throws XPathException {
    final QName fnName = QName.fromJavaQName(functionSignature.getName());
    final int arity = functionSignature.getArgumentCount();
    return xquery.getContext().resolveFunction(fnName, arity);
}
Also used : QName(org.exist.dom.QName)

Example 43 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class LuceneIndexConfig method match.

public boolean match(NodePath other) {
    if (isQNameIndex) {
        final QName qn1 = path.getLastComponent();
        final QName qn2 = other.getLastComponent();
        return qn1.getNameType() == qn2.getNameType() && qn2.equals(qn1);
    }
    return path.match(other);
}
Also used : QName(org.exist.dom.QName)

Example 44 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class LuceneIndexConfig method parseQName.

protected static QName parseQName(String name, Map<String, String> namespaces) throws DatabaseConfigurationException {
    boolean isAttribute = false;
    if (name.startsWith("@")) {
        isAttribute = true;
        name = name.substring(1);
    }
    try {
        String prefix = QName.extractPrefix(name);
        String localName = QName.extractLocalName(name);
        String namespaceURI = "";
        if (prefix != null) {
            namespaceURI = namespaces.get(prefix);
            if (namespaceURI == null) {
                throw new DatabaseConfigurationException("No namespace defined for prefix: " + prefix + " in index definition");
            }
        }
        final QName qname;
        if (isAttribute) {
            qname = new QName(localName, namespaceURI, prefix, ElementValue.ATTRIBUTE);
        } else {
            qname = new QName(localName, namespaceURI, prefix);
        }
        return qname;
    } catch (QName.IllegalQNameException e) {
        throw new DatabaseConfigurationException("Lucene index configuration error: " + e.getMessage(), e);
    }
}
Also used : DatabaseConfigurationException(org.exist.util.DatabaseConfigurationException) QName(org.exist.dom.QName)

Example 45 with QName

use of org.exist.dom.QName in project exist by eXist-db.

the class ZipFileFunctions method extractEntries.

private Sequence extractEntries(XmldbURI uri) throws XPathException {
    ZipFileSource zipFileSource = new ZipFileFromDb(uri);
    ZipInputStream zis = null;
    Sequence xmlResponse = null;
    context.pushDocumentContext();
    try {
        MemTreeBuilder builder = context.getDocumentBuilder();
        builder.startDocument();
        builder.startElement(new QName("file", ZipModule.NAMESPACE_URI, ZipModule.PREFIX), null);
        builder.addAttribute(new QName("href", null, null), uri.toString());
        try {
            zis = zipFileSource.getStream();
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                if (zipEntry.isDirectory()) {
                    builder.startElement(new QName("dir", ZipModule.NAMESPACE_URI, ZipModule.PREFIX), null);
                    builder.addAttribute(new QName("name", null, null), zipEntry.toString());
                    builder.endElement();
                } else {
                    logger.debug("file: {}", zipEntry.getName());
                    builder.startElement(new QName("entry", ZipModule.NAMESPACE_URI, ZipModule.PREFIX), null);
                    builder.addAttribute(new QName("name", null, null), zipEntry.toString());
                    builder.endElement();
                }
            }
        } catch (PermissionDeniedException pde) {
            logger.error(pde.getMessage(), pde);
            throw new XPathException("Permission denied to read the source zip");
        } catch (IOException ioe) {
            logger.error(ioe.getMessage(), ioe);
            throw new XPathException("IO exception while reading the source zip");
        }
        builder.endElement();
        xmlResponse = (NodeValue) builder.getDocument().getDocumentElement();
        return (xmlResponse);
    } finally {
        context.popDocumentContext();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) QName(org.exist.dom.QName) ZipEntry(java.util.zip.ZipEntry) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException)

Aggregations

QName (org.exist.dom.QName)271 Test (org.junit.Test)54 Sequence (org.exist.xquery.value.Sequence)39 DBBroker (org.exist.storage.DBBroker)31 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)28 IOException (java.io.IOException)23 Document (org.w3c.dom.Document)23 DocumentSet (org.exist.dom.persistent.DocumentSet)20 Text (org.w3c.dom.Text)20 AttributesImpl (org.xml.sax.helpers.AttributesImpl)18 NameTest (org.exist.xquery.NameTest)17 XPathException (org.exist.xquery.XPathException)17 BrokerPool (org.exist.storage.BrokerPool)15 IllegalQNameException (org.exist.dom.QName.IllegalQNameException)13 Node (org.w3c.dom.Node)12 ReentrantLock (java.util.concurrent.locks.ReentrantLock)11 NodeSet (org.exist.dom.persistent.NodeSet)11 SAXException (org.xml.sax.SAXException)11 DefaultDocumentSet (org.exist.dom.persistent.DefaultDocumentSet)10 MutableDocumentSet (org.exist.dom.persistent.MutableDocumentSet)10