use of org.exist.util.serializer.AttrList in project exist by eXist-db.
the class Serializer method serializeTypePreNode.
/**
* Writes a start element for DOCUMENT, ATTRIBUTE and TEXT nodes.
* This is required for the XQJ API implementation.
*
* @param item a NodeValue which will be wrapped in a element.
* @throws SAXException if the element can't be serialized
* @author Charles Foster
*/
protected void serializeTypePreNode(NodeValue item) throws SAXException {
AttrList attrs = null;
switch(item.getType()) {
case Type.DOCUMENT:
final String baseUri = ((Document) item).getBaseURI();
attrs = new AttrList();
if (baseUri != null && baseUri.length() > 0) {
attrs.addAttribute(ATTR_URI_QNAME, baseUri);
}
if (((Document) item).getDocumentElement() == null) {
attrs.addAttribute(ATTR_HAS_ELEMENT_QNAME, "false");
}
receiver.startElement(ELEM_DOC_QNAME, attrs);
break;
case Type.ATTRIBUTE:
attrs = new AttrList();
String attributeValue;
if ((attributeValue = item.getNode().getLocalName()) != null && attributeValue.length() > 0) {
attrs.addAttribute(ATTR_LOCAL_QNAME, attributeValue);
}
if ((attributeValue = item.getNode().getNamespaceURI()) != null && attributeValue.length() > 0) {
attrs.addAttribute(ATTR_TNS_QNAME, attributeValue);
}
if ((attributeValue = item.getNode().getPrefix()) != null && attributeValue.length() > 0) {
attrs.addAttribute(ATTR_PREFIX_QNAME, attributeValue);
}
receiver.startElement(ELEM_ATTR_QNAME, attrs);
break;
case Type.TEXT:
receiver.startElement(ELEM_TEXT_QNAME, null);
break;
default:
}
}
use of org.exist.util.serializer.AttrList 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;
}
}
use of org.exist.util.serializer.AttrList in project exist by eXist-db.
the class DocumentImpl method startNode.
private void startNode(final Serializer serializer, final NodeImpl node, final Receiver receiver) throws SAXException {
final int nr = node.nodeNumber;
switch(node.getNodeType()) {
case Node.ELEMENT_NODE:
final QName nodeName = document.nodeName[nr];
// Output required namespace declarations
int ns = document.alphaLen[nr];
if (ns > -1) {
while ((ns < document.nextNamespace) && (document.namespaceParent[ns] == nr)) {
final QName nsQName = document.namespaceCode[ns];
if (XMLConstants.XMLNS_ATTRIBUTE.equals(nsQName.getLocalPart())) {
receiver.startPrefixMapping(XMLConstants.DEFAULT_NS_PREFIX, nsQName.getNamespaceURI());
} else {
receiver.startPrefixMapping(nsQName.getLocalPart(), nsQName.getNamespaceURI());
}
++ns;
}
}
// Create the attribute list
AttrList attribs = null;
int attr = document.alpha[nr];
if (attr > -1) {
attribs = new AttrList();
while ((attr < document.nextAttr) && (document.attrParent[attr] == nr)) {
final QName attrQName = document.attrName[attr];
attribs.addAttribute(attrQName, attrValue[attr]);
++attr;
}
}
receiver.startElement(nodeName, attribs);
break;
case Node.TEXT_NODE:
receiver.characters(new String(document.characters, document.alpha[nr], document.alphaLen[nr]));
break;
case Node.ATTRIBUTE_NODE:
final QName attrQName = document.attrName[nr];
receiver.attribute(attrQName, attrValue[nr]);
break;
case Node.COMMENT_NODE:
receiver.comment(document.characters, document.alpha[nr], document.alphaLen[nr]);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
final QName qn = document.nodeName[nr];
final String data = new String(document.characters, document.alpha[nr], document.alphaLen[nr]);
receiver.processingInstruction(qn.getLocalPart(), data);
break;
case Node.CDATA_SECTION_NODE:
receiver.cdataSection(document.characters, document.alpha[nr], document.alphaLen[nr]);
break;
case NodeImpl.REFERENCE_NODE:
serializer.toReceiver(document.references[document.alpha[nr]], true, false);
break;
}
}
Aggregations