DataLayout.java

  1. /**
  2.  *
  3.  * Copyright 2014 Anno van Vliet, 2019-2021 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdatalayout.packet;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import javax.xml.namespace.QName;

  21. import org.jivesoftware.smack.packet.ExtensionElement;
  22. import org.jivesoftware.smack.util.XmlStringBuilder;

  23. /**
  24.  * DataLayout Extension according to XEP-0141: Data Forms Layout.
  25.  * Defines a backwards-compatible extension to the XMPP Data Forms protocol that
  26.  * enables an application to specify form layouts, including the layout of
  27.  * form fields, sections within pages, and subsections within sections.
  28.  *
  29.  * @author Anno van Vliet
  30.  */
  31. public class DataLayout implements ExtensionElement {

  32.     public static final String ELEMENT = "page";
  33.     public static final String NAMESPACE = "http://jabber.org/protocol/xdata-layout";

  34.     private final List<DataFormLayoutElement> pageLayout = new ArrayList<>();
  35.     private final String label;

  36.     /**
  37.      * Data layout constructor.
  38.      * @param label TODO javadoc me please
  39.      */
  40.     public DataLayout(String label) {
  41.         this.label = label;
  42.     }

  43.     /**
  44.      * Gets the value of the pageLayout property.
  45.      * <p>
  46.      * Objects of the following type(s) are allowed in the list: {@link String },
  47.      * {@link Section }, {@link Fieldref } and {@link Reportedref }
  48.      *
  49.      * @return list of DataFormLayoutElements.
  50.      */
  51.     public List<DataFormLayoutElement> getPageLayout() {
  52.         return this.pageLayout;
  53.     }

  54.     /**
  55.      * Gets the value of the label property.
  56.      *
  57.      * @return possible object is {@link String }
  58.      */
  59.     public String getLabel() {
  60.         return label;
  61.     }

  62.     /*
  63.      * (non-Javadoc)
  64.      * @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
  65.      */
  66.     @Override
  67.     public String getElementName() {
  68.         return ELEMENT;
  69.     }

  70.     /*
  71.      * (non-Javadoc)
  72.      * @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
  73.      */
  74.     @Override
  75.     public String getNamespace() {
  76.         return NAMESPACE;
  77.     }

  78.     /*
  79.      * (non-Javadoc)
  80.      * @see org.jivesoftware.smack.packet.PacketExtension#toXML()
  81.      */
  82.     @Override
  83.     public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  84.         XmlStringBuilder buf = new XmlStringBuilder(this);
  85.         buf.optAttribute("label", getLabel());
  86.         buf.rightAngleBracket();

  87.         buf.append(getPageLayout());

  88.         buf.closeElement(this);

  89.         return buf;
  90.     }

  91.     public static class Fieldref extends DataFormLayoutElement{

  92.         public static final String ELEMENT = "fieldref";
  93.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  94.         private final String var;

  95.         /**
  96.          * Field ref constructor.
  97.          * @param var reference to a field
  98.          */
  99.         public Fieldref(String var) {
  100.             this.var = var;
  101.         }

  102.         @Override
  103.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  104.             XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
  105.             buf.attribute("var", getVar());
  106.             buf.closeEmptyElement();
  107.             return buf;
  108.         }

  109.         /**
  110.          * Gets the value of the var property.
  111.          *
  112.          * @return possible object is {@link String }
  113.          */
  114.         public String getVar() {
  115.             return var;
  116.         }

  117.         @Override
  118.         public String getElementName() {
  119.             return ELEMENT;
  120.         }

  121.     }

  122.     public static class Section extends DataFormLayoutElement{

  123.         public static final String ELEMENT = "section";
  124.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
  125.         private final List<DataFormLayoutElement> sectionLayout = new ArrayList<>();
  126.         private final String label;

  127.         /**
  128.          * Section constructor.
  129.          * @param label TODO javadoc me please
  130.          */
  131.         public Section(String label) {
  132.             this.label = label;
  133.         }

  134.         /**
  135.          * Gets the value of the sectionLayout property.
  136.          * <p>
  137.          * This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you
  138.          * make to the returned list will be present inside the object. This is why there is not a <CODE>set</CODE>
  139.          * method for the sectionLayout property.
  140.          * <p>
  141.          * For example, to add a new item, do as follows:
  142.          *
  143.          * <pre>
  144.          * getSectionLayout().add(newItem);
  145.          * </pre>
  146.          * <p>
  147.          * Objects of the following type(s) are allowed in the list: {@link String },
  148.          * {@link Section }, {@link Fieldref } and {@link Reportedref }
  149.          *
  150.          * @return list of DataFormLayoutElements.
  151.          */
  152.         public List<DataFormLayoutElement> getSectionLayout() {
  153.             return this.sectionLayout;
  154.         }

  155.         @Override
  156.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  157.             XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
  158.             buf.optAttribute("label", getLabel());
  159.             buf.rightAngleBracket();

  160.             buf.append(getSectionLayout());

  161.             buf.closeElement(ELEMENT);
  162.             return buf;
  163.         }

  164.         /**
  165.          * Gets the value of the label property.
  166.          *
  167.          * @return possible object is {@link String }
  168.          */
  169.         public String getLabel() {
  170.             return label;
  171.         }

  172.         @Override
  173.         public String getElementName() {
  174.             return ELEMENT;
  175.         }

  176.     }

  177.     public static class Reportedref extends DataFormLayoutElement{

  178.         public static final String ELEMENT = "reportedref";
  179.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);

  180.         @Override
  181.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  182.             XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
  183.             buf.closeEmptyElement();
  184.             return buf;
  185.         }

  186.         @Override
  187.         public String getElementName() {
  188.             return ELEMENT;
  189.         }

  190.     }

  191.     public static class Text extends DataFormLayoutElement{
  192.         public static final String ELEMENT = "text";
  193.         public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
  194.         private final String text;

  195.         /**
  196.          * Text constructor.
  197.          * @param text reference to a field
  198.          */
  199.         public Text(String text) {
  200.             this.text = text;
  201.         }

  202.         @Override
  203.         public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  204.             XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
  205.             buf.rightAngleBracket();
  206.             buf.escape(getText());
  207.             buf.closeElement(this);
  208.             return buf;
  209.         }

  210.         /**
  211.          * Gets the value of the var property.
  212.          *
  213.          * @return possible object is {@link String }
  214.          */
  215.         public String getText() {
  216.             return text;
  217.         }

  218.         @Override
  219.         public String getElementName() {
  220.             return ELEMENT;
  221.         }

  222.     }

  223.     public abstract static class DataFormLayoutElement implements ExtensionElement {
  224.         @Override
  225.         public final String getNamespace() {
  226.             return NAMESPACE;
  227.         }
  228.     }

  229. }