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