1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-06-26 13:24:49 +02:00
Smack/smack-extensions/src/main/java/org/jivesoftware/smackx/xdatalayout/packet/DataLayout.java
Florian Schmaus 3d4e7938a7 Make ExtensionElement marker interface wrt. QNAME field
ExtensionElement is now a marker interface that requires all
implementation non-abstract classes to carry a static final QNAME
field (of type QName). This is verified by a new unit test.

Also FullyQualifiedElement is renamed to simply XmlElement. XmlElement
is used over ExtensionElement when implementing classes do not
statically know the qualified name of the XML elements they
represent. In general, XmlElement should be used sparingly, and every
XML element should be modeled by its own Java class (implementing
ExtensionElement).
2021-04-18 21:07:19 +02:00

276 lines
7.9 KiB
Java

/**
*
* Copyright 2014 Anno van Vliet, 2019-2021 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smackx.xdatalayout.packet;
import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import org.jivesoftware.smack.packet.ExtensionElement;
import org.jivesoftware.smack.util.XmlStringBuilder;
/**
* DataLayout Extension according to XEP-0141: Data Forms Layout.
* Defines a backwards-compatible extension to the XMPP Data Forms protocol that
* enables an application to specify form layouts, including the layout of
* form fields, sections within pages, and subsections within sections.
*
* @author Anno van Vliet
*/
public class DataLayout implements ExtensionElement {
public static final String ELEMENT = "page";
public static final String NAMESPACE = "http://jabber.org/protocol/xdata-layout";
private final List<DataFormLayoutElement> pageLayout = new ArrayList<>();
private final String label;
/**
* Data layout constructor.
* @param label TODO javadoc me please
*/
public DataLayout(String label) {
this.label = label;
}
/**
* Gets the value of the pageLayout property.
* <p>
* Objects of the following type(s) are allowed in the list: {@link String },
* {@link Section }, {@link Fieldref } and {@link Reportedref }
*
* @return list of DataFormLayoutElements.
*/
public List<DataFormLayoutElement> getPageLayout() {
return this.pageLayout;
}
/**
* Gets the value of the label property.
*
* @return possible object is {@link String }
*/
public String getLabel() {
return label;
}
/*
* (non-Javadoc)
* @see org.jivesoftware.smack.packet.PacketExtension#getElementName()
*/
@Override
public String getElementName() {
return ELEMENT;
}
/*
* (non-Javadoc)
* @see org.jivesoftware.smack.packet.PacketExtension#getNamespace()
*/
@Override
public String getNamespace() {
return NAMESPACE;
}
/*
* (non-Javadoc)
* @see org.jivesoftware.smack.packet.PacketExtension#toXML()
*/
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this);
buf.optAttribute("label", getLabel());
buf.rightAngleBracket();
buf.append(getPageLayout());
buf.closeElement(this);
return buf;
}
public static class Fieldref extends DataFormLayoutElement{
public static final String ELEMENT = "fieldref";
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
private final String var;
/**
* Field ref constructor.
* @param var reference to a field
*/
public Fieldref(String var) {
this.var = var;
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.attribute("var", getVar());
buf.closeEmptyElement();
return buf;
}
/**
* Gets the value of the var property.
*
* @return possible object is {@link String }
*/
public String getVar() {
return var;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class Section extends DataFormLayoutElement{
public static final String ELEMENT = "section";
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
private final List<DataFormLayoutElement> sectionLayout = new ArrayList<>();
private final String label;
/**
* Section constructor.
* @param label TODO javadoc me please
*/
public Section(String label) {
this.label = label;
}
/**
* Gets the value of the sectionLayout property.
* <p>
* This accessor method returns a reference to the live list, not a snapshot. Therefore any modification you
* make to the returned list will be present inside the object. This is why there is not a <CODE>set</CODE>
* method for the sectionLayout property.
* <p>
* For example, to add a new item, do as follows:
*
* <pre>
* getSectionLayout().add(newItem);
* </pre>
* <p>
* Objects of the following type(s) are allowed in the list: {@link String },
* {@link Section }, {@link Fieldref } and {@link Reportedref }
*
* @return list of DataFormLayoutElements.
*/
public List<DataFormLayoutElement> getSectionLayout() {
return this.sectionLayout;
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.optAttribute("label", getLabel());
buf.rightAngleBracket();
buf.append(getSectionLayout());
buf.closeElement(ELEMENT);
return buf;
}
/**
* Gets the value of the label property.
*
* @return possible object is {@link String }
*/
public String getLabel() {
return label;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class Reportedref extends DataFormLayoutElement{
public static final String ELEMENT = "reportedref";
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.closeEmptyElement();
return buf;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public static class Text extends DataFormLayoutElement{
public static final String ELEMENT = "text";
public static final QName QNAME = new QName(NAMESPACE, ELEMENT);
private final String text;
/**
* Text constructor.
* @param text reference to a field
*/
public Text(String text) {
this.text = text;
}
@Override
public XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
XmlStringBuilder buf = new XmlStringBuilder(this, enclosingNamespace);
buf.rightAngleBracket();
buf.escape(getText());
buf.closeElement(this);
return buf;
}
/**
* Gets the value of the var property.
*
* @return possible object is {@link String }
*/
public String getText() {
return text;
}
@Override
public String getElementName() {
return ELEMENT;
}
}
public abstract static class DataFormLayoutElement implements ExtensionElement {
@Override
public final String getNamespace() {
return NAMESPACE;
}
}
}