Example usage for org.jdom2 Text append

List of usage examples for org.jdom2 Text append

Introduction

In this page you can find the example usage for org.jdom2 Text append.

Prototype

public void append(Text text) 

Source Link

Document

This will append the content of another Text node to this node.

Usage

From source file:org.openconcerto.xml.JDOM2Utils.java

License:Open Source License

/**
 * Get the filtered content of an element, optionnaly merging adjacent {@link Text}. Adjacent
 * text can only happen programmatically.
 * //from w w  w.java 2 s  . co  m
 * @param elem the parent.
 * @param pred which content to return.
 * @param mergeText <code>true</code> if adjacent Text should be merged into one,
 *        <code>false</code> to leave the list as it is.
 * @return the filtered content (not supportting {@link Iterator#remove()}).
 */
public static Iterator<Content> getContent(final Element elem, final IPredicate<? super Content> pred,
        final boolean mergeText) {
    final Iterator<Content> iter = (Iterator<Content>) elem.getContent(new AbstractFilter<Content>() {
        @Override
        public Content filter(Object obj) {
            final Content c = (Content) obj;
            return pred.evaluateChecked(c) ? c : null;
        }
    }).iterator();
    if (!mergeText)
        return iter;

    return new Iterator<Content>() {

        private Content next = null;

        @Override
        public boolean hasNext() {
            return this.next != null || iter.hasNext();
        }

        @Override
        public Content next() {
            if (this.next != null) {
                final Content res = this.next;
                this.next = null;
                return res;
            }

            Content res = iter.next();
            assert res != null;
            if (res instanceof Text && iter.hasNext()) {
                this.next = iter.next();
                Text concatText = null;
                while (this.next instanceof Text) {
                    if (concatText == null) {
                        concatText = new Text(res.getValue());
                    }
                    concatText.append((Text) this.next);
                    this.next = iter.hasNext() ? iter.next() : null;
                }
                assert this.next != null;
                if (concatText != null)
                    res = concatText;
            }

            return res;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    };
}