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