Search in sources :

Example 6 with IStoredNode

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

the class IndexController method getReindexRoot.

/**
 * When adding or removing nodes to or from the document tree, it might become
 * necessary to re-index some parts of the tree, in particular if indexes are defined
 * on mixed content nodes. This method will return the top-most root.
 *
 * @param node the node to be modified.
 * @param path path the NodePath of the node
 * @param insert TODO: document
 * @param includeSelf if set to true, the current node itself will be included in the check
 * @return the top-most root node to be re-indexed
 */
public IStoredNode getReindexRoot(final IStoredNode node, final NodePath path, final boolean insert, final boolean includeSelf) {
    IStoredNode next;
    IStoredNode top = null;
    for (final IndexWorker indexWorker : indexWorkers.values()) {
        next = indexWorker.getReindexRoot(node, path, insert, includeSelf);
        if (next != null && (top == null || top.getNodeId().isDescendantOf(next.getNodeId()))) {
            top = next;
        }
    }
    if (top != null && top.getNodeId().equals(node.getNodeId())) {
        top = node;
    }
    return top;
}
Also used : IStoredNode(org.exist.dom.persistent.IStoredNode)

Example 7 with IStoredNode

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

the class NativeSerializer method serializeToReceiver.

protected void serializeToReceiver(IStoredNode node, INodeIterator iter, DocumentImpl doc, boolean first, Match match, Set<String> namespaces) throws SAXException {
    if (node == null && iter.hasNext()) {
        node = iter.next();
    }
    if (node == null) {
        return;
    }
    // char ch[];
    String cdata;
    switch(node.getNodeType()) {
        case Node.ELEMENT_NODE:
            receiver.setCurrentNode(node);
            String defaultNS = null;
            if (((ElementImpl) node).declaresNamespacePrefixes()) {
                // declare namespaces used by this element
                String prefix, uri;
                for (final Iterator<String> i = ((ElementImpl) node).getPrefixes(); i.hasNext(); ) {
                    prefix = i.next();
                    if (prefix.isEmpty()) {
                        defaultNS = ((ElementImpl) node).getNamespaceForPrefix(prefix);
                        receiver.startPrefixMapping(XMLConstants.DEFAULT_NS_PREFIX, defaultNS);
                        namespaces.add(defaultNS);
                    } else {
                        uri = ((ElementImpl) node).getNamespaceForPrefix(prefix);
                        receiver.startPrefixMapping(prefix, uri);
                        namespaces.add(uri);
                    }
                }
            }
            final String ns = defaultNS == null ? node.getNamespaceURI() : defaultNS;
            if (ns != null && ns.length() > 0 && (!namespaces.contains(ns))) {
                String prefix = node.getPrefix();
                if (prefix == null) {
                    prefix = XMLConstants.DEFAULT_NS_PREFIX;
                }
                receiver.startPrefixMapping(prefix, ns);
            }
            final AttrList attribs = new AttrList();
            if ((first && showId == EXIST_ID_ELEMENT) || showId == EXIST_ID_ALL) {
                attribs.addAttribute(ID_ATTRIB, node.getNodeId().toString());
            /* 
             * This is a proposed fix-up that the serializer could do
             * to make sure elements always have the namespace declarations
             *
            } else {
               // This is fix-up for when the node has a namespace but there is no
               // namespace declaration.
               String elementNS = node.getNamespaceURI();
               Node parent = node.getParentNode();
               if (parent instanceof ElementImpl) {
                  ElementImpl parentElement = (ElementImpl)parent;
                  String declaredNS = parentElement.getNamespaceForPrefix(node.getPrefix());
                  if (elementNS!=null && declaredNS==null) {
                     // We need to declare the prefix as it was missed somehow
                     receiver.startPrefixMapping(node.getPrefix(), elementNS);
                  } else if (elementNS==null && declaredNS!=null) {
                     // We need to declare the default namespace to be the no namespace
                     receiver.startPrefixMapping(node.getPrefix(), elementNS);
                  } else if (!elementNS.equals(defaultNS)) {
                     // Same prefix but different namespace
                     receiver.startPrefixMapping(node.getPrefix(), elementNS);
                  }
               } else if (elementNS!=null) {
                  // If the parent is the document, we must have a namespace
                  // declaration when there is a namespace URI.
                  receiver.startPrefixMapping(node.getPrefix(), elementNS);
               }
             */
            }
            if (first && showId > 0) {
                // String src = doc.getCollection().getName() + "/" + doc.getFileName();
                attribs.addAttribute(SOURCE_ATTRIB, doc.getFileURI().toString());
            }
            final int children = node.getChildCount();
            int count = 0;
            IStoredNode child = null;
            StringBuilder matchAttrCdata = null;
            StringBuilder matchAttrOffsetsCdata = null;
            StringBuilder matchAttrLengthsCdata = null;
            while (count < children) {
                child = iter.hasNext() ? iter.next() : null;
                if (child != null && child.getNodeType() == Node.ATTRIBUTE_NODE) {
                    if ((getHighlightingMode() & TAG_ATTRIBUTE_MATCHES) == TAG_ATTRIBUTE_MATCHES && match != null && child.getNodeId().equals(match.getNodeId())) {
                        if (matchAttrCdata == null) {
                            matchAttrCdata = new StringBuilder();
                            matchAttrOffsetsCdata = new StringBuilder();
                            matchAttrLengthsCdata = new StringBuilder();
                        } else {
                            matchAttrCdata.append(",");
                            matchAttrOffsetsCdata.append(",");
                            matchAttrLengthsCdata.append(",");
                        }
                        matchAttrCdata.append(child.getQName().toString());
                        matchAttrOffsetsCdata.append(match.getOffset(0).getOffset());
                        matchAttrLengthsCdata.append(match.getOffset(0).getLength());
                        match = match.getNextMatch();
                    }
                    cdata = ((AttrImpl) child).getValue();
                    attribs.addAttribute(child.getQName(), cdata);
                    count++;
                    child.release();
                } else {
                    break;
                }
            }
            if (matchAttrCdata != null) {
                attribs.addAttribute(MATCHES_ATTRIB, matchAttrCdata.toString());
                // mask the full-text index which doesn't provide offset and length
                M_ZERO_VALUES.reset(matchAttrOffsetsCdata);
                final boolean offsetsIsZero = M_ZERO_VALUES.matches();
                M_ZERO_VALUES.reset(matchAttrLengthsCdata);
                final boolean lengthsIsZero = M_ZERO_VALUES.matches();
                if (!offsetsIsZero && !lengthsIsZero) {
                    attribs.addAttribute(MATCHES_OFFSET_ATTRIB, matchAttrOffsetsCdata.toString());
                    attribs.addAttribute(MATCHES_LENGTH_ATTRIB, matchAttrLengthsCdata.toString());
                }
            }
            receiver.setCurrentNode(node);
            receiver.startElement(node.getQName(), attribs);
            while (count < children) {
                serializeToReceiver(child, iter, doc, false, match, namespaces);
                if (++count < children) {
                    child = iter.hasNext() ? iter.next() : null;
                } else {
                    break;
                }
            }
            receiver.setCurrentNode(node);
            receiver.endElement(node.getQName());
            if (((ElementImpl) node).declaresNamespacePrefixes()) {
                for (final Iterator<String> i = ((ElementImpl) node).getPrefixes(); i.hasNext(); ) {
                    final String prefix = i.next();
                    receiver.endPrefixMapping(prefix);
                }
            }
            if (ns != null && ns.length() > 0 && (!namespaces.contains(ns))) {
                String prefix = node.getPrefix();
                if (prefix == null) {
                    prefix = XMLConstants.DEFAULT_NS_PREFIX;
                }
                receiver.endPrefixMapping(prefix);
            }
            node.release();
            break;
        case Node.TEXT_NODE:
            if (first && createContainerElements) {
                final AttrList tattribs = new AttrList();
                if (showId > 0) {
                    tattribs.addAttribute(ID_ATTRIB, node.getNodeId().toString());
                    tattribs.addAttribute(SOURCE_ATTRIB, doc.getFileURI().toString());
                }
                receiver.startElement(TEXT_ELEMENT, tattribs);
            }
            receiver.setCurrentNode(node);
            receiver.characters(((TextImpl) node).getXMLString());
            if (first && createContainerElements) {
                receiver.endElement(TEXT_ELEMENT);
            }
            node.release();
            break;
        case Node.ATTRIBUTE_NODE:
            if ((getHighlightingMode() & TAG_ATTRIBUTE_MATCHES) == TAG_ATTRIBUTE_MATCHES && match != null && node.getNodeId().equals(match.getNodeId())) {
            // TODO(AR) do we need to expand attribute matches here also? see {@code matchAttrCdata} above
            }
            cdata = ((AttrImpl) node).getValue();
            if (first) {
                if (createContainerElements) {
                    final AttrList tattribs = new AttrList();
                    if (showId > 0) {
                        tattribs.addAttribute(ID_ATTRIB, node.getNodeId().toString());
                        tattribs.addAttribute(SOURCE_ATTRIB, doc.getFileURI().toString());
                    }
                    tattribs.addAttribute(node.getQName(), cdata);
                    receiver.startElement(ATTRIB_ELEMENT, tattribs);
                    receiver.endElement(ATTRIB_ELEMENT);
                } else {
                    if (this.outputProperties.getProperty("output-method") != null && "text".equals(this.outputProperties.getProperty("output-method"))) {
                        receiver.characters(node.getNodeValue());
                    } else {
                        LOG.warn("Error SENR0001: attribute '{}' has no parent element. While serializing document {}", node.getQName(), doc.getURI());
                        throw new SAXException("Error SENR0001: attribute '" + node.getQName() + "' has no parent element");
                    }
                }
            } else {
                receiver.attribute(node.getQName(), cdata);
            }
            node.release();
            break;
        case Node.DOCUMENT_TYPE_NODE:
            final String systemId = ((DocumentTypeImpl) node).getSystemId();
            final String publicId = ((DocumentTypeImpl) node).getPublicId();
            final String name = ((DocumentTypeImpl) node).getName();
            receiver.documentType(name, publicId, systemId);
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            receiver.processingInstruction(((ProcessingInstructionImpl) node).getTarget(), ((ProcessingInstructionImpl) node).getData());
            node.release();
            break;
        case Node.COMMENT_NODE:
            final String comment = ((CommentImpl) node).getData();
            char[] data = new char[comment.length()];
            comment.getChars(0, data.length, data, 0);
            receiver.comment(data, 0, data.length);
            node.release();
            break;
        case Node.CDATA_SECTION_NODE:
            final String str = ((CDATASectionImpl) node).getData();
            if (first) {
                receiver.characters(str);
            } else {
                data = new char[str.length()];
                str.getChars(0, str.length(), data, 0);
                receiver.cdataSection(data, 0, data.length);
            }
            break;
    }
}
Also used : DocumentTypeImpl(org.exist.dom.persistent.DocumentTypeImpl) SAXException(org.xml.sax.SAXException) ElementImpl(org.exist.dom.persistent.ElementImpl) AttrList(org.exist.util.serializer.AttrList) CommentImpl(org.exist.dom.persistent.CommentImpl) CDATASectionImpl(org.exist.dom.persistent.CDATASectionImpl) IStoredNode(org.exist.dom.persistent.IStoredNode)

