MoodElement.java

  1. /**
  2.  *
  3.  * Copyright 2018 Paul Schaub.
  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.mood.element;

  18. import javax.xml.namespace.QName;

  19. import org.jivesoftware.smack.packet.ExtensionElement;
  20. import org.jivesoftware.smack.packet.Message;
  21. import org.jivesoftware.smack.packet.NamedElement;
  22. import org.jivesoftware.smack.packet.XmlElement;
  23. import org.jivesoftware.smack.packet.XmlEnvironment;
  24. import org.jivesoftware.smack.util.Objects;
  25. import org.jivesoftware.smack.util.XmlStringBuilder;

  26. import org.jivesoftware.smackx.mood.Mood;

  27. /**
  28.  * {@link ExtensionElement} that contains the users mood.
  29.  *
  30.  * Optionally this element also contains a text node, which contains a natural language description or
  31.  * reason for the mood.
  32.  */
  33. public class MoodElement implements ExtensionElement {

  34.     public static final String NAMESPACE = "http://jabber.org/protocol/mood";
  35.     public static final String ELEMENT = "mood";
  36.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  37.     public static final String ELEM_TEXT = "text";

  38.     private final MoodSubjectElement mood;
  39.     private final String text;

  40.     public MoodElement(MoodSubjectElement mood, String text) {
  41.         if (mood == null && text != null) {
  42.             throw new IllegalArgumentException("If <mood/> is null, text MUST be null too.");
  43.         }

  44.         this.mood = mood;
  45.         this.text = text;
  46.     }

  47.     /**
  48.      * Return the senders mood.
  49.      * This method returns null in case the sender wants to stop sending their mood.
  50.      *
  51.      * @return mood or null
  52.      */
  53.     public Mood getMood() {
  54.         return mood != null ? mood.getMood() : null;
  55.     }

  56.     /**
  57.      * The user might set a reason or description for/of their mood.
  58.      * This method returns a natural language reason for the mood.
  59.      *
  60.      * @return text or null.
  61.      */
  62.     public String getText() {
  63.         return text;
  64.     }

  65.     /**
  66.      * Returns true, if the user gives a reason for their mood.
  67.      *
  68.      * @return true or false
  69.      */
  70.     public boolean hasText() {
  71.         return getText() != null;
  72.     }

  73.     /**
  74.      * Implementors might implement custom concretisations of mood.
  75.      * This method returns any custom concretisation of the mood the user might have set.
  76.      *
  77.      * @return concretisation or null.
  78.      */
  79.     public MoodConcretisation getMoodConcretisation() {
  80.         return mood != null ? mood.getConcretisation() : null;
  81.     }

  82.     /**
  83.      * Return true, if this mood has a concretisation.
  84.      *
  85.      * @return true or false
  86.      */
  87.     public boolean hasConcretisation() {
  88.         return getMoodConcretisation() != null;
  89.     }

  90.     @Override
  91.     public String getNamespace() {
  92.         return NAMESPACE;
  93.     }

  94.     @Override
  95.     public String getElementName() {
  96.         return ELEMENT;
  97.     }

  98.     @Override
  99.     public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
  100.         XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);

  101.         if (mood == null && text == null) {
  102.             // Empty mood element used as STOP signal
  103.             return xml.closeEmptyElement();
  104.         }
  105.         xml.rightAngleBracket();

  106.         xml.optAppend(mood);

  107.         if (text != null) {
  108.             xml.openElement(ELEM_TEXT)
  109.                     .append(text)
  110.                     .closeElement(ELEM_TEXT);
  111.         }
  112.         return xml.closeElement(getElementName());
  113.     }

  114.     /**
  115.      * Extract a {@link MoodElement} from a message.
  116.      *
  117.      * @param message message
  118.      *
  119.      * @return {@link MoodElement} or null.
  120.      */
  121.     public static MoodElement fromMessage(Message message) {
  122.         return message.getExtension(MoodElement.class);
  123.     }

  124.     /**
  125.      * Return true, if the {@code message} has a {@link MoodElement}, otherwise false.
  126.      *
  127.      * @param message message
  128.      *
  129.      * @return true of false
  130.      */
  131.     public static boolean hasMoodElement(Message message) {
  132.         return message.hasExtension(ELEMENT, NAMESPACE);
  133.     }

  134.     /**
  135.      * {@link NamedElement} which represents the mood.
  136.      * This element has the element name of the mood selected from {@link Mood}.
  137.      */
  138.     public static class MoodSubjectElement implements XmlElement {

  139.         private final Mood mood;
  140.         private final MoodConcretisation concretisation;

  141.         public MoodSubjectElement(Mood mood, MoodConcretisation concretisation) {
  142.             this.mood = Objects.requireNonNull(mood);
  143.             this.concretisation = concretisation;
  144.         }

  145.         @Override
  146.         public String getElementName() {
  147.             return mood.toString();
  148.         }

  149.         @Override
  150.         public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) {
  151.             XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment);

  152.             if (concretisation == null) {
  153.                 return xml.closeEmptyElement();
  154.             }

  155.             xml.rightAngleBracket()
  156.                 .append(concretisation)
  157.                 .closeElement(this);
  158.             return xml;
  159.         }

  160.         /**
  161.          * Return the mood of the user.
  162.          *
  163.          * @return mood or null
  164.          */
  165.         public Mood getMood() {
  166.             return mood;
  167.         }

  168.         /**
  169.          * Return the concretisation of the mood.
  170.          *
  171.          * @return concretisation or null
  172.          */
  173.         public MoodConcretisation getConcretisation() {
  174.             return concretisation;
  175.         }

  176.         @Override
  177.         public String getNamespace() {
  178.             return NAMESPACE;
  179.         }
  180.     }
  181. }