use of org.dom4j.XPath in project OpenOLAT by OpenOLAT.
the class ScormCPManifestTreeModel method buildNode.
private GenericTreeNode buildNode(Element item) {
GenericTreeNode treeNode = new GenericTreeNode();
// extract title
String title = item.elementText("title");
if (title == null)
title = item.attributeValue("identifier");
treeNode.setAltText(title);
treeNode.setTitle(title);
if (item.getName().equals("organization")) {
treeNode.setIconCssClass("o_scorm_org");
treeNode.setAccessible(false);
} else if (item.getName().equals("item")) {
scormIdToNode.put(new Integer(nodeId).toString(), treeNode);
nodeToId.put(treeNode, new Integer(nodeId));
// set node images according to scorm sco status
String itemStatusDesc = itemStatus.get(Integer.toString(nodeId));
treeNode.setIconCssClass("o_scorm_item");
if (itemStatusDesc != null && !"not_attempted".equals(itemStatusDesc)) {
// add icon decorator for current status
String decorator = "o_scorm_" + itemStatusDesc;
treeNode.setIconDecorator1CssClass(decorator);
}
nodeId++;
// set resolved file path directly
String identifierref = item.attributeValue("identifierref");
XPath meta = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
meta.setNamespaceURIs(nsuris);
String href = resources.get(identifierref);
if (href != null) {
treeNode.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
hrefToTreeNode.put(href, treeNode);
} else
treeNode.setAccessible(false);
}
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);
treeNode.addChild(gtnchild);
}
return treeNode;
}
use of org.dom4j.XPath in project OpenOLAT by OpenOLAT.
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 OpenOLAT.
the class ImsCPFileResource method validateImsManifest.
private 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;
// get all organization elements. need to set namespace
Element rootElement = doc.getRootElement();
String nsuri = rootElement.getNamespace().getURI();
Map<String, String> 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.
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>, 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.
XPath resourcesXPath = rootElement.createXPath("//ns:resources");
resourcesXPath.setNamespaceURIs(nsuris);
Element elResources = (Element) resourcesXPath.selectSingleNode(rootElement);
if (elResources == null) {
// no <resources> element.
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;
}
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;
}
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) {
// success.
return true;
}
}
} catch (Exception e) {
log.warn("", e);
}
return false;
}
use of org.dom4j.XPath in project openolat by klemens.
the class ScormCPManifestTreeModel method buildNode.
private GenericTreeNode buildNode(Element item) {
GenericTreeNode treeNode = new GenericTreeNode();
// extract title
String title = item.elementText("title");
if (title == null)
title = item.attributeValue("identifier");
treeNode.setAltText(title);
treeNode.setTitle(title);
if (item.getName().equals("organization")) {
treeNode.setIconCssClass("o_scorm_org");
treeNode.setAccessible(false);
} else if (item.getName().equals("item")) {
scormIdToNode.put(new Integer(nodeId).toString(), treeNode);
nodeToId.put(treeNode, new Integer(nodeId));
// set node images according to scorm sco status
String itemStatusDesc = itemStatus.get(Integer.toString(nodeId));
treeNode.setIconCssClass("o_scorm_item");
if (itemStatusDesc != null && !"not_attempted".equals(itemStatusDesc)) {
// add icon decorator for current status
String decorator = "o_scorm_" + itemStatusDesc;
treeNode.setIconDecorator1CssClass(decorator);
}
nodeId++;
// set resolved file path directly
String identifierref = item.attributeValue("identifierref");
XPath meta = rootElement.createXPath("//ns:resource[@identifier='" + identifierref + "']");
meta.setNamespaceURIs(nsuris);
String href = resources.get(identifierref);
if (href != null) {
treeNode.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
hrefToTreeNode.put(href, treeNode);
} else
treeNode.setAccessible(false);
}
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);
treeNode.addChild(gtnchild);
}
return treeNode;
}
use of org.dom4j.XPath in project pentaho-kettle by pentaho.
the class GetXMLData method processPutRow.
private Object[] processPutRow(Node node) throws KettleException {
// Create new row...
Object[] outputRowData = buildEmptyRow();
// Create new row or clone
if (meta.isInFields()) {
System.arraycopy(data.readrow, 0, outputRowData, 0, data.nrReadRow);
}
try {
data.nodenr++;
// Read fields...
for (int i = 0; i < data.nrInputFields; i++) {
// Get field
GetXMLDataField xmlDataField = meta.getInputFields()[i];
// Get the Path to look for
String XPathValue = xmlDataField.getResolvedXPath();
if (meta.isuseToken()) {
// See if user use Token inside path field
// The syntax is : @_Fieldname-
// PDI will search for Fieldname value and replace it
// Fieldname must be defined before the current node
XPathValue = substituteToken(XPathValue, outputRowData);
if (isDetailed()) {
logDetailed(XPathValue);
}
}
// Get node value
String nodevalue;
Boolean xmlMissingTagYieldsNullValue = convertStringToBoolean(Const.NVL(System.getProperty(Const.KETTLE_XML_MISSING_TAG_YIELDS_NULL_VALUE, "N"), "N"));
// Handle namespaces
if (meta.isNamespaceAware()) {
XPath xpathField = node.createXPath(addNSPrefix(XPathValue, data.PathValue));
xpathField.setNamespaceURIs(data.NAMESPACE);
if (xmlDataField.getResultType() == GetXMLDataField.RESULT_TYPE_VALUE_OF) {
if (xmlMissingTagYieldsNullValue) {
nodevalue = xpathField.selectSingleNode(node) != null ? xpathField.valueOf(node) : null;
} else {
nodevalue = xpathField.valueOf(node);
}
} else {
Node n = xpathField.selectSingleNode(node);
if (n != null) {
nodevalue = n.asXML();
} else {
nodevalue = xmlMissingTagYieldsNullValue ? null : "";
}
}
} else {
if (xmlDataField.getResultType() == GetXMLDataField.RESULT_TYPE_VALUE_OF) {
if (xmlMissingTagYieldsNullValue) {
nodevalue = node.selectSingleNode(XPathValue) != null ? node.valueOf(XPathValue) : null;
} else {
nodevalue = node.valueOf(XPathValue);
}
} else {
Node n = node.selectSingleNode(XPathValue);
if (n != null) {
nodevalue = n.asXML();
} else {
nodevalue = xmlMissingTagYieldsNullValue ? null : "";
}
}
}
// Do trimming
switch(xmlDataField.getTrimType()) {
case GetXMLDataField.TYPE_TRIM_LEFT:
nodevalue = Const.ltrim(nodevalue);
break;
case GetXMLDataField.TYPE_TRIM_RIGHT:
nodevalue = Const.rtrim(nodevalue);
break;
case GetXMLDataField.TYPE_TRIM_BOTH:
nodevalue = Const.trim(nodevalue);
break;
default:
break;
}
// Do conversions
//
ValueMetaInterface targetValueMeta = data.outputRowMeta.getValueMeta(data.totalpreviousfields + i);
ValueMetaInterface sourceValueMeta = data.convertRowMeta.getValueMeta(data.totalpreviousfields + i);
outputRowData[data.totalpreviousfields + i] = targetValueMeta.convertData(sourceValueMeta, nodevalue);
// Do we need to repeat this field if it is null?
if (meta.getInputFields()[i].isRepeated()) {
if (data.previousRow != null && Utils.isEmpty(nodevalue)) {
outputRowData[data.totalpreviousfields + i] = data.previousRow[data.totalpreviousfields + i];
}
}
}
// End of loop over fields...
int rowIndex = data.totalpreviousfields + data.nrInputFields;
// See if we need to add the filename to the row...
if (meta.includeFilename() && !Utils.isEmpty(meta.getFilenameField())) {
outputRowData[rowIndex++] = data.filename;
}
// See if we need to add the row number to the row...
if (meta.includeRowNumber() && !Utils.isEmpty(meta.getRowNumberField())) {
outputRowData[rowIndex++] = data.rownr;
}
// Possibly add short filename...
if (meta.getShortFileNameField() != null && meta.getShortFileNameField().length() > 0) {
outputRowData[rowIndex++] = data.shortFilename;
}
// Add Extension
if (meta.getExtensionField() != null && meta.getExtensionField().length() > 0) {
outputRowData[rowIndex++] = data.extension;
}
// add path
if (meta.getPathField() != null && meta.getPathField().length() > 0) {
outputRowData[rowIndex++] = data.path;
}
// Add Size
if (meta.getSizeField() != null && meta.getSizeField().length() > 0) {
outputRowData[rowIndex++] = data.size;
}
// add Hidden
if (meta.isHiddenField() != null && meta.isHiddenField().length() > 0) {
outputRowData[rowIndex++] = Boolean.valueOf(data.path);
}
// Add modification date
if (meta.getLastModificationDateField() != null && meta.getLastModificationDateField().length() > 0) {
outputRowData[rowIndex++] = data.lastModificationDateTime;
}
// Add Uri
if (meta.getUriField() != null && meta.getUriField().length() > 0) {
outputRowData[rowIndex++] = data.uriName;
}
// Add RootUri
if (meta.getRootUriField() != null && meta.getRootUriField().length() > 0) {
outputRowData[rowIndex] = data.rootUriName;
}
RowMetaInterface irow = getInputRowMeta();
if (irow == null) {
data.previousRow = outputRowData;
} else {
// clone to previously allocated array to make sure next step doesn't
// change it in between...
System.arraycopy(outputRowData, 0, this.prevRow, 0, outputRowData.length);
// Pick up everything else that needs a real deep clone
data.previousRow = irow.cloneRow(outputRowData, this.prevRow);
}
} catch (Exception e) {
if (getStepMeta().isDoingErrorHandling()) {
// Simply add this row to the error row
putError(data.outputRowMeta, outputRowData, 1, e.toString(), null, "GetXMLData001");
data.errorInRowButContinue = true;
return null;
} else {
logError(e.toString());
throw new KettleException(e.toString());
}
}
return outputRowData;
}
Aggregations