001/**
002 *
003 * Copyright © 2016 Fernando Ramirez, 2018-2020 Florian Schmaus
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.chat_markers.element;
018
019import javax.xml.namespace.QName;
020
021import org.jivesoftware.smack.packet.ExtensionElement;
022import org.jivesoftware.smack.packet.Message;
023import org.jivesoftware.smack.util.StringUtils;
024import org.jivesoftware.smack.util.XmlStringBuilder;
025
026import org.jivesoftware.smackx.chat_markers.ChatMarkersState;
027
028/**
029 * Chat Markers elements (XEP-0333).
030 *
031 * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
032 *      Markers</a>
033 * @author Fernando Ramirez
034 *
035 */
036public class ChatMarkersElements {
037
038    public static final String NAMESPACE = "urn:xmpp:chat-markers:0";
039
040    /**
041     * Markable extension class.
042     *
043     * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
044     *      Markers</a>
045     * @author Fernando Ramirez
046     *
047     */
048    public static final class MarkableExtension implements ExtensionElement {
049
050        public static final MarkableExtension INSTANCE = new MarkableExtension();
051        /**
052         * markable element.
053         */
054        public static final String ELEMENT = ChatMarkersState.markable.toString();
055        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
056
057        private MarkableExtension() {
058        }
059
060        @Override
061        public String getElementName() {
062            return ELEMENT;
063        }
064
065        @Override
066        public String getNamespace() {
067            return NAMESPACE;
068        }
069
070        @Override
071        public CharSequence toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
072            XmlStringBuilder xml = new XmlStringBuilder(this);
073            xml.closeEmptyElement();
074            return xml;
075        }
076
077        public static MarkableExtension from(Message message) {
078            return message.getExtension(MarkableExtension.class);
079        }
080    }
081
082    protected abstract static class ChatMarkerExtensionWithId implements ExtensionElement {
083        protected final String id;
084
085        protected ChatMarkerExtensionWithId(String id) {
086            this.id = StringUtils.requireNotNullNorEmpty(id, "Message ID must not be null");
087        }
088
089        /**
090         * Get the id.
091         *
092         * @return the id
093         */
094        public final String getId() {
095            return id;
096        }
097
098        @Override
099        public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
100            XmlStringBuilder xml = new XmlStringBuilder(this);
101            xml.attribute("id", id);
102            xml.closeEmptyElement();
103            return xml;
104        }
105    }
106
107    /**
108     * Received extension class.
109     *
110     * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
111     *      Markers</a>
112     * @author Fernando Ramirez
113     *
114     */
115    public static class ReceivedExtension extends ChatMarkerExtensionWithId {
116
117        /**
118         * received element.
119         */
120        public static final String ELEMENT = ChatMarkersState.received.toString();
121        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
122
123        public ReceivedExtension(String id) {
124            super(id);
125        }
126
127        @Override
128        public String getElementName() {
129            return ELEMENT;
130        }
131
132        @Override
133        public String getNamespace() {
134            return NAMESPACE;
135        }
136
137        public static ReceivedExtension from(Message message) {
138            return message.getExtension(ReceivedExtension.class);
139        }
140    }
141
142    /**
143     * Displayed extension class.
144     *
145     * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
146     *      Markers</a>
147     * @author Fernando Ramirez
148     *
149     */
150    public static class DisplayedExtension extends ChatMarkerExtensionWithId {
151
152        /**
153         * displayed element.
154         */
155        public static final String ELEMENT = ChatMarkersState.displayed.toString();
156        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
157
158        public DisplayedExtension(String id) {
159            super(id);
160        }
161
162        @Override
163        public String getElementName() {
164            return ELEMENT;
165        }
166
167        @Override
168        public String getNamespace() {
169            return NAMESPACE;
170        }
171
172        public static DisplayedExtension from(Message message) {
173            return message.getExtension(DisplayedExtension.class);
174        }
175    }
176
177    /**
178     * Acknowledged extension class.
179     *
180     * @see <a href="http://xmpp.org/extensions/xep-0333.html">XEP-0333: Chat
181     *      Markers</a>
182     * @author Fernando Ramirez
183     *
184     */
185    public static class AcknowledgedExtension extends ChatMarkerExtensionWithId {
186
187        /**
188         * acknowledged element.
189         */
190        public static final String ELEMENT = ChatMarkersState.acknowledged.toString();
191        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
192
193        public AcknowledgedExtension(String id) {
194            super(id);
195        }
196
197        @Override
198        public String getElementName() {
199            return ELEMENT;
200        }
201
202        @Override
203        public String getNamespace() {
204            return NAMESPACE;
205        }
206
207        public static AcknowledgedExtension from(Message message) {
208            return message.getExtension(AcknowledgedExtension.class);
209        }
210    }
211
212}