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    /*
073     * (non-Javadoc)
074     * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
075     */
076    @Override
077    public String getElementName() {
078        return ELEMENT;
079    }
080
081    /*
082     * (non-Javadoc)
083     * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
084     */
085    @Override
086    public String getNamespace() {
087        return NAMESPACE;
088    }
089
090    /*
091     * (non-Javadoc)
092     * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
093     */
094    @Override
095    public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
096        XmlStringBuilder buf = new XmlStringBuilder(this);
097        buf.optAttribute("label", getLabel());
098        buf.rightAngleBracket();
099
100        buf.append(getPageLayout());
101
102        buf.closeElement(this);
103
104        return buf;
105    }
106
107    public static class Fieldref extends DataFormLayoutElement{
108
109        public static final String ELEMENT = "fieldref";
110        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
111
112        private final String var;
113
114        /**
115         * Field ref constructor.
116         * @param var reference to a field
117         */
118        public Fieldref(String var) {
119            this.var = var;
120        }
121
122        @Override
123        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
124            XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
125            buf.attribute("var", getVar());
126            buf.closeEmptyElement();
127            return buf;
128        }
129
130        /**
131         * Gets the value of the var property.
132         *
133         * @return possible object is {@link String }
134         */
135        public String getVar() {
136            return var;
137        }
138
139        @Override
140        public String getElementName() {
141            return ELEMENT;
142        }
143
144    }
145
146    public static class Section extends DataFormLayoutElement{
147
148        public static final String ELEMENT = "section";
149        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
150        private final List<DataFormLayoutElement> sectionLayout = new ArrayList<>();
151        private final String label;
152
153        /**
154         * Section constructor.
155         * @param label TODO javadoc me please
156         */
157        public Section(String label) {
158            this.label = label;
159        }
160
161        /**
162         * Gets the value of the sectionLayout property.
163         * <p>
164         * This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you
165         * make to the returned list will be present inside the object. This is why there is not a <CODE>set</CODE>
166         * method for the sectionLayout property.
167         * <p>
168         * For example, to add a new item, do as follows:
169         *
170         * <pre>
171         * getSectionLayout().add(newItem);
172         * </pre>
173         * <p>
174         * Objects of the following type(s) are allowed in the list: {@link String },
175         * {@link Section }, {@link Fieldref } and {@link Reportedref }
176         *
177         * @return list of DataFormLayoutElements.
178         */
179        public List<DataFormLayoutElement> getSectionLayout() {
180            return this.sectionLayout;
181        }
182
183        @Override
184        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
185            XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
186            buf.optAttribute("label", getLabel());
187            buf.rightAngleBracket();
188
189            buf.append(getSectionLayout());
190
191            buf.closeElement(ELEMENT);
192            return buf;
193        }
194
195        /**
196         * Gets the value of the label property.
197         *
198         * @return possible object is {@link String }
199         */
200        public String getLabel() {
201            return label;
202        }
203
204        @Override
205        public String getElementName() {
206            return ELEMENT;
207        }
208
209    }
210
211    public static class Reportedref extends DataFormLayoutElement{
212
213        public static final String ELEMENT = "reportedref";
214        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
215
216        @Override
217        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
218            XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
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 extends DataFormLayoutElement{
231        public static final String ELEMENT = "text";
232        public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
233        private final String text;
234
235        /**
236         * Text constructor.
237         * @param text reference to a field
238         */
239        public Text(String text) {
240            this.text = text;
241        }
242
243        @Override
244        public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
245            XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
246            buf.rightAngleBracket();
247            buf.escape(getText());
248            buf.closeElement(this);
249            return buf;
250        }
251
252        /**
253         * Gets the value of the var property.
254         *
255         * @return possible object is {@link String }
256         */
257        public String getText() {
258            return text;
259        }
260
261        @Override
262        public String getElementName() {
263            return ELEMENT;
264        }
265
266    }
267
268    public abstract static class DataFormLayoutElement implements ExtensionElement {
269        @Override
270        public final String getNamespace() {
271            return NAMESPACE;
272        }
273    }
274
275}