Search in sources :

Example 26 with XPath

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;
}
Also used : XPath(org.dom4j.XPath) AssertException(org.olat.core.logging.AssertException) GenericTreeNode(org.olat.core.gui.components.tree.GenericTreeNode) Element(org.dom4j.Element) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 27 with XPath

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;
}
Also used : XPath(org.dom4j.XPath) GenericTreeNode(org.olat.core.gui.components.tree.GenericTreeNode) Element(org.dom4j.Element)

Aggregations

XPath (org.dom4j.XPath)27 Element (org.dom4j.Element)18 List (java.util.List)9 Iterator (java.util.Iterator)8 HashMap (java.util.HashMap)7 GenericTreeNode (org.olat.core.gui.components.tree.GenericTreeNode)6 Node (org.dom4j.Node)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 FileSystemException (org.apache.commons.vfs2.FileSystemException)3 KettleException (org.pentaho.di.core.exception.KettleException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Date (java.util.Date)2 Set (java.util.Set)2 FileObject (org.apache.commons.vfs2.FileObject)2 Attribute (org.dom4j.Attribute)2 Document (org.dom4j.Document)2 DocumentFactory (org.dom4j.DocumentFactory)2 Namespace (org.dom4j.Namespace)2