*/
private void calculateXPath(boolean recurse) {
// Get the parent of this instance, which is expected to be either null
// or another instance of this class
final ODOMElement parent = (ODOMElement) getParent();
if (parent == null) {
// No parent, so we're top-level, so we don't need an xpath
xpath = null;
} else if (parent instanceof ProxyElement) {
Namespace ns = details.getElementNamespace();
if (ns != null) {
String uri = ns.getURI();
if (uri == null || "".equals(uri)) {
ns = null;
} else {
String prefix = ns.getPrefix();
if ((prefix == null) ||
("".equals(prefix))) {
// @todo dream up a unique internal prefix
prefix = "lpdm";
ns = Namespace.getNamespace(prefix, ns.getURI());
}
}
}
// Parent is one of us, so we know our xpath will not be null, and
// therefore we'll need the namespace in a form suitable for the
// XPath constructors
final Namespace[] namespaces =
(ns == null
? null
: new Namespace[]{ns});
// Now get the parent's xpath: if it's null, our xpath is just an
// (absolute) path to the proxied element; otherwise, we just
// append the name of the proxied element to the parent's xpath
final XPath parentXPath = ((ProxyElement) parent).xpath;
if (parentXPath == null) {
if (ns != null) {
xpath = new ODOMXPath(new StringBuffer(ns.getPrefix()).
append(':').
append(details.getElementName()).
toString(),
namespaces);
} else {
xpath = new ODOMXPath(details.getElementName(), namespaces);
}
} else {
if (ns != null) {
xpath = new ODOMXPath(parentXPath,
new StringBuffer(ns.getPrefix()).
append(':').
append(details.getElementName()).
toString(),
namespaces);
} else {
xpath = new ODOMXPath
(parentXPath, details.getElementName(), namespaces);
}
}
} else {
// Parent is a non-null non-ProxyElement
throw new IllegalStateException("PE's parent is not PE");
}
// Last thing to do is get all this instance's children to re-calculate
// their xpaths, because their xpaths depend on their parent's just
// like ours did above
if (recurse) {
final Iterator childIter = getChildren().iterator();
while (childIter.hasNext()) {
ODOMElement child = (ODOMElement) childIter.next();
if (child instanceof ProxyElement) {
((ProxyElement) child).calculateXPath(true);
} else {
throw new IllegalStateException("Child is not a PE");
}