1
\$\begingroup\$

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<? 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 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?

\$\endgroup\$
1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Sharad Ahire Mar 1 '12 at 10:50
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Quentin Pradet Mar 1 '12 at 12:56
  • \$\begingroup\$ 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. \$\endgroup\$ – Sharad Ahire Mar 4 '12 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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