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