package net.sf.jpluck.jxl;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.Pointer;
import org.w3c.dom.Element;
import java.util.Iterator;
import net.sf.jpluck.util.EmptyIterator;
public abstract class JXLElement {
protected Element element;
private JXPathContext context;
protected JXLElement(Element element) {
this.element = element;
}
protected Element getElement() {
return element;
}
private JXPathContext getContext() {
// Lazy intialization
if (context == null) {
context = JXPathContext.newContext(element);
context.setFactory(new ElementFactory());
}
return context;
}
protected Iterator iterate(String xPath) {
try {
return getContext().iterate(xPath);
} catch (JXPathException e) {
return new EmptyIterator();
}
}
protected Iterator iteratePointers(String xPath) {
try {
return getContext().iteratePointers(xPath);
} catch (JXPathException e) {
return new EmptyIterator();
}
}
protected Pointer getPointer(String xPath) {
try {
return getContext().getPointer(xPath);
} catch (JXPathException e) {
return null;
}
}
protected Object getValue(String xPath) {
try {
return getContext().getValue(xPath);
} catch (JXPathException e) {
return null;
}
}
protected Object getValue(String xPath, Object defaultValue) {
Object value = getValue(xPath);
if (value != null) {
return value;
} else {
return defaultValue;
}
}
protected Element getElement(String xPath) {
Pointer p = getPointer(xPath);
if (p != null) {
return (Element) p.getNode();
} else {
return null;
}
}
}