Handling XPATH input from a user I want to design API which could handle XPATH input from a user. I currently model the XPATH input in the following way:

public interface ICondition {
    String getConditionString();
}

public class XPathCondition implements ICondition {

    private Class&lt;? extends XPATHFunction&gt; clazz = null;

    private Operator operator = null;

    private String compValue = null;

    private String param = null;

    public void setXPathFunction(Class&lt;? extends XPATHFunction&gt; clazz) {
        this.clazz = clazz;
    }

    public void setComparisionType(Operator operator) {
        this.operator = operator;
    }

    public void setComparisionValue(String value) {
        this.compValue = value;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getConditionString() {
        XPATHFunction function = null;
        try {
            function = (XPATHFunction) clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return function.call(param) + operator.getOprValue() + compValue;
    }

    public static void main(String[] args) {
        XPathCondition xpathCond = new XPathCondition();
        xpathCond.setXPathFunction(CountFunction.class);
        xpathCond.setParam("/CPRRegistrationInfo/*");
        xpathCond.setComparisionType(Operator.GT);
        xpathCond.setComparisionValue("0");
        System.out.println(xpathCond.getConditionString());
    }
}


public interface XPATHFunction {

    public String call(String param);

}


public class CountFunction implements XPATHFunction {

    public String call(String param) {
        return "count(" + param + ") ";
    }

}


There could be other XPATH functions which have to implement and interface XPATHFunction and implement it in its way. The API just has to create XPATHCondition and set appropriate functions and call the getConditionString() method to get the final xpath.

Is there any better way in which I can model XPATH input?
Can I ask why you're trying to do this? If this is homework or a way to "practice Java/Object-Oriented programming", please say so. If it's a real question, the best way to represent XPath input is actually a string. XPath is way more complicated than what you've (XPath 2.0 even more so), and you would need a lot of more work to model this properly. If it's a subset of XPAth, then what subset?

Edit: A few examples.


Valid XPath expressions:

ancestor-or-self::*
../employee[@secretary and @assistant]
substring("12345", 1.5, 2.6)
child::para[position()=5][attribute::type="warning"]

Valid XPath 2.0 expressions:

($x div $y) + xs:decimal($z)
fn:error(xs:QName("app:err057"), "Unexpected value", fn:string($v))



Also note that you can evaluate XPath expressions using the standard javax.xml.xpath, thus not needing rolling your own. If you need something else, maybe reuse the source code of javax.xml.xpath to suit your needs?