Example usage for org.dom4j Element createCopy

List of usage examples for org.dom4j Element createCopy

Introduction

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

Prototype

Element createCopy();

Source Link

Document

Creates a deep copy of this element The new element is detached from its parent, and getParent() on the clone will return null.

Usage

From source file:org.frogx.service.core.iq.IQDiscoInfoHandler.java

License:Open Source License

/**
 * Handle a disco#info query and get the resulting IQ packet.
 * /*from w  w  w  .j  a v a  2 s . c o  m*/
 * @param packet The IQ Query which is handled.
 * @return The IQ reply resulting from the query.
 */
public IQ handleIQ(IQ packet) {
    if (packet.getType() == IQ.Type.result) {
        // TODO: Maybe we want to detect a local MUC service
        return null;
    }

    if (packet.getType() == IQ.Type.get) {
        // create an empty reply
        IQ reply = IQ.createResultIQ(packet);

        String roomName = packet.getTo().getNode();
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        reply.setChildElement(iq.createCopy());
        Element queryElement = reply.getChildElement();

        if ((roomName == null) && (node == null)) {
            // Create and add a the identity of the mug service
            Element identity = queryElement.addElement("identity");
            identity.addAttribute("category", "game");
            identity.addAttribute("type", "multi-user");
            identity.addAttribute("name", service.getDescription());

            // Add Extra Identities (if exist)
            if (service.getExtraIdentities() != null)
                for (Element el : service.getExtraIdentities())
                    queryElement.add(el);

            // Create and add a the disco#info feature
            Element feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/disco#info");
            // Create and add a the disco#item feature
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", "http://jabber.org/protocol/disco#items");
            // Create and add a the search feature
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", "jabber:iq:search");
            // Create and add a the feature provided by the mug service
            feature = queryElement.addElement("feature");
            feature.addAttribute("var", MUGService.mugNS);

            // Create and add the supported game features
            Map<String, MultiUserGame> gameClasses = service.getSupportedGames();
            for (String namespace : gameClasses.keySet()) {
                feature = queryElement.addElement("feature");
                feature.addAttribute("var", namespace);
            }

            // Add Extra Features
            if (service.getExtraFeatures() != null)
                for (String ns : service.getExtraFeatures()) {
                    feature = queryElement.addElement("feature");
                    feature.addAttribute("var", ns);
                }
        } else if (roomName != null && node == null) {
            // Answer the identity and features of a given room
            MUGRoom room = service.getGameRoom(roomName);
            if (room != null && room.isPublicRoom() && !room.isLocked()) {
                Element identity = queryElement.addElement("identity");
                identity.addAttribute("category", "game");
                identity.addAttribute("name", room.getNaturalLanguageName());
                identity.addAttribute("type", "multi-user");

                // Create and add a the feature provided by the mug service
                Element feature = queryElement.addElement("feature");
                feature.addAttribute("var", MUGService.mugNS);

                // Create and add the supported game features
                feature = queryElement.addElement("feature");
                feature.addAttribute("var", room.getGame().getNamespace());

                // Always add public since only public rooms can be discovered
                feature = queryElement.addElement("feature");
                feature.addAttribute("var", "mug_public");

                feature = queryElement.addElement("feature");
                if (room.isMembersOnly()) {
                    feature.addAttribute("var", "mug_membersonly");
                } else {
                    feature.addAttribute("var", "mug_open");
                }

                feature = queryElement.addElement("feature");
                if (room.isModerated()) {
                    feature.addAttribute("var", "mug_moderated");
                } else {
                    feature.addAttribute("var", "mug_unmoderated");
                }

                feature = queryElement.addElement("feature");
                if (room.isNonAnonymous()) {
                    feature.addAttribute("var", "mug_nonanonymous");
                } else if (room.isSemiAnonymous()) {
                    feature.addAttribute("var", "mug_semianonymous");
                } else if (room.isFullyAnonymous()) {
                    feature.addAttribute("var", "mug_fullyanonymous");
                }

                feature = queryElement.addElement("feature");
                if (room.isPasswordProtected()) {
                    feature.addAttribute("var", "mug_passwordprotected");
                } else {
                    feature.addAttribute("var", "mug_unsecured");
                }

                if (room.getExtraFeatures() != null)
                    for (String ns : room.getExtraFeatures()) {
                        feature = queryElement.addElement("feature");
                        feature.addAttribute("var", ns);
                    }

                DataForm dataForm = new DataForm(DataForm.Type.result);

                FormField field = dataForm.addField();
                field.setVariable("FORM_TYPE");
                field.setType(FormField.Type.hidden);
                field.addValue(MUGService.mugNS + "#matchinfo");

                field = dataForm.addField();
                field.setVariable("mug#game");
                field.setType(FormField.Type.hidden);
                field.addValue(room.getGame().getNamespace());

                field = dataForm.addField();
                field.setVariable("mug#matchinfo_roomname");
                field.setLabel(locale.getLocalizedString("mug.extended.info.name"));
                field.addValue(room.getNaturalLanguageName());

                field = dataForm.addField();
                field.setVariable("mug#matchinfo_description");
                field.setLabel(locale.getLocalizedString("mug.extended.info.desc"));
                field.addValue(room.getDescription());

                field = dataForm.addField();
                field.setVariable("mug#matchinfo_category");
                field.setLabel(locale.getLocalizedString("mug.extended.info.category"));
                field.addValue(room.getGame().getCategory());

                field = dataForm.addField();
                field.setVariable("mug#match_occupants");
                field.setLabel(locale.getLocalizedString("mug.extended.info.occupants"));
                field.addValue(Integer.toString(room.getOccupantsCount()));

                field = dataForm.addField();
                field.setVariable("mug#match_players");
                field.setLabel(locale.getLocalizedString("mug.extended.info.players"));
                field.addValue(Integer.toString(room.getPlayersCount()));

                if (room.getMaxOccupants() > 0) {
                    field = dataForm.addField();
                    field.setVariable("mug#match_maxoccupants");
                    field.setLabel(locale.getLocalizedString("mug.extended.info.num_occupants"));
                    field.addValue(Integer.toString(room.getMaxOccupants()));
                }

                if (room.getExtraExtendedDiscoFields() != null)
                    for (FormField fld : room.getExtraExtendedDiscoFields()) {
                        //TODO: The fields shoundn't copied this way, please fix!
                        field = dataForm.addField();
                        field.setVariable(fld.getVariable());
                        field.setLabel(fld.getLabel());
                        field.addValue(fld.getValues());
                    }

                queryElement.add(dataForm.getElement());
            } else
                reply.setError(PacketError.Condition.item_not_found);
        } else
            reply.setError(PacketError.Condition.item_not_found);
        return reply;
    } else {
        // Ignore packets from Type error or set
        return null;
    }
}

