package org.jostraca.tree.path;
import java.io.StringReader;
import java.util.List;
import org.jostraca.tree.Node;
public class Path {
private StepPath mStepPath;
//two functions: select(node,path) and value(node,path) can be used inside paths
//eg: /some/other/branch[@id=value($node,sub/@ref)]
//where $node has child <sub ref="..." />
public Path( String pPathStr ) throws Exception {
StringReader pathReader = new StringReader( pPathStr );
PathLexer pathLexer = new PathLexer( pathReader );
PathParser pathParser = new PathParser( pathLexer );
pathParser.main();
mStepPath = pathParser.getStepPath();
}
public Path( StepPath pStepPath ) {
mStepPath = pStepPath;
}
public List<Node> select(Node pNode) {
return mStepPath.select(pNode);
}
public String value(Node node) {
String value = "";
List<Node> nodes = mStepPath.select(node);
if( 0 < nodes.size() ) {
Node n = nodes.get(0);
String attr = mStepPath.getAttribute();
String attrtype = mStepPath.getAttributeType();
if( null != attr ) {
value = n.getAttr( attr, attrtype );
}
else {
value = n.getText();
}
}
return value;
}
public String toString() {
return mStepPath.toString();
}
public boolean hasAttribute() {
return null != mStepPath.getAttribute();
}
}