1
我想要設計可以處理來自用戶的XPATH輸入的API。 目前我有以下方式XPATH輸入模型,用於從用戶獲取xpath輸入的設計API
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 + ") ";
}
}
有可能是具有實施和接口XPATHFunction和它的方式實現它的其他XPath函數。
API只是創建XPATHCondition並設置適當的函數並調用getConditionString()方法來獲取最終的xpath。
有沒有更好的方法,我們可以模擬XPATH輸入?
請幫助我重新考慮上述設計。
我不明白是什麼你想做嗎。用戶輸入XPath? ...該系統應該用這個XPath做什麼..? – MiMo 2012-03-22 16:30:32
目前我們正在接受XPATH作爲純字符串input.I想要使用一些API來構建xpath,所以它會最大限度地減少錯誤的機會。所以用戶將通過使用我們的API給出xpath,並且最終輸出API是完整的xpath字符串。 – 2012-03-29 05:04:14
謝謝,現在我不知道 – MiMo 2012-03-29 13:52:25