Example usage for org.dom4j Element getNamespace

List of usage examples for org.dom4j Element getNamespace

Introduction

In this page you can find the example usage for org.dom4j Element getNamespace.

Prototype

Namespace getNamespace();

Source Link

Document

Returns the Namespace of this element if one exists otherwise Namespace.NO_NAMESPACE is returned.

Usage

From source file:org.nuxeo.ecm.platform.webdav.helpers.DavRequestXMLHelper.java

License:Open Source License

@SuppressWarnings("unchecked")
public static DavLockInfo extractLockInfo(Element root) {

    DavLockInfo lockInfo = new DavLockInfo();

    if (root == null) {
        return lockInfo;
    }//www.  j  a  va 2  s . c o  m

    Iterator<Element> eiter = root.elementIterator();

    while (eiter.hasNext()) {
        Element child = eiter.next();
        String tagName = child.getName();
        String uri = child.getNamespace().getURI();
        if (uri.equals(WebDavConst.DAV_XML_URI) && tagName.equals("lockinfo")) {
            Iterator<Element> citer = child.elementIterator();
            while (citer.hasNext()) {
                Element subChild = citer.next();
                if (uri.equals(WebDavConst.DAV_XML_URI) && tagName.equals("owner")) {
                    if (subChild.nodeCount() > 0) {
                        lockInfo.setOwner(subChild.getText());
                    } else {
                        lockInfo.setOwner(subChild.node(0).getStringValue());
                    }
                }
            }
            return lockInfo;
        }
    }

    return lockInfo;
}

From source file:org.nuxeo.ecm.platform.webdav.helpers.DavRequestXMLHelper.java

License:Open Source License

@SuppressWarnings("unchecked")
public static Map<String, List<String>> extractProperties(Element root) {
    Map<String, List<String>> result = new HashMap<String, List<String>>();

    if (root == null) {
        result.put("*", null);
        return result;
    }//  w w  w . j  a  v  a  2  s .  co  m

    Iterator<Element> eiter = root.elementIterator();

    while (eiter.hasNext()) {
        Element child = eiter.next();
        String tagName = child.getName();
        String uri = child.getNamespace().getURI();
        if (uri.equals(WebDavConst.DAV_XML_URI) && tagName.equals("prop")) {
            extractProperty(child, result);
        } else if (uri.equals(WebDavConst.DAV_XML_URI) && tagName.equals("allprop")) {
            result.put("*", null);
            return result;
        } else if (uri.equals(WebDavConst.DAV_XML_URI) && tagName.equals("propname")) {
            result.put("propname", null);
            return result;
        }

    }
    //System.out.print(result);
    return result;
}

From source file:org.nuxeo.ecm.platform.webdav.helpers.DavRequestXMLHelper.java

License:Open Source License

@SuppressWarnings("unchecked")
private static void extractProperty(Element prop, Map<String, List<String>> result) {
    Iterator<Element> eiter = prop.elementIterator();
    while (eiter.hasNext()) {
        Element child = eiter.next();
        String tagName = child.getName();
        String prefix = child.getNamespace().getPrefix();
        String uri = child.getNamespace().getURI();

        // store in order to use the same prefix in response
        NameSpaceHelper.addNameSpace(uri, prefix);

        if (result.containsKey(uri)) {
            List<String> fields = result.get(uri);
            if (fields == null) {
                fields = new ArrayList<String>();
            }// w  w w .j a v a2  s.co  m
            fields.add(tagName);
            result.put(uri, fields);
        } else {
            List<String> fields = new ArrayList<String>();
            fields.add(tagName);
            result.put(uri, fields);
        }
    }
}

From source file:org.olat.fileresource.types.ImsCPFileResource.java

License:Apache License

/**
 * Check for title and at least one resource.
 * /*from   w w w  .  ja v a 2 s  .  com*/
 * @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");
}

From source file:org.olat.fileresource.types.ScormCPFileResource.java

License:Apache License

