package net.sf.jpluck.xml;
import java.util.Iterator;
import net.sf.jpluck.util.EmptyIterator;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.JXPathException;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.ri.model.beans.NullPointer;
public class ContextAdapter {
private JXPathContext context;
public ContextAdapter(Object object) {
this.context=JXPathContext.newContext(object);
}
public ContextAdapter(JXPathContext context) {
this.context=context;
}
public Iterator iterate(String xPath) {
try {
return context.iterate(xPath);
} catch (JXPathException e) {
return new EmptyIterator();
}
}
public Iterator iteratePointers(String xPath) {
try {
return context.iteratePointers(xPath);
} catch (JXPathException e) {
return new EmptyIterator();
}
}
public Pointer getPointer(String xPath) {
try {
Pointer p =context.getPointer(xPath);
if (p instanceof NullPointer) {
return null;
} else {
return p;
}
} catch (JXPathException e) {
return null;
}
}
public Object getValue(String xPath) {
try {
return context.getValue(xPath);
} catch (JXPathException e) {
return null;
}
}
public Object getValue(String xPath, Object defaultValue) {
Object value = getValue(xPath);
if (value != null) {
return value;
} else {
return defaultValue;
}
}
public void setValue(String xPath, Object value) {
context.createPathAndSetValue(xPath, value);
}
public void remove(String xPath) {
try {
context.removePath(xPath);
}
catch (JXPathException e) {
}
}
public void removeAll(String xPath) {
try {
context.removeAll(xPath);
}
catch (JXPathException e) {
}
}
}