Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

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

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

From source file:com.magicpwd._comn.item.AreaItem.java

License:Open Source License

@Override
public boolean exportAsXml(Element element) {
    element.addElement("name").setText(getName());
    element.addElement("data").setText(getData());
    return true;//from ww w .  ja  va  2 s.c  o  m
}

From source file:com.magicpwd._util.Jxml.java

License:Open Source License

public static void save(String file, MpwdHeader keys, List pwds) {
    Document doc = create();/* w ww .  j a v  a2  s .  co m*/

    // root element
    Element root = doc.addElement("MagicPwd").addAttribute("site", ConsEnv.HOMEPAGE);

    Element node = root.addElement("Key").addAttribute("type", "pwds");
    node.addElement(Lang.getLang(LangRes.P30F1C01, "status"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C02, "?"))
            .setText(keys.getP30F0102() + "");
    node.addElement(Lang.getLang(LangRes.P30F1C03, "catalog"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C04, "")).setText(keys.getP30F0106());
    node.addElement(Lang.getLang(LangRes.P30F1C04, "create"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C06, ""))
            .setText(keys.getP30F0107().toString());
    node.addElement(Lang.getLang(LangRes.P30F1C07, "title"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C08, "?")).setText(keys.getP30F0109());
    node.addElement(Lang.getLang(LangRes.P30F1C09, "meta"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C0A, "?")).setText(keys.getP30F010A());
    node.addElement(Lang.getLang(LangRes.P30F1C0B, "logo"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C0C, "?")).setText(keys.getP30F010B());
    node.addElement(Lang.getLang(LangRes.P30F1C0D, "limit"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C0E, ""))
            .setText(keys.getP30F010E().toString());
    node.addElement(Lang.getLang(LangRes.P30F1C0F, "hint"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C10, "??")).setText(keys.getP30F010F());
    node.addElement(Lang.getLang(LangRes.P30F1C11, "memo"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C12, "")).setText(keys.getP30F0110());
}

From source file:com.magicpwd._util.Jxml.java

License:Open Source License

