ListElement.java

  1. /**
  2.  *
  3.  * Copyright © 2018 Paul Schaub
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.message_markup.element;

  18. import java.util.Collections;
  19. import java.util.List;

  20. import javax.xml.namespace.QName;

  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. public class ListElement extends MarkupElement.NonEmptyChildElement {

  24.     public static final String ELEMENT = "list";
  25.     public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  26.     private final List<ListEntryElement> entries;

  27.     /**
  28.      * Create a new List element.
  29.      *
  30.      * @param start start index of the list
  31.      * @param end end index of the list
  32.      * @param entries list entries
  33.      */
  34.     public ListElement(int start, int end, List<ListEntryElement> entries) {
  35.         super(start, end);
  36.         this.entries = Collections.unmodifiableList(entries);
  37.     }

  38.     /**
  39.      * Return a list of all list entries.
  40.      *
  41.      * @return entries TODO javadoc me please
  42.      */
  43.     public List<ListEntryElement> getEntries() {
  44.         return entries;
  45.     }

  46.     @Override
  47.     public String getElementName() {
  48.         return QNAME.getLocalPart();
  49.     }

  50.     @Override
  51.     public void appendInnerXml(XmlStringBuilder xml) {
  52.         xml.append(getEntries());
  53.     }

  54.     public static class ListEntryElement implements ExtensionElement {

  55.         public static final String ELEMENT = "li";

  56.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  57.         private final int start;

  58.         /**
  59.          * Create a new ListEntry element.
  60.          *
  61.          * @param start start index
  62.          */
  63.         public ListEntryElement(int start) {
  64.             this.start = start;
  65.         }

  66.         /**
  67.          * Return the start index of this entry.
  68.          * @return start index
  69.          */
  70.         public int getStart() {
  71.             return start;
  72.         }

  73.         @Override
  74.         public String getElementName() {
  75.             return QNAME.getLocalPart();
  76.         }

  77.         @Override
  78.         public String getNamespace() {
  79.             return QNAME.getNamespaceURI();
  80.         }

  81.         @Override
  82.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  83.             XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
  84.             xml.attribute(MarkupElement.MarkupChildElement.ATTR_START, getStart());
  85.             xml.closeEmptyElement();
  86.             return xml;
  87.         }
  88.     }
  89. }