Search in sources :

Example 1 with CommentImpl

use of org.exist.dom.persistent.CommentImpl 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 2 with CommentImpl

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

the class Indexer method comment.

@Override
public void comment(final char[] ch, final int start, final int length) {
    if (insideDTD) {
        return;
    }
    final CommentImpl comment = new CommentImpl(ch, start, length);
    comment.setOwnerDocument(document);
    if (stack.isEmpty()) {
        comment.setNodeId(broker.getBrokerPool().getNodeFactory().createInstance(nodeFactoryInstanceCnt++));
        if (!validate) {
            broker.storeNode(transaction, comment, currentPath, indexSpec);
        }
        document.appendChild((NodeHandle) comment);
    } else {
        final ElementImpl last = stack.peek();
        processText(last, ProcessTextParent.COMMENT);
        last.appendChildInternal(prevNode, comment);
        setPrevious(comment);
        if (!validate) {
            broker.storeNode(transaction, comment, currentPath, indexSpec);
        }
    }
}
Also used : ElementImpl(org.exist.dom.persistent.ElementImpl) CommentImpl(org.exist.dom.persistent.CommentImpl)

Aggregations

CommentImpl (org.exist.dom.persistent.CommentImpl)2 ElementImpl (org.exist.dom.persistent.ElementImpl)2 CDATASectionImpl (org.exist.dom.persistent.CDATASectionImpl)1 DocumentTypeImpl (org.exist.dom.persistent.DocumentTypeImpl)1 IStoredNode (org.exist.dom.persistent.IStoredNode)1 AttrList (org.exist.util.serializer.AttrList)1 SAXException (org.xml.sax.SAXException)1