Java Code Examples for org.dom4j.XPath#selectSingleNode()
The following examples show how to use
org.dom4j.XPath#selectSingleNode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CPManifestTreeModel.java From olat with Apache License 2.0 | 4 votes |
/** * Constructor of the content packaging tree model * * @param manifest * the imsmanifest.xml file */ public CPManifestTreeModel(final VFSLeaf manifest) { final Document doc = loadDocument(manifest); // get all organization elements. need to set namespace rootElement = doc.getRootElement(); final String nsuri = rootElement.getNamespace().getURI(); nsuris.put("ns", nsuri); final XPath meta = rootElement.createXPath("//ns:organization"); meta.setNamespaceURIs(nsuris); final Element orgaEl = (Element) meta.selectSingleNode(rootElement); // TODO: accept several organizations? if (orgaEl == null) { throw new AssertException("could not find element organization"); } final XPath metares = rootElement.createXPath("//ns:resources"); metares.setNamespaceURIs(nsuris); final Element elResources = (Element) metares.selectSingleNode(rootElement); if (elResources == null) { throw new AssertException("could not find element resources"); } final List resourcesList = elResources.elements("resource"); resources = new HashMap(resourcesList.size()); for (final Iterator iter = resourcesList.iterator(); iter.hasNext();) { final Element elRes = (Element) iter.next(); final 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 (final UnsupportedEncodingException e) { // each JVM must implement UTF-8 } } resources.put(identVal, hrefVal); } final GenericTreeNode gtn = buildNode(orgaEl); setRootNode(gtn); rootElement = null; // help gc resources = null; }
Example 2
Source File: ImsCPFileResource.java From olat with Apache License 2.0 | 4 votes |
/** * Check for title and at least one resource. * * @param unzippedDir * @return True if is of type. */ public static boolean validate(final File unzippedDir) throws AddingResourceException { final File fManifest = new File(unzippedDir, "imsmanifest.xml"); final Document doc = IMSLoader.loadIMSDocument(fManifest); // do not throw exception already here, as it might be only a generic zip file if (doc == null) { return false; } // get all organization elements. need to set namespace final Element rootElement = doc.getRootElement(); final String nsuri = rootElement.getNamespace().getURI(); final Map nsuris = new HashMap(1); nsuris.put("ns", nsuri); // Check for organiztaion element. Must provide at least one... title gets ectracted from either // the (optional) <title> element or the mandatory identifier attribute. // This makes sure, at least a root node gets created in CPManifestTreeModel. final XPath meta = rootElement.createXPath("//ns:organization"); meta.setNamespaceURIs(nsuris); final Element orgaEl = (Element) meta.selectSingleNode(rootElement); // TODO: accept several organizations? if (orgaEl == null) { throw new AddingResourceException("resource.no.organisation"); } // Check for at least one <item> element referencing a <resource>, which will serve as an entry point. // This is mandatory, as we need an entry point as the user has the option of setting // CPDisplayController to not display a menu at all, in which case the first <item>/<resource> // element pair gets displayed. final XPath resourcesXPath = rootElement.createXPath("//ns:resources"); resourcesXPath.setNamespaceURIs(nsuris); final Element elResources = (Element) resourcesXPath.selectSingleNode(rootElement); if (elResources == null) { throw new AddingResourceException("resource.no.resource"); // no <resources> element. } final XPath itemsXPath = rootElement.createXPath("//ns:item"); itemsXPath.setNamespaceURIs(nsuris); final List items = itemsXPath.selectNodes(rootElement); if (items.size() == 0) { throw new AddingResourceException("resource.no.item"); // no <item> element. } for (final Iterator iter = items.iterator(); iter.hasNext();) { final Element item = (Element) iter.next(); final String identifierref = item.attributeValue("identifierref"); if (identifierref == null) { continue; } final XPath resourceXPath = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']"); resourceXPath.setNamespaceURIs(nsuris); final Element elResource = (Element) resourceXPath.selectSingleNode(elResources); if (elResource == null) { throw new AddingResourceException("resource.no.matching.resource"); } if (elResource.attribute("scormtype") != null) { return false; } if (elResource.attribute("scormType") != null) { return false; } if (elResource.attribute("SCORMTYPE") != null) { return false; } if (elResource.attributeValue("href") != null) { return true; // success. } } return false; // throw new AddingResourceException("resource.general.error"); }
Example 3
Source File: ScormCPManifestTreeModel.java From olat with Apache License 2.0 | 4 votes |
/** * Constructor of the content packaging tree model * * @param scormFolderPath * the imsmanifest.xml file * @param itemStatus * a Map containing the status of each item like "completed, not attempted, ..." */ public ScormCPManifestTreeModel(final String scormFolderPath, final Map itemStatus) { ScormEBL scormEbl = CoreSpringFactory.getBean(ScormEBL.class); this.itemStatus = itemStatus; final Document doc = loadDocument(scormEbl.getManifestFile(scormFolderPath)); // get all organization elements. need to set namespace rootElement = doc.getRootElement(); final String nsuri = rootElement.getNamespace().getURI(); nsuris.put("ns", nsuri); final XPath meta = rootElement.createXPath("//ns:organization"); meta.setNamespaceURIs(nsuris); final XPath metares = rootElement.createXPath("//ns:resources"); metares.setNamespaceURIs(nsuris); final Element elResources = (Element) metares.selectSingleNode(rootElement); if (elResources == null) { throw new AssertException("could not find element resources"); } final List resourcesList = elResources.elements("resource"); resources = new HashMap(resourcesList.size()); for (final Iterator iter = resourcesList.iterator(); iter.hasNext();) { final Element elRes = (Element) iter.next(); final 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 (final UnsupportedEncodingException e) { // each JVM must implement UTF-8 } } resources.put(identVal, hrefVal); } /* * Get all organizations */ List organizations = new LinkedList(); organizations = meta.selectNodes(rootElement); if (organizations.isEmpty()) { throw new AssertException("could not find element organization"); } final GenericTreeNode gtn = buildTreeNodes(organizations); setRootNode(gtn); rootElement = null; // help gc resources = null; }
Example 4
Source File: CPManifestTreeModel.java From olat with Apache License 2.0 | 4 votes |
/** * Constructor of the content packaging tree model * * @param manifest * the imsmanifest.xml file */ public CPManifestTreeModel(final VFSLeaf manifest) { final Document doc = loadDocument(manifest); // get all organization elements. need to set namespace rootElement = doc.getRootElement(); final String nsuri = rootElement.getNamespace().getURI(); nsuris.put("ns", nsuri); final XPath meta = rootElement.createXPath("//ns:organization"); meta.setNamespaceURIs(nsuris); final Element orgaEl = (Element) meta.selectSingleNode(rootElement); // TODO: accept several organizations? if (orgaEl == null) { throw new AssertException("could not find element organization"); } final XPath metares = rootElement.createXPath("//ns:resources"); metares.setNamespaceURIs(nsuris); final Element elResources = (Element) metares.selectSingleNode(rootElement); if (elResources == null) { throw new AssertException("could not find element resources"); } final List resourcesList = elResources.elements("resource"); resources = new HashMap(resourcesList.size()); for (final Iterator iter = resourcesList.iterator(); iter.hasNext();) { final Element elRes = (Element) iter.next(); final 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 (final UnsupportedEncodingException e) { // each JVM must implement UTF-8 } } resources.put(identVal, hrefVal); } final GenericTreeNode gtn = buildNode(orgaEl); setRootNode(gtn); rootElement = null; // help gc resources = null; }
Example 5
Source File: ImsCPFileResource.java From olat with Apache License 2.0 | 4 votes |
/** * Check for title and at least one resource. * * @param unzippedDir * @return True if is of type. */ public static boolean validate(final File unzippedDir) throws AddingResourceException { final File fManifest = new File(unzippedDir, "imsmanifest.xml"); final Document doc = IMSLoader.loadIMSDocument(fManifest); // do not throw exception already here, as it might be only a generic zip file if (doc == null) { return false; } // get all organization elements. need to set namespace final Element rootElement = doc.getRootElement(); final String nsuri = rootElement.getNamespace().getURI(); final Map nsuris = new HashMap(1); nsuris.put("ns", nsuri); // Check for organiztaion element. Must provide at least one... title gets ectracted from either // the (optional) <title> element or the mandatory identifier attribute. // This makes sure, at least a root node gets created in CPManifestTreeModel. final XPath meta = rootElement.createXPath("//ns:organization"); meta.setNamespaceURIs(nsuris); final Element orgaEl = (Element) meta.selectSingleNode(rootElement); // TODO: accept several organizations? if (orgaEl == null) { throw new AddingResourceException("resource.no.organisation"); } // Check for at least one <item> element referencing a <resource>, which will serve as an entry point. // This is mandatory, as we need an entry point as the user has the option of setting // CPDisplayController to not display a menu at all, in which case the first <item>/<resource> // element pair gets displayed. final XPath resourcesXPath = rootElement.createXPath("//ns:resources"); resourcesXPath.setNamespaceURIs(nsuris); final Element elResources = (Element) resourcesXPath.selectSingleNode(rootElement); if (elResources == null) { throw new AddingResourceException("resource.no.resource"); // no <resources> element. } final XPath itemsXPath = rootElement.createXPath("//ns:item"); itemsXPath.setNamespaceURIs(nsuris); final List items = itemsXPath.selectNodes(rootElement); if (items.size() == 0) { throw new AddingResourceException("resource.no.item"); // no <item> element. } for (final Iterator iter = items.iterator(); iter.hasNext();) { final Element item = (Element) iter.next(); final String identifierref = item.attributeValue("identifierref"); if (identifierref == null) { continue; } final XPath resourceXPath = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']"); resourceXPath.setNamespaceURIs(nsuris); final Element elResource = (Element) resourceXPath.selectSingleNode(elResources); if (elResource == null) { throw new AddingResourceException("resource.no.matching.resource"); } if (elResource.attribute("scormtype") != null) { return false; } if (elResource.attribute("scormType") != null) { return false; } if (elResource.attribute("SCORMTYPE") != null) { return false; } if (elResource.attributeValue("href") != null) { return true; // success. } } return false; // throw new AddingResourceException("resource.general.error"); }
Example 6
Source File: ScormCPManifestTreeModel.java From olat with Apache License 2.0 | 4 votes |
/** * Constructor of the content packaging tree model * * @param scormFolderPath * the imsmanifest.xml file * @param itemStatus * a Map containing the status of each item like "completed, not attempted, ..." */ public ScormCPManifestTreeModel(final String scormFolderPath, final Map itemStatus) { ScormEBL scormEbl = CoreSpringFactory.getBean(ScormEBL.class); this.itemStatus = itemStatus; final Document doc = loadDocument(scormEbl.getManifestFile(scormFolderPath)); // get all organization elements. need to set namespace rootElement = doc.getRootElement(); final String nsuri = rootElement.getNamespace().getURI(); nsuris.put("ns", nsuri); final XPath meta = rootElement.createXPath("//ns:organization"); meta.setNamespaceURIs(nsuris); final XPath metares = rootElement.createXPath("//ns:resources"); metares.setNamespaceURIs(nsuris); final Element elResources = (Element) metares.selectSingleNode(rootElement); if (elResources == null) { throw new AssertException("could not find element resources"); } final List resourcesList = elResources.elements("resource"); resources = new HashMap(resourcesList.size()); for (final Iterator iter = resourcesList.iterator(); iter.hasNext();) { final Element elRes = (Element) iter.next(); final 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 (final UnsupportedEncodingException e) { // each JVM must implement UTF-8 } } resources.put(identVal, hrefVal); } /* * Get all organizations */ List organizations = new LinkedList(); organizations = meta.selectNodes(rootElement); if (organizations.isEmpty()) { throw new AssertException("could not find element organization"); } final GenericTreeNode gtn = buildTreeNodes(organizations); setRootNode(gtn); rootElement = null; // help gc resources = null; }