DocumentFragment domTemplate = null;
String src = DomHelper.getAttribute(bindingElm, "src", null);
if (src != null) {
ServiceManager manager = assistant.getServiceManager();
SourceResolver sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE);
Source source = null;
try {
source = sourceResolver.resolveURI(src);
Document document = SourceUtil.toDOM(source);
Element element = document.getDocumentElement();
String xpath = DomHelper.getAttribute(bindingElm, "xpath", null);
if (xpath != null) {
XPathProcessor xpathProcessor = (XPathProcessor) manager.lookup(XPathProcessor.ROLE);
try {
Node node = xpathProcessor.selectSingleNode(document, xpath);
if (node == null) {
throw new BindingException("XPath expression '" + xpath + "' didn't return a result.",
DomHelper.getLocationObject(bindingElm));
}
if (!(node instanceof Element)) {
throw new BindingException("XPath expression '" + xpath + "' did not return an element node.",
DomHelper.getLocationObject(bindingElm));
}
element = (Element) node;
} finally {
manager.release(xpathProcessor);
}
}
domTemplate = document.createDocumentFragment();
domTemplate.appendChild(element);
} finally {
if (source != null) {
sourceResolver.release(source);
}
manager.release(sourceResolver);
}
} else if (bindingElm.hasChildNodes()) {
// FIXME: using the binding's document prevents it to be garbage collected.
// --> create a new Document and use doc.importNode();
domTemplate = bindingElm.getOwnerDocument().createDocumentFragment();