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.List;
021
022import org.jivesoftware.smack.packet.ExtensionElement;
023import org.jivesoftware.smack.util.XmlStringBuilder;
024
025public class ListElement extends MarkupElement.NonEmptyChildElement {
026
027    public static final String ELEMENT = "list";
028    public static final String ELEM_LI = "li";
029
030    private final List<ListEntryElement> entries;
031
032    /**
033     * Create a new List element.
034     *
035     * @param start start index of the list
036     * @param end end index of the list
037     * @param entries list entries
038     */
039    public ListElement(int start, int end, List<ListEntryElement> entries) {
040        super(start, end);
041        this.entries = Collections.unmodifiableList(entries);
042    }
043
044    /**
045     * Return a list of all list entries.
046     *
047     * @return entries TODO javadoc me please
048     */
049    public List<ListEntryElement> getEntries() {
050        return entries;
051    }
052
053    @Override
054    public String getElementName() {
055        return ELEMENT;
056    }
057
058    @Override
059    public void appendInnerXml(XmlStringBuilder xml) {
060        xml.append(getEntries());
061    }
062
063    public static class ListEntryElement implements ExtensionElement {
064
065        private final int start;
066
067        /**
068         * Create a new ListEntry element.
069         *
070         * @param start start index
071         */
072        public ListEntryElement(int start) {
073            this.start = start;
074        }
075
076        /**
077         * Return the start index of this entry.
078         * @return start index
079         */
080        public int getStart() {
081            return start;
082        }
083
084        @Override
085        public String getElementName() {
086            return ELEM_LI;
087        }
088
089        @Override
090        public String getNamespace() {
091            return MarkupElement.NAMESPACE;
092        }
093
094        @Override
095        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
096            XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
097            xml.attribute(MarkupElement.MarkupChildElement.ATTR_START, getStart());
098            xml.closeEmptyElement();
099            return xml;
100        }
101    }
102}