/**
 * Check for title and at least one resource.
 * /*from w  w  w.ja  v  a2 s.c o m*/
 * @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;
    }

    String adluri = null;
    String seqencingUri = null;
    String simpleSeqencingUri = null;
    // get all organization elements. need to set namespace
    final Element rootElement = doc.getRootElement();
    final String nsuri = rootElement.getNamespace().getURI();
    // look for the adl cp namespace that differs a scorm package from a normal cp package
    final Namespace nsADL = rootElement.getNamespaceForPrefix("adlcp");
    if (nsADL != null) {
        adluri = nsADL.getURI();
    }
    final Namespace nsADLSeq = rootElement.getNamespaceForPrefix("adlseq");
    if (nsADLSeq != null) {
        seqencingUri = nsADLSeq.getURI();
    }
    final 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
        throw new AddingResourceException("scorm.no.scorm.namespace");
    }

    final Map nsuris = new HashMap(5);
    nsuris.put("ns", nsuri);
    nsuris.put("adluri", adluri);
    // 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 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> of adlcp:scormtype="sco" or "asset",
    // which will serve as an entry point.
    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("scorm.no.item"); // no <item> element.
    }

    // check for scorm 2004 simple sequencing stuff which we do not yet support
    if (seqencingUri != null) {
        final XPath seqencingXPath = rootElement.createXPath("//ns:imsss");
        final List sequences = seqencingXPath.selectNodes(rootElement);
        if (sequences.size() > 0) {
            throw new AddingResourceException("scorm.found.seqencing"); // seqencing elements found -> scorm 2004
        }
    }

    final Set set = new HashSet();
    for (final Iterator iter = items.iterator(); iter.hasNext();) {
        final Element item = (Element) iter.next();
        final String identifier = item.attributeValue("identifier");
        // check if identifiers are unique, reject if not so
        if (!set.add(identifier)) {
            throw new AddingResourceException("resource.general.error");// TODO:create special error message for non unique ids
        }
    }

    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");
        }
        // check for scorm attribute
        final Attribute scormAttr = elResource.attribute("scormtype");
        // some packages have attribute written like "scormType"
        final Attribute scormAttrUpper = elResource.attribute("scormType");
        if (scormAttr == null && scormAttrUpper == null) {
            throw new AddingResourceException("scorm.no.attribute.scormtype");
        }
        String attr = "";
        if (scormAttr != null) {
            attr = scormAttr.getStringValue();
        }
        if (scormAttrUpper != null) {
            attr = scormAttrUpper.getStringValue();
        }
        if (attr == null) {
            throw new AddingResourceException("scorm.no.attribute.value");
        }
        if (elResource.attributeValue("href") != null
                && (attr.equalsIgnoreCase("sco") || attr.equalsIgnoreCase("asset"))) {
            return true; // success.
        }
    }
    throw new AddingResourceException("resource.general.error");
}

From source file:org.onesocialweb.openfire.handler.activity.IQSubscribeInterceptor.java

License:Apache License

public void interceptPacket(Packet packet, Session session, boolean incoming, boolean processed)
        throws PacketRejectedException {

    // We only care for incoming IQ that have not yet been processed
    if (incoming && !processed && packet instanceof IQ) {

        final IQ iq = (IQ) packet;
        final JID fromJID = iq.getFrom();
        final JID toJID = iq.getTo();

        // Must be iq of type set and sent to remote users
        if (!iq.getType().equals(IQ.Type.set) || server.isLocal(toJID)) {
            return;
        }// w w  w  .j a  va  2  s.c o  m

        // With a pubsub requests
        Element requestElement = iq.getChildElement();
        if (!requestElement.getNamespace().equals(Namespace.get("http://jabber.org/protocol/pubsub"))) {
            return;
        }

        // With a subscibe or unsubscribe command
        Element commandElement = requestElement.element("subscribe");
        if (commandElement == null) {
            commandElement = requestElement.element("unsubscribe");
            if (commandElement == null) {
                return;
            }
        }

        // Relating to the microblogging node
        Attribute nodeAttribute = commandElement.attribute("node");
        if (!(nodeAttribute != null && nodeAttribute.getValue().equals(PEPActivityHandler.NODE))) {
            return;
        }

        // Then we keep track of the subscribe/unsubscribe request
        if (commandElement.getName().equals("subscribe")) {
            ActivityManager.getInstance().subscribe(fromJID.toBareJID(), toJID.toBareJID());
        } else {
            ActivityManager.getInstance().unsubscribe(fromJID.toBareJID(), toJID.toBareJID());
        }

    }
}

