Example usage for org.jdom2 Text clone

List of usage examples for org.jdom2 Text clone

Introduction

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

Prototype

@Override
    public Text clone() 

Source Link

Usage

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

License:Apache License

/**
 * Method insertAtPreferredLocation./*from w w  w . j a  v a2  s.com*/
 * 
 * @param parent
 * @param counter
 * @param child
 */
public static void insertAtPreferredLocation(final Element parent, final Element child,
        final IndentationCounter counter) {
    int contentIndex = 0;
    int elementCounter = 0;
    final Iterator it = parent.getContent().iterator();
    Text lastText = null;
    int offset = 0;
    while (it.hasNext() && elementCounter <= counter.getCurrentIndex()) {
        final Object next = it.next();
        offset = offset + 1;
        if (next instanceof Element) {
            elementCounter = elementCounter + 1;
            contentIndex = contentIndex + offset;
            offset = 0;
        }
        if (next instanceof Text && it.hasNext()) {
            lastText = (Text) next;
        }
    }
    if (lastText != null && lastText.getTextTrim().length() == 0) {
        lastText = lastText.clone();
    } else {
        String starter = lineSeparator;
        for (int i = 0; i < counter.getDepth(); i++) {
            starter = starter + INDENT; // TODO make settable?
        }
        lastText = factory.text(starter);
    }
    if (parent.getContentSize() == 0) {
        final Text finalText = lastText.clone();
        finalText.setText(finalText.getText().substring(0, finalText.getText().length() - INDENT.length()));
        parent.addContent(contentIndex, finalText);
    }
    parent.addContent(contentIndex, child);
    parent.addContent(contentIndex, lastText);
}

From source file:org.apache.maven.toolchain.model.io.jdom.MavenToolchainsJDOMWriter.java

License:Apache License

/**
 * Method insertAtPreferredLocation./*  w  w w.  j av  a2 s .co  m*/
 * 
 * @param parent
 * @param counter
 * @param child
 */
protected void insertAtPreferredLocation(final Element parent, final Element child,
        final IndentationCounter counter) {
    int contentIndex = 0;
    int elementCounter = 0;
    final Iterator it = parent.getContent().iterator();
    Text lastText = null;
    int offset = 0;
    while (it.hasNext() && elementCounter <= counter.getCurrentIndex()) {
        final Object next = it.next();
        offset = offset + 1;
        if (next instanceof Element) {
            elementCounter = elementCounter + 1;
            contentIndex = contentIndex + offset;
            offset = 0;
        }
        if (next instanceof Text && it.hasNext()) {
            lastText = (Text) next;
        }
    }
    if (lastText != null && lastText.getTextTrim().length() == 0) {
        lastText = lastText.clone();
    } else {
        String starter = lineSeparator;
        for (int i = 0; i < counter.getDepth(); i++) {
            starter = starter + INDENT; // TODO make settable?
        }
        lastText = factory.text(starter);
    }
    if (parent.getContentSize() == 0) {
        final Text finalText = lastText.clone();
        finalText.setText(finalText.getText().substring(0, finalText.getText().length() - INDENT.length()));
        parent.addContent(contentIndex, finalText);
    }
    parent.addContent(contentIndex, child);
    parent.addContent(contentIndex, lastText);
}