Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to design API which could handle XPATH input from user. Currently i have model the XPATH input in following way,

public interface ICondition {
    String getConditionString();
}

public class XPathCondition implements ICondition {

    private Class<? extends XPATHFunction> clazz = null;

    private Operator operator = null;

    private String compValue = null;

    private String param = null;

    public void setXPathFunction(Class<? extends XPATHFunction> 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 function which have to implement and interface XPATHFunction and implement it in its way.

API just have create XPATHCondition and set appropriate function and call getConditionString() method to get the final xpath.

Is there any better way, we can model XPATH input?

Please help me to re factor the above design.

share|improve this question

1 Answer

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?

share|improve this answer
Cygal, This not homework.I am designing BPEL genertation tool and my BPEL tool API is such way that user should give XPATH as input.However enter xpath as String leads to error prone.That why i want model user input xpath into OOP. For you information BPEL contains xpath to navigate through the xml node. – Sheru Mar 1 '12 at 10:50
1  
So you want to validate XPath expressions? Are you aware that you can use javax.xml.xpath for this? Use XPath.evaluate() and catch the XPathExpressionException. I'd be glad to help if I didn't understand once again what you're willing to do. – Quentin Pradet Mar 1 '12 at 12:56
Cygal,We are generating BPEL tool by accepting some of inputs from user from those inputs one input is XPATH.There could be 2 options we can accept the XPATH from user 1:take XPATH as regular expression as it is.for example setXPATH("/bookstore/book/title") 2: We can model input side using Object Oriented way.In short i want give API wrapper on xpath input side. I don not want validate the XPATH.My goal is accept xpath input from user using classes and object and use this input objects and classes while generating BPEL file. – Sheru Mar 4 '12 at 11:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.