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    public static Map<String, String> getSpoilers(Message message) {
118        if (!containsSpoiler(message)) {
119            return Collections.emptyMap();
120        }
121
122        List<SpoilerElement> spoilers = message.getExtensions(SpoilerElement.class);
123        Map<String, String> map = new HashMap<>();
124
125        for (SpoilerElement s : spoilers) {
126            if (s.getLanguage() == null || s.getLanguage().equals("")) {
127                map.put("", s.getHint());
128            } else {
129                map.put(s.getLanguage(), s.getHint());
130            }
131        }
132
133        return map;
134    }
135
136    @Override
137    public String getLanguage() {
138        return language;
139    }
140
141    @Override
142    public String getNamespace() {
143        return NAMESPACE;
144    }
145
146    @Override
147    public String getElementName() {
148        return ELEMENT;
149    }
150
151    @Override
152    public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
153        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
154        if (getHint() == null) {
155            xml.closeEmptyElement();
156        } else {
157            xml.rightAngleBracket();
158            xml.append(getHint());
159            xml.closeElement(this);
160        }
161        return xml;
162    }
163}