Search in sources :

Example 81 with Attribute

use of org.dom4j.Attribute in project OpenOLAT by OpenOLAT.

the class SurveyFileResource method validateQti.

private static ResourceEvaluation validateQti(Document doc, ResourceEvaluation eval) {
    if (doc == null) {
        eval.setValid(false);
    } else {
        List assessment = doc.selectNodes("questestinterop/assessment");
        if (assessment.size() == 1) {
            Object assessmentObj = assessment.get(0);
            if (assessmentObj instanceof Element) {
                Element assessmentEl = (Element) assessmentObj;
                Attribute title = assessmentEl.attribute("title");
                if (title != null) {
                    eval.setDisplayname(title.getValue());
                }
                List metas = assessmentEl.selectNodes("qtimetadata/qtimetadatafield");
                for (Iterator iter = metas.iterator(); iter.hasNext(); ) {
                    Element el_metafield = (Element) iter.next();
                    Element el_label = (Element) el_metafield.selectSingleNode("fieldlabel");
                    String label = el_label.getText();
                    if (label.equals(AssessmentInstance.QMD_LABEL_TYPE)) {
                        // type meta
                        Element el_entry = (Element) el_metafield.selectSingleNode("fieldentry");
                        String entry = el_entry.getText();
                        if (entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_SURVEY)) {
                            eval.setValid(true);
                        }
                    }
                }
            }
        }
    }
    return eval;
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Iterator(java.util.Iterator) List(java.util.List)

Example 82 with Attribute

use of org.dom4j.Attribute in project OpenOLAT by OpenOLAT.

the class TestFileResource method validateQti.

private static ResourceEvaluation validateQti(Document doc, ResourceEvaluation eval) {
    if (doc == null) {
        eval.setValid(false);
    } else {
        boolean validType = false;
        boolean validScore = true;
        List assessment = doc.selectNodes("questestinterop/assessment");
        if (assessment.size() == 1) {
            Object assessmentObj = assessment.get(0);
            if (assessmentObj instanceof Element) {
                Element assessmentEl = (Element) assessmentObj;
                Attribute title = assessmentEl.attribute("title");
                if (title != null) {
                    eval.setDisplayname(title.getValue());
                }
                // check if this is marked as test
                List metas = assessmentEl.selectNodes("qtimetadata/qtimetadatafield");
                for (Iterator iter = metas.iterator(); iter.hasNext(); ) {
                    Element el_metafield = (Element) iter.next();
                    Element el_label = (Element) el_metafield.selectSingleNode("fieldlabel");
                    String label = el_label.getText();
                    if (label.equals(AssessmentInstance.QMD_LABEL_TYPE)) {
                        // type meta
                        Element el_entry = (Element) el_metafield.selectSingleNode("fieldentry");
                        String entry = el_entry.getText();
                        if (entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_SELF) || entry.equals(AssessmentInstance.QMD_ENTRY_TYPE_ASSESS)) {
                            validType = true;
                        }
                    }
                }
                // check if at least one section with one item
                List<Element> sectionItems = assessmentEl.selectNodes("section/item");
                if (sectionItems.size() > 0) {
                    for (Element it : sectionItems) {
                        List<?> sv = it.selectNodes("resprocessing/outcomes/decvar[@varname='SCORE']");
                        // the QTIv1.2 system relies on the SCORE variable of items
                        if (sv.size() != 1) {
                            validScore &= false;
                        }
                    }
                }
            }
        }
        eval.setValid(validType && validScore);
    }
    return eval;
}
Also used : Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Iterator(java.util.Iterator) List(java.util.List)

Example 83 with Attribute

use of org.dom4j.Attribute 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;
    }
}
Also used : XPath(org.dom4j.XPath) HashMap(java.util.HashMap) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) Namespace(org.dom4j.Namespace) IOException(java.io.IOException) Iterator(java.util.Iterator) List(java.util.List) HashSet(java.util.HashSet)

Example 84 with Attribute

use of org.dom4j.Attribute in project archiva by apache.

the class XMLReader method removeNamespaces.

/**
 * Remove namespaces from element recursively.
 */
