All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.simy4.xpath.dom4j.navigator.node.Dom4jElement Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
package com.github.simy4.xpath.dom4j.navigator.node;

import com.github.simy4.xpath.util.Function;
import com.github.simy4.xpath.util.TransformingIterator;
import org.dom4j.Attribute;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Namespace;

import javax.xml.namespace.QName;
import java.util.Iterator;

public final class Dom4jElement extends AbstractDom4jNode {

    public Dom4jElement(Element element) {
        super(element);
    }

    @Override
    public QName getName() {
        final Namespace namespace = getNode().getNamespace();
        return new QName(namespace.getURI(), getNode().getName(), namespace.getPrefix());
    }

    @Override
    public Iterable> elements() {
        return new Iterable>() {
            @Override
            public Iterator> iterator() {
                return new TransformingIterator>(getNode().elementIterator(),
                        new Dom4jElementWrapper());
            }
        };
    }

    @Override
    public Iterable> attributes() {
        return new Iterable>() {
            @Override
            public Iterator> iterator() {
                return new TransformingIterator>(getNode().attributeIterator(),
                        new Dom4jAttributeWrapper());
            }
        };
    }

    @Override
    public Dom4jNode createAttribute(org.dom4j.QName attribute) {
        final Attribute attr = DocumentHelper.createAttribute(getNode(), attribute, "");
        getNode().attributes().add(attr);
        return new Dom4jAttribute(attr);
    }

    @Override
    public Dom4jNode createElement(org.dom4j.QName element) {
        return new Dom4jElement(this.getNode().addElement(element));
    }

    private static final class Dom4jAttributeWrapper implements Function> {

        @Override
        public Dom4jNode apply(Attribute attribute) {
            return new Dom4jAttribute(attribute);
        }

    }

    private static final class Dom4jElementWrapper implements Function> {

        @Override
        public Dom4jNode apply(Element element) {
            return new Dom4jElement(element);
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy