Search in sources :

Example 1 with NamespaceNode

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

the class AdaptiveWriter method write.

/**
 * Serialize the given sequence using adaptive output mode.
 *
 * @param sequence input sequence
 * @param itemSep separator string to output between items in the sequence
 * @param enclose if set to true: enclose sequences of items into parentheses
 * @throws SAXException if an error occurs during serialization
 * @throws XPathException if an XPath error occurs
 * @throws TransformerException if an error occurs whilst transforming
 */
public void write(final Sequence sequence, final String itemSep, final boolean enclose) throws SAXException, XPathException, TransformerException {
    try {
        if (enclose && sequence.getItemCount() != 1) {
            writer.write('(');
        }
        for (final SequenceIterator si = sequence.iterate(); si.hasNext(); ) {
            final Item item = si.nextItem();
            switch(item.getType()) {
                case Type.DOCUMENT:
                case Type.ELEMENT:
                case Type.TEXT:
                case Type.COMMENT:
                case Type.CDATA_SECTION:
                case Type.PROCESSING_INSTRUCTION:
                    writeXML(item);
                    break;
                case Type.ATTRIBUTE:
                    final Attr node = (Attr) ((NodeValue) item).getNode();
                    writeText(node.getName() + "=\"" + node.getValue() + '"');
                    break;
                case Type.NAMESPACE:
                    final NamespaceNode ns = (NamespaceNode) item;
                    writeText(ns.getName() + "=\"" + ns.getValue() + '"');
                    break;
                case Type.STRING:
                case Type.NORMALIZED_STRING:
                case Type.TOKEN:
                case Type.LANGUAGE:
                case Type.NMTOKEN:
                case Type.NAME:
                case Type.NCNAME:
                case Type.ID:
                case Type.IDREF:
                case Type.ENTITY:
                case Type.UNTYPED_ATOMIC:
                case Type.ANY_URI:
                    final String v = item.getStringValue();
                    writeText('"' + escapeQuotes(v) + '"');
                    break;
                case Type.INTEGER:
                case Type.DECIMAL:
                case Type.INT:
                case Type.LONG:
                case Type.SHORT:
                case Type.BYTE:
                case Type.UNSIGNED_LONG:
                case Type.UNSIGNED_INT:
                case Type.UNSIGNED_SHORT:
                case Type.UNSIGNED_BYTE:
                case Type.NON_NEGATIVE_INTEGER:
                case Type.NON_POSITIVE_INTEGER:
                case Type.POSITIVE_INTEGER:
                case Type.NEGATIVE_INTEGER:
                    writeText(item.getStringValue());
                    break;
                case Type.DOUBLE:
                    writeDouble((DoubleValue) item);
                    break;
                case Type.BOOLEAN:
                    writeText(item.getStringValue() + "()");
                    break;
                case Type.QNAME:
                case Type.NOTATION:
                    final QName qn = ((QNameValue) item).getQName();
                    writeText("Q{" + qn.getNamespaceURI() + '}' + qn.getLocalPart());
                    break;
                case Type.ARRAY:
                    writeArray((ArrayType) item);
                    break;
                case Type.MAP:
                    writeMap((AbstractMapType) item);
                    break;
                case Type.FUNCTION_REFERENCE:
                    writeFunctionItem((FunctionReference) item);
                    break;
                default:
                    writeAtomic(item.atomize());
                    break;
            }
            if (si.hasNext()) {
                try {
                    writer.write(itemSep);
                } catch (IOException e) {
                    throw new SAXException(e.getMessage());
                }
            }
        }
        if (enclose && sequence.getItemCount() != 1) {
            writer.write(')');
        }
    } catch (IOException e) {
        throw new SAXException(e.getMessage(), e);
    }
}
Also used : QName(org.exist.dom.QName) NamespaceNode(org.exist.dom.memtree.NamespaceNode) IOException(java.io.IOException) Attr(org.w3c.dom.Attr) SAXException(org.xml.sax.SAXException)

Aggregations

IOException (java.io.IOException)1 QName (org.exist.dom.QName)1 NamespaceNode (org.exist.dom.memtree.NamespaceNode)1 Attr (org.w3c.dom.Attr)1 SAXException (org.xml.sax.SAXException)1