Search in sources :

Example 6 with MXParser

use of org.xmlpull.mxp1.MXParser in project Spark by igniterealtime.

the class PluginClassLoader method loadSmackProvider.

private void loadSmackProvider(InputStream providerStream) throws Exception {
    try {
        XmlPullParser parser = new MXParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        parser.setInput(providerStream, "UTF-8");
        int eventType = parser.getEventType();
        do {
            if (eventType == XmlPullParser.START_TAG) {
                if (parser.getName().equals("iqProvider")) {
                    parser.next();
                    parser.next();
                    String elementName = parser.nextText();
                    parser.next();
                    parser.next();
                    String namespace = parser.nextText();
                    parser.next();
                    parser.next();
                    String className = parser.nextText();
                    // reflection later to create instances of the class.
                    try {
                        // Add the provider to the map.
                        Class<?> provider = this.loadClass(className);
                        if (IQProvider.class.isAssignableFrom(provider)) {
                            ProviderManager.addIQProvider(elementName, namespace, provider.newInstance());
                        } else if (IQ.class.isAssignableFrom(provider)) {
                            ProviderManager.addIQProvider(elementName, namespace, provider.newInstance());
                        }
                    } catch (ClassNotFoundException cnfe) {
                        cnfe.printStackTrace();
                    }
                } else if (parser.getName().equals("extensionProvider")) {
                    parser.next();
                    parser.next();
                    String elementName = parser.nextText();
                    parser.next();
                    parser.next();
                    String namespace = parser.nextText();
                    parser.next();
                    parser.next();
                    String className = parser.nextText();
                    // of the class.
                    try {
                        // Add the provider to the map.
                        Class<?> provider = this.loadClass(className);
                        if (ExtensionElementProvider.class.isAssignableFrom(provider)) {
                            ProviderManager.addExtensionProvider(elementName, namespace, provider.newInstance());
                        } else if (ExtensionElement.class.isAssignableFrom(provider)) {
                            ProviderManager.addExtensionProvider(elementName, namespace, provider.newInstance());
                        }
                    } catch (ClassNotFoundException cnfe) {
                        cnfe.printStackTrace();
                    }
                }
            }
            eventType = parser.next();
        } while (eventType != XmlPullParser.END_DOCUMENT);
    } finally {
        try {
            providerStream.close();
        } catch (Exception e) {
        // Nothing to do
        }
    }
}
Also used : ExtensionElementProvider(org.jivesoftware.smack.provider.ExtensionElementProvider) MXParser(org.xmlpull.mxp1.MXParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) IQ(org.jivesoftware.smack.packet.IQ) MalformedURLException(java.net.MalformedURLException)

Example 7 with MXParser

use of org.xmlpull.mxp1.MXParser in project ecf by eclipse.

the class PacketReader method resetParser.

/**
 * Resets the parser using the latest connection's reader. Reseting the parser is necessary
 * when the plain connection has been secured or when a new opening stream element is going
 * to be sent by the server.
 */
private void resetParser() {
    try {
        parser = new MXParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        parser.setInput(connection.reader);
    } catch (XmlPullParserException xppe) {
        xppe.printStackTrace();
    }
}
Also used : MXParser(org.xmlpull.mxp1.MXParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 8 with MXParser

use of org.xmlpull.mxp1.MXParser in project ecf by eclipse.

the class ProviderManager method initialize.

protected void initialize() {
    // Load IQ processing providers.
    try {
        // Get an array of class loaders to try loading the providers files from.
        ClassLoader[] classLoaders = getClassLoaders();
        for (ClassLoader classLoader : classLoaders) {
            Enumeration<URL> providerEnum = classLoader.getResources("META-INF/smack.providers");
            while (providerEnum.hasMoreElements()) {
                URL url = providerEnum.nextElement();
                InputStream providerStream = null;
                try {
                    providerStream = url.openStream();
                    XmlPullParser parser = new MXParser();
                    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
                    parser.setInput(providerStream, "UTF-8");
                    int eventType = parser.getEventType();
                    do {
                        if (eventType == XmlPullParser.START_TAG) {
                            if (parser.getName().equals("iqProvider")) {
                                parser.next();
                                parser.next();
                                String elementName = parser.nextText();
                                parser.next();
                                parser.next();
                                String namespace = parser.nextText();
                                parser.next();
                                parser.next();
                                String className = parser.nextText();
                                // Only add the provider for the namespace if one isn't
                                // already registered.
                                String key = getProviderKey(elementName, namespace);
                                if (!iqProviders.containsKey(key)) {
                                    // reflection later to create instances of the class.
                                    try {
                                        // Add the provider to the map.
                                        Class<?> provider = Class.forName(className);
                                        if (IQProvider.class.isAssignableFrom(provider)) {
                                            iqProviders.put(key, provider.newInstance());
                                        } else if (IQ.class.isAssignableFrom(provider)) {
                                            iqProviders.put(key, provider);
                                        }
                                    } catch (ClassNotFoundException cnfe) {
                                        cnfe.printStackTrace();
                                    }
                                }
                            } else if (parser.getName().equals("extensionProvider")) {
                                parser.next();
                                parser.next();
                                String elementName = parser.nextText();
                                parser.next();
                                parser.next();
                                String namespace = parser.nextText();
                                parser.next();
                                parser.next();
                                String className = parser.nextText();
                                // Only add the provider for the namespace if one isn't
                                // already registered.
                                String key = getProviderKey(elementName, namespace);
                                if (!extensionProviders.containsKey(key)) {
                                    // of the class.
                                    try {
                                        // Add the provider to the map.
                                        Class<?> provider = Class.forName(className);
                                        if (PacketExtensionProvider.class.isAssignableFrom(provider)) {
                                            extensionProviders.put(key, provider.newInstance());
                                        } else if (PacketExtension.class.isAssignableFrom(provider)) {
                                            extensionProviders.put(key, provider);
                                        }
                                    } catch (ClassNotFoundException cnfe) {
                                        cnfe.printStackTrace();
                                    }
                                }
                            }
                        }
                        eventType = parser.next();
                    } while (eventType != XmlPullParser.END_DOCUMENT);
                } finally {
                    try {
                        providerStream.close();
                    } catch (Exception e) {
                    // Ignore.
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MXParser(org.xmlpull.mxp1.MXParser) InputStream(java.io.InputStream) XmlPullParser(org.xmlpull.v1.XmlPullParser) IQ(org.jivesoftware.smack.packet.IQ) URL(java.net.URL)

Aggregations

MXParser (org.xmlpull.mxp1.MXParser)8 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 XmlPullParser (org.xmlpull.v1.XmlPullParser)3 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 IQ (org.jivesoftware.smack.packet.IQ)2 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)2 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 ExtensionElementProvider (org.jivesoftware.smack.provider.ExtensionElementProvider)1 IQProvider (org.jivesoftware.smack.provider.IQProvider)1