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.packet.XmlEnvironment;
023import org.jivesoftware.smack.provider.ExtensionElementProvider;
024import org.jivesoftware.smack.util.ParserUtils;
025import org.jivesoftware.smack.xml.XmlPullParser;
026import org.jivesoftware.smack.xml.XmlPullParserException;
027
028import org.jivesoftware.smackx.address.packet.MultipleAddresses;
029import org.jivesoftware.smackx.address.packet.MultipleAddresses.Type;
030
031import org.jxmpp.jid.Jid;
032
033/**
034 * The MultipleAddressesProvider parses {@link MultipleAddresses} packets.
035 *
036 * @author Gaston Dombiak
037 */
038public class MultipleAddressesProvider extends ExtensionElementProvider<MultipleAddresses> {
039
040    @Override
041    public MultipleAddresses parse(XmlPullParser parser,
042                    int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
043                    IOException {
044        MultipleAddresses multipleAddresses = new MultipleAddresses();
045        outerloop: while (true) {
046            XmlPullParser.Event eventType = parser.next();
047            switch (eventType) {
048            case START_ELEMENT:
049                String name = parser.getName();
050                switch (name) {
051                case MultipleAddresses.Address.ELEMENT:
052                    String typeString = parser.getAttributeValue("", "type");
053                    Type type = Type.valueOf(typeString);
054                    Jid jid = ParserUtils.getJidAttribute(parser, "jid");
055                    String node = parser.getAttributeValue("", "node");
056                    String desc = parser.getAttributeValue("", "desc");
057                    boolean delivered = "true".equals(parser.getAttributeValue("", "delivered"));
058                    String uri = parser.getAttributeValue("", "uri");
059                    // Add the parsed address
060                    multipleAddresses.addAddress(type, jid, node, desc, delivered, uri);
061                    break;
062                }
063                break;
064            case END_ELEMENT:
065                if (parser.getDepth() == initialDepth) {
066                    break outerloop;
067                }
068                break;
069            default:
070                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
071                break;
072            }
073        }
074        return multipleAddresses;
075    }
076}