From source file:org.frogx.service.core.iq.IQDiscoItemsHandler.java

License:Open Source License

/**
 * Handle a disco#items query and get the resulting IQ packet.
 * /* www.j av  a  2 s .  c om*/
 * @param packet The IQ Query which is handled.
 * @return The IQ reply resulting from the query.
 */
public IQ handleIQ(IQ packet) {
    System.out.println("DiscoItemsHandler");

    if (packet.getType() == IQ.Type.result) {
        // TODO: Maybe we want to detect a local MUC service
        return null;
    }

    if (packet.getType() == IQ.Type.get) {
        // create an empty reply
        IQ reply = IQ.createResultIQ(packet);

        String roomName = packet.getTo().getNode();
        Element iq = packet.getChildElement();
        String node = iq.attributeValue("node");
        reply.setChildElement(iq.createCopy());
        Element queryElement = reply.getChildElement();

        if ((roomName == null) && (node == null)) {
            // List all public rooms
            for (MUGRoom room : service.getGameRooms()) {
                if (room.isPublicRoom() && !room.isLocked()) {
                    Element item = queryElement.addElement("item");
                    item.addAttribute("jid", room.getJID().toBareJID());
                    item.addAttribute("name", room.getNaturalLanguageName());
                }
            }
        } else if (roomName != null && node == null) {
            // If it's allowed list all occupants
            MUGRoom room = service.getGameRoom(roomName);
            if (room != null) {
                if (room.canDiscoverOccupants()) {
                    for (MUGOccupant occupant : room.getOccupants()) {
                        Element item = queryElement.addElement("item");
                        item.addAttribute("jid", occupant.getRoomAddress().toString());
                    }
                }
            } else
                reply.setError(PacketError.Condition.item_not_found);
        } else
            reply.setError(PacketError.Condition.item_not_found);
        return reply;
    } else {
        // Ignore packets from Type error or set
        return null;
    }
}