Example 8 with IStoredNode

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

the class NativeSerializer method serializeToReceiver.

protected void serializeToReceiver(DocumentImpl doc, boolean generateDocEvent) throws SAXException {
    final long start = System.currentTimeMillis();
    setDocument(doc);
    final NodeList children = doc.getChildNodes();
    if (generateDocEvent && !documentStarted) {
        receiver.startDocument();
        documentStarted = true;
    }
    if (doc.getDoctype() != null) {
        if ("yes".equals(getProperty(EXistOutputKeys.OUTPUT_DOCTYPE, "no"))) {
            final DocumentTypeImpl docType = (DocumentTypeImpl) doc.getDoctype();
            serializeToReceiver(docType, null, docType.getOwnerDocument(), true, null, new TreeSet<>());
        }
    }
    // iterate through children
    for (int i = 0; i < children.getLength(); i++) {
        final IStoredNode<?> node = (IStoredNode<?>) children.item(i);
        try (final INodeIterator domIter = broker.getNodeIterator(node)) {
            domIter.next();
            final NodeProxy p = new NodeProxy(node);
            serializeToReceiver(node, domIter, (DocumentImpl) node.getOwnerDocument(), true, p.getMatches(), new TreeSet<>());
        } catch (final IOException ioe) {
            LOG.warn("Unable to close node iterator", ioe);
        }
    }
    if (generateDocEvent) {
        receiver.endDocument();
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("serializing document {} ({}) to SAX took {} msec", doc.getDocId(), doc.getURI(), System.currentTimeMillis() - start);
    }
}
Also used : INodeIterator(org.exist.storage.dom.INodeIterator) NodeList(org.w3c.dom.NodeList) DocumentTypeImpl(org.exist.dom.persistent.DocumentTypeImpl) IOException(java.io.IOException) NodeProxy(org.exist.dom.persistent.NodeProxy) IStoredNode(org.exist.dom.persistent.IStoredNode)

