001/** 002 * 003 * Copyright 2018 Paul Schaub. 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.jivesoftware.smackx.mood.element; 018 019import javax.xml.namespace.QName; 020 021import org.jivesoftware.smack.packet.ExtensionElement; 022import org.jivesoftware.smack.packet.FullyQualifiedElement; 023import org.jivesoftware.smack.packet.Message; 024import org.jivesoftware.smack.packet.NamedElement; 025import org.jivesoftware.smack.packet.XmlEnvironment; 026import org.jivesoftware.smack.util.Objects; 027import org.jivesoftware.smack.util.XmlStringBuilder; 028 029import org.jivesoftware.smackx.mood.Mood; 030 031/** 032 * {@link ExtensionElement} that contains the users mood. 033 * 034 * Optionally this element also contains a text node, which contains a natural language description or 035 * reason for the mood. 036 */ 037public class MoodElement implements ExtensionElement { 038 039 public static final String NAMESPACE = "http://jabber.org/protocol/mood"; 040 public static final String ELEMENT = "mood"; 041 public static final QName QNAME = new QName(NAMESPACE, ELEMENT); 042 043 public static final String ELEM_TEXT = "text"; 044 045 private final MoodSubjectElement mood; 046 private final String text; 047 048 public MoodElement(MoodSubjectElement mood, String text) { 049 if (mood == null && text != null) { 050 throw new IllegalArgumentException("If <mood/> is null, text MUST be null too."); 051 } 052 053 this.mood = mood; 054 this.text = text; 055 } 056 057 /** 058 * Return the senders mood. 059 * This method returns null in case the sender wants to stop sending their mood. 060 * 061 * @return mood or null 062 */ 063 public Mood getMood() { 064 return mood != null ? mood.getMood() : null; 065 } 066 067 /** 068 * The user might set a reason or description for/of their mood. 069 * This method returns a natural language reason for the mood. 070 * 071 * @return text or null. 072 */ 073 public String getText() { 074 return text; 075 } 076 077 /** 078 * Returns true, if the user gives a reason for their mood. 079 * 080 * @return true or false 081 */ 082 public boolean hasText() { 083 return getText() != null; 084 } 085 086 /** 087 * Implementors might implement custom concretisations of mood. 088 * This method returns any custom concretisation of the mood the user might have set. 089 * 090 * @return concretisation or null. 091 */ 092 public MoodConcretisation getMoodConcretisation() { 093 return mood != null ? mood.getConcretisation() : null; 094 } 095 096 /** 097 * Return true, if this mood has a concretisation. 098 * 099 * @return true or false 100 */ 101 public boolean hasConcretisation() { 102 return getMoodConcretisation() != null; 103 } 104 105 @Override 106 public String getNamespace() { 107 return NAMESPACE; 108 } 109 110 @Override 111 public String getElementName() { 112 return ELEMENT; 113 } 114 115 @Override 116 public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { 117 XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment); 118 119 if (mood == null && text == null) { 120 // Empty mood element used as STOP signal 121 return xml.closeEmptyElement(); 122 } 123 xml.rightAngleBracket(); 124 125 xml.optAppend(mood); 126 127 if (text != null) { 128 xml.openElement(ELEM_TEXT) 129 .append(text) 130 .closeElement(ELEM_TEXT); 131 } 132 return xml.closeElement(getElementName()); 133 } 134 135 /** 136 * Extract a {@link MoodElement} from a message. 137 * 138 * @param message message 139 * 140 * @return {@link MoodElement} or null. 141 */ 142 public static MoodElement fromMessage(Message message) { 143 return message.getExtension(MoodElement.class); 144 } 145 146 /** 147 * Return true, if the {@code message} has a {@link MoodElement}, otherwise false. 148 * 149 * @param message message 150 * 151 * @return true of false 152 */ 153 public static boolean hasMoodElement(Message message) { 154 return message.hasExtension(ELEMENT, NAMESPACE); 155 } 156 157 /** 158 * {@link NamedElement} which represents the mood. 159 * This element has the element name of the mood selected from {@link Mood}. 160 */ 161 public static class MoodSubjectElement implements FullyQualifiedElement { 162 163 private final Mood mood; 164 private final MoodConcretisation concretisation; 165 166 public MoodSubjectElement(Mood mood, MoodConcretisation concretisation) { 167 this.mood = Objects.requireNonNull(mood); 168 this.concretisation = concretisation; 169 } 170 171 @Override 172 public String getElementName() { 173 return mood.toString(); 174 } 175 176 @Override 177 public XmlStringBuilder toXML(XmlEnvironment xmlEnvironment) { 178 XmlStringBuilder xml = new XmlStringBuilder(this, xmlEnvironment); 179 180 if (concretisation == null) { 181 return xml.closeEmptyElement(); 182 } 183 184 xml.rightAngleBracket() 185 .append(concretisation) 186 .closeElement(this); 187 return xml; 188 } 189 190 /** 191 * Return the mood of the user. 192 * 193 * @return mood or null 194 */ 195 public Mood getMood() { 196 return mood; 197 } 198 199 /** 200 * Return the concretisation of the mood. 201 * 202 * @return concretisation or null 203 */ 204 public MoodConcretisation getConcretisation() { 205 return concretisation; 206 } 207 208 @Override 209 public String getNamespace() { 210 return NAMESPACE; 211 } 212 } 213}