001/**
002 *
003 * Copyright 2003-2006 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 */
017
018package org.jivesoftware.smackx.address.provider;
019
020import java.io.IOException;
021
022import org.jivesoftware.smack.provider.ExtensionElementProvider;
023import org.jivesoftware.smack.util.ParserUtils;
024
025import org.jivesoftware.smackx.address.packet.MultipleAddresses;
026import org.jivesoftware.smackx.address.packet.MultipleAddresses.Type;
027
028import org.jxmpp.jid.Jid;
029import org.xmlpull.v1.XmlPullParser;
030import org.xmlpull.v1.XmlPullParserException;
031
032/**
033 * The MultipleAddressesProvider parses {@link MultipleAddresses} packets.
034 *
035 * @author Gaston Dombiak
036 */
037public class MultipleAddressesProvider extends ExtensionElementProvider<MultipleAddresses> {
038
039    @Override
040    public MultipleAddresses parse(XmlPullParser parser,
041                    int initialDepth) throws XmlPullParserException,
042                    IOException {
043        MultipleAddresses multipleAddresses = new MultipleAddresses();
044        outerloop: while (true) {
045            int eventType = parser.next();
046            switch (eventType) {
047            case XmlPullParser.START_TAG:
048                String name = parser.getName();
049                switch (name) {
050                case MultipleAddresses.Address.ELEMENT:
051                    String typeString = parser.getAttributeValue("", "type");
052                    Type type = Type.valueOf(typeString);
053                    Jid jid = ParserUtils.getJidAttribute(parser, "jid");
054                    String node = parser.getAttributeValue("", "node");
055                    String desc = parser.getAttributeValue("", "desc");
056                    boolean delivered = "true".equals(parser.getAttributeValue("", "delivered"));
057                    String uri = parser.getAttributeValue("", "uri");
058                    // Add the parsed address
059                    multipleAddresses.addAddress(type, jid, node, desc, delivered, uri);
060                    break;
061                }
062                break;
063            case XmlPullParser.END_TAG:
064                if (parser.getDepth() == initialDepth) {
065                    break outerloop;
066                }
067                break;
068            }
069        }
070        return multipleAddresses;
071    }
072}