Example 9 with IStoredNode

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

the class LuceneMatchListener method getPath.

public static NodePath getPath(final NodeProxy proxy) {
    final NodePath2 path = new NodePath2();
    final IStoredNode<?> node = (IStoredNode<?>) proxy.getNode();
    walkAncestor(node, path);
    return path;
}
Also used : NodePath2(org.exist.storage.NodePath2) IStoredNode(org.exist.dom.persistent.IStoredNode)

Aggregations

IStoredNode (org.exist.dom.persistent.IStoredNode)9 IOException (java.io.IOException)2 DocumentTypeImpl (org.exist.dom.persistent.DocumentTypeImpl)2 NodePath2 (org.exist.storage.NodePath2)2 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 QName (org.exist.dom.QName)1 CDATASectionImpl (org.exist.dom.persistent.CDATASectionImpl)1 CommentImpl (org.exist.dom.persistent.CommentImpl)1 ElementImpl (org.exist.dom.persistent.ElementImpl)1 NodeProxy (org.exist.dom.persistent.NodeProxy)1 IndexSpec (org.exist.storage.IndexSpec)1 BTreeException (org.exist.storage.btree.BTreeException)1 INodeIterator (org.exist.storage.dom.INodeIterator)1 LockException (org.exist.util.LockException)1 AttrList (org.exist.util.serializer.AttrList)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1