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