use of org.dom4j.XPath in project tdi-studio-se by Talend.
the class DocumentExtractor method getStructureResult.
public List<String> getStructureResult(String structureName, List<String> names) {
final String finalStructureName = replaceNamespace(structureName);
XPath xpath = org.dom4j.DocumentHelper.createXPath(sb.append("/").append(finalFunction).append("/OUTPUT/").append(finalStructureName).append("|").append("/").append(finalFunction).append("/CHANGING/").append(finalStructureName).toString());
sb.setLength(0);
Node node = xpath.selectSingleNode(doc);
if (node == null) {
return null;
}
List<String> result = new ArrayList<String>();
for (String name : names) {
final String finalName = replaceNamespace(name);
Node subNode = node.selectSingleNode(finalName);
if (subNode == null) {
result.add(null);
} else {
result.add(subNode.getText());
}
}
return result;
}
use of org.dom4j.XPath in project OpenOLAT by OpenOLAT.
the class ItemContext method shuffle.
/**
* Method shuffle. shuffle clones the current item (since the whole qti tree
* is readonly) and shuffles it
*
* @param item
* @return Element
*/
private Element shuffle(Element item) {
// get the render_choice
XPath choice = DocumentHelper.createXPath(".//render_choice[@shuffle=\"Yes\"]");
Element tel_rendchoice = (Element) choice.selectSingleNode(item);
// if shuffle is disable, just return the item
if (tel_rendchoice == null)
return item;
// else: we have to shuffle
// assume: all response_label have same parent: either render_choice or a
// flow_label
Element shuffleItem = item.createCopy();
// clone the whole item
Element el_rendchoice = (Element) choice.selectSingleNode(shuffleItem);
// <!ELEMENT render_choice ((material | material_ref | response_label |
// flow_label)* ,response_na?)>
// <!ATTLIST response_label rshuffle (Yes | No ) 'Yes' .....
List el_labels = el_rendchoice.selectNodes(".//response_label[@rshuffle=\"Yes\"]");
int shusize = el_labels.size();
// set up a list of children with their parents and the position of the
// child (in case several children have the same parent
List<Element> respList = new ArrayList<>(shusize);
List<Element> parentList = new ArrayList<>(shusize);
int[] posList = new int[shusize];
int j = 0;
for (Iterator responses = el_labels.iterator(); responses.hasNext(); ) {
Element response = (Element) responses.next();
Element parent = response.getParent();
int pos = parent.indexOf(response);
posList[j++] = pos;
// need to use clones so they are not
respList.add((Element) response.clone());
// attached anymore
parentList.add(parent);
}
Collections.shuffle(respList);
// put the children back to the parents
for (int i = 0; i < parentList.size(); i++) {
Element parent = parentList.get(i);
int pos = posList[i];
Element child = respList.get(i);
parent.elements().set(pos, child);
}
return shuffleItem;
}
use of org.dom4j.XPath in project OpenOLAT by OpenOLAT.
the class ScormCPFileResource method validateImsManifest.
public static boolean validateImsManifest(Document doc) {
try {
// do not throw exception already here, as it might be only a generic zip file
if (doc == null)
return false;
String adluri = null;
String seqencingUri = null;
String simpleSeqencingUri = null;
// get all organization elements. need to set namespace
Element rootElement = doc.getRootElement();
String nsuri = rootElement.getNamespace().getURI();
// look for the adl cp namespace that differs a scorm package from a normal cp package
Namespace nsADL = rootElement.getNamespaceForPrefix("adlcp");
if (nsADL != null)
adluri = nsADL.getURI();
Namespace nsADLSeq = rootElement.getNamespaceForPrefix("adlseq");
if (nsADLSeq != null)
seqencingUri = nsADLSeq.getURI();
Namespace nsADLSS = rootElement.getNamespaceForPrefix("imsss");
if (nsADLSS != null)
simpleSeqencingUri = nsADLSS.getURI();
// we can only support scorm 1.2 so far.
if (adluri != null && !((adluri.indexOf("adlcp_rootv1p2") != -1) || (adluri.indexOf("adlcp_rootv1p3") != -1))) {
// we dont have have scorm 1.2 or 1.3 namespace so it can't be a scorm package
return false;
}
Map<String, Object> nsuris = new HashMap<>(5);
nsuris.put("ns", nsuri);
// we might have a scorm 2004 which we do not yet support
if (seqencingUri != null)
nsuris.put("adlseq", seqencingUri);
if (simpleSeqencingUri != null)
nsuris.put("imsss", simpleSeqencingUri);
// Check for organization element. Must provide at least one... title gets extracted from either
// the (optional) <title> element or the mandatory identifier attribute.
// This makes sure, at least a root node gets created in CPManifestTreeModel.
XPath meta = rootElement.createXPath("//ns:organization");
meta.setNamespaceURIs(nsuris);
// TODO: accept several organizations?
Element orgaEl = (Element) meta.selectSingleNode(rootElement);
if (orgaEl == null) {
return false;
}
// Check for at least one <item> element referencing a <resource> of adlcp:scormtype="sco" or "asset",
// which will serve as an entry point.
XPath resourcesXPath = rootElement.createXPath("//ns:resources");
resourcesXPath.setNamespaceURIs(nsuris);
Element elResources = (Element) resourcesXPath.selectSingleNode(rootElement);
if (elResources == null) {
return false;
}
XPath itemsXPath = rootElement.createXPath("//ns:item");
itemsXPath.setNamespaceURIs(nsuris);
List items = itemsXPath.selectNodes(rootElement);
if (items.size() == 0) {
// no <item> element.
return false;
}
// check for scorm 2004 simple sequencing stuff which we do not yet support
if (seqencingUri != null) {
XPath seqencingXPath = rootElement.createXPath("//ns:imsss");
List sequences = seqencingXPath.selectNodes(rootElement);
if (sequences.size() > 0) {
// seqencing elements found -> scorm 2004
return false;
}
}
Set<String> set = new HashSet<String>();
for (Iterator iter = items.iterator(); iter.hasNext(); ) {
Element item = (Element) iter.next();
String identifier = item.attributeValue("identifier");
// check if identifiers are unique, reject if not so
if (!set.add(identifier)) {
// TODO:create special error message for non unique ids
return false;
}
}
for (Iterator iter = items.iterator(); iter.hasNext(); ) {
Element item = (Element) iter.next();
String identifierref = item.attributeValue("identifierref");
if (identifierref == null)
continue;
XPath resourceXPath = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
resourceXPath.setNamespaceURIs(nsuris);
Element elResource = (Element) resourceXPath.selectSingleNode(elResources);
if (elResource == null) {
return false;
}
// check for scorm attribute
Attribute scormAttr = elResource.attribute("scormtype");
// some packages have attribute written like "scormType"
Attribute scormAttrUpper = elResource.attribute("scormType");
if (scormAttr == null && scormAttrUpper == null) {
return false;
}
String attr = "";
if (scormAttr != null)
attr = scormAttr.getStringValue();
if (scormAttrUpper != null)
attr = scormAttrUpper.getStringValue();
if (attr == null) {
return false;
}
if (elResource.attributeValue("href") != null && (attr.equalsIgnoreCase("sco") || attr.equalsIgnoreCase("asset"))) {
// success.
return true;
}
}
return false;
} catch (Exception e) {
log.warn("Not a valid SCORM package", e);
return false;
}
}
use of org.dom4j.XPath in project OpenOLAT by OpenOLAT.
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;
}
use of org.dom4j.XPath in project archiva by apache.
the class XMLReader method getElement.
public Element getElement(String xpathExpr) throws XMLException {
XPath xpath = createXPath(xpathExpr);
Object evaluated = xpath.selectSingleNode(document);
if (evaluated == null) {
return null;
}
if (evaluated instanceof Element) {
return (Element) evaluated;
} else {
// Unknown evaluated type.
throw new XMLException(".getElement( Expr: " + xpathExpr + " ) resulted in non-Element type -> (" + evaluated.getClass().getName() + ") " + evaluated);
}
}
Aggregations