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