Example usage for org.jdom2.output.support Walker isCDATA

List of usage examples for org.jdom2.output.support Walker isCDATA

Introduction

In this page you can find the example usage for org.jdom2.output.support Walker isCDATA.

Prototype

public abstract boolean isCDATA();

Source Link

Document

If the previous next() method returned null, then this will indicate whether the current text() value is CDATA or regular Text.

Usage

From source file:org.apache.maven.io.util.AbstractJDOMWriter.java

License:Apache License

public final void write(final T source, final Document document, final Writer writer, final Format jdomFormat,
        final DocumentModifier modifier) throws java.io.IOException {
    if (modifier != null) {
        modifier.preProcess(document);/*w ww . ja v a2 s. c  o m*/
    }
    update(source, new IndentationCounter(0), document.getRootElement());
    if (modifier != null) {
        modifier.postProcess(document);
    }
    // Override XMLOutputter to correct initial comment trailing newlines.
    final XMLOutputter outputter = new XMLOutputter(new AbstractXMLOutputProcessor() {
        /**
         * This will handle printing of a {@link Document}.
         *
         * @param out    <code>Writer</code> to use.
         * @param fstack the FormatStack
         * @param nstack the NamespaceStack
         * @param doc    <code>Document</code> to write.
         * @throws IOException if the destination Writer fails
         */
        @Override
        protected void printDocument(Writer out, FormatStack fstack, NamespaceStack nstack, Document doc)
                throws IOException {

            // If there is no root element then we cannot use the normal ways to
            // access the ContentList because Document throws an exception.
            // so we hack it and just access it by index.
            List<Content> list = doc.hasRootElement() ? doc.getContent()
                    : new ArrayList<Content>(doc.getContentSize());
            if (list.isEmpty()) {
                final int sz = doc.getContentSize();
                for (int i = 0; i < sz; i++) {
                    list.add(doc.getContent(i));
                }
            }

            printDeclaration(out, fstack);

            Walker walker = buildWalker(fstack, list, true);
            if (walker.hasNext()) {
                while (walker.hasNext()) {

                    final Content c = walker.next();
                    // we do not ignore Text-like things in the Document.
                    // the walker creates the indenting for us.
                    if (c == null) {
                        // but, what we do is ensure it is all whitespace, and not CDATA
                        final String padding = walker.text();
                        if (padding != null && Verifier.isAllXMLWhitespace(padding) && !walker.isCDATA()) {
                            // we do not use the escaping or text* method because this
                            // content is outside of the root element, and thus is not
                            // strict text.
                            write(out, padding);
                        }
                    } else {
                        switch (c.getCType()) {
                        case Comment:
                            printComment(out, fstack, (Comment) c);
                            // This modification we have made to the overridden method in order
                            // to correct newline declarations.
                            write(out, fstack.getLineSeparator());
                            break;
                        case DocType:
                            printDocType(out, fstack, (DocType) c);
                            break;
                        case Element:
                            printElement(out, fstack, nstack, (Element) c);
                            if (walker.hasNext()) {
                                // This modification we have made to the overridden method in order
                                // to correct newline declarations.
                                write(out, fstack.getLineSeparator());
                            }
                            break;
                        case ProcessingInstruction:
                            printProcessingInstruction(out, fstack, (ProcessingInstruction) c);
                            break;
                        case Text:
                            final String padding = ((Text) c).getText();
                            if (padding != null && Verifier.isAllXMLWhitespace(padding)) {
                                // we do not use the escaping or text* method because this
                                // content is outside of the root element, and thus is not
                                // strict text.
                                write(out, padding);
                            }
                        default:
                            // do nothing.
                        }
                    }

                }

                if (fstack.getLineSeparator() != null) {
                    write(out, fstack.getLineSeparator());
                }
            }
        }
    });

    outputter.setFormat(jdomFormat);
    outputter.output(document, writer);
}