Example usage for org.jdom2 Element addContent

List of usage examples for org.jdom2 Element addContent

Introduction

In this page you can find the example usage for org.jdom2 Element addContent.

Prototype

@Override
public Element addContent(final Collection<? extends Content> newContent) 

Source Link

Document

Appends all children in the given collection to the end of the content list.

Usage

From source file:com.kixeye.kixmpp.server.module.muc.DefaultMucRoomEventHandler.java

License:Apache License

private Element createMessage(String id, KixmppJid from, KixmppJid to, String type, String bodyText) {
    Element message = new Element("message");

    message.setAttribute("to", to.getFullJid());
    message.setAttribute("from", from.getFullJid());
    message.setAttribute("type", type);
    message.setAttribute("id", id);

    Element body = new Element("body");
    body.addContent(bodyText);

    message.addContent(body);// ww  w.  j  a  v  a2 s. c  o m

    return message;
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

/**
 * A user requests to join the room.//from w w  w.j  a v  a  2s  . com
 *
 * @param channel
 * @param nickname
 * @param mucStanza
 */
public void join(final Channel channel, String nickname, Element mucStanza) {
    KixmppJid jid = channel.attr(BindKixmppServerModule.JID).get();

    if (settings.isOpen() && !jidRoles.containsKey(jid.withoutResource())) {
        addUser(jid, nickname, MucRole.Participant, MucAffiliation.Member);
    }

    verifyMembership(jid.withoutResource());
    checkForNicknameInUse(nickname, jid);

    User user = usersByNickname.get(nickname);

    boolean existingUser = true;
    if (user == null) {
        user = new User(nickname, jid.withoutResource());
        usersByNickname.put(nickname, user);
        MucRoomEventHandler handler = service.getServer().getMucRoomEventHandler();
        if (handler != null) {
            handler.userAdded(this, user);
        }
        existingUser = false;
    }

    Client client = user.addClient(new Client(jid, nickname, channel));

    // xep-0045 7.2.3 begin
    // self presence
    KixmppJid fromRoomJid = roomJid.withResource(nickname);
    channel.writeAndFlush(createPresence(fromRoomJid, jid, MucRole.Participant, null));

    if (settings.isPresenceEnabled() && !existingUser) {
        // Send presence from existing occupants to new occupant
        sendExistingOccupantsPresenceToNewOccupant(user, channel);

        // Send new occupant's presence to all occupants
        broadcastPresence(fromRoomJid, MucRole.Participant, null);
    }
    // xep-0045 7.2.3 end

    if (settings.getSubject() != null) {
        Element message = new Element("message");
        message.setAttribute("id", UUID.randomUUID().toString());
        message.setAttribute("from", roomJid.withResource(nickname).toString());
        message.setAttribute("to", channel.attr(BindKixmppServerModule.JID).get().toString());
        message.setAttribute("type", "groupchat");

        message.addContent(new Element("subject").setText(settings.getSubject()));

        channel.writeAndFlush(message);
    }

    if (mucStanza != null) {
        Element history = mucStanza.getChild("history", mucStanza.getNamespace());

        if (history != null) {
            MucHistoryProvider historyProvider = mucModule.getHistoryProvider();

            if (historyProvider != null) {
                Integer maxChars = null;
                Integer maxStanzas = null;
                Integer seconds = null;

                String parsableString = history.getAttributeValue("maxchars");
                if (parsableString != null) {
                    try {
                        maxChars = Integer.parseInt(parsableString);
                    } catch (Exception e) {
                    }
                }
                parsableString = history.getAttributeValue("maxstanzas");
                if (parsableString != null) {
                    try {
                        maxStanzas = Integer.parseInt(parsableString);
                    } catch (Exception e) {
                    }
                }
                parsableString = history.getAttributeValue("seconds");
                if (parsableString != null) {
                    try {
                        seconds = Integer.parseInt(parsableString);
                    } catch (Exception e) {
                    }
                }

                String since = history.getAttributeValue("since");

                historyProvider.getHistory(roomJid, user.getBareJid(), maxChars, maxStanzas, seconds, since)
                        .addListener(new GenericFutureListener<Future<List<MucHistory>>>() {
                            @Override
                            public void operationComplete(Future<List<MucHistory>> future) throws Exception {
                                if (future.isSuccess()) {
                                    List<MucHistory> historyItems = future.get();
                                    if (historyItems != null) {
                                        for (MucHistory historyItem : historyItems) {
                                            Element message = new Element("message")
                                                    .setAttribute("id", UUID.randomUUID().toString())
                                                    .setAttribute("from",
                                                            roomJid.withResource(historyItem.getNickname())
                                                                    .toString())
                                                    .setAttribute("to",
                                                            channel.attr(BindKixmppServerModule.JID).get()
                                                                    .toString())
                                                    .setAttribute("type", "groupchat");
                                            message.addContent(
                                                    new Element("body").setText(historyItem.getBody()));

                                            Element addresses = new Element("addresses", Namespace
                                                    .getNamespace("http://jabber.org/protocol/address"));
                                            addresses
                                                    .addContent(new Element("address", addresses.getNamespace())
                                                            .setAttribute("type", "ofrom").setAttribute("jid",
                                                                    historyItem.getFrom().toString()));
                                            message.addContent(addresses);

                                            message.addContent(new Element("delay",
                                                    Namespace.getNamespace("urn:xmpp:delay"))
                                                            .setAttribute("from", roomJid.toString())
                                                            .setAttribute("stamp", XmppDateUtils
                                                                    .format(historyItem.getTimestamp())));

                                            channel.write(message);
                                        }
                                        channel.flush();
                                    }
                                }
                            }
                        });
            }
        }
    }

    channel.closeFuture().addListener(new CloseChannelListener(client));
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

private Element createPresence(KixmppJid from, KixmppJid to, MucRole role, String type) {
    Element presence = new Element("presence");

    presence.setAttribute("id", UUID.randomUUID().toString());
    presence.setAttribute("from", from.toString());
    presence.setAttribute("to", to.toString());

    Element x = new Element("x", Namespace.getNamespace("http://jabber.org/protocol/muc#user"));

    if (type != null) {
        presence.setAttribute("type", type);
    }/*from w  w w.  java  2  s  .  c  om*/

    Element item = new Element("item", Namespace.getNamespace("http://jabber.org/protocol/muc#user"));
    item.setAttribute("affiliation", "member");

    switch (role) {
    case Participant:
        item.setAttribute("role", "participant");
        break;
    case Moderator:
        item.setAttribute("role", "moderator");
        break;
    case Visitor:
        item.setAttribute("role", "visitor");
        break;
    }
    x.addContent(item);
    presence.addContent(x);

    return presence;
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

/**
 * Sends an direct invitation to a user.  Note: The smack client does not recognize this as a valid room invite.
 *///from   w  w  w .  ja  va  2 s  .  co  m
public void sendDirectInvite(KixmppJid from, Channel userChannelToInvite, String reason) {
    Element message = new Element("message");
    message.setAttribute("to", userChannelToInvite.attr(BindKixmppServerModule.JID).get().getFullJid());
    if (from != null) {
        message.setAttribute("from", from.getFullJid());
    }

    Element x = new Element("x", Namespace.getNamespace("jabber:x:conference"));
    x.setAttribute("jid", roomJid.getFullJid());
    if (reason != null) {
        x.setAttribute("reason", reason);
    }

    message.addContent(x);

    userChannelToInvite.writeAndFlush(message);
}

From source file:com.kixeye.kixmpp.server.module.muc.MucRoom.java

License:Apache License

/**
 * Sends a mediated invitation to a user.
 *///from  www.  j a v  a 2s .c  om
public void sendMediatedInvite(KixmppJid from, Channel userChannelToInvite, String reason) {

    Element invite = new Element("invite");
    invite.setAttribute("from", from.getFullJid());
    if (reason != null) {
        Element el = new Element("reason");
        el.setText(reason);
        invite.addContent(el);
    }

    Element x = new Element("x", Namespace.getNamespace("http://jabber.org/protocol/muc#user"));
    x.addContent(invite);

    Element message = new Element("message");
    message.setAttribute("to", userChannelToInvite.attr(BindKixmppServerModule.JID).get().getFullJid());
    message.setAttribute("from", roomJid.getFullJid());
    message.addContent(x);

    userChannelToInvite.writeAndFlush(message);
}

From source file:com.lapis.jsfexporter.xml.XMLExportType.java

License:Apache License

@Override
public Element exportRow(IExportRow row) {
    Element currentElement = (Element) row.getParentRowId();
    if (currentElement == null) {
        currentElement = rootElement;/* ww w. j  a  v a2s.c  o m*/
    }

    for (String namePart : row.getName()) {
        Element subElement = new Element(cleanElementName(namePart));
        currentElement.addContent(subElement);
        currentElement = subElement;
    }

    for (IExportCell cell : row.getCells()) {
        Element cellElement = currentElement;
        for (String namePart : cell.getName()) {
            Element subElement = cellElement.getChild(namePart);
            if (subElement == null) {
                subElement = new Element(cleanElementName(namePart));
                cellElement.addContent(subElement);
            }
            cellElement = subElement;
        }

        if (!cellElement.getText().equals("")) {
            String cellName = cellElement.getName();
            cellElement = cellElement.getParentElement();
            Element newCellElement = new Element(cellName);
            cellElement.addContent(newCellElement);
            cellElement = newCellElement;
        }
        cellElement.setText(cell.getValue());
    }

    return currentElement;
}

From source file:com.medvision360.medrecord.basex.AbstractXmlConverter.java

License:Creative Commons License

protected void set(Element element, String xpath, String value) {
    Element currentElement = element;
    String[] paths = xpath.split("/");
    for (int i = 0; i < paths.length; i++) {
        String pathPart = paths[i].trim();
        if (pathPart.isEmpty()) {
            continue;
        }//  w w w . ja v a 2 s . c o  m

        Element newElement = element.getChild(pathPart);
        if (newElement == null) {
            newElement = new Element(pathPart);
            currentElement.addContent(newElement);
        }
        currentElement = newElement;
    }
    currentElement.setText(value);
}

From source file:com.medvision360.medrecord.basex.MockLocatableSerializer.java

License:Creative Commons License

private void set(Element element, String xpath, String value) {
    Element currentElement = element;
    String[] paths = xpath.split("/");
    for (int i = 0; i < paths.length; i++) {
        String pathPart = paths[i].trim();
        if (pathPart.isEmpty()) {
            continue;
        }/*from   w w w. jav a 2  s.c  o  m*/

        Element newElement = element.getChild(pathPart);
        if (newElement == null) {
            newElement = new Element(pathPart);
        }
        currentElement.addContent(newElement);
        currentElement = newElement;
    }
    currentElement.setText(value);
}

From source file:com.novell.ldapchai.cr.ChaiResponseSet.java

License:Open Source License

static String rsToChaiXML(final ChaiResponseSet rs) throws ChaiValidationException, ChaiOperationException {
    final Element rootElement = new Element(XML_NODE_ROOT);
    rootElement.setAttribute(XML_ATTRIBUTE_MIN_RANDOM_REQUIRED,
            String.valueOf(rs.getChallengeSet().getMinRandomRequired()));
    rootElement.setAttribute(XML_ATTRIBUTE_LOCALE, rs.getChallengeSet().getLocale().toString());
    rootElement.setAttribute(XML_ATTRIBUTE_VERSION, VALUE_VERSION);
    rootElement.setAttribute(XML_ATTRIBUTE_CHAI_VERSION, ChaiConstant.CHAI_API_VERSION);

    if (rs.caseInsensitive) {
        rootElement.setAttribute(XML_ATTRIBUTE_CASE_INSENSITIVE, "true");
    }//from w  w w.ja v  a 2s. c  om

    if (rs.csIdentifier != null) {
        rootElement.setAttribute(XML_ATTRIBUTE_CHALLENGE_SET_IDENTIFER, rs.csIdentifier);
    }

    if (rs.timestamp != null) {
        rootElement.setAttribute(XML_ATTRIBUTE_TIMESTAMP, DATE_FORMATTER.format(rs.timestamp));
    }

    if (rs.crMap != null) {
        for (final Challenge loopChallenge : rs.crMap.keySet()) {
            final Answer answer = rs.crMap.get(loopChallenge);
            final Element responseElement = challengeToXml(loopChallenge, answer, XML_NODE_RESPONSE);
            rootElement.addContent(responseElement);
        }
    }

    if (rs.helpdeskCrMap != null) {
        for (final Challenge loopChallenge : rs.helpdeskCrMap.keySet()) {
            final Answer answer = rs.helpdeskCrMap.get(loopChallenge);
            final Element responseElement = challengeToXml(loopChallenge, answer, XML_NODE_HELPDESK_RESPONSE);
            rootElement.addContent(responseElement);
        }
    }

    final Document doc = new Document(rootElement);
    final XMLOutputter outputter = new XMLOutputter();
    final Format format = Format.getRawFormat();
    format.setTextMode(Format.TextMode.PRESERVE);
    format.setLineSeparator("");
    outputter.setFormat(format);
    return outputter.outputString(doc);
}

From source file:com.novell.ldapchai.cr.ChaiResponseSet.java

License:Open Source License

private static Element challengeToXml(final Challenge loopChallenge, final Answer answer,
        final String elementName) throws ChaiOperationException {
    final Element responseElement = new Element(elementName);
    responseElement
            .addContent(new Element(XML_NODE_CHALLENGE).addContent(new Text(loopChallenge.getChallengeText())));
    final Element answerElement = answer.toXml();
    responseElement.addContent(answerElement);
    responseElement.setAttribute(XML_ATTRIBUTE_ADMIN_DEFINED, String.valueOf(loopChallenge.isAdminDefined()));
    responseElement.setAttribute(XML_ATTRIBUTE_REQUIRED, String.valueOf(loopChallenge.isRequired()));
    responseElement.setAttribute(XNL_ATTRIBUTE_MIN_LENGTH, String.valueOf(loopChallenge.getMinLength()));
    responseElement.setAttribute(XNL_ATTRIBUTE_MAX_LENGTH, String.valueOf(loopChallenge.getMaxLength()));
    return responseElement;
}