use of org.exist.util.serializer.AttrList in project exist by eXist-db.
the class SystemExport method writeXML.
/**
* Serialize a document to XML, based on {@link XMLStreamReader}.
*
* @param doc the document to serialize
* @param receiver the output handler
*/
private void writeXML(final DocumentImpl doc, final Receiver receiver) {
try {
char[] ch;
int nsdecls;
final NamespaceSupport nsSupport = new NamespaceSupport();
final NodeList children = doc.getChildNodes();
final DocumentType docType = doc.getDoctype();
if (docType != null) {
receiver.documentType(docType.getName(), docType.getPublicId(), docType.getSystemId());
}
for (int i = 0; i < children.getLength(); i++) {
final StoredNode child = (StoredNode) children.item(i);
final int thisLevel = child.getNodeId().getTreeLevel();
final int childLevel = child.getNodeType() == Node.ELEMENT_NODE ? thisLevel + 1 : thisLevel;
final XMLStreamReader reader = broker.getXMLStreamReader(child, false);
while (reader.hasNext()) {
final int status = reader.next();
switch(status) {
case XMLStreamReader.START_DOCUMENT:
case XMLStreamReader.END_DOCUMENT:
break;
case XMLStreamReader.START_ELEMENT:
nsdecls = reader.getNamespaceCount();
for (int ni = 0; ni < nsdecls; ni++) {
receiver.startPrefixMapping(reader.getNamespacePrefix(ni), reader.getNamespaceURI(ni));
}
final AttrList attribs = new AttrList();
for (int j = 0; j < reader.getAttributeCount(); j++) {
final QName qn = new QName(reader.getAttributeLocalName(j), reader.getAttributeNamespace(j), reader.getAttributePrefix(j));
attribs.addAttribute(qn, reader.getAttributeValue(j));
}
receiver.startElement(new QName(reader.getLocalName(), reader.getNamespaceURI(), reader.getPrefix()), attribs);
break;
case XMLStreamReader.END_ELEMENT:
receiver.endElement(new QName(reader.getLocalName(), reader.getNamespaceURI(), reader.getPrefix()));
nsdecls = reader.getNamespaceCount();
for (int ni = 0; ni < nsdecls; ni++) {
receiver.endPrefixMapping(reader.getNamespacePrefix(ni));
}
final NodeId otherId = (NodeId) reader.getProperty(ExtendedXMLStreamReader.PROPERTY_NODE_ID);
final int otherLevel = otherId.getTreeLevel();
if (childLevel != thisLevel && otherLevel == thisLevel) {
// exit-while
break;
}
break;
case XMLStreamReader.CHARACTERS:
receiver.characters(reader.getText());
break;
case XMLStreamReader.CDATA:
ch = reader.getTextCharacters();
receiver.cdataSection(ch, 0, ch.length);
break;
case XMLStreamReader.COMMENT:
ch = reader.getTextCharacters();
receiver.comment(ch, 0, ch.length);
break;
case XMLStreamReader.PROCESSING_INSTRUCTION:
receiver.processingInstruction(reader.getPITarget(), reader.getPIData());
break;
}
if (child.getNodeType() == Node.COMMENT_NODE || child.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
break;
}
}
nsSupport.reset();
}
} catch (final IOException | SAXException | XMLStreamException e) {
e.printStackTrace();
}
}
use of org.exist.util.serializer.AttrList in project exist by eXist-db.
the class EmbeddedXMLStreamReader method readAttributes.
private void readAttributes() {
if (attributes == null) {
final ElementEvent parent = elementStack.peek();
final int count = getAttributeCount();
attributes = new AttrList();
for (int i = 0; i < count; i++) {
final Value v = iterator.next();
AttrImpl.addToList(broker, v.data(), v.start(), v.getLength(), attributes);
parent.incrementChild();
}
}
}
use of org.exist.util.serializer.AttrList in project exist by eXist-db.
the class Serializer method toSAX.
/**
* Serializes an Item
*
* @param item The item to serialize
* @param wrap Indicates whether the output should be wrapped
* @param typed Indicates whether the output types should be wrapped
*
* @throws SAXException If an error occurs during serialization
*/
public void toSAX(final Item item, final boolean wrap, final boolean typed) throws SAXException {
try {
setStylesheetFromProperties(null);
} catch (final TransformerConfigurationException e) {
throw new SAXException(e.getMessage(), e);
}
setXSLHandler(null, false);
final AttrList attrs = new AttrList();
attrs.addAttribute(ATTR_HITS_QNAME, "1");
attrs.addAttribute(ATTR_START_QNAME, "1");
attrs.addAttribute(ATTR_COUNT_QNAME, "1");
if (outputProperties.getProperty(PROPERTY_SESSION_ID) != null) {
attrs.addAttribute(ATTR_SESSION_ID, outputProperties.getProperty(PROPERTY_SESSION_ID));
}
if (!documentStarted) {
receiver.startDocument();
documentStarted = true;
}
if (wrap) {
receiver.startPrefixMapping("exist", Namespaces.EXIST_NS);
receiver.startElement(ELEM_RESULT_QNAME, attrs);
}
itemToSAX(item, typed, wrap);
if (wrap) {
receiver.endElement(ELEM_RESULT_QNAME);
receiver.endPrefixMapping("exist");
}
receiver.endDocument();
}
use of org.exist.util.serializer.AttrList 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
* @param start The position in the sequence to start serialization from
* @param count The number of items from the start position to serialize
* @param wrap Indicates whether the output should be wrapped
* @param typed Indicates whether the output types should be wrapped
* @param compilationTime The time taken to compile the query which produced the sequence
* @param executionTime The time taken to execute the query which produced the sequence
*
* @throws SAXException If an error occurs during serialization
*/
public void toSAX(final Sequence seq, int start, final int count, final boolean wrap, final boolean typed, final long compilationTime, final long executionTime) throws SAXException {
try {
setStylesheetFromProperties(null);
} catch (final TransformerConfigurationException e) {
throw new SAXException(e.getMessage(), e);
}
setXSLHandler(null, false);
final AttrList attrs = new AttrList();
attrs.addAttribute(ATTR_HITS_QNAME, Integer.toString(seq.getItemCount()));
attrs.addAttribute(ATTR_START_QNAME, Integer.toString(start));
attrs.addAttribute(ATTR_COUNT_QNAME, Integer.toString(count));
if (outputProperties.getProperty(PROPERTY_SESSION_ID) != null) {
attrs.addAttribute(ATTR_SESSION_ID, outputProperties.getProperty(PROPERTY_SESSION_ID));
}
attrs.addAttribute(ATTR_COMPILATION_TIME_QNAME, Long.toString(compilationTime));
attrs.addAttribute(ATTR_EXECUTION_TIME_QNAME, Long.toString(compilationTime));
if (!documentStarted) {
receiver.startDocument();
documentStarted = true;
}
if (wrap) {
receiver.startPrefixMapping("exist", Namespaces.EXIST_NS);
receiver.startElement(ELEM_RESULT_QNAME, attrs);
}
for (int i = --start; i < start + count; i++) {
final Item item = seq.itemAt(i);
if (item == null) {
LOG.debug("item {} not found", i);
continue;
}
itemToSAX(item, typed, wrap);
}
if (wrap) {
receiver.endElement(ELEM_RESULT_QNAME);
receiver.endPrefixMapping("exist");
}
receiver.endDocument();
}
use of org.exist.util.serializer.AttrList in project exist by eXist-db.
the class Serializer method itemToSAX.
private void itemToSAX(final Item item, final boolean typed, final boolean wrap) throws SAXException {
if (Type.subTypeOf(item.getType(), Type.NODE)) {
final NodeValue node = (NodeValue) item;
if (typed) {
// TODO the typed and wrapped stuff should ideally be replaced
// with Marshaller.marshallItem
// unfortrunately calling Marshaller.marshallItem(broker, item, new SAXToReceiver(receiver))
// results in a stack overflow
// TODO consider a full XDM serializer in place of this for these special needs
serializeTypePreNode(node);
if (node.getType() == Type.ATTRIBUTE) {
serializeTypeAttributeValue(node);
} else {
serializeToReceiver(node, false);
}
serializeTypePostNode(node);
} else {
serializeToReceiver(node, false);
}
} else {
if (wrap) {
final AttrList attrs = new AttrList();
attrs.addAttribute(ATTR_TYPE_QNAME, Type.getTypeName(item.getType()));
receiver.startElement(ELEM_VALUE_QNAME, attrs);
}
try {
receiver.characters(item.getStringValue());
} catch (final XPathException e) {
throw new SAXException(e.getMessage(), e);
}
if (wrap) {
receiver.endElement(ELEM_VALUE_QNAME);
}
}
}
Aggregations