Example usage for org.jdom2 Element getNamespace

List of usage examples for org.jdom2 Element getNamespace

Introduction

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

Prototype

public Namespace getNamespace() 

Source Link

Document

Returns the element's Namespace .

Usage

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

License:Apache License

/**
 * Method updateElement.//w  w  w .  j a  v a2  s. c  o  m
 * 
 * @param counter
 * @param shouldExist
 * @param name
 * @param parent
 * @return Element
 */
public static Element updateElement(final IndentationCounter counter, final Element parent, final String name,
        final boolean shouldExist) {
    Element element = parent.getChild(name, parent.getNamespace());
    if (shouldExist) {
        if (element == null) {
            element = factory.element(name, parent.getNamespace());
            insertAtPreferredLocation(parent, element, counter);
        }
        counter.increaseCount();
    } else if (element != null) {
        final int index = parent.indexOf(element);
        if (index > 0) {
            final Content previous = parent.getContent(index - 1);
            if (previous instanceof Text) {
                final Text txt = (Text) previous;
                if (txt.getTextTrim().length() == 0) {
                    parent.removeContent(txt);
                }
            }
        }
        parent.removeContent(element);
    }
    return element;
}

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

License:Apache License

/**
 * Method replaceXpp3DOM./*from   w w w.  jav  a2s .  co  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());
        }
    }
}

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

License:Apache License

/**
 * Method findAndReplaceSimpleElement.//  w  w w .jav  a  2  s .c  o  m
 * 
 * @param counter
 * @param defaultValue
 * @param text
 * @param name
 * @param parent
 * @return Element
 */
public static Element findAndReplaceSimpleElement(final IndentationCounter counter, final Element parent,
        final String name, final String text, final String defaultValue) {
    if ((defaultValue != null) && (text != null) && defaultValue.equals(text)) {
        final Element element = parent.getChild(name, parent.getNamespace());
        // if exist and is default value or if doesn't exist.. just keep the way it is..
        if ((element != null && defaultValue.equals(element.getText())) || element == null) {
            return element;
        }
    }
    final boolean shouldExist = (text != null) && (text.trim().length() >= 0);
    final Element element = updateElement(counter, parent, name, shouldExist);
    if (shouldExist) {
        element.setText(text);
    }
    return element;
}

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

License:Apache License

/**
 * Method findAndReplaceSimpleLists./*from ww w .j a v  a2s. c o m*/
 * 
 * @param counter
 * @param childName
 * @param parentName
 * @param list
 * @param parent
 * @return Element
 */
public static Element findAndReplaceSimpleLists(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final String parentName, final String childName) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentName, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childName, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final String value = (String) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childName, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            el.setText(value);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
    return element;
}

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

License:Apache License

/**
 * Method iterateContributor.//w  w w . j av a  2s . c om
 * 
 * @param counter
 * @param childTag
 * @param parentTag
 * @param list
 * @param parent
 */
protected void iterateContributor(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final java.lang.String parentTag, final java.lang.String childTag) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentTag, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childTag, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final Contributor value = (Contributor) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childTag, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            updateContributor(value, childTag, innerCount, el);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
}

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

License:Apache License

/**
 * Method iterateDependency./* www . j  a v  a2  s.  c o  m*/
 * 
 * @param counter
 * @param childTag
 * @param parentTag
 * @param list
 * @param parent
 */
protected void iterateDependency(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final java.lang.String parentTag, final java.lang.String childTag) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentTag, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childTag, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final Dependency value = (Dependency) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childTag, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            updateDependency(value, childTag, innerCount, el);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
}

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

License:Apache License

/**
 * Method iterateDeveloper.//from   w  w  w .j  av a 2s . c om
 * 
 * @param counter
 * @param childTag
 * @param parentTag
 * @param list
 * @param parent
 */
protected void iterateDeveloper(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final java.lang.String parentTag, final java.lang.String childTag) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentTag, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childTag, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final Developer value = (Developer) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childTag, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            updateDeveloper(value, childTag, innerCount, el);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
}

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

License:Apache License

/**
 * Method iterateExclusion.//from   w  ww.  j  av a 2  s.  com
 * 
 * @param counter
 * @param childTag
 * @param parentTag
 * @param list
 * @param parent
 */
protected void iterateExclusion(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final java.lang.String parentTag, final java.lang.String childTag) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentTag, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childTag, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final Exclusion value = (Exclusion) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childTag, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            updateExclusion(value, childTag, innerCount, el);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
}

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

License:Apache License

/**
 * Method iterateExtension./* w  w w. ja v a  2  s  .com*/
 * 
 * @param counter
 * @param childTag
 * @param parentTag
 * @param list
 * @param parent
 */
protected void iterateExtension(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final java.lang.String parentTag, final java.lang.String childTag) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentTag, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childTag, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final Extension value = (Extension) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childTag, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            updateExtension(value, childTag, innerCount, el);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
}

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

License:Apache License

/**
 * Method iterateLicense./*  w  w w  .  ja  v a  2  s .  com*/
 * 
 * @param counter
 * @param childTag
 * @param parentTag
 * @param list
 * @param parent
 */
protected void iterateLicense(final IndentationCounter counter, final Element parent,
        final java.util.Collection list, final java.lang.String parentTag, final java.lang.String childTag) {
    final boolean shouldExist = (list != null) && (list.size() > 0);
    final Element element = updateElement(counter, parent, parentTag, shouldExist);
    if (shouldExist) {
        final Iterator it = list.iterator();
        Iterator elIt = element.getChildren(childTag, element.getNamespace()).iterator();
        if (!elIt.hasNext()) {
            elIt = null;
        }
        final IndentationCounter innerCount = new IndentationCounter(counter.getDepth() + 1);
        while (it.hasNext()) {
            final License value = (License) it.next();
            Element el;
            if (elIt != null && elIt.hasNext()) {
                el = (Element) elIt.next();
                if (!elIt.hasNext()) {
                    elIt = null;
                }
            } else {
                el = factory.element(childTag, element.getNamespace());
                insertAtPreferredLocation(element, el, innerCount);
            }
            updateLicense(value, childTag, innerCount, el);
            innerCount.increaseCount();
        }
        if (elIt != null) {
            while (elIt.hasNext()) {
                elIt.next();
                elIt.remove();
            }
        }
    }
}