From source file:org.onosproject.yang.serializers.xml.DefaultXmlWalker.java

License:Apache License

/**
 * Determine the type of an element./*from w  w  w  . ja v a2  s.c  o m*/
 *
 * @param element to be analysed
 * @return type of the element
 */
private XmlNodeType getElementType(Element element) {
    Element newElement = element.createCopy();
    newElement.remove(element.getNamespace());
    return newElement.hasContent() && newElement.isTextOnly() ? TEXT_NODE : OBJECT_NODE;
}

From source file:org.onosproject.yang.serializers.xml.XmlSerializerListener.java

License:Apache License

@Override
public void enterXmlElement(Element element, XmlNodeType nodeType, Element rootElement) {

    // root element should not be added to data node
    if (element.equals(rootElement)) {
        return;/*from   www  . j  a v a  2 s . co  m*/
    }

    if (nodeType == OBJECT_NODE) {
        if (dnBuilder != null) {
            dnBuilder = addDataNode(dnBuilder, element.getName(), element.getNamespace().getURI(), null, null);
        }
    } else if (nodeType == TEXT_NODE) {
        if (dnBuilder != null) {
            dnBuilder = addDataNode(dnBuilder, element.getName(), element.getNamespace().getURI(),
                    element.getText(), null);
        }
    }
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.DefaultXmlCodecWalker.java

License:Apache License

@Override
public void walk(XmlListener listener, Element element, Element rootElement) {
    try {//from  w w w .  j a  v  a 2s .c  o m
        Element newElement = element.createCopy();
        newElement.remove(element.getNamespace());

        listener.enterXmlElement(element, getElementType(newElement), rootElement);

        if (element.hasContent() && !element.isTextOnly()) {
            for (Iterator i = element.elementIterator(); i.hasNext();) {
                Element childElement = (Element) i.next();
                walk(listener, childElement, rootElement);
            }
        }

        listener.exitXmlElement(element, getElementType(element), rootElement);
    } catch (Exception e) {
        log.error("Exception occurred when walk xml element: {}", element);
    }
}

From source file:org.onosproject.yms.app.ych.defaultcodecs.xml.XmlCodecListener.java

License:Apache License

@Override
public void enterXmlElement(Element element, XmlNodeType nodeType, Element rootElement) {
    if (element.equals(rootElement)) {
        return;/*  w ww  .ja  va 2 s.com*/
    }

    YdtContextOperationType opType = null;

    for (Iterator iter = element.attributeIterator(); iter.hasNext();) {
        Attribute attr = (Attribute) iter.next();
        if (attr.getName().equals(OPERATION)) {
            opType = YdtContextOperationType.valueOf(attr.getValue().toUpperCase());
        }
    }

    String nameSpace = null;
    if (element.getNamespace() != null) {
        nameSpace = element.getNamespace().getURI();
    }

    /*
     * When new module has to be added, and if curnode has reference of
     * previous module, then we need to traverse back to parent(logical root
     * node).
     */
    if (ydtExtBuilder.getRootNode() == ydtExtBuilder.getCurNode().getParent() && prevNodeNamespace != null
            && !prevNodeNamespace.equals(nameSpace)) {
        ydtExtBuilder.traverseToParent();
    }

    if (nodeType == OBJECT_NODE && element.content() == null || element.content().isEmpty()) {
        nodeType = TEXT_NODE;
    }
    if (nodeType == OBJECT_NODE) {
        if (ydtExtBuilder != null) {
            if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) {
                ydtExtBuilder.addChild(null, nameSpace, opType);
            }
            ydtExtBuilder.addChild(element.getName(), nameSpace, opType);
        }
    } else if (nodeType == TEXT_NODE) {

        if (ydtExtBuilder != null) {
            if (ydtExtBuilder.getCurNode() == ydtExtBuilder.getRootNode()) {
                ydtExtBuilder.addChild(null, nameSpace, opType);
            }
            ydtExtBuilder.addLeaf(element.getName(), nameSpace, element.getText());
        }
    }

    if (nameSpace != null) {
        prevNodeNamespace = nameSpace;
    }
}