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