private void createPathQuery(String path, int operation) {
MergingPathQueryNode pathNode = new MergingPathQueryNode(operation);
pathNode.setAbsolute(true);
if (path.equals("/")) {
pathNode.addPathStep(new LocationStepQueryNode(pathNode));
pathConstraints.add(pathNode);
return;
}
String[] names = path.split("/");
for (int i = 0; i < names.length; i++) {
if (names[i].length() == 0) {
if (i == 0) {
// root
pathNode.addPathStep(new LocationStepQueryNode(pathNode));
} else {
// descendant '//' -> invalid path
// todo throw or ignore?
// we currently do not throw and add location step for an
// empty name (which is basically the root node)
pathNode.addPathStep(new LocationStepQueryNode(pathNode));
}
} else {
int idx = names[i].indexOf('[');
String name;
int index = LocationStepQueryNode.NONE;
if (idx > -1) {
// contains index
name = names[i].substring(0, idx);
String suffix = names[i].substring(idx);
String indexStr = suffix.substring(1, suffix.length() - 1);
if (indexStr.equals("%")) {
// select all same name siblings
index = LocationStepQueryNode.NONE;
} else {
try {
index = Integer.parseInt(indexStr);
} catch (NumberFormatException e) {
log.warn("Unable to parse index for path element: " + names[i]);
}
}
if (name.equals("%")) {
name = null;
}
} else {
// no index specified
// - index defaults to 1 if there is an explicit name test
// - index defaults to NONE if name test is %
name = names[i];
if (name.equals("%")) {
name = null;
} else {
index = 1;
}
}
QName qName = null;
if (name != null) {
try {
qName = QName.fromJCRName(name, resolver);
} catch (IllegalNameException e) {
throw new IllegalArgumentException("Illegal name: " + name);
} catch (UnknownPrefixException e) {
throw new IllegalArgumentException("Unknown prefix: " + name);
}
}
// if name test is % this means also search descendants
boolean descendant = name == null;
LocationStepQueryNode step = new LocationStepQueryNode(pathNode, qName, descendant);
if (index > 0) {
step.setIndex(index);
}
pathNode.addPathStep(step);
}
}
pathConstraints.add(pathNode);