001/** 002 * 003 * Copyright 2014 Anno van Vliet 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.xdatalayout.packet; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.jivesoftware.smack.packet.NamedElement; 023import org.jivesoftware.smack.packet.ExtensionElement; 024import org.jivesoftware.smack.util.XmlStringBuilder; 025 026/** 027 * DataLayout Extension according to XEP-0141: Data Forms Layout. 028 * Defines a backwards-compatible extension to the XMPP Data Forms protocol that 029 * enables an application to specify form layouts, including the layout of 030 * form fields, sections within pages, and subsections within sections. 031 * 032 * @author Anno van Vliet 033 */ 034public class DataLayout implements ExtensionElement { 035 036 public static final String ELEMENT = "page"; 037 public static final String NAMESPACE = "http://jabber.org/protocol/xdata-layout"; 038 039 private final List<DataFormLayoutElement> pageLayout = new ArrayList<DataFormLayoutElement>(); 040 private final String label; 041 042 /** 043 * Data layout constructor. 044 * @param label 045 */ 046 public DataLayout(String label) { 047 this.label = label; 048 } 049 050 /** 051 * Gets the value of the pageLayout property. 052 * <p> 053 * Objects of the following type(s) are allowed in the list: {@link String }, 054 * {@link Section }, {@link Fieldref } and {@link Reportedref } 055 */ 056 public List<DataFormLayoutElement> getPageLayout() { 057 return this.pageLayout; 058 } 059 060 /** 061 * Gets the value of the label property. 062 * 063 * @return possible object is {@link String } 064 */ 065 public String getLabel() { 066 return label; 067 } 068 069 /* 070 * (non-Javadoc) 071 * @see org.jivesoftware.smack.packet.PacketExtension#getElementName() 072 */ 073 @Override 074 public String getElementName() { 075 return ELEMENT; 076 } 077 078 /* 079 * (non-Javadoc) 080 * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace() 081 */ 082 @Override 083 public String getNamespace() { 084 return NAMESPACE; 085 } 086 087 /* 088 * (non-Javadoc) 089 * @see org.jivesoftware.smack.packet.PacketExtension#toXML() 090 */ 091 @Override 092 public XmlStringBuilder toXML() { 093 XmlStringBuilder buf = new XmlStringBuilder(this); 094 buf.optAttribute("label", getLabel()); 095 buf.rightAngleBracket(); 096 097 walkList(buf, getPageLayout()); 098 099 buf.closeElement(this); 100 101 return buf; 102 } 103 104 /** 105 * @param buf 106 * @param pageLayout2 107 */ 108 private static void walkList(XmlStringBuilder buf, List<DataFormLayoutElement> pageLayout) { 109 for (DataFormLayoutElement object : pageLayout) { 110 buf.append(object.toXML()); 111 } 112 } 113 114 public static class Fieldref implements DataFormLayoutElement{ 115 116 public static final String ELEMENT = "fieldref"; 117 private final String var; 118 119 /** 120 * Field ref constructor. 121 * @param var reference to a field 122 */ 123 public Fieldref(String var) { 124 this.var = var; 125 } 126 127 @Override 128 public XmlStringBuilder toXML() { 129 XmlStringBuilder buf = new XmlStringBuilder(this); 130 buf.attribute("var", getVar()); 131 buf.closeEmptyElement(); 132 return buf; 133 } 134 135 /** 136 * Gets the value of the var property. 137 * 138 * @return possible object is {@link String } 139 */ 140 public String getVar() { 141 return var; 142 } 143 144 @Override 145 public String getElementName() { 146 return ELEMENT; 147 } 148 149 } 150 151 public static class Section implements DataFormLayoutElement{ 152 153 public static final String ELEMENT = "section"; 154 private final List<DataFormLayoutElement> sectionLayout = new ArrayList<DataFormLayoutElement>(); 155 private final String label; 156 157 /** 158 * Section constructor. 159 * @param label 160 */ 161 public Section(String label) { 162 this.label = label; 163 } 164 165 /** 166 * Gets the value of the sectionLayout property. 167 * <p> 168 * This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you 169 * make to the returned list will be present inside the object. This is why there is not a <CODE>set</CODE> 170 * method for the sectionLayout property. 171 * <p> 172 * For example, to add a new item, do as follows: 173 * 174 * <pre> 175 * getSectionLayout().add(newItem); 176 * </pre> 177 * <p> 178 * Objects of the following type(s) are allowed in the list: {@link String }, 179 * {@link Section }, {@link Fieldref } and {@link Reportedref } 180 */ 181 public List<DataFormLayoutElement> getSectionLayout() { 182 return this.sectionLayout; 183 } 184 185 @Override 186 public XmlStringBuilder toXML() { 187 XmlStringBuilder buf = new XmlStringBuilder(this); 188 buf.optAttribute("label", getLabel()); 189 buf.rightAngleBracket(); 190 191 walkList(buf, getSectionLayout()); 192 buf.closeElement(ELEMENT); 193 return buf; 194 } 195 196 /** 197 * Gets the value of the label property. 198 * 199 * @return possible object is {@link String } 200 */ 201 public String getLabel() { 202 return label; 203 } 204 205 @Override 206 public String getElementName() { 207 return ELEMENT; 208 } 209 210 } 211 212 public static class Reportedref implements DataFormLayoutElement{ 213 214 public static final String ELEMENT = "reportedref"; 215 216 @Override 217 public XmlStringBuilder toXML() { 218 XmlStringBuilder buf = new XmlStringBuilder(this); 219 buf.closeEmptyElement(); 220 return buf; 221 } 222 223 @Override 224 public String getElementName() { 225 return ELEMENT; 226 } 227 228 } 229 230 public static class Text implements DataFormLayoutElement{ 231 public static final String ELEMENT = "text"; 232 private final String text; 233 234 /** 235 * Text constructor. 236 * @param text reference to a field 237 */ 238 public Text(String text) { 239 this.text = text; 240 } 241 242 @Override 243 public XmlStringBuilder toXML() { 244 XmlStringBuilder buf = new XmlStringBuilder(); 245 buf.element(ELEMENT, getText()); 246 return buf; 247 } 248 249 /** 250 * Gets the value of the var property. 251 * 252 * @return possible object is {@link String } 253 */ 254 public String getText() { 255 return text; 256 } 257 258 @Override 259 public String getElementName() { 260 return ELEMENT; 261 } 262 263 } 264 265 public static interface DataFormLayoutElement extends NamedElement { 266 } 267 268}