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.NamedElement; 023import org.jivesoftware.smack.util.XmlStringBuilder; 024 025public class ListElement implements MarkupElement.MarkupChildElement { 026 027 public static final String ELEMENT = "list"; 028 public static final String ELEM_LI = "li"; 029 030 private final int start, end; 031 private final List<ListEntryElement> entries; 032 033 /** 034 * Create a new List element. 035 * 036 * @param start start index of the list 037 * @param end end index of the list 038 * @param entries list entries 039 */ 040 public ListElement(int start, int end, List<ListEntryElement> entries) { 041 this.start = start; 042 this.end = end; 043 this.entries = Collections.unmodifiableList(entries); 044 } 045 046 @Override 047 public int getStart() { 048 return start; 049 } 050 051 @Override 052 public int getEnd() { 053 return end; 054 } 055 056 /** 057 * Return a list of all list entries. 058 * 059 * @return entries 060 */ 061 public List<ListEntryElement> getEntries() { 062 return entries; 063 } 064 065 @Override 066 public String getElementName() { 067 return ELEMENT; 068 } 069 070 @Override 071 public CharSequence toXML(String enclosingNamespace) { 072 XmlStringBuilder xml = new XmlStringBuilder(); 073 xml.halfOpenElement(this); 074 xml.attribute(ATTR_START, getStart()); 075 xml.attribute(ATTR_END, getEnd()); 076 xml.rightAngleBracket(); 077 078 for (ListEntryElement li : getEntries()) { 079 xml.append(li.toXML(null)); 080 } 081 082 xml.closeElement(this); 083 return xml; 084 } 085 086 public static class ListEntryElement implements NamedElement { 087 088 private final int start; 089 090 /** 091 * Create a new ListEntry element. 092 * 093 * @param start start index 094 */ 095 public ListEntryElement(int start) { 096 this.start = start; 097 } 098 099 /** 100 * Return the start index of this entry. 101 * @return start index 102 */ 103 public int getStart() { 104 return start; 105 } 106 107 @Override 108 public String getElementName() { 109 return ELEM_LI; 110 } 111 112 @Override 113 public XmlStringBuilder toXML(String enclosingNamespace) { 114 XmlStringBuilder xml = new XmlStringBuilder(); 115 xml.halfOpenElement(this); 116 xml.attribute(ATTR_START, getStart()); 117 xml.closeEmptyElement(); 118 return xml; 119 } 120 } 121}