From source file:org.jage.platform.config.xml.loaders.MultipleTagDuplicator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void extractMultiples(final Document document) throws ConfigurationException {
    for (Element multipleElement : (List<Element>) COLLECTION_XPATH.selectNodes(document)) {
        final Element collectionElement = multipleElement.getParent();
        final int count = toInteger(getRequiredAttribute(multipleElement, ConfigAttributes.COUNT));

        // We get the position of <multiple> in the parent
        int index = collectionElement.elements().indexOf(multipleElement);

        Element child = getOnlyElement(consumingIterable((List<Element>) multipleElement.elements()));
        if (!(is(child, ConfigTags.REFERENCE) || is(child, ConfigTags.VALUE))) {
            // Child is some component so we move the definition to the parent and replace it with a reference
            collectionElement.elements().add(index++, child);
            child = newReferenceElement(getRequiredAttribute(child, ConfigAttributes.NAME));
        }/*from w  w w  .java2  s.  c om*/

        // No we are sure child is a reference or value
        // Add 'count' copies to the collection, at the same index
        for (int i = 0; i < count; i++) {
            collectionElement.elements().add(index, child.createCopy());
        }

        // Finally, remove the multiple element
        collectionElement.elements().remove(multipleElement);
    }
}

From source file:org.jboss.mx.metadata.JBossXMBean10.java

License:Open Source License

/**
 * Convert org.dom4j.Element->org.w3c.dom.Element
 */// w w w  .  j  a va 2 s.  c  om
private org.w3c.dom.Element toW3CElement(org.dom4j.Element d4element) throws org.dom4j.DocumentException {
    // prepare
    org.dom4j.Document d4doc = org.dom4j.DocumentFactory.getInstance().createDocument();
    org.dom4j.io.DOMWriter d4Writer = new org.dom4j.io.DOMWriter();
    // copy
    d4doc.setRootElement(d4element.createCopy());
    // convert
    org.w3c.dom.Document doc = d4Writer.write(d4doc);
    // return root Element - should I copy again?
    return doc.getDocumentElement();
}

From source file:org.jinglenodes.relay.RelayEventIQ.java

License:Open Source License

public Element getElement() {
    Element e = super.getElement();
    e = e.createCopy();
    e.add(getChildElement());
    return e;
}

From source file:org.jivesoftware.admin.AdminConsole.java

License:Open Source License

/**
 * Rebuilds the generated model./* ww  w .ja  va 2s. c om*/
 */
