MultipleAddressesProvider.java

  1. /**
  2.  *
  3.  * Copyright 2003-2006 Jive Software.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.jivesoftware.smackx.address.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  20. import org.jivesoftware.smack.util.ParserUtils;
  21. import org.jivesoftware.smackx.address.packet.MultipleAddresses;
  22. import org.jivesoftware.smackx.address.packet.MultipleAddresses.Type;
  23. import org.jxmpp.jid.Jid;
  24. import org.xmlpull.v1.XmlPullParser;
  25. import org.xmlpull.v1.XmlPullParserException;

  26. /**
  27.  * The MultipleAddressesProvider parses {@link MultipleAddresses} packets.
  28.  *
  29.  * @author Gaston Dombiak
  30.  */
  31. public class MultipleAddressesProvider extends ExtensionElementProvider<MultipleAddresses> {

  32.     @Override
  33.     public MultipleAddresses parse(XmlPullParser parser,
  34.                     int initialDepth) throws XmlPullParserException,
  35.                     IOException {
  36.         MultipleAddresses multipleAddresses = new MultipleAddresses();
  37.         outerloop: while (true) {
  38.             int eventType = parser.next();
  39.             switch (eventType) {
  40.             case XmlPullParser.START_TAG:
  41.                 String name = parser.getName();
  42.                 switch (name) {
  43.                 case MultipleAddresses.Address.ELEMENT:
  44.                     String typeString = parser.getAttributeValue("", "type");
  45.                     Type type = Type.valueOf(typeString);
  46.                     Jid jid = ParserUtils.getJidAttribute(parser, "jid");
  47.                     String node = parser.getAttributeValue("", "node");
  48.                     String desc = parser.getAttributeValue("", "desc");
  49.                     boolean delivered = "true".equals(parser.getAttributeValue("", "delivered"));
  50.                     String uri = parser.getAttributeValue("", "uri");
  51.                     // Add the parsed address
  52.                     multipleAddresses.addAddress(type, jid, node, desc, delivered, uri);
  53.                     break;
  54.                 }
  55.                 break;
  56.             case XmlPullParser.END_TAG:
  57.                 if (parser.getDepth() == initialDepth) {
  58.                     break outerloop;
  59.                 }
  60.                 break;
  61.             }
  62.         }
  63.         return multipleAddresses;
  64.     }
  65. }