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