org.jivesoftware.smack.packet.Message.toXML()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(15.3k)|赞(0)|评价(0)|浏览(198)

本文整理了Java中org.jivesoftware.smack.packet.Message.toXML()方法的一些代码示例,展示了Message.toXML()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Message.toXML()方法的具体详情如下:
包路径:org.jivesoftware.smack.packet.Message
类名称:Message
方法名:toXML

Message.toXML介绍

暂无

代码示例

代码示例来源:origin: igniterealtime/Smack

@Test
public void messageXmlLangTest() throws IOException, SAXException {
  final String lang = "sp";
  StringBuilder controlBuilder = new StringBuilder();
  controlBuilder.append("<message")
      .append(" xml:lang=\"")
      .append(lang)
      .append("\">")
      .append("</message>");
  String control = controlBuilder.toString();
  Message message = getNewMessage();
  message.setLanguage(lang);
  assertXMLEqual(control, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

/**
 * Creates a new {@link Chat} based on the message. May returns null if no chat could be
 * created, e.g. because the message comes without from.
 *
 * @param message
 * @return a Chat or null if none can be created
 */
private Chat createChat(Message message) {
  Jid from = message.getFrom();
  // According to RFC6120 8.1.2.1 4. messages without a 'from' attribute are valid, but they
  // are of no use in this case for ChatManager
  if (from == null) {
    return null;
  }
  EntityJid userJID = from.asEntityJidIfPossible();
  if (userJID == null) {
    LOGGER.warning("Message from JID without localpart: '" + message.toXML(null) + "'");
    return null;
  }
  String threadID = message.getThread();
  if (threadID == null) {
    threadID = nextID();
  }
  return createChat(userJID, threadID, false);
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void oneMessageBodyTest() throws IOException, SAXException {
  final String messageBody = "This is a test of the emergency broadcast system.";
  StringBuilder controlBuilder = new StringBuilder();
  controlBuilder.append("<message>")
      .append("<body>")
      .append(messageBody)
      .append("</body>")
      .append("</message>");
  String control = controlBuilder.toString();
  Message message = getNewMessage();
  message.setBody(messageBody);
  assertEquals(messageBody, message.getBody());
  assertXMLEqual(control, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void setMessageSubjectTest() throws IOException, SAXException {
  final String messageSubject = "This is a test of the emergency broadcast system.";
  StringBuilder controlBuilder = new StringBuilder();
  controlBuilder.append("<message>")
      .append("<subject>")
      .append(messageSubject)
      .append("</subject>")
      .append("</message>");
  String control = controlBuilder.toString();
  Message message = getNewMessage();
  message.setSubject(messageSubject);
  assertEquals(messageSubject, message.getSubject());
  assertXMLEqual(control, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void setMessageThreadTest() throws IOException, SAXException {
  final String messageThread = "1234";
  StringBuilder controlBuilder = new StringBuilder();
  controlBuilder.append("<message>")
      .append("<thread>")
      .append(messageThread)
      .append("</thread>")
      .append("</message>");
  String control = controlBuilder.toString();
  Message message = getNewMessage();
  message.setThread(messageThread);
  assertEquals(messageThread, message.getThread());
  assertXMLEqual(control, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

message.addBody(lang2, messageBody2);
message.addBody(lang3, messageBody3);
XmlUnitUtils.assertSimilar(control, message.toXML(StreamOpen.CLIENT_NAMESPACE));

代码示例来源:origin: igniterealtime/Smack

@Test
public void setMessageTypeTest() throws IOException, SAXException {
  Message.Type type = Message.Type.chat;
  Message.Type type2 = Message.Type.headline;
  StringBuilder controlBuilder = new StringBuilder();
  controlBuilder.append("<message")
      .append(" type=\"")
      .append(type)
      .append("\">")
      .append("</message>");
  String control = controlBuilder.toString();
  Message messageTypeInConstructor = new Message(null, Message.Type.chat);
  messageTypeInConstructor.setStanzaId(null);
  assertEquals(type, messageTypeInConstructor.getType());
  assertXMLEqual(control, messageTypeInConstructor.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
  controlBuilder = new StringBuilder();
  controlBuilder.append("<message")
      .append(" type=\"")
      .append(type2)
      .append("\">")
      .append("</message>");
  control = controlBuilder.toString();
  Message messageTypeSet = getNewMessage();
  messageTypeSet.setType(type2);
  assertEquals(type2, messageTypeSet.getType());
  assertXMLEqual(control, messageTypeSet.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Override
  public void processStanza(Stanza packet) {
    final Message message = (Message) packet;
    // Get the MUCUser extension
    final MUCUser mucUser = MUCUser.from(message);
    // Check if the MUCUser extension includes an invitation
    if (mucUser.getInvite() != null) {
      EntityBareJid mucJid = message.getFrom().asEntityBareJidIfPossible();
      if (mucJid == null) {
        LOGGER.warning("Invite to non bare JID: '" + message.toXML(null) + "'");
        return;
      }
      // Fire event for invitation listeners
      final MultiUserChat muc = getMultiUserChat(mucJid);
      final XMPPConnection connection = connection();
      final MUCUser.Invite invite = mucUser.getInvite();
      final EntityJid from = invite.getFrom();
      final String reason = invite.getReason();
      final String password = mucUser.getPassword();
      for (final InvitationListener listener : invitationsListeners) {
        listener.invitationReceived(connection, muc, from, reason, password, message, invite);
      }
    }
  }
};

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkStanzas() throws Exception {
  Message initialMessage = PacketParserUtils.parseStanza(initialMessageXml);
  MessageCorrectExtension messageCorrectExtension = new MessageCorrectExtension(idInitialMessage);
  Assert.assertEquals(messageCorrectExtension.toXML(null).toString(), messageCorrectionXml.toString());
  initialMessage.addExtension(messageCorrectExtension);
  Assert.assertEquals(initialMessage.toXML(null), expectedXml.toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkReceivedExtension() throws Exception {
  Message message = new Message(JidCreate.from("northumberland@shakespeare.lit/westminster"));
  message.setStanzaId("message-2");
  message.addExtension(new ChatMarkersElements.ReceivedExtension("message-1"));
  Assert.assertEquals(receivedMessageStanza, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkDisplayedExtension() throws Exception {
  Message message = new Message(JidCreate.from("northumberland@shakespeare.lit/westminster"));
  message.setStanzaId("message-2");
  message.addExtension(new ChatMarkersElements.AcknowledgedExtension("message-1"));
  Assert.assertEquals(acknowledgedMessageStanza, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkMarkableExtension() throws Exception {
  Message message = new Message(JidCreate.from("ingrichard@royalty.england.lit/throne"));
  message.setStanzaId("message-1");
  message.setBody("My lord, dispatch; read o'er these articles.");
  message.addExtension(ChatMarkersElements.MarkableExtension.INSTANCE);
  Assert.assertEquals(markableMessageStanza, message.toXML(null).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkDisplayedExtension() throws Exception {
  Message message = new Message(JidCreate.from("northumberland@shakespeare.lit/westminster"));
  message.setStanzaId("message-2");
  message.addExtension(new ChatMarkersElements.DisplayedExtension("message-1"));
  Assert.assertEquals(displayedMessageStanza, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkConfigsChangeExtension() throws Exception {
  Message configurationsMessage = PacketParserUtils.parseStanza(messageWithConfigsChangeExample);
  ConfigurationsChangeExtension configurationsChangeExtension = ConfigurationsChangeExtension
      .from(configurationsMessage);
  Assert.assertEquals("zaqwsx", configurationsChangeExtension.getPrevVersion());
  Assert.assertEquals("zxcvbnm", configurationsChangeExtension.getVersion());
  Assert.assertEquals("A Darker Cave", configurationsChangeExtension.getRoomName());
  Assert.assertNull(configurationsChangeExtension.getSubject());
  Assert.assertEquals("blue", configurationsChangeExtension.getCustomConfigs().get("color"));
  Assert.assertEquals(messageWithConfigsChangeExample, configurationsMessage.toXML(null).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkSubjectChangeExtension() throws Exception {
  Message configurationsMessage = PacketParserUtils.parseStanza(messageWithSubjectChangeExample);
  ConfigurationsChangeExtension configurationsChangeExtension = ConfigurationsChangeExtension
      .from(configurationsMessage);
  Assert.assertEquals("asdfghj000", configurationsChangeExtension.getPrevVersion());
  Assert.assertEquals("asdfghj", configurationsChangeExtension.getVersion());
  Assert.assertEquals("To be or not to be?", configurationsChangeExtension.getSubject());
  Assert.assertNull(configurationsChangeExtension.getRoomName());
  Assert.assertNull(configurationsChangeExtension.getCustomConfigs());
  Assert.assertEquals(messageWithSubjectChangeExample, configurationsMessage.toXML(null).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkRoomNameChangeExtension() throws Exception {
  Message configurationsMessage = PacketParserUtils.parseStanza(messageWithRoomNameChangeExample);
  ConfigurationsChangeExtension configurationsChangeExtension = ConfigurationsChangeExtension
      .from(configurationsMessage);
  Assert.assertEquals("zaqwsx", configurationsChangeExtension.getPrevVersion());
  Assert.assertEquals("zxcvbnm", configurationsChangeExtension.getVersion());
  Assert.assertEquals("A Darker Cave", configurationsChangeExtension.getRoomName());
  Assert.assertNull(configurationsChangeExtension.getSubject());
  Assert.assertNull(configurationsChangeExtension.getCustomConfigs());
  Assert.assertEquals(messageWithRoomNameChangeExample, configurationsMessage.toXML(null).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Ignore
@Test
public void duplicateMessageBodiesTest()
    throws FactoryConfigurationError, XmlPullParserException, IOException, Exception {
  String defaultLanguage = Stanza.getDefaultLanguage();
  // message has default language, first body no language, second body default language
  String control = XMLBuilder.create("message")
    .namespace(StreamOpen.CLIENT_NAMESPACE)
    .a("from", "romeo@montague.lit/orchard")
    .a("to", "juliet@capulet.lit/balcony")
    .a("id", "zid615d9")
    .a("type", "chat")
    .a("xml:lang", defaultLanguage)
    .e("body")
      .t(defaultLanguage)
    .up()
    .e("body")
      .a("xml:lang", defaultLanguage)
      .t(defaultLanguage + "2")
    .asString(outputProperties);
  Message message = PacketParserUtils
          .parseMessage(PacketParserUtils.getParserFor(control));
  assertEquals(defaultLanguage, message.getBody());
  assertEquals(defaultLanguage, message.getBody(defaultLanguage));
  assertEquals(1, message.getBodies().size());
  assertEquals(0, message.getBodyLanguages().size());
  assertXMLNotEqual(control, message.toXML(null).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void messageNoLanguageFirstBodyNoLanguageSecondBodyOtherTest()
    throws FactoryConfigurationError, XmlPullParserException, IOException, Exception {
  String defaultLanguage = Stanza.getDefaultLanguage();
  String otherLanguage = determineNonDefaultLanguage();
  // message has no language, first body no language, second body other language
  String control = XMLBuilder.create("message")
    .namespace(StreamOpen.CLIENT_NAMESPACE)
    .a("from", "romeo@montague.lit/orchard")
    .a("to", "juliet@capulet.lit/balcony")
    .a("id", "zid615d9")
    .a("type", "chat")
    .e("body")
    // TODO change default language into something else
      .t(defaultLanguage)
    .up()
    .e("body")
      .a("xml:lang", otherLanguage)
      .t(otherLanguage)
    .asString(outputProperties);
  Message message = PacketParserUtils
          .parseMessage(PacketParserUtils.getParserFor(control));
  assertEquals(defaultLanguage, message.getBody());
  assertEquals(defaultLanguage, message.getBody(null));
  assertEquals(otherLanguage, message.getBody(otherLanguage));
  assertEquals(2, message.getBodies().size());
  assertEquals(1, message.getBodyLanguages().size());
  assertXMLEqual(control, message.toXML(null).toString());
}

代码示例来源:origin: igniterealtime/Smack

@Test
public void checkMamQueryResults() throws Exception {
  Message message = new Message();
  message.setStanzaId("iasd207");
  message.setFrom(JidCreate.from("coven@chat.shakespeare.lit"));
  message.setTo(JidCreate.from("hag66@shakespeare.lit/pda"));
  GregorianCalendar calendar = new GregorianCalendar(2002, 10 - 1, 13, 23, 58, 37);
  calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
  Date date = calendar.getTime();
  DelayInformation delay = new DelayInformation(date);
  Message forwardedMessage = new Message();
  forwardedMessage.setFrom(JidCreate.from("coven@chat.shakespeare.lit/firstwitch"));
  forwardedMessage.setStanzaId("162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2");
  forwardedMessage.setType(Type.chat);
  forwardedMessage.setBody("Thrice the brinded cat hath mew.");
  Forwarded forwarded = new Forwarded(delay, forwardedMessage);
  message.addExtension(new MamResultExtension("g27", "34482-21985-73620", forwarded));
  // FIXME: The order of assertEquals is reversed, fix it by switching it.
  Assert.assertEquals(message.toXML(StreamOpen.CLIENT_NAMESPACE).toString(), mamQueryResultExample);
  MamResultExtension mamResultExtension = MamResultExtension.from(message);
  Assert.assertEquals(mamResultExtension.getId(), "34482-21985-73620");
  Assert.assertEquals(mamResultExtension.getForwarded().getDelayInformation().getStamp(), date);
  Message resultMessage = (Message) mamResultExtension.getForwarded().getForwardedStanza();
  Assert.assertEquals(resultMessage.getFrom(), JidCreate.from("coven@chat.shakespeare.lit/firstwitch"));
  Assert.assertEquals(resultMessage.getStanzaId(), "162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2");
  Assert.assertEquals(resultMessage.getType(), Type.chat);
  Assert.assertEquals(resultMessage.getBody(), "Thrice the brinded cat hath mew.");
}

代码示例来源:origin: igniterealtime/Smack

/**
 * RFC6121 5.2.3 explicitly disallows mixed content in <body/> elements. Make sure that we throw
 * an exception if we encounter such an element.
 *
 * @throws Exception
 */
@Test(expected = XmlPullParserException.class)
public void invalidMessageBodyContainingTagTest() throws Exception {
  String control = XMLBuilder.create("message")
    .namespace(StreamOpen.CLIENT_NAMESPACE)
    .a("from", "romeo@montague.lit/orchard")
    .a("to", "juliet@capulet.lit/balcony")
    .a("id", "zid615d9")
    .a("type", "chat")
    .a("xml:lang", "en")
    .e("body")
      .a("xmlns", "http://www.w3.org/1999/xhtml")
      .e("span")
        .a("style", "font-weight: bold;")
        .t("Bad Message Body")
    .asString(outputProperties);
  Message message = PacketParserUtils.parseMessage(TestUtils.getMessageParser(control));
  fail("Should throw exception. Instead got message: " + message.toXML(null).toString());
}

相关文章