Affiliation.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;

  18. import org.jivesoftware.smack.XMPPConnection;
  19. import org.jivesoftware.smack.packet.ExtensionElement;

  20. /**
  21.  * Represents a affiliation between a user and a node, where the {@link #type} defines
  22.  * the type of affiliation.
  23.  *
  24.  * Affiliations are retrieved from the {@link PubSubManager#getAffiliations()} method, which
  25.  * gets affiliations for the calling user, based on the identity that is associated with
  26.  * the {@link XMPPConnection}.
  27.  *
  28.  * @author Robin Collier
  29.  */
  30. public class Affiliation implements ExtensionElement
  31. {
  32.     protected String node;
  33.     protected Type type;
  34.    
  35.     public enum Type
  36.     {
  37.         member, none, outcast, owner, publisher
  38.     }

  39.     /**
  40.      * Constructs an affiliation.
  41.      *
  42.      * @param nodeId The node the user is affiliated with.
  43.      * @param affiliation The type of affiliation.
  44.      */
  45.     public Affiliation(String nodeId, Type affiliation)
  46.     {
  47.         node = nodeId;
  48.         type = affiliation;
  49.     }
  50.    
  51.     public String getNodeId()
  52.     {
  53.         return node;
  54.     }
  55.    
  56.     public Type getType()
  57.     {
  58.         return type;
  59.     }
  60.    
  61.     public String getElementName()
  62.     {
  63.         return "subscription";
  64.     }

  65.     public String getNamespace()
  66.     {
  67.         return null;
  68.     }

  69.     public String toXML()
  70.     {
  71.         StringBuilder builder = new StringBuilder("<");
  72.         builder.append(getElementName());
  73.         appendAttribute(builder, "node", node);
  74.         appendAttribute(builder, "affiliation", type.toString());
  75.        
  76.         builder.append("/>");
  77.         return builder.toString();
  78.     }

  79.     private void appendAttribute(StringBuilder builder, String att, String value)
  80.     {
  81.         builder.append(" ");
  82.         builder.append(att);
  83.         builder.append("='");
  84.         builder.append(value);
  85.         builder.append("'");
  86.     }
  87. }