private static synchronized void rebuildModel() {
    Document doc = DocumentFactory.getInstance().createDocument();
    generatedModel = coreModel.createCopy();
    doc.add(generatedModel);

    // Add in all overrides.
    for (Element element : overrideModels.values()) {
        // See if global settings are overriden.
        Element appName = (Element) element.selectSingleNode("//adminconsole/global/appname");
        if (appName != null) {
            Element existingAppName = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/appname");
            existingAppName.setText(appName.getText());
            if (appName.attributeValue("plugin") != null) {
                existingAppName.addAttribute("plugin", appName.attributeValue("plugin"));
            }
        }
        Element appLogoImage = (Element) element.selectSingleNode("//adminconsole/global/logo-image");
        if (appLogoImage != null) {
            Element existingLogoImage = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/logo-image");
            existingLogoImage.setText(appLogoImage.getText());
            if (appLogoImage.attributeValue("plugin") != null) {
                existingLogoImage.addAttribute("plugin", appLogoImage.attributeValue("plugin"));
            }
        }
        Element appLoginImage = (Element) element.selectSingleNode("//adminconsole/global/login-image");
        if (appLoginImage != null) {
            Element existingLoginImage = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/login-image");
            existingLoginImage.setText(appLoginImage.getText());
            if (appLoginImage.attributeValue("plugin") != null) {
                existingLoginImage.addAttribute("plugin", appLoginImage.attributeValue("plugin"));
            }
        }
        Element appVersion = (Element) element.selectSingleNode("//adminconsole/global/version");
        if (appVersion != null) {
            Element existingVersion = (Element) generatedModel
                    .selectSingleNode("//adminconsole/global/version");
            if (existingVersion != null) {
                existingVersion.setText(appVersion.getText());
                if (appVersion.attributeValue("plugin") != null) {
                    existingVersion.addAttribute("plugin", appVersion.attributeValue("plugin"));
                }
            } else {
                ((Element) generatedModel.selectSingleNode("//adminconsole/global"))
                        .add(appVersion.createCopy());
            }
        }
        // Tabs
        for (Object o : element.selectNodes("//tab")) {
            Element tab = (Element) o;
            String id = tab.attributeValue("id");
            Element existingTab = getElemnetByID(id);
            // Simple case, there is no existing tab with the same id.
            if (existingTab == null) {
                // Make sure that the URL on the tab is set. If not, default to the
                // url of the first item.
                if (tab.attributeValue("url") == null) {
                    Element firstItem = (Element) tab.selectSingleNode("//item[@url]");
                    if (firstItem != null) {
                        tab.addAttribute("url", firstItem.attributeValue("url"));
                    }
                }
                generatedModel.add(tab.createCopy());
            }
            // More complex case -- a tab with the same id already exists.
            // In this case, we have to overrite only the difference between
            // the two elements.
            else {
                overrideTab(existingTab, tab);
            }
        }
    }

    // Special case: show a link to Clearspace admin console if it is integrated with
    // Openfire.
    if (ClearspaceManager.isEnabled()) {
        Element clearspace = generatedModel.addElement("tab");
        clearspace.addAttribute("id", "tab-clearspace");
        clearspace.addAttribute("name", LocaleUtils.getLocalizedString("tab.tab-clearspace"));
        clearspace.addAttribute("url", "clearspace-status.jsp");
        clearspace.addAttribute("description", LocaleUtils.getLocalizedString("tab.tab-clearspace.descr"));
        Element sidebar = clearspace.addElement("sidebar");
        sidebar.addAttribute("id", "sidebar-clearspace");
        sidebar.addAttribute("name", LocaleUtils.getLocalizedString("sidebar.sidebar-clearspace"));

        Element statusItem = sidebar.addElement("item");
        statusItem.addAttribute("id", "clearspace-status");
        statusItem.addAttribute("name", LocaleUtils.getLocalizedString("sidebar.clearspace-status"));
        statusItem.addAttribute("url", "clearspace-status.jsp");
        statusItem.addAttribute("description",
                LocaleUtils.getLocalizedString("sidebar.clearspace-status.descr"));

        Element adminItem = sidebar.addElement("item");
        adminItem.addAttribute("id", "clearspace-admin");
        adminItem.addAttribute("name", LocaleUtils.getLocalizedString("sidebar.clearspace-admin"));
        adminItem.addAttribute("url", "clearspace-admin.jsp");
        adminItem.addAttribute("description", LocaleUtils.getLocalizedString("sidebar.clearspace-admin.descr"));

    }
}

From source file:org.jivesoftware.admin.AdminConsole.java

License:Open Source License

private static void overrideTab(Element tab, Element overrideTab) {
    // Override name, url, description.
    if (overrideTab.attributeValue("name") != null) {
        tab.addAttribute("name", overrideTab.attributeValue("name"));
    }/*from  w w  w . java  2  s. c om*/
    if (overrideTab.attributeValue("url") != null) {
        tab.addAttribute("url", overrideTab.attributeValue("url"));
    }
    if (overrideTab.attributeValue("description") != null) {
        tab.addAttribute("description", overrideTab.attributeValue("description"));
    }
    if (overrideTab.attributeValue("plugin") != null) {
        tab.addAttribute("plugin", overrideTab.attributeValue("plugin"));
    }
    // Override sidebar items.
    for (Iterator i = overrideTab.elementIterator(); i.hasNext();) {
        Element sidebar = (Element) i.next();
        String id = sidebar.attributeValue("id");
        Element existingSidebar = getElemnetByID(id);
        // Simple case, there is no existing sidebar with the same id.
        if (existingSidebar == null) {
            tab.add(sidebar.createCopy());
        }
        // More complex case -- a sidebar with the same id already exists.
        // In this case, we have to overrite only the difference between
        // the two elements.
        else {
            overrideSidebar(existingSidebar, sidebar);
        }
    }
}

