com.github.simy4.xpath.jdom.navigator.node.JDomElement Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xpath-to-xml-jdom Show documentation
Show all versions of xpath-to-xml-jdom Show documentation
Convenient utility to build XML models by evaluating XPath expressions
package com.github.simy4.xpath.jdom.navigator.node;
import com.github.simy4.xpath.XmlBuilderException;
import org.jdom2.Attribute;
import org.jdom2.Content;
import org.jdom2.Element;
import org.jdom2.IllegalAddException;
import org.jdom2.Parent;
import org.jdom2.Text;
import org.jdom2.filter.Filter;
import org.jdom2.filter.Filters;
import javax.xml.namespace.QName;
import java.util.Iterator;
import java.util.List;
public final class JDomElement extends AbstractJDomNode {
private static final long serialVersionUID = 1L;
public JDomElement(Element element) {
super(element);
}
@Override
public QName getName() {
return new QName(getNode().getNamespaceURI(), getNode().getName(), getNode().getNamespacePrefix());
}
@Override
public String getText() {
return getNode().getText();
}
@Override
public JDomNode getRoot() {
return new JDomDocument(getNode().getDocument());
}
@Override
@SuppressWarnings("ReferenceEquality")
public JDomNode getParent() {
final Element node = getNode();
final Parent parent = node.getParent();
return null == parent ? node.getDocument().getRootElement() == node ? getRoot() : null
: new JDomElement((Element) parent);
}
@Override
public Iterable extends JDomNode> elements() {
return new Iterable() {
@Override
public Iterator iterator() {
return new JDomElementIterator(getNode().getChildren().iterator());
}
};
}
@Override
public Iterable extends JDomNode> attributes() {
return new Iterable() {
@Override
public Iterator iterator() {
return new JDomAttributeIterator(getNode().getAttributes().iterator());
}
};
}
@Override
public JDomNode appendAttribute(Attribute attribute) throws XmlBuilderException {
try {
getNode().setAttribute(attribute);
return new JDomAttribute(attribute);
} catch (IllegalAddException iae) {
throw new XmlBuilderException("Unable to append an attribute to " + getNode(), iae);
}
}
@Override
public JDomNode appendElement(Element element) throws XmlBuilderException {
try {
getNode().addContent(element);
return new JDomElement(element);
} catch (IllegalAddException iae) {
throw new XmlBuilderException("Unable to append an element to " + getNode(), iae);
}
}
@Override
public void prependCopy() throws XmlBuilderException {
final Element node = getNode();
final Parent parent = node.getParent();
if (null == parent) {
throw new XmlBuilderException("Unable to prepend - no parent found of " + node);
}
final int prependIndex = parent.indexOf(node);
final Element copy = node.clone();
parent.addContent(prependIndex, copy);
}
@Override
@SuppressWarnings("unchecked")
public void setText(String text) throws XmlBuilderException {
try {
final Filter filter = (Filter) Filters.text().negate();
final List content = getNode().getContent(filter);
getNode().setContent(content);
getNode().addContent(new Text(text));
} catch (IllegalAddException iae) {
throw new XmlBuilderException("Unable to set value to " + getNode(), iae);
}
}
@Override
public void remove() {
getNode().detach();
}
private static final class JDomAttributeIterator implements Iterator {
private final Iterator attributeIterator;
private JDomAttributeIterator(Iterator attributeIterator) {
this.attributeIterator = attributeIterator;
}
@Override
public boolean hasNext() {
return attributeIterator.hasNext();
}
@Override
public JDomAttribute next() {
return new JDomAttribute(attributeIterator.next());
}
@Override
public void remove() {
attributeIterator.remove();
}
}
private static final class JDomElementIterator implements Iterator {
private final Iterator elementIterator;
private JDomElementIterator(Iterator elementIterator) {
this.elementIterator = elementIterator;
}
@Override
public boolean hasNext() {
return elementIterator.hasNext();
}
@Override
public JDomElement next() {
return new JDomElement(elementIterator.next());
}
@Override
public void remove() {
elementIterator.remove();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy