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;
029
030import org.jxmpp.jid.Jid;
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, XmlEnvironment xmlEnvironment) throws XmlPullParserException,
042                    IOException {
043        MultipleAddresses multipleAddresses = new MultipleAddresses();
044        outerloop: while (true) {
045            XmlPullParser.Event eventType = parser.next();
046            switch (eventType) {
047            case START_ELEMENT:
048                String name = parser.getName();
049                switch (name) {
050                case MultipleAddresses.Address.ELEMENT:
051                    String typeString = parser.getAttributeValue("", "type");
052                    MultipleAddresses.Type type = MultipleAddresses.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 END_ELEMENT:
064                if (parser.getDepth() == initialDepth) {
065                    break outerloop;
066                }
067                break;
068            default:
069                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
070                break;
071            }
072        }
073        return multipleAddresses;
074    }
075}