001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.iqregister.provider;
018
019import java.util.HashMap;
020import java.util.LinkedList;
021import java.util.List;
022import java.util.Map;
023
024import org.jivesoftware.smack.packet.IQ;
025import org.jivesoftware.smack.packet.ExtensionElement;
026import org.jivesoftware.smack.provider.IQProvider;
027import org.jivesoftware.smack.util.PacketParserUtils;
028import org.jivesoftware.smackx.iqregister.packet.Registration;
029import org.xmlpull.v1.XmlPullParser;
030
031public class RegistrationProvider extends IQProvider<Registration> {
032
033    @Override
034    public Registration parse(XmlPullParser parser, int initialDepth)
035                    throws Exception {
036        String instruction = null;
037        Map<String, String> fields = new HashMap<String, String>();
038        List<ExtensionElement> packetExtensions = new LinkedList<ExtensionElement>();
039        outerloop:
040        while (true) {
041            int eventType = parser.next();
042            if (eventType == XmlPullParser.START_TAG) {
043                // Any element that's in the jabber:iq:register namespace,
044                // attempt to parse it if it's in the form <name>value</name>.
045                if (parser.getNamespace().equals(Registration.NAMESPACE)) {
046                    String name = parser.getName();
047                    String value = "";
048
049                    if (parser.next() == XmlPullParser.TEXT) {
050                        value = parser.getText();
051                    }
052                    // Ignore instructions, but anything else should be added to the map.
053                    if (!name.equals("instructions")) {
054                        fields.put(name, value);
055                    }
056                    else {
057                        instruction = value;
058                    }
059                }
060                // Otherwise, it must be a packet extension.
061                else {
062                    PacketParserUtils.addExtensionElement(packetExtensions, parser);
063                }
064            }
065            else if (eventType == XmlPullParser.END_TAG) {
066                if (parser.getName().equals(IQ.QUERY_ELEMENT)) {
067                    break outerloop;
068                }
069            }
070        }
071        Registration registration = new Registration(instruction, fields);
072        registration.addExtensions(packetExtensions);
073        return registration;
074    }
075
076}