MUCLightElements.java

  1. /**
  2.  *
  3.  * Copyright 2016 Fernando Ramirez
  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.muclight.element;

  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.Map;

  21. import javax.xml.namespace.QName;

  22. import org.jivesoftware.smack.packet.Element;
  23. import org.jivesoftware.smack.packet.ExtensionElement;
  24. import org.jivesoftware.smack.packet.Message;
  25. import org.jivesoftware.smack.util.XmlStringBuilder;

  26. import org.jivesoftware.smackx.muclight.MUCLightAffiliation;
  27. import org.jivesoftware.smackx.muclight.MUCLightRoomConfiguration;
  28. import org.jivesoftware.smackx.muclight.MultiUserChatLight;
  29. import org.jivesoftware.smackx.xdata.packet.DataForm;

  30. import org.jxmpp.jid.Jid;

  31. public abstract class MUCLightElements {

  32.     /**
  33.      * Affiliations change extension element class.
  34.      *
  35.      * @author Fernando Ramirez
  36.      *
  37.      */
  38.     public static class AffiliationsChangeExtension implements ExtensionElement {

  39.         public static final String ELEMENT = DataForm.ELEMENT;
  40.         public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.AFFILIATIONS;
  41.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  42.         private final HashMap<Jid, MUCLightAffiliation> affiliations;
  43.         private final String prevVersion;
  44.         private final String version;

  45.         public AffiliationsChangeExtension(HashMap<Jid, MUCLightAffiliation> affiliations, String prevVersion,
  46.                 String version) {
  47.             this.affiliations = affiliations;
  48.             this.prevVersion = prevVersion;
  49.             this.version = version;
  50.         }

  51.         @Override
  52.         public String getElementName() {
  53.             return ELEMENT;
  54.         }

  55.         @Override
  56.         public String getNamespace() {
  57.             return NAMESPACE;
  58.         }

  59.         /**
  60.          * Get the affiliations.
  61.          *
  62.          * @return the affiliations
  63.          */
  64.         public HashMap<Jid, MUCLightAffiliation> getAffiliations() {
  65.             return affiliations;
  66.         }

  67.         /**
  68.          * Get the previous version.
  69.          *
  70.          * @return the previous version
  71.          */
  72.         public String getPrevVersion() {
  73.             return prevVersion;
  74.         }

  75.         /**
  76.          * Get the version.
  77.          *
  78.          * @return the version
  79.          */
  80.         public String getVersion() {
  81.             return version;
  82.         }

  83.         @Override
  84.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  85.             XmlStringBuilder xml = new XmlStringBuilder(this);
  86.             xml.rightAngleBracket();

  87.             xml.optElement("prev-version", prevVersion);
  88.             xml.optElement("version", version);

  89.             Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = affiliations.entrySet().iterator();
  90.             while (it.hasNext()) {
  91.                 Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
  92.                 xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
  93.             }

  94.             xml.closeElement(this);
  95.             return xml;
  96.         }

  97.         public static AffiliationsChangeExtension from(Message message) {
  98.             return message.getExtension(AffiliationsChangeExtension.class);
  99.         }

  100.     }

  101.     /**
  102.      * Configurations change extension element class.
  103.      *
  104.      * @author Fernando Ramirez
  105.      *
  106.      */
  107.     public static class ConfigurationsChangeExtension implements ExtensionElement {

  108.         public static final String ELEMENT = DataForm.ELEMENT;
  109.         public static final String NAMESPACE = MultiUserChatLight.NAMESPACE + MultiUserChatLight.CONFIGURATION;

  110.         private final String prevVersion;
  111.         private final String version;
  112.         private final String roomName;
  113.         private final String subject;
  114.         private final HashMap<String, String> customConfigs;

  115.         /**
  116.          * Configurations change extension constructor.
  117.          *
  118.          * @param prevVersion TODO javadoc me please
  119.          * @param version TODO javadoc me please
  120.          * @param roomName TODO javadoc me please
  121.          * @param subject TODO javadoc me please
  122.          * @param customConfigs TODO javadoc me please
  123.          */
  124.         public ConfigurationsChangeExtension(String prevVersion, String version, String roomName, String subject,
  125.                 HashMap<String, String> customConfigs) {
  126.             this.prevVersion = prevVersion;
  127.             this.version = version;
  128.             this.roomName = roomName;
  129.             this.subject = subject;
  130.             this.customConfigs = customConfigs;
  131.         }

  132.         @Override
  133.         public String getElementName() {
  134.             return ELEMENT;
  135.         }

  136.         @Override
  137.         public String getNamespace() {
  138.             return NAMESPACE;
  139.         }

  140.         /**
  141.          * Get the previous version.
  142.          *
  143.          * @return the previous version
  144.          */
  145.         public String getPrevVersion() {
  146.             return prevVersion;
  147.         }

  148.         /**
  149.          * Get the version.
  150.          *
  151.          * @return the version
  152.          */
  153.         public String getVersion() {
  154.             return version;
  155.         }

  156.         /**
  157.          * Get the room name.
  158.          *
  159.          * @return the room name
  160.          */
  161.         public String getRoomName() {
  162.             return roomName;
  163.         }

  164.         /**
  165.          * Get the room subject.
  166.          *
  167.          * @return the room subject
  168.          */
  169.         public String getSubject() {
  170.             return subject;
  171.         }

  172.         /**
  173.          * Get the room custom configurations.
  174.          *
  175.          * @return the room custom configurations
  176.          */
  177.         public HashMap<String, String> getCustomConfigs() {
  178.             return customConfigs;
  179.         }

  180.         @Override
  181.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  182.             XmlStringBuilder xml = new XmlStringBuilder(this);
  183.             xml.rightAngleBracket();

  184.             xml.optElement("prev-version", prevVersion);
  185.             xml.optElement("version", version);
  186.             xml.optElement("roomname", roomName);
  187.             xml.optElement("subject", subject);

  188.             if (customConfigs != null) {
  189.                 Iterator<Map.Entry<String, String>> it = customConfigs.entrySet().iterator();
  190.                 while (it.hasNext()) {
  191.                     Map.Entry<String, String> pair = it.next();
  192.                     xml.element(pair.getKey(), pair.getValue());
  193.                 }
  194.             }

  195.             xml.closeElement(this);
  196.             return xml;
  197.         }

  198.         public static ConfigurationsChangeExtension from(Message message) {
  199.             return (ConfigurationsChangeExtension) message.getExtensionElement(ConfigurationsChangeExtension.ELEMENT, ConfigurationsChangeExtension.NAMESPACE);
  200.         }

  201.     }

  202.     /**
  203.      * Configuration element class.
  204.      *
  205.      * @author Fernando Ramirez
  206.      *
  207.      */
  208.     public static class ConfigurationElement implements Element {

  209.         private MUCLightRoomConfiguration configuration;

  210.         /**
  211.          * Configuration element constructor.
  212.          *
  213.          * @param configuration TODO javadoc me please
  214.          */
  215.         public ConfigurationElement(MUCLightRoomConfiguration configuration) {
  216.             this.configuration = configuration;
  217.         }

  218.         @Override
  219.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  220.             XmlStringBuilder xml = new XmlStringBuilder();
  221.             xml.openElement("configuration");

  222.             xml.element("roomname", configuration.getRoomName());
  223.             xml.optElement("subject", configuration.getSubject());

  224.             if (configuration.getCustomConfigs() != null) {
  225.                 Iterator<Map.Entry<String, String>> it = configuration.getCustomConfigs().entrySet().iterator();
  226.                 while (it.hasNext()) {
  227.                     Map.Entry<String, String> pair = it.next();
  228.                     xml.element(pair.getKey(), pair.getValue());
  229.                 }
  230.             }

  231.             xml.closeElement("configuration");
  232.             return xml;
  233.         }

  234.     }

  235.     /**
  236.      * Occupants element class.
  237.      *
  238.      * @author Fernando Ramirez
  239.      *
  240.      */
  241.     public static class OccupantsElement implements Element {

  242.         private HashMap<Jid, MUCLightAffiliation> occupants;

  243.         /**
  244.          * Occupants element constructor.
  245.          *
  246.          * @param occupants TODO javadoc me please
  247.          */
  248.         public OccupantsElement(HashMap<Jid, MUCLightAffiliation> occupants) {
  249.             this.occupants = occupants;
  250.         }

  251.         @Override
  252.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  253.             XmlStringBuilder xml = new XmlStringBuilder();
  254.             xml.openElement("occupants");

  255.             Iterator<Map.Entry<Jid, MUCLightAffiliation>> it = occupants.entrySet().iterator();
  256.             while (it.hasNext()) {
  257.                 Map.Entry<Jid, MUCLightAffiliation> pair = it.next();
  258.                 xml.append(new UserWithAffiliationElement(pair.getKey(), pair.getValue()));
  259.             }

  260.             xml.closeElement("occupants");
  261.             return xml;
  262.         }

  263.     }

  264.     /**
  265.      * User with affiliation element class.
  266.      *
  267.      * @author Fernando Ramirez
  268.      *
  269.      */
  270.     public static class UserWithAffiliationElement implements Element {

  271.         private Jid user;
  272.         private MUCLightAffiliation affiliation;

  273.         /**
  274.          * User with affiliations element constructor.
  275.          *
  276.          * @param user TODO javadoc me please
  277.          * @param affiliation TODO javadoc me please
  278.          */
  279.         public UserWithAffiliationElement(Jid user, MUCLightAffiliation affiliation) {
  280.             this.user = user;
  281.             this.affiliation = affiliation;
  282.         }

  283.         @Override
  284.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  285.             XmlStringBuilder xml = new XmlStringBuilder();
  286.             xml.halfOpenElement("user");
  287.             xml.attribute("affiliation", affiliation);
  288.             xml.rightAngleBracket();
  289.             xml.escape(user);
  290.             xml.closeElement("user");
  291.             return xml;
  292.         }

  293.     }

  294.     /**
  295.      * Blocking element class.
  296.      *
  297.      * @author Fernando Ramirez
  298.      *
  299.      */
  300.     public static class BlockingElement implements Element {

  301.         private Jid jid;
  302.         private Boolean allow;
  303.         private Boolean isRoom;

  304.         /**
  305.          * Blocking element constructor.
  306.          *
  307.          * @param jid TODO javadoc me please
  308.          * @param allow TODO javadoc me please
  309.          * @param isRoom TODO javadoc me please
  310.          */
  311.         public BlockingElement(Jid jid, Boolean allow, Boolean isRoom) {
  312.             this.jid = jid;
  313.             this.allow = allow;
  314.             this.isRoom = isRoom;
  315.         }

  316.         @Override
  317.         public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  318.             XmlStringBuilder xml = new XmlStringBuilder();

  319.             String tag = isRoom ? "room" : "user";
  320.             xml.halfOpenElement(tag);

  321.             String action = allow ? "allow" : "deny";
  322.             xml.attribute("action", action);
  323.             xml.rightAngleBracket();

  324.             xml.escape(jid);

  325.             xml.closeElement(tag);
  326.             return xml;
  327.         }

  328.     }

  329. }