Search in sources :

Example 1 with PacketExtensionProvider

use of org.jivesoftware.smack.provider.PacketExtensionProvider in project ecf by eclipse.

the class PacketParserUtils method parsePacketExtension.

/**
 * Parses a packet extension sub-packet.
 *
 * @param elementName the XML element name of the packet extension.
 * @param namespace the XML namespace of the packet extension.
 * @param parser the XML parser, positioned at the starting element of the extension.
 * @return a PacketExtension.
 * @throws Exception if a parsing error occurs.
 */
public static PacketExtension parsePacketExtension(String elementName, String namespace, XmlPullParser parser) throws Exception {
    // See if a provider is registered to handle the extension.
    Object provider = ProviderManager.getInstance().getExtensionProvider(elementName, namespace);
    if (provider != null) {
        if (provider instanceof PacketExtensionProvider) {
            return ((PacketExtensionProvider) provider).parseExtension(parser);
        } else if (provider instanceof Class) {
            return (PacketExtension) parseWithIntrospection(elementName, (Class<?>) provider, parser);
        }
    }
    // No providers registered, so use a default extension.
    DefaultPacketExtension extension = new DefaultPacketExtension(elementName, namespace);
    boolean done = false;
    while (!done) {
        int eventType = parser.next();
        if (eventType == XmlPullParser.START_TAG) {
            String name = parser.getName();
            // If an empty element, set the value with the empty string.
            if (parser.isEmptyElementTag()) {
                extension.setValue(name, "");
            } else // Otherwise, get the the element text.
            {
                eventType = parser.next();
                if (eventType == XmlPullParser.TEXT) {
                    String value = parser.getText();
                    extension.setValue(name, value);
                }
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (parser.getName().equals(elementName)) {
                done = true;
            }
        }
    }
    return extension;
}
Also used : DefaultPacketExtension(org.jivesoftware.smack.packet.DefaultPacketExtension) PacketExtensionProvider(org.jivesoftware.smack.provider.PacketExtensionProvider)

Aggregations

DefaultPacketExtension (org.jivesoftware.smack.packet.DefaultPacketExtension)1 PacketExtensionProvider (org.jivesoftware.smack.provider.PacketExtensionProvider)1