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