Search in sources :

Example 56 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class DataLayoutTest method testLayoutFromFile.

@Test
public void testLayoutFromFile() throws Exception {
    DataFormProvider pr = new DataFormProvider();
    InputStreamReader inputStreamReader = new InputStreamReader(this.getClass().getResourceAsStream(TEST_INPUT_1), "UTF-8");
    XmlPullParser parser = PacketParserUtils.getParserFor(inputStreamReader);
    DataForm form = pr.parse(parser);
    assertNotNull(form);
    assertEquals(1, form.getExtensionElements().size());
    DataLayout layout = (DataLayout) form.getExtensionElements().get(0);
    assertEquals(5, layout.getPageLayout().size());
    assertEquals("Label - & \u00E9 \u00E1 ", layout.getLabel());
    Section section = (Section) layout.getPageLayout().get(1);
    assertEquals("section Label - & \u00E9 \u00E1 ", section.getLabel());
    Text text = (Text) layout.getPageLayout().get(2);
    assertEquals("PageText - & \u00E9 \u00E1 ", text.getText());
    section = (Section) layout.getPageLayout().get(3);
    assertEquals("<html>Number of Persons by<br/> Nationality and Status</html>", section.getLabel());
    text = (Text) layout.getPageLayout().get(4);
    assertEquals("<html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html>", text.getText());
    assertNotNull(layout.toXML());
    String output = layout.toXML().toString();
    assertEquals(TEST_OUTPUT_SPECIAL, output);
}
Also used : InputStreamReader(java.io.InputStreamReader) DataFormProvider(org.jivesoftware.smackx.xdata.provider.DataFormProvider) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) Text(org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text) Section(org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section) Test(org.junit.jupiter.api.Test)

Example 57 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class DataFormProviderTest method testRetrieveFieldWithEmptyLabel.

@Test
public void testRetrieveFieldWithEmptyLabel() throws XmlPullParserException, IOException, SmackParsingException {
    String form = "<x xmlns='jabber:x:data' type='form'>" + "  <title>Advanced User Search</title>" + "  <instructions>The following fields are available for searching. Wildcard (*) characters are allowed as part of the query.</instructions>" + "  <field var='FORM_TYPE' label='' type='hidden'>" + "    <value>jabber:iq:search</value>" + "  </field>" + "  <field label='Search' var='search'>" + "    <required/>" + "  </field>" + "  <field label='Username' var='Username' type='boolean'>" + "    <value>true</value>" + "  </field>" + "  <field label='Name' var='Name' type='boolean'>" + "    <value>true</value>" + "  </field>" + "  <field label='Email' var='Email' type='boolean'>" + "    <value>true</value>" + "  </field>" + "</x>";
    XmlPullParser parser = PacketParserUtils.getParserFor(form);
    DataForm dataForm = DataFormProvider.INSTANCE.parse(parser);
    FormField usernameFormField = dataForm.getField("FORM_TYPE");
    assertEquals(FormField.Type.hidden, usernameFormField.getType());
    assertEquals("", usernameFormField.getLabel());
}
Also used : XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) DataForm(org.jivesoftware.smackx.xdata.packet.DataForm) FormField(org.jivesoftware.smackx.xdata.FormField) Test(org.junit.jupiter.api.Test)

Example 58 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class XHTMLExtensionProviderTest method parsesWell.

@Test
public void parsesWell() throws IOException, XmlPullParserException {
    InputStream inputStream = getClass().getResourceAsStream(XHTML_EXTENSION_SAMPLE_RESOURCE_NAME);
    XmlPullParser parser = PacketParserUtils.getParserFor(inputStream);
    XHTMLExtensionProvider provider = new XHTMLExtensionProvider();
    ExtensionElement extension = provider.parse(parser, parser.getDepth(), null);
    assertThat(extension, instanceOf(XHTMLExtension.class));
    XHTMLExtension attachmentsInfo = (XHTMLExtension) extension;
    assertThat(sampleXhtml(), equalsCharSequence(attachmentsInfo.getBodies().get(0)));
}
Also used : InputStream(java.io.InputStream) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) XHTMLExtension(org.jivesoftware.smackx.xhtmlim.packet.XHTMLExtension) Test(org.junit.jupiter.api.Test)

Example 59 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class OfferRequestProvider method parse.

