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.spoiler.element; 018 019import java.util.Collections; 020import java.util.HashMap; 021import java.util.List; 022import java.util.Map; 023 024import javax.xml.namespace.QName; 025 026import org.jivesoftware.smack.packet.ExtensionElement; 027import org.jivesoftware.smack.packet.Message; 028import org.jivesoftware.smack.util.StringUtils; 029import org.jivesoftware.smack.util.XmlStringBuilder; 030 031import org.jivesoftware.smackx.spoiler.SpoilerManager; 032 033public class SpoilerElement implements ExtensionElement { 034 035 public static final String ELEMENT = "spoiler"; 036 public static final String NAMESPACE = SpoilerManager.NAMESPACE_0; 037 public static final QName QNAME = new QName(NAMESPACE, ELEMENT); 038 039 public static final SpoilerElement EMPTY = new SpoilerElement(null, null); 040 041 private final String hint; 042 private final String language; 043 044 /** 045 * Create a new SpoilerElement with a hint about a content and a language attribute. 046 * 047 * @param language language of the hint. 048 * @param hint hint about the content. 049 */ 050 public SpoilerElement(String language, String hint) { 051 if (StringUtils.isNotEmpty(language) && StringUtils.isNullOrEmpty(hint)) { 052 throw new IllegalArgumentException("Hint cannot be null or empty if language is not empty."); 053 } 054 this.language = language; 055 this.hint = hint; 056 } 057 058 /** 059 * Return the hint text of the spoiler. 060 * May be null. 061 * 062 * @return hint text 063 */ 064 public String getHint() { 065 return hint; 066 } 067 068 /** 069 * Add a SpoilerElement to a message. 070 * 071 * @param message message to add the Spoiler to. 072 */ 073 public static void addSpoiler(Message message) { 074 message.addExtension(SpoilerElement.EMPTY); 075 } 076 077 /** 078 * Add a SpoilerElement with a hint to a message. 079 * 080 * @param message Message to add the Spoiler to. 081 * @param hint Hint about the Spoilers content. 082 */ 083 public static void addSpoiler(Message message, String hint) { 084 message.addExtension(new SpoilerElement(null, hint)); 085 } 086 087 /** 088 * Add a SpoilerElement with a hint in a certain language to a message. 089 * 090 * @param message Message to add the Spoiler to. 091 * @param lang language of the Spoiler hint. 092 * @param hint hint. 093 */ 094 public static void addSpoiler(Message message, String lang, String hint) { 095 message.addExtension(new SpoilerElement(lang, hint)); 096 } 097 098 099 /** 100 * Returns true, if the message has at least one spoiler element. 101 * 102 * @param message message 103 * @return true if message has spoiler extension 104 */ 105 public static boolean containsSpoiler(Message message) { 106 return message.hasExtension(SpoilerElement.ELEMENT, NAMESPACE); 107 } 108 109 /** 110 * Return a map of all spoilers contained in a message. 111 * The map uses the language of a spoiler as key. 112 * If a spoiler has no language attribute, its key will be an empty String. 113 * 114 * @param message message 115 * @return map of spoilers 116 */ 117 @SuppressWarnings("MixedMutabilityReturnType") 118 public static Map<String, String> getSpoilers(Message message) { 119 if (!containsSpoiler(message)) { 120 return Collections.emptyMap(); 121 } 122 123 List<SpoilerElement> spoilers = message.getExtensions(SpoilerElement.class); 124 Map<String, String> map = new HashMap<>(); 125 126 for (SpoilerElement s : spoilers) { 127 if (s.getLanguage() == null || s.getLanguage().equals("")) { 128 map.put("", s.getHint()); 129 } else { 130 map.put(s.getLanguage(), s.getHint()); 131 } 132 } 133 134 return map; 135 } 136 137 @Override 138 public String getLanguage() { 139 return language; 140 } 141 142 @Override 143 public String getNamespace() { 144 return NAMESPACE; 145 } 146 147 @Override 148 public String getElementName() { 149 return ELEMENT; 150 } 151 152 @Override 153 public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 154 XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace); 155 if (getHint() == null) { 156 xml.closeEmptyElement(); 157 } else { 158 xml.rightAngleBracket(); 159 xml.append(getHint()); 160 xml.closeElement(this); 161 } 162 return xml; 163 } 164}