use of org.dom4j.XPath in project openolat by klemens.
the class CPManifestTreeModel method initDocument.
private void initDocument(Document doc) {
// get all organization elements. need to set namespace
rootElement = doc.getRootElement();
String nsuri = rootElement.getNamespace().getURI();
nsuris.put("ns", nsuri);
XPath meta = rootElement.createXPath("//ns:organization");
meta.setNamespaceURIs(nsuris);
// TODO: accept several organizations?
Element orgaEl = (Element) meta.selectSingleNode(rootElement);
if (orgaEl == null)
throw new AssertException("could not find element organization");
XPath metares = rootElement.createXPath("//ns:resources");
metares.setNamespaceURIs(nsuris);
Element elResources = (Element) metares.selectSingleNode(rootElement);
if (elResources == null)
throw new AssertException("could not find element resources");
@SuppressWarnings("unchecked") List<Element> resourcesList = elResources.elements("resource");
resources = new HashMap<String, String>(resourcesList.size());
for (Iterator<Element> iter = resourcesList.iterator(); iter.hasNext(); ) {
Element elRes = iter.next();
String identVal = elRes.attributeValue("identifier");
String hrefVal = elRes.attributeValue("href");
if (hrefVal != null) {
// href is optional element for resource element
try {
hrefVal = URLDecoder.decode(hrefVal, "UTF-8");
} catch (UnsupportedEncodingException e) {
// each JVM must implement UTF-8
}
}
resources.put(identVal, hrefVal);
}
GenericTreeNode gtn = buildNode(orgaEl);
setRootNode(gtn);
// help gc
rootElement = null;
resources = null;
}
use of org.dom4j.XPath in project openolat by klemens.
the class CPManifestTreeModel method buildNode.
private GenericTreeNode buildNode(Element item) {
GenericTreeNode gtn = new GenericTreeNode();
// fxdiff VCRP-13: cp navigation
treeNodes.add(gtn);
// extract title
String title = item.elementText("title");
if (title == null)
title = item.attributeValue("identifier");
String identifier = item.attributeValue("identifier");
gtn.setAltText(title);
gtn.setTitle(title);
if (item.getName().equals("organization")) {
// Add first level item for organization
gtn.setIconCssClass("o_cp_org");
gtn.setAccessible(false);
// Special case check: CP with only one page: hide the page and show it directly under the organization element
@SuppressWarnings("unchecked") List<Element> chds = item.elements("item");
if (chds.size() == 1) {
// check 1: only one child
Element childitem = chds.get(0);
@SuppressWarnings("unchecked") List<Element> grandChds = childitem.elements("item");
if (grandChds.size() == 0) {
// check 2: no grand children
String identifierref = childitem.attributeValue("identifierref");
String href = resources.get(identifierref);
if (href != null) {
// check 3: a resource is attached to the child
// = success, we have a CP with only one page. Use this page and exit
XPath meta = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
meta.setNamespaceURIs(nsuris);
gtn.setAccessible(true);
gtn.setUserObject(href);
if (hrefToTreeNode.containsKey(href)) {
log.debug("Duplicate href::" + href + " for identifierref::" + identifierref + " and identifier::" + identifier + ", use first one");
} else {
hrefToTreeNode.put(href, gtn);
}
return gtn;
}
}
}
} else if (item.getName().equals("item")) {
gtn.setIconCssClass("o_cp_item");
// set resolved file path directly
String identifierref = item.attributeValue("identifierref");
if (identifierref != null) {
gtn.setIdent("cp" + Encoder.md5hash(identPrefix + identifierref));
}
XPath meta = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
meta.setNamespaceURIs(nsuris);
String href = resources.get(identifierref);
if (href != null) {
gtn.setUserObject(href);
// allow lookup of a treenode given a href so we can quickly adjust the menu if the user clicks on hyperlinks within the text
if (hrefToTreeNode.containsKey(href)) {
log.debug("Duplicate href::" + href + " for identifierref::" + identifierref + " and identifier::" + identifier + ", use first one");
} else {
hrefToTreeNode.put(href, gtn);
}
} else {
gtn.setAccessible(false);
}
}
@SuppressWarnings("unchecked") List<Element> chds = item.elements("item");
int childcnt = chds.size();
for (int i = 0; i < childcnt; i++) {
Element childitem = chds.get(i);
GenericTreeNode gtnchild = buildNode(childitem);
gtn.addChild(gtnchild);
// set the first accessible child in the hierarchy as delegate when the node itself is not accessible
if (gtn.isAccessible() == false) {
GenericTreeNode nextHierarchyChild = gtnchild;
while (gtn.getDelegate() == null && nextHierarchyChild != null) {
if (nextHierarchyChild.isAccessible()) {
gtn.setDelegate(nextHierarchyChild);
} else {
if (nextHierarchyChild.getChildCount() > 0) {
nextHierarchyChild = (GenericTreeNode) nextHierarchyChild.getChildAt(0);
} else {
nextHierarchyChild = null;
}
}
}
if (!gtn.isAccessible()) {
log.debug("No accessible child found that could be used as delegate for identifier::" + identifier);
}
}
}
return gtn;
}
Aggregations