From source file:org.jivesoftware.admin.AdminConsole.java

License:Open Source License

private static void overrideSidebar(Element sidebar, Element overrideSidebar) {
    // Override name.
    if (overrideSidebar.attributeValue("name") != null) {
        sidebar.addAttribute("name", overrideSidebar.attributeValue("name"));
    }/*from   www .j a v  a 2  s . co  m*/
    if (overrideSidebar.attributeValue("plugin") != null) {
        sidebar.addAttribute("plugin", overrideSidebar.attributeValue("plugin"));
    }
    // Override entries.
    for (Iterator i = overrideSidebar.elementIterator(); i.hasNext();) {
        Element entry = (Element) i.next();
        String id = entry.attributeValue("id");
        Element existingEntry = getElemnetByID(id);
        // Simple case, there is no existing sidebar with the same id.
        if (existingEntry == null) {
            sidebar.add(entry.createCopy());
        }
        // More complex case -- an entry with the same id already exists.
        // In this case, we have to overrite only the difference between
        // the two elements.
        else {
            overrideEntry(existingEntry, entry);
        }
    }
}

From source file:org.jivesoftware.admin.AdminConsole.java

License:Open Source License

private static void overrideEntry(Element entry, Element overrideEntry) {
    // Override name.
    if (overrideEntry.attributeValue("name") != null) {
        entry.addAttribute("name", overrideEntry.attributeValue("name"));
    }// ww  w .ja  v a 2s .  c  om
    if (overrideEntry.attributeValue("url") != null) {
        entry.addAttribute("url", overrideEntry.attributeValue("url"));
    }
    if (overrideEntry.attributeValue("description") != null) {
        entry.addAttribute("description", overrideEntry.attributeValue("description"));
    }
    if (overrideEntry.attributeValue("plugin") != null) {
        entry.addAttribute("plugin", overrideEntry.attributeValue("plugin"));
    }
    // Override any sidebars contained in the entry.
    for (Iterator i = overrideEntry.elementIterator(); i.hasNext();) {
        Element sidebar = (Element) i.next();
        String id = sidebar.attributeValue("id");
        Element existingSidebar = getElemnetByID(id);
        // Simple case, there is no existing sidebar with the same id.
        if (existingSidebar == null) {
            entry.add(sidebar.createCopy());
        }
        // More complex case -- a sidebar with the same id already exists.
        // In this case, we have to overrite only the difference between
        // the two elements.
        else {
            overrideSidebar(existingSidebar, sidebar);
        }
    }
}

From source file:org.jivesoftware.multiplexer.net.http.HttpSession.java

License:Open Source License

private void failDelivery(Collection<Element> packets) {
    if (packets == null) {
        // Do nothing if someone asked to deliver nothing :)
        return;//from   ww  w  .  jav  a  2  s. c  om
    }
    for (Element packet : packets) {
        // Inform the server that the wrapped stanza was not delivered
        String tag = packet.getName();
        if ("message".equals(tag)) {
            connectionManager.getServerSurrogate().deliveryFailed(packet, getStreamID());
        } else if ("iq".equals(tag)) {
            String type = packet.attributeValue("type", "get");
            if ("get".equals(type) || "set".equals(type)) {
                // Build IQ of type ERROR
                Element reply = packet.createCopy();
                reply.addAttribute("type", "error");
                reply.addAttribute("from", packet.attributeValue("to"));
                reply.addAttribute("to", packet.attributeValue("from"));
                Element error = reply.addElement("error");
                error.addAttribute("type", "wait");
                error.addElement("unexpected-request").addAttribute("xmlns",
                        "urn:ietf:params:xml:ns:xmpp-stanzas");
                // Bounce the failed IQ packet
                connectionManager.getServerSurrogate().send(reply.asXML(), getStreamID());
            }
        }
    }
}