Example usage for org.apache.commons.io Document clone

List of usage examples for org.apache.commons.io Document clone

Introduction

In this page you can find the example usage for org.apache.commons.io Document clone.

Prototype

@HotSpotIntrinsicCandidate
protected native Object clone() throws CloneNotSupportedException;

Source Link

Document

Creates and returns a copy of this object.

Usage

From source file:com.jetbrains.pluginUtils.xml.JDOMXIncluder.java

public static Document resolve(@NotNull Document original, @Nullable String base) throws XIncludeException {
    Document result = (Document) original.clone();

    Element root = result.getRootElement();
    List<Content> resolved = resolve(root, base);

    // check that the list returned contains
    // exactly one root element
    Element newRoot = null;/* w w  w  . ja  v  a 2 s.com*/
    Iterator<Content> iterator = resolved.iterator();
    while (iterator.hasNext()) {
        Content o = iterator.next();
        if (o instanceof Element) {
            if (newRoot != null) {
                throw new XIncludeException("Tried to include multiple roots");
            }
            newRoot = (Element) o;
        } else if (o instanceof Comment || o instanceof ProcessingInstruction) {
            // do nothing
        } else if (o instanceof Text) {
            throw new XIncludeException("Tried to include text node outside of root element");
        } else if (o instanceof EntityRef) {
            throw new XIncludeException("Tried to include a general entity reference outside of root element");
        } else {
            throw new XIncludeException("Unexpected type " + o.getClass());
        }

    }

    if (newRoot == null) {
        throw new XIncludeException("No root element");
    }

    // Could probably combine two loops
    List<Content> newContent = result.getContent();
    // resolved contains list of new content
    // use it to replace old root element
    iterator = resolved.iterator();

    // put in nodes before root element
    int rootPosition = newContent.indexOf(result.getRootElement());
    while (iterator.hasNext()) {
        Content o = iterator.next();
        if (o instanceof Comment || o instanceof ProcessingInstruction) {
            newContent.add(rootPosition, o);
            rootPosition++;
        } else if (o instanceof Element) { // the root
            break;
        } else {
            // throw exception????
        }
    }

    // put in root element
    result.setRootElement(newRoot);

    int addPosition = rootPosition + 1;
    // put in nodes after root element
    while (iterator.hasNext()) {
        Content o = iterator.next();
        if (o instanceof Comment || o instanceof ProcessingInstruction) {
            newContent.add(addPosition, o);
            addPosition++;
        } else {
            // throw exception????
        }
    }

    return result;
}