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     * @param label
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    public List<DataFormLayoutElement> getPageLayout() {
056        return this.pageLayout;
057    }
058
059    /**
060     * Gets the value of the label property.
061     * 
062     * @return possible object is {@link String }
063     */
064    public String getLabel() {
065        return label;
066    }
067
068    /*
069     * (non-Javadoc)
070     * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
071     */
072    @Override
073    public String getElementName() {
074        return ELEMENT;
075    }
076
077    /*
078     * (non-Javadoc)
079     * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
080     */
081    @Override
082    public String getNamespace() {
083        return NAMESPACE;
084    }
085
086    /*
087     * (non-Javadoc)
088     * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
089     */
090    @Override
091    public XmlStringBuilder toXML() {
092        XmlStringBuilder buf = new XmlStringBuilder(this);
093        buf.optAttribute("label", getLabel());
094        buf.rightAngleBracket();
095
096        walkList(buf, getPageLayout());
097
098        buf.closeElement(this);
099
100        return buf;
101    }
102
103    /**
104     * @param buf
105     * @param pageLayout2
106     */
107    private static void walkList(XmlStringBuilder buf, List<DataFormLayoutElement> pageLayout) {
108        for (DataFormLayoutElement object : pageLayout) {
109            buf.append(object.toXML());
110        }
111    }
112
113    public static class Fieldref implements DataFormLayoutElement{
114
115        public static final String ELEMENT = "fieldref";
116        private final String var;
117
118        /**
119         * @param var reference to a field
120         */
121        public Fieldref(String var) {
122            this.var = var;
123        }
124
125        public XmlStringBuilder toXML() {
126            XmlStringBuilder buf = new XmlStringBuilder(this);
127            buf.attribute("var", getVar());
128            buf.closeEmptyElement();
129            return buf;
130        }
131
132        /**
133         * Gets the value of the var property.
134         * 
135         * @return possible object is {@link String }
136         */
137        public String getVar() {
138            return var;
139        }
140
141        @Override
142        public String getElementName() {
143            return ELEMENT;
144        }
145
146    }
147
148    public static class Section implements DataFormLayoutElement{
149
150        public static final String ELEMENT = "section";
151        private final List<DataFormLayoutElement> sectionLayout = new ArrayList<DataFormLayoutElement>();
152        private final String label;
153
154        /**
155         * @param label
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        public List<DataFormLayoutElement> getSectionLayout() {
178            return this.sectionLayout;
179        }
180
181        public XmlStringBuilder toXML() {
182            XmlStringBuilder buf = new XmlStringBuilder(this);
183            buf.optAttribute("label", getLabel());
184            buf.rightAngleBracket();
185
186            walkList(buf, getSectionLayout());
187            buf.closeElement(ELEMENT);
188            return buf;
189        }
190
191        /**
192         * Gets the value of the label property.
193         * 
194         * @return possible object is {@link String }
195         */
196        public String getLabel() {
197            return label;
198        }
199
200        @Override
201        public String getElementName() {
202            return ELEMENT;
203        }
204
205    }
206
207    public static class Reportedref implements DataFormLayoutElement{
208
209        public static final String ELEMENT = "reportedref";
210
211        public XmlStringBuilder toXML() {
212            XmlStringBuilder buf = new XmlStringBuilder(this);
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 implements DataFormLayoutElement{
225        public static final String ELEMENT = "text";
226        private final String text;
227
228        /**
229         * @param text reference to a field
230         */
231        public Text(String text) {
232            this.text = text;
233        }
234
235        public XmlStringBuilder toXML() {
236            XmlStringBuilder buf = new XmlStringBuilder();
237            buf.element(ELEMENT, getText());
238            return buf;
239        }
240
241        /**
242         * Gets the value of the var property.
243         * 
244         * @return possible object is {@link String }
245         */
246        public String getText() {
247            return text;
248        }
249
250        @Override
251        public String getElementName() {
252            return ELEMENT;
253        }
254
255    }
256
257    public static interface DataFormLayoutElement extends NamedElement {
258    }
259
260}