Search in sources :

Example 1 with Builder

use of org.jivesoftware.smack.packet.StandardExtensionElement.Builder in project Smack by igniterealtime.

the class StandardExtensionElementParserTest method buildAndParse.

@Test
public void buildAndParse() throws Exception {
    Builder builder = StandardExtensionElement.builder("foo", "ns1");
    builder.addAttribute("attr1", "attr1-value");
    builder.addElement(StandardExtensionElement.builder("bar", "ns2").addAttribute("attr2", "attr2-value").build());
    builder.addElement("another-element", "another-element-text");
    final String elementString = builder.build().toXML().toString();
    StandardExtensionElement parsedElement = StandardExtensionElementProvider.INSTANCE.parse(getParserFor(elementString));
    assertEquals("foo", parsedElement.getElementName());
    assertEquals("ns1", parsedElement.getNamespace());
    assertEquals("attr1-value", parsedElement.getAttributeValue("attr1"));
    StandardExtensionElement barNs2Element = parsedElement.getFirstElement("bar", "ns2");
    assertEquals("bar", barNs2Element.getElementName());
    assertEquals("ns2", barNs2Element.getNamespace());
    assertEquals("attr2-value", barNs2Element.getAttributeValue("attr2"));
    assertEquals("another-element-text", parsedElement.getFirstElement("another-element").getText());
}
Also used : Builder(org.jivesoftware.smack.packet.StandardExtensionElement.Builder) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Test(org.junit.Test)

Example 2 with Builder

use of org.jivesoftware.smack.packet.StandardExtensionElement.Builder in project Smack by igniterealtime.

the class StandardExtensionElementParserTest method buildWithAttrNamespacesAndParse.

@Test
public void buildWithAttrNamespacesAndParse() throws Exception {
    Builder builder = StandardExtensionElement.builder("foo", "ns1-value");
    builder.addAttribute("xmlns:ns2", "ns2-value");
    builder.addAttribute("ns2:bar", "bar-ns2-value");
    final String elementString = builder.build().toXML().toString();
    StandardExtensionElement parsedElement = StandardExtensionElementProvider.INSTANCE.parse(getParserFor(elementString));
    assertEquals("foo", parsedElement.getElementName());
    assertEquals("ns1-value", parsedElement.getNamespace());
    String barNs2Value = parsedElement.getAttributeValue("ns2:bar");
    assertEquals("bar-ns2-value", barNs2Value);
    String ns2Value = parsedElement.getAttributeValue("xmlns:ns2");
    assertEquals("ns2-value", ns2Value);
}
Also used : Builder(org.jivesoftware.smack.packet.StandardExtensionElement.Builder) StandardExtensionElement(org.jivesoftware.smack.packet.StandardExtensionElement) Test(org.junit.Test)

Example 3 with Builder

use of org.jivesoftware.smack.packet.StandardExtensionElement.Builder in project Smack by igniterealtime.

the class StandardExtensionElementProvider method parse.

@Override
public StandardExtensionElement parse(final XmlPullParser parser, final int initialDepth) throws XmlPullParserException, IOException {
    // Unlike most (all?) other providers, we don't know the name and namespace of the element
    // we are parsing here.
    String name = parser.getName();
    String namespace = parser.getNamespace();
    Builder builder = StandardExtensionElement.builder(name, namespace);
    final int namespaceCount = parser.getNamespaceCount(initialDepth);
    final int attributeCount = parser.getAttributeCount();
    final Map<String, String> attributes = new LinkedHashMap<>(namespaceCount + attributeCount);
    for (int i = 0; i < namespaceCount; i++) {
        String nsprefix = parser.getNamespacePrefix(i);
        if (nsprefix == null) {
            // Skip the default namespace.
            continue;
        }
        // XmlPullParser must either return null or a non-empty String.
        assert StringUtils.isNotEmpty(nsprefix);
        String nsuri = parser.getNamespaceUri(i);
        attributes.put("xmlns:" + nsprefix, nsuri);
    }
    for (int i = 0; i < attributeCount; i++) {
        String attributePrefix = parser.getAttributePrefix(i);
        String attributeName = parser.getAttributeName(i);
        String attributeValue = parser.getAttributeValue(i);
        String attributeKey;
        if (StringUtils.isNullOrEmpty(attributePrefix)) {
            attributeKey = attributeName;
        } else {
            attributeKey = attributePrefix + ':' + attributeName;
        }
        attributes.put(attributeKey, attributeValue);
    }
    builder.addAttributes(attributes);
    outerloop: while (true) {
        int event = parser.next();
        switch(event) {
            case XmlPullParser.START_TAG:
                builder.addElement(parse(parser, parser.getDepth()));
                break;
            case XmlPullParser.TEXT:
                builder.setText(parser.getText());
                break;
            case XmlPullParser.END_TAG:
                if (initialDepth == parser.getDepth()) {
                    break outerloop;
                }
                break;
        }
    }
    ParserUtils.assertAtEndTag(parser);
    return builder.build();
}
Also used : Builder(org.jivesoftware.smack.packet.StandardExtensionElement.Builder) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

Builder (org.jivesoftware.smack.packet.StandardExtensionElement.Builder)3 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)2 Test (org.junit.Test)2 LinkedHashMap (java.util.LinkedHashMap)1