@SuppressWarnings("unchecked")
public void removeNamespaces(Element elem) {
    elem.setQName(QName.get(elem.getName(), Namespace.NO_NAMESPACE, elem.getQualifiedName()));
    Node n;
    Iterator<Node> it = elem.elementIterator();
    while (it.hasNext()) {
        n = it.next();
        switch(n.getNodeType()) {
            case Node.ATTRIBUTE_NODE:
                ((Attribute) n).setNamespace(Namespace.NO_NAMESPACE);
                break;
            case Node.ELEMENT_NODE:
                removeNamespaces((Element) n);
                break;
        }
    }
}
Also used : Attribute(org.dom4j.Attribute) Node(org.dom4j.Node)

Example 85 with Attribute

use of org.dom4j.Attribute in project jbosstools-hibernate by jbosstools.

the class OpenMappingUtils method searchInMappingFiles.

/**
 * Trying to find hibernate console config mapping file,
 * which is corresponding to provided element.
 *
 * @param consoleConfig
 * @param element
 * @return
 */
@SuppressWarnings("unchecked")
public static IFile searchInMappingFiles(ConsoleConfiguration consoleConfig, Object element) {
    IFile file = null;
    if (consoleConfig == null) {
        return file;
    }
    java.io.File configXMLFile = consoleConfig.getConfigXMLFile();
    if (!consoleConfig.hasConfiguration()) {
        consoleConfig.build();
        consoleConfig.buildSessionFactory();
    }
    EntityResolver entityResolver = consoleConfig.getConfiguration().getEntityResolver();
    Document doc = getDocument(configXMLFile, entityResolver);
    if (doc == null) {
        return file;
    }
    // 
    IPackageFragmentRoot[] packageFragments = getCCPackageFragmentRoots(consoleConfig);
    // 
    ArrayList<IPath> paths = new ArrayList<IPath>();
    for (int i = 0; i < packageFragments.length; i++) {
        paths.add(packageFragments[i].getPath());
    }
    // last chance to find file is the same place as configXMLFile
    paths.add(Path.fromOSString(configXMLFile.getParent()));
    // 
    for (int i = 0; i < paths.size() && file == null; i++) {
        Element sfNode = doc.getRootElement().element(HIBERNATE_TAG_SESSION_FACTORY);
        Iterator<Element> elements = sfNode.elements(HIBERNATE_TAG_MAPPING).iterator();
        while (elements.hasNext() && file == null) {
            Element subelement = elements.next();
            Attribute resourceAttr = subelement.attribute(HIBERNATE_TAG_RESOURCE);
            if (resourceAttr == null) {
                continue;
            }
            IPath path = paths.get(i).append(resourceAttr.getValue().trim());
            file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
            if (file == null || !file.exists()) {
                file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
            }
            if (file != null && file.exists()) {
                if (elementInFile(consoleConfig, file, element)) {
                    break;
                }
            }
            file = null;
        }
    }
    return file;
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) EntityResolver(org.xml.sax.EntityResolver) Document(org.dom4j.Document) IDocument(org.eclipse.jface.text.IDocument) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Aggregations

Attribute (org.dom4j.Attribute)114 Element (org.dom4j.Element)88 Document (org.dom4j.Document)30 ArrayList (java.util.ArrayList)28 List (java.util.List)26 Iterator (java.util.Iterator)23 Node (org.dom4j.Node)14 HashMap (java.util.HashMap)12 File (java.io.File)11 SAXReader (org.dom4j.io.SAXReader)11 IOException (java.io.IOException)10 Map (java.util.Map)6 VFSItem (org.olat.core.util.vfs.VFSItem)6 QTIObject (org.olat.ims.qti.editor.beecom.objects.QTIObject)6 Namespace (org.dom4j.Namespace)5 QName (org.dom4j.QName)5 PropertyString (org.pentaho.commons.util.repository.type.PropertyString)5 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 OutcomesProcessing (org.olat.ims.qti.editor.beecom.objects.OutcomesProcessing)4 AnnotatedElement (java.lang.reflect.AnnotatedElement)3