Example usage for org.jdom2 Element getNamespacesInherited

List of usage examples for org.jdom2 Element getNamespacesInherited

Introduction

In this page you can find the example usage for org.jdom2 Element getNamespacesInherited.

Prototype

@Override
    public List<Namespace> getNamespacesInherited() 

Source Link

Usage

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

License:Apache License

/**
 * Method replaceXpp3DOM.//from www.  j  a v  a2 s .c o m
 * 
 * @param parent
 * @param counter
 * @param parentDom
 */
public static void replaceXpp3DOM(final Element parent, final Xpp3Dom parentDom,
        final IndentationCounter counter) {
    if (parentDom.getChildCount() > 0) {
        final Xpp3Dom[] childs = parentDom.getChildren();
        final Collection domChilds = new ArrayList();
        for (final Xpp3Dom child : childs) {
            domChilds.add(child);
        }
        final ListIterator it = parent.getChildren().listIterator();
        while (it.hasNext()) {
            final Element elem = (Element) it.next();
            final Iterator it2 = domChilds.iterator();
            Xpp3Dom corrDom = null;
            while (it2.hasNext()) {
                final Xpp3Dom dm = (Xpp3Dom) it2.next();
                if (dm.getName().equals(elem.getName())) {
                    corrDom = dm;
                    break;
                }
            }
            if (corrDom != null) {
                domChilds.remove(corrDom);
                replaceXpp3DOM(elem, corrDom, new IndentationCounter(counter.getDepth() + 1));
                counter.increaseCount();
            } else {
                it.remove();
            }
        }
        final Iterator it2 = domChilds.iterator();
        while (it2.hasNext()) {
            final Xpp3Dom dm = (Xpp3Dom) it2.next();
            final String rawName = dm.getName();
            final String[] parts = rawName.split(":");

            Element elem;
            if (parts.length > 1) {
                final String nsId = parts[0];
                String nsUrl = dm.getAttribute("xmlns:" + nsId);
                final String name = parts[1];
                if (nsUrl == null) {
                    nsUrl = parentDom.getAttribute("xmlns:" + nsId);
                }
                elem = factory.element(name, Namespace.getNamespace(nsId, nsUrl));
            } else {
                Namespace root = parent.getNamespace();
                for (Namespace n : parent.getNamespacesInherited()) {
                    if (n.getPrefix() == null || n.getPrefix().length() == 0) {
                        root = n;
                        break;
                    }
                }
                elem = factory.element(dm.getName(), root);
            }

            final String[] attributeNames = dm.getAttributeNames();
            for (final String attrName : attributeNames) {
                if (attrName.startsWith("xmlns:")) {
                    continue;
                }
                elem.setAttribute(attrName, dm.getAttribute(attrName));
            }

            insertAtPreferredLocation(parent, elem, counter);
            counter.increaseCount();
            replaceXpp3DOM(elem, dm, new IndentationCounter(counter.getDepth() + 1));
        }
    } else if (parentDom.getValue() != null) {
        List<Content> cl = parent.getContent();
        boolean foundCdata = false;
        for (Content c : cl) {
            if (c instanceof CDATA) {
                foundCdata = true;
            }
        }

        if (!foundCdata) {
            parent.setText(parentDom.getValue());
        }
    }
}