Example usage for org.jdom2 Verifier isAllXMLWhitespace

List of usage examples for org.jdom2 Verifier isAllXMLWhitespace

Introduction

In this page you can find the example usage for org.jdom2 Verifier isAllXMLWhitespace.

Prototype

public static final boolean isAllXMLWhitespace(final String value) 

Source Link

Document

This is a utility function for determining whether a specified String is a whitespace character according to production 3 of the XML 1.0 specification.

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  w  w  .ja va  2s .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);
}