001/**
002 *
003 * Copyright 2016 Fernando Ramirez
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.message_correct.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
026/**
027 * An Extension that implements XEP-0308: Last Message Correction
028 *
029 * This extension is expected to be added to message stanzas. Please refer to
030 * the XEP for more implementation guidelines.
031 *
032 * @author Fernando Ramirez, f.e.ramirez94@gmail.com
033 * @see <a href="http://xmpp.org/extensions/xep-0308.html">XEP-0308:&nbsp;Last
034 *      &nbsp;Message&nbsp;Correction</a>
035 */
036public class MessageCorrectExtension implements ExtensionElement {
037
038    /**
039     * The XML element name of a 'message correct' extension.
040     */
041    public static final String ELEMENT = "replace";
042
043    /**
044     * The namespace that qualifies the XML element of a 'message correct'
045     * extension.
046     */
047    public static final String NAMESPACE = "urn:xmpp:message-correct:0";
048
049    public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
050
051    /**
052     * The id tag of a 'message correct' extension.
053     */
054    public static final String ID_TAG = "id";
055
056    /**
057     * The id of the message to correct.
058     */
059    private final String idInitialMessage;
060
061    public MessageCorrectExtension(String idInitialMessage) {
062        this.idInitialMessage = StringUtils.requireNotNullNorEmpty(idInitialMessage, "idInitialMessage must not be null");
063    }
064
065    public String getIdInitialMessage() {
066        return idInitialMessage;
067    }
068
069    @Override
070    public String getElementName() {
071        return ELEMENT;
072    }
073
074    @Override
075    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
076        XmlStringBuilder xml = new XmlStringBuilder(this);
077        xml.attribute(ID_TAG, getIdInitialMessage());
078        xml.closeEmptyElement();
079        return xml;
080    }
081
082    @Override
083    public String getNamespace() {
084        return NAMESPACE;
085    }
086
087    public static MessageCorrectExtension from(Message message) {
088        return message.getExtension(MessageCorrectExtension.class);
089    }
090
091}