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.message_markup.element;
018
019import java.util.Collections;
020import java.util.Set;
021
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024public class SpanElement implements MarkupElement.MarkupChildElement {
025
026    public static final String ELEMENT = "span";
027
028    private final int start, end;
029    private final Set<SpanStyle> styles;
030
031    /**
032     * Create a new Span element.
033     *
034     * @param start start index
035     * @param end end index
036     * @param styles list of styles that apply to this span
037     */
038    public SpanElement(int start, int end, Set<SpanStyle> styles) {
039        this.start = start;
040        this.end = end;
041        this.styles = Collections.unmodifiableSet(styles);
042    }
043
044    @Override
045    public int getStart() {
046        return start;
047    }
048
049    @Override
050    public int getEnd() {
051        return end;
052    }
053
054    /**
055     * Return all styles of this span.
056     *
057     * @return styles
058     */
059    public Set<SpanStyle> getStyles() {
060        return styles;
061    }
062
063    public static final String emphasis = "emphasis";
064    public static final String code = "code";
065    public static final String deleted = "deleted";
066
067    public enum SpanStyle {
068        emphasis,
069        code,
070        deleted
071    }
072
073    @Override
074    public String getElementName() {
075        return ELEMENT;
076    }
077
078    @Override
079    public XmlStringBuilder toXML(String enclosingNamespace) {
080        XmlStringBuilder xml = new XmlStringBuilder();
081        xml.halfOpenElement(this);
082        xml.attribute(ATTR_START, getStart());
083        xml.attribute(ATTR_END, getEnd());
084        xml.rightAngleBracket();
085
086        for (SpanStyle style : getStyles()) {
087            xml.halfOpenElement(style.toString()).closeEmptyElement();
088        }
089
090        xml.closeElement(this);
091        return xml;
092    }
093}