Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

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

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.adspore.splat.xep0060.PubSubEngine.java

License:Open Source License

private static IQ getDefaultNodeConfiguration(PubSubService service, IQ iq, Element childElement,
        Element defaultElement) {
    String type = defaultElement.attributeValue("type");
    type = type == null ? "leaf" : type;

    boolean isLeafType = "leaf".equals(type);
    DefaultNodeConfiguration config = service.getDefaultNodeConfiguration(isLeafType);
    if (config == null) {
        // Service does not support the requested node type so return an error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", isLeafType ? "leaf" : "collections");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }// w ww  .  j  a  v a 2  s  .  co m

    // Return data form containing default node configuration
    IQ reply = IQ.createResultIQ(iq);
    Element replyChildElement = childElement.createCopy();
    reply.setChildElement(replyChildElement);
    replyChildElement.element("default").add(config.getConfigurationForm().getElement());
    return reply;
}

From source file:com.adspore.splat.xep0060.PubSubEngine.java

License:Open Source License

private static IQ purgeNode(PubSubService service, IQ iq, Element purgeElement) {
    String nodeID = purgeElement.attributeValue("node");
    if (nodeID == null) {
        // NodeID was not provided. Return bad-request error
        return createErrorPacket(iq, PacketError.Condition.bad_request, null);
    }/* www  .j  av  a 2s .  c  o  m*/
    Node node = service.getNode(nodeID);
    if (node == null) {
        // Node does not exist. Return item-not-found error
        return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
    }
    if (!node.isAdmin(iq.getFrom())) {
        // Requesting entity is prohibited from configuring this node. Return forbidden error
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }
    if (!((LeafNode) node).isPersistPublishedItems()) {
        // Node does not persist items. Return feature-not-implemented error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "persistent-items");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }
    if (node.isCollectionNode()) {
        // Node is a collection node. Return feature-not-implemented error
        Element pubsubError = DocumentHelper
                .createElement(QName.get("unsupported", "http://jabber.org/protocol/pubsub#errors"));
        pubsubError.addAttribute("feature", "purge-nodes");
        return createErrorPacket(iq, PacketError.Condition.feature_not_implemented, pubsubError);
    }

    // Purge the node
    ((LeafNode) node).purge();
    return createSuccessPacket(iq);
}

From source file:com.adspore.splat.xep0060.PubSubEngine.java

License:Open Source License

private static IQ modifyNodeAffiliations(PubSubService service, IQ iq, Element entitiesElement) {
    String nodeID = entitiesElement.attributeValue("node");
    if (nodeID == null) {
        // NodeID was not provided. Return bad-request error.
        return createErrorPacket(iq, PacketError.Condition.bad_request, null);
    }/*from  w  w  w .  j  a  va2  s . co m*/
    Node node = service.getNode(nodeID);
    if (node == null) {
        // Node does not exist. Return item-not-found error.
        return createErrorPacket(iq, PacketError.Condition.item_not_found, null);
    }
    if (!node.isAdmin(iq.getFrom())) {
        // Requesting entity is prohibited from getting affiliates list. Return forbidden error.
        return createErrorPacket(iq, PacketError.Condition.forbidden, null);
    }

    IQ reply = IQ.createResultIQ(iq);
    Collection<JID> invalidAffiliates = new ArrayList<JID>();

    // Process modifications or creations of affiliations
    for (Iterator it = entitiesElement.elementIterator("affiliation"); it.hasNext();) {
        Element affiliation = (Element) it.next();
        JID owner = new JID(affiliation.attributeValue("jid"));
        String newAffiliation = affiliation.attributeValue("affiliation");
        // Get current affiliation of this user (if any)
        NodeAffiliate affiliate = node.getAffiliate(owner);

        // Check that we are not removing the only owner of the node
        if (affiliate != null && !affiliate.getAffiliation().name().equals(newAffiliation)) {
            // Trying to modify an existing affiliation
            if (affiliate.getAffiliation() == NodeAffiliate.Affiliation.owner && node.getOwners().size() == 1) {
                // Trying to remove the unique owner of the node. Include in error answer.
                invalidAffiliates.add(owner);
                continue;
            }
        }

        // Owner is setting affiliations for new entities or modifying
        // existing affiliations
        if ("owner".equals(newAffiliation)) {
            node.addOwner(owner);
        } else if ("publisher".equals(newAffiliation)) {
            node.addPublisher(owner);
        } else if ("none".equals(newAffiliation)) {
            node.addNoneAffiliation(owner);
        } else {
            node.addOutcast(owner);
        }
    }

    // Process invalid entities that tried to remove node owners. Send original affiliation
    // of the invalid entities.
    if (!invalidAffiliates.isEmpty()) {
        reply.setError(PacketError.Condition.not_acceptable);
        Element child = reply.setChildElement("pubsub", "http://jabber.org/protocol/pubsub#owner");
        Element entities = child.addElement("affiliations");
        if (!node.isRootCollectionNode()) {
            entities.addAttribute("node", node.getNodeID());
        }
        for (JID affiliateJID : invalidAffiliates) {
            NodeAffiliate affiliate = node.getAffiliate(affiliateJID);
            Element entity = entities.addElement("affiliation");
            entity.addAttribute("jid", affiliate.getJID().toString());
            entity.addAttribute("affiliation", affiliate.getAffiliation().name());
        }
    }
    return reply;
}

From source file:com.ah.be.ls.stat.StatManager.java

public Document stat() throws DocumentException, IOException, Exception {
    log.info("StatManager", "stats.size=" + stats.size());
    if (stats == null || stats.isEmpty()) {
        throw new Exception("no stat config");
    }//from  w  w w  . j ava2s  .co  m
    boolean isHMOL = NmsUtil.isProduction();
    boolean isHM = !NmsUtil.isHostedHMApplication();
    if (!isHMOL && !isHM) {
        log.warn("StatManager", "the server is not production HMOL or Stand alone HM, do not do data mining.");
        return null;
    }

    Document document = generateDocumentTemplate();

    Map<String, List<Element>> entity = new HashMap<String, List<Element>>();

    getTargetedHiveApVhmMapping();

    if (isHMOL) {// HMOL
        vhms = getHmolVhmList();
        if (vhms.isEmpty()) {
            log.warn("StatManager", "the server do not have any HMOL customer, do not do data mining.");
            return null;
        }
    }

    for (StatConfig stat : stats) {
        Map<String, Element> featureElements = buildOfDocumentFragment(stat, isHMOL);
        if (null == featureElements) {
            continue;
        }
        // store all document fragment
        for (String key : featureElements.keySet()) {
            List<Element> elements = entity.get(key);
            if (null == elements) {
                elements = new ArrayList<Element>();
                entity.put(key, elements);
            }
            elements.add(featureElements.get(key));
        }
    }

    // generate document
    if (isHMOL) {
        // create document root element
        Element root = document.addElement("stats");
        root.addAttribute("version", VERSION);
        for (String vhmId : entity.keySet()) {
            Element vhmRoot = createVhmStatElement(vhmId);
            List<Element> elements = entity.get(vhmId);
            for (Element element : elements) {
                vhmRoot.add(element);
            }
            root.add(vhmRoot);
        }
    } else {
        // fetch system id
        String systemId = "";
        LicenseInfo licenseInfo = HmBeLicenseUtil.getLicenseInfo();
        if (licenseInfo != null && null != licenseInfo.getSystemId()) {
            systemId = licenseInfo.getSystemId();
        }
        // create document root element
        Element root = document.addElement("stat");
        root.addAttribute("version", VERSION);
        root.addAttribute("system-id", systemId);
        List<Element> elements = entity.get("HM");
        if (null != elements) {
            for (Element element : elements) {
                root.add(element);
            }
        }
    }
    return document;
}

From source file:com.ah.be.ls.stat.StatManager.java

private Element createFeatureElement(int featureId, String featureName) {
    Element ele = DocumentHelper.createElement("feature");
    ele.addAttribute("id", String.valueOf(featureId));
    ele.addAttribute("name", featureName);
    return ele;/*  w w  w .ja v a  2 s  .  c  om*/
}

From source file:com.ah.be.ls.stat.StatManager.java

private Element createVhmStatElement(String vhmId) {
    Element elem = DocumentHelper.createElement("stat");
    return elem.addAttribute("vhm-id", vhmId);
}

From source file:com.ah.be.ls.stat.StatManager.java

private Element createPlatformElement(String apModel) {
    Element ele = DocumentHelper.createElement("platform");
    ele.addAttribute("type", String.valueOf(apModel));
    return ele;//from  w  w  w. ja  v  a  2 s.c  om
}

From source file:com.ah.be.ls.stat.StatManager.java

private Element createL2Element(int number) {
    Element ele = DocumentHelper.createElement("L2");
    return ele.addAttribute("count", String.valueOf(number));
}

From source file:com.ah.be.ls.stat.StatManager.java

private Element createL3Element(int number) {
    Element ele = DocumentHelper.createElement("L3");
    return ele.addAttribute("count", String.valueOf(number));
}

From source file:com.ah.be.ls.stat.StatManager.java

private Element createSwitchElement(int number) {
    Element ele = DocumentHelper.createElement("SW");
    return ele.addAttribute("count", String.valueOf(number));
}