AffiliationProvider.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  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.pubsub.provider;

  18. import java.io.IOException;

  19. import org.jivesoftware.smack.packet.XmlEnvironment;
  20. import org.jivesoftware.smack.provider.ExtensionElementProvider;
  21. import org.jivesoftware.smack.util.ParserUtils;
  22. import org.jivesoftware.smack.xml.XmlPullParser;

  23. import org.jivesoftware.smackx.pubsub.Affiliation;
  24. import org.jivesoftware.smackx.pubsub.Affiliation.AffiliationNamespace;

  25. import org.jxmpp.jid.BareJid;

  26. /**
  27.  * Parses the affiliation element out of the reply stanza from the server
  28.  * as specified in the <a href="http://xmpp.org/extensions/xep-0060.html#schemas-pubsub">affiliation schema</a>.
  29.  *
  30.  * @author Robin Collier
  31.  */
  32. public class AffiliationProvider extends ExtensionElementProvider<Affiliation> {

  33.     @Override
  34.     public Affiliation parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws IOException {
  35.         String node = parser.getAttributeValue(null, "node");
  36.         BareJid jid = ParserUtils.getBareJidAttribute(parser);
  37.         String namespaceString = parser.getNamespace();
  38.         AffiliationNamespace namespace = AffiliationNamespace.fromXmlns(namespaceString);

  39.         String affiliationString = parser.getAttributeValue(null, "affiliation");
  40.         Affiliation.Type affiliationType = null;
  41.         if (affiliationString != null) {
  42.             affiliationType = Affiliation.Type.valueOf(affiliationString);
  43.         }
  44.         Affiliation affiliation;
  45.         if (node != null && jid == null) {
  46.             // affiliationType may be empty
  47.             affiliation = new Affiliation(node, affiliationType, namespace);
  48.         }
  49.         else if (node == null && jid != null) {
  50.             affiliation = new Affiliation(jid, affiliationType, namespace);
  51.         }
  52.         else {
  53.             // TODO: Should be SmackParsingException.
  54.             throw new IOException("Invalid affililation. Either one of 'node' or 'jid' must be set"
  55.                     + ". Node: " + node
  56.                     + ". Jid: " + jid
  57.                     + '.');
  58.         }
  59.         return affiliation;
  60.     }

  61. }