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.io.IOException;
020import java.util.HashMap;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.Map;
024
025import org.jivesoftware.smack.SmackException;
026import org.jivesoftware.smack.packet.IQ;
027import org.jivesoftware.smack.packet.ExtensionElement;
028import org.jivesoftware.smack.provider.IQProvider;
029import org.jivesoftware.smack.util.PacketParserUtils;
030import org.jivesoftware.smackx.iqregister.packet.Registration;
031import org.xmlpull.v1.XmlPullParser;
032import org.xmlpull.v1.XmlPullParserException;
033
034public class RegistrationProvider extends IQProvider<Registration> {
035
036    @Override
037    public Registration parse(XmlPullParser parser, int initialDepth)
038                    throws XmlPullParserException, IOException, SmackException {
039        String instruction = null;
040        Map<String, String> fields = new HashMap<String, String>();
041        List<ExtensionElement> packetExtensions = new LinkedList<ExtensionElement>();
042        outerloop:
043        while (true) {
044            int eventType = parser.next();
045            if (eventType == XmlPullParser.START_TAG) {
046                // Any element that's in the jabber:iq:register namespace,
047                // attempt to parse it if it's in the form <name>value</name>.
048                if (parser.getNamespace().equals(Registration.NAMESPACE)) {
049                    String name = parser.getName();
050                    String value = "";
051
052                    if (parser.next() == XmlPullParser.TEXT) {
053                        value = parser.getText();
054                    }
055                    // Ignore instructions, but anything else should be added to the map.
056                    if (!name.equals("instructions")) {
057                        fields.put(name, value);
058                    }
059                    else {
060                        instruction = value;
061                    }
062                }
063                // Otherwise, it must be a packet extension.
064                else {
065                    PacketParserUtils.addExtensionElement(packetExtensions, parser);
066                }
067            }
068            else if (eventType == XmlPullParser.END_TAG) {
069                if (parser.getName().equals(IQ.QUERY_ELEMENT)) {
070                    break outerloop;
071                }
072            }
073        }
074        Registration registration = new Registration(instruction, fields);
075        registration.addExtensions(packetExtensions);
076        return registration;
077        }
078
079}