public static void save(String file, MpwdHeader keys) {
    Document doc = create();/*ww w  .  j  a  va  2 s  . c  o m*/

    // root element
    Element root = doc.addElement("MagicPwd").addAttribute("site", ConsEnv.HOMEPAGE);

    Element node = root.addElement("Key").addAttribute("type", "note");
    node.addElement(Lang.getLang(LangRes.P30F1C03, "catalog"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C04, "")).setText(keys.getP30F0106());
    node.addElement(Lang.getLang(LangRes.P30F1C07, "title"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C08, "?")).setText(keys.getP30F0109());
    node.addElement(Lang.getLang(LangRes.P30F1C09, "meta"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C0A, "?")).setText(keys.getP30F010A());
    node.addElement(Lang.getLang(LangRes.P30F1C0B, "logo"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C0C, "?")).setText(keys.getP30F010B());
    node.addElement(Lang.getLang(LangRes.P30F1C11, "memo"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C12, "")).setText(keys.getP30F0110());
    node.addElement(Lang.getLang(LangRes.P30F1C11, "content"))
            .addAttribute("name", Lang.getLang(LangRes.P30F1C12, ""))
            .setText(keys.getMpwd().getP30F0203().toString());
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.bot.RPSLSPlayerBotProcessor.java

License:Apache License

/**
 * Build the acceptance message./*from   ww w.j  a va  2s .c  om*/
 * @param sourceFrom
 * @param sourceTo
 * @param gameInfo
 * @return
 */
protected Message buildAcceptanceMessage(JID sourceFrom, JID sourceTo, RPSLSGameInfo gameInfo) {
    JID fromJID = sourceTo;
    JID toJID = sourceFrom;

    String appId = JIDUtil.getAppId(toJID);
    Message message = new Message();
    message.setTo(toJID);
    message.setFrom(fromJID);

    MessageIdGenerator generator = new MessageIdGeneratorImpl();
    String id = generator.generate(toJID.getNode(), appId, null);
    message.setID(id);
    message.setType(Message.Type.chat);

    Element mmx = message.addChildElement(Constants.MMX, Constants.MMX_NS_MSG_PAYLOAD);

    Element internalMeta = mmx.addElement(Constants.MMX_MMXMETA);
    String userId = JIDUtil.getUserId(toJID);
    String devId = fromJID.getResource();
    //mmx meta
    String mmxMetaJSON = MMXMetaBuilder.build(userId, devId);
    internalMeta.setText(mmxMetaJSON);

    Element meta = mmx.addElement(Constants.MMX_META);
    AcceptanceRPSLSGameInfo acceptance = new AcceptanceRPSLSGameInfo();
    acceptance.setGameId(gameInfo.gameId);
    acceptance.setLosses(0);
    acceptance.setWins(0);
    acceptance.setTies(0);
    Date current = new Date();
    acceptance.setTimestamp(current.getTime());
    acceptance.setType(RPSLSMessageType.ACCEPTANCE);
    String myUserId = JIDUtil.getUserId(sourceTo);
    acceptance.setUsername(myUserId); // user id of the bot user.

    String acceptanceJSON = acceptance.toJson();
    meta.setText(acceptanceJSON);

    Element payloadElement = mmx.addElement(Constants.MMX_PAYLOAD);
    DateFormat fmt = Utils.buildISO8601DateFormat();
    String formattedDateTime = fmt.format(new Date());
    payloadElement.addAttribute(Constants.MMX_ATTR_STAMP, formattedDateTime);
    String text = INVITE_REPLY_MESSAGE;
    payloadElement.setText(text);
    payloadElement.addAttribute(Constants.MMX_ATTR_CHUNK, MessageBuilder.buildChunkAttributeValue(text));
    return message;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.bot.RPSLSPlayerBotProcessor.java

License:Apache License

/**
 * Build a message by randomly choosing from the possible choices.
 * @param sourceFrom/*ww  w. java  2  s  .c  o  m*/
 * @param sourceTo
 * @param gameInfo
 * @return
 */
protected Message buildChoiceMessage(JID sourceFrom, JID sourceTo, RPSLSGameInfo gameInfo) {
    JID fromJID = sourceTo;
    JID toJID = sourceFrom;

    String appId = JIDUtil.getAppId(toJID);

    Message choiceMessage = new Message();

    MessageIdGenerator generator = new MessageIdGeneratorImpl();
    String id = generator.generate(toJID.getNode(), appId, null);
    choiceMessage.setID(id);
    choiceMessage.setType(Message.Type.chat);
    choiceMessage.setTo(toJID);
    choiceMessage.setFrom(fromJID);

    Element mmx = choiceMessage.addChildElement(Constants.MMX, Constants.MMX_NS_MSG_PAYLOAD);
    Element internalMeta = mmx.addElement(Constants.MMX_MMXMETA);
    String userId = JIDUtil.getUserId(toJID);
    String devId = fromJID.getResource();
    //mmx meta
    String mmxMetaJSON = MMXMetaBuilder.build(userId, devId);
    internalMeta.setText(mmxMetaJSON);

    Element meta = mmx.addElement(Constants.MMX_META);
    ChoiceRPSLSGameInfo choice = new ChoiceRPSLSGameInfo();
    choice.setGameId(gameInfo.getGameId());
    choice.setLosses(0);
    choice.setWins(0);
    choice.setTies(0);
    Date current = new Date();
    choice.setTimestamp(current.getTime());
    choice.setType(RPSLSMessageType.CHOICE);
    String myUserId = JIDUtil.getUserId(sourceTo);
    choice.setUsername(myUserId); // user id of the bot user.
    String choiceValue = getRandomChoice();
    choice.setChoice(choiceValue);

    String choiceJSON = choice.toJson();
    meta.setText(choiceJSON);

    Element payloadElement = mmx.addElement(Constants.MMX_PAYLOAD);
    DateFormat fmt = Utils.buildISO8601DateFormat();
    String formattedDateTime = fmt.format(new Date());
    payloadElement.addAttribute(Constants.MMX_ATTR_STAMP, formattedDateTime);
    String text = String.format(CHOICE_TEMPLATE, choiceValue);
    payloadElement.setText(text);
    payloadElement.addAttribute(Constants.MMX_ATTR_CHUNK, MessageBuilder.buildChunkAttributeValue(text));

    return choiceMessage;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.handler.MMXChannelManager.java

License:Apache License

private boolean sendLastPublishedItem(PublishedItem publishedItem, NodeSubscription subNode, JID to) {
    if (!subNode.canSendPublicationEvent(publishedItem.getNode(), publishedItem)) {
        return false;
    }/*from w ww  . j a  v  a  2 s  . c o m*/
    Node node = subNode.getNode();
    Message notification = new Message();
    Element event = notification.getElement().addElement("event", "http://jabber.org/protocol/pubsub#event");
    Element items = event.addElement("items");
    items.addAttribute("node", node.getNodeID());
    Element item = items.addElement("item");
    if (publishedItem.getNode().isItemRequired()) {
        item.addAttribute("id", publishedItem.getID());
    }
    if (node.isPayloadDelivered() && publishedItem.getPayload() != null) {
        item.add(publishedItem.getPayload().createCopy());
    }
    // Add a message body (if required)
    if (subNode.isIncludingBody()) {
        notification.setBody(LocaleUtils.getLocalizedString("pubsub.notification.message.body"));
    }
    // Include date when published item was created
    notification.getElement().addElement("delay", "urn:xmpp:delay").addAttribute("stamp",
            XMPPDateTimeFormat.format(publishedItem.getCreationDate()));
    // Send the event notification to the subscriber
    node.getService().sendNotification(node, notification, to);
    //    node.getService().sendNotification(node, notification, subNode.getJID());
    return true;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.handler.MMXChannelManager.java

License:Apache License

private boolean sendLastPublishedItems(List<PublishedItem> publishedItems, NodeSubscription subNode, JID to) {
    PublishedItem pubItem = publishedItems.get(0);
    if (!subNode.canSendPublicationEvent(pubItem.getNode(), pubItem)) {
        return false;
    }/* w  ww .  ja  v a  2  s  .c o  m*/
    Node node = subNode.getNode();
    Message notification = new Message();
    Element event = notification.getElement().addElement("event", "http://jabber.org/protocol/pubsub#event");
    Element items = event.addElement("items");
    items.addAttribute("node", node.getNodeID());
    for (PublishedItem publishedItem : publishedItems) {
        Element item = items.addElement("item");
        if (publishedItem.getNode().isItemRequired()) {
            item.addAttribute("id", publishedItem.getID());
        }
        if (node.isPayloadDelivered() && publishedItem.getPayload() != null) {
            item.add(publishedItem.getPayload().createCopy());
        }
    }
    // Add a message body (if required)
    if (subNode.isIncludingBody()) {
        notification.setBody(LocaleUtils.getLocalizedString("pubsub.notification.message.body"));
    }
    // Include date when published item was created
    notification.getElement().addElement("delay", "urn:xmpp:delay").addAttribute("stamp",
            XMPPDateTimeFormat.format(pubItem.getCreationDate()));
    // Send the event notification to the subscriber
    node.getService().sendNotification(node, notification, to);
    // node.getService().sendNotification(node, notification, subNode.getJID());
    return true;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.interceptor.MessageDistributedAnnotator.java

License:Apache License

@Override
public void annotate(Message message) {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Annotating a message with id:{}", message.getID());
    }/*  ww w  .j  a va2 s  . c o m*/
    Element mmx = message.getChildElement(Constants.MMX, Constants.MMX_NS_MSG_PAYLOAD);
    Element internalMeta = mmx.element(Constants.MMX_MMXMETA);
    String currentInternalMetaJSON = internalMeta != null ? internalMeta.getText() : null;

    JsonObject jsonObject = null;

    if (currentInternalMetaJSON != null && !currentInternalMetaJSON.isEmpty()) {
        JsonParser parser = new JsonParser();
        try {
            jsonObject = parser.parse(currentInternalMetaJSON).getAsJsonObject();
        } catch (JsonSyntaxException e) {
            LOGGER.warn("Failed to parse mmxmeta string:{} as JSON string", currentInternalMetaJSON, e);
            //assume that the mmxmeta was some invalid JSON.
            jsonObject = new JsonObject();
        }
    } else {
        jsonObject = new JsonObject();
    }
    jsonObject.addProperty(MMXServerConstants.DISTRIBUTED_KEY, true);
    ;
    String updatedInternalMetaJSON = jsonObject.toString();
    if (internalMeta == null) {
        internalMeta = mmx.addElement(Constants.MMX_MMXMETA);
    }
    internalMeta.setText(updatedInternalMetaJSON);
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.message.MessageBuilder.java

License:Apache License

public Message build() {
    Message message = new Message();
    String id = idGenerator.generate(userId, appEntity.getAppId(),
            deviceEntity != null ? deviceEntity.getDeviceId() : null);
    message.setID(id);/*from w w w. j  a v  a 2s .  com*/
    Element mmxElement = message.addChildElement(Constants.MMX, Constants.MMX_NS_MSG_PAYLOAD);

    if (replyTo != null) {
        if (metadata == null) {
            metadata = new HashMap<String, String>();
        }
        metadata.put(MMXServerConstants.REPLY_TO, formatReplyTo(replyTo));
    }

    if (metadata == null) {
        metadata = new HashMap<String, String>();
    }
    //as per the latest changes we need to add the content to the metadata using key "textContent"
    metadata.put(MMXServerConstants.TEXT_CONTENT_KEY, messageContent);

    Map<String, String> meta = metadata;
    String metaJSON = GsonData.getGson().toJson(meta);
    Element metaElement = mmxElement.addElement(Constants.MMX_META);
    metaElement.setText(metaJSON);

    Element payloadElement = mmxElement.addElement(Constants.MMX_PAYLOAD);

    DateFormat fmt = Utils.buildISO8601DateFormat();
    String formattedDateTime = fmt.format(new Date(utcTime));
    payloadElement.addAttribute(Constants.MMX_ATTR_STAMP, formattedDateTime);
    String text = messageContent;
    payloadElement.setText(text);
    payloadElement.addAttribute(Constants.MMX_ATTR_CHUNK, buildChunkAttributeValue(text));

    message.setType(Message.Type.chat);
    message.setFrom(buildFromJID());
    message.setTo(buildToJID());
    if (receipt) {
        //add the element for requesting read receipt
        message.addChildElement(Constants.XMPP_REQUEST, Constants.XMPP_NS_RECEIPTS);
    }
    //https://magneteng.atlassian.net/browse/MOB-2035
    //add a body
    message.setBody(MMXServerConstants.MESSAGE_BODY_DOT);
    return message;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.message.ServerAckMessageBuilder.java

License:Apache License

/**
 * Build the ServerAckMessage//w  w  w .  j a va  2 s  .  c o  m
 * @return Message
 */
public Message build() {

    JID sender = originalMessage.getFrom();
    String senderUserId = JIDUtil.getUserId(sender);
    String senderDeviceId = sender.getResource();

    JID receiver = originalMessage.getTo();
    String receiverUserId = JIDUtil.getUserId(receiver);
    String receiverDeviceId = receiver.getResource();

    Message ackMessage = new Message();
    ackMessage.setType(Message.Type.chat);
    ackMessage.setFrom(appId + "%" + appId + "@" + XMPPServer.getInstance().getServerInfo().getXMPPDomain());
    ackMessage.setTo(sender);
    ackMessage.setID(new MessageIdGeneratorImpl().generate(sender.toString(), appId, senderDeviceId));
    Element mmxElement = ackMessage.addChildElement(Constants.MMX, Constants.MMX_NS_MSG_SIGNAL);
    Element mmxMetaElement = mmxElement.addElement(Constants.MMX_MMXMETA);
    Map<String, ServerAckMmxMeta> mmxMetaMap = new HashMap<String, ServerAckMmxMeta>();
    ServerAckMmxMeta meta = new ServerAckMmxMeta();
    meta.setAckForMsgId(originalMessage.getID());
    meta.setReceiver(receiverUserId, receiverDeviceId);
    meta.setSender(senderUserId, senderDeviceId);
    mmxMetaMap.put(MMXServerConstants.SERVER_ACK_KEY, meta);

    String mmxMetaJSON = GsonData.getGson().toJson(mmxMetaMap);
    mmxMetaElement.setText(mmxMetaJSON);

    Element payloadElement = mmxElement.addElement(Constants.MMX_PAYLOAD);

    DateFormat fmt = Utils.buildISO8601DateFormat();
    String formattedDateTime = fmt.format(new Date());
    payloadElement.addAttribute(Constants.MMX_ATTR_STAMP, formattedDateTime);
    String text = ".";
    payloadElement.setText(text);
    payloadElement.addAttribute(Constants.MMX_ATTR_CHUNK, MessageBuilder.buildChunkAttributeValue(text));
    ackMessage.setBody(MMXServerConstants.MESSAGE_BODY_DOT);
    return ackMessage;
}