// FIXME It seems because OfferRequestPacket is also defined here, we can
// not add it as generic to the provider, the provider and the packet should
// be split, but since this is legacy code, I don't think that this will
// happen anytime soon.
@Override
public OfferRequestPacket parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException {
    XmlPullParser.Event eventType = parser.getEventType();
    String sessionID = null;
    int timeout = -1;
    OfferContent content = null;
    boolean done = false;
    Map<String, List<String>> metaData = new HashMap<>();
    if (eventType != XmlPullParser.Event.START_ELEMENT) {
    // throw exception
    }
    Jid userJID = ParserUtils.getJidAttribute(parser);
    // Default userID to the JID.
    Jid userID = userJID;
    while (!done) {
        eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            String elemName = parser.getName();
            if ("timeout".equals(elemName)) {
                timeout = Integer.parseInt(parser.nextText());
            } else if (MetaData.ELEMENT_NAME.equals(elemName)) {
                metaData = MetaDataUtils.parseMetaData(parser);
            } else if (SessionID.ELEMENT_NAME.equals(elemName)) {
                sessionID = parser.getAttributeValue("", "id");
            } else if (UserID.ELEMENT_NAME.equals(elemName)) {
                userID = ParserUtils.getJidAttribute(parser, "id");
            } else if ("user-request".equals(elemName)) {
                content = UserRequest.getInstance();
            } else if (RoomInvitation.ELEMENT_NAME.equals(elemName)) {
                RoomInvitation invitation = (RoomInvitation) PacketParserUtils.parseExtensionElement(RoomInvitation.ELEMENT_NAME, RoomInvitation.NAMESPACE, parser, xmlEnvironment);
                content = new InvitationRequest(invitation.getInviter(), invitation.getRoom(), invitation.getReason());
            } else if (RoomTransfer.ELEMENT_NAME.equals(elemName)) {
                RoomTransfer transfer = (RoomTransfer) PacketParserUtils.parseExtensionElement(RoomTransfer.ELEMENT_NAME, RoomTransfer.NAMESPACE, parser, xmlEnvironment);
                content = new TransferRequest(transfer.getInviter(), transfer.getRoom(), transfer.getReason());
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if ("offer".equals(parser.getName())) {
                done = true;
            }
        }
    }
    OfferRequestPacket offerRequest = new OfferRequestPacket(userJID, userID, timeout, metaData, sessionID, content);
    offerRequest.setType(IQ.Type.set);
    return offerRequest;
}
Also used : Jid(org.jxmpp.jid.Jid) HashMap(java.util.HashMap) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) InvitationRequest(org.jivesoftware.smackx.workgroup.agent.InvitationRequest) TransferRequest(org.jivesoftware.smackx.workgroup.agent.TransferRequest) OfferContent(org.jivesoftware.smackx.workgroup.agent.OfferContent) List(java.util.List)

Example 60 with XmlPullParser

use of org.jivesoftware.smack.xml.XmlPullParser in project Smack by igniterealtime.

the class TranscriptsProvider method parseAgents.

private static List<Transcripts.AgentDetail> parseAgents(XmlPullParser parser) throws IOException, XmlPullParserException, ParseException {
    List<Transcripts.AgentDetail> agents = new ArrayList<>();
    String agentJID = null;
    Date joinTime = null;
    Date leftTime = null;
    boolean done = false;
    while (!done) {
        XmlPullParser.Event eventType = parser.next();
        if (eventType == XmlPullParser.Event.START_ELEMENT) {
            if (parser.getName().equals("agentJID")) {
                agentJID = parser.nextText();
            } else if (parser.getName().equals("joinTime")) {
                synchronized (UTC_FORMAT) {
                    joinTime = UTC_FORMAT.parse(parser.nextText());
                }
            } else if (parser.getName().equals("leftTime")) {
                synchronized (UTC_FORMAT) {
                    leftTime = UTC_FORMAT.parse(parser.nextText());
                }
            } else if (parser.getName().equals("agent")) {
                agentJID = null;
                joinTime = null;
                leftTime = null;
            }
        } else if (eventType == XmlPullParser.Event.END_ELEMENT) {
            if (parser.getName().equals("agents")) {
                done = true;
            } else if (parser.getName().equals("agent")) {
                agents.add(new Transcripts.AgentDetail(agentJID, joinTime, leftTime));
            }
        }
    }
    return agents;
}
Also used : ArrayList(java.util.ArrayList) XmlPullParser(org.jivesoftware.smack.xml.XmlPullParser) Date(java.util.Date)

Aggregations

XmlPullParser (org.jivesoftware.smack.xml.XmlPullParser)139 Test (org.junit.jupiter.api.Test)69 Message (org.jivesoftware.smack.packet.Message)15 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 Date (java.util.Date)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 XmlPullParserException (org.jivesoftware.smack.xml.XmlPullParserException)10 EnumSource (org.junit.jupiter.params.provider.EnumSource)9 IQ (org.jivesoftware.smack.packet.IQ)8 Test (org.junit.Test)8 MarkupElement (org.jivesoftware.smackx.message_markup.element.MarkupElement)7 MarkupElementProvider (org.jivesoftware.smackx.message_markup.provider.MarkupElementProvider)7 XmlElement (org.jivesoftware.smack.packet.XmlElement)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 NamedElement (org.jivesoftware.smack.packet.NamedElement)4 HttpOverXmppResp (org.jivesoftware.smackx.hoxt.packet.HttpOverXmppResp)4