mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-08 14:11:07 +01:00
Support for XEP-0141: Data Forms Layout.
Data Forms layouts are a part of Data Forms and implemented as extensions, added to a Dataform. Fixes issue SMACK-612.
This commit is contained in:
parent
6e569701b3
commit
ff977825da
8 changed files with 726 additions and 0 deletions
|
|
@ -23,6 +23,8 @@ import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
|||
import org.jivesoftware.smack.provider.RosterPacketProvider;
|
||||
import org.jivesoftware.smackx.xdata.FormField;
|
||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
||||
import org.jivesoftware.smackx.xdatalayout.provider.DataLayoutProvider;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
|
|
@ -64,6 +66,10 @@ public class DataFormProvider extends PacketExtensionProvider<DataForm> {
|
|||
else if (parser.getName().equals(RosterPacket.ELEMENT) && parser.getNamespace().equals(RosterPacket.NAMESPACE)) {
|
||||
dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
|
||||
}
|
||||
// See XEP-141 Data Forms Layout
|
||||
else if (parser.getName().equals(DataLayout.ELEMENT) && parser.getNamespace().equals(DataLayout.NAMESPACE)) {
|
||||
dataForm.addExtensionElement(DataLayoutProvider.INSTANCE.parse(parser));
|
||||
}
|
||||
} else if (eventType == XmlPullParser.END_TAG) {
|
||||
if (parser.getName().equals(dataForm.getElementName())) {
|
||||
done = true;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,260 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2014 Anno van Vliet
|
||||
*
|
||||
* 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 org.jivesoftware.smack.packet.NamedElement;
|
||||
import org.jivesoftware.smack.packet.PacketExtension;
|
||||
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 PacketExtension {
|
||||
|
||||
public static final String ELEMENT = "page";
|
||||
public static final String NAMESPACE = "http://jabber.org/protocol/xdata-layout";
|
||||
|
||||
private final List<DataFormLayoutElement> pageLayout = new ArrayList<DataFormLayoutElement>();
|
||||
private final String label;
|
||||
|
||||
/**
|
||||
* @param label
|
||||
*/
|
||||
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 }
|
||||
*/
|
||||
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() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder(this);
|
||||
buf.optAttribute("label", getLabel());
|
||||
buf.rightAngleBracket();
|
||||
|
||||
walkList(buf, getPageLayout());
|
||||
|
||||
buf.closeElement(this);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param buf
|
||||
* @param pageLayout2
|
||||
*/
|
||||
private static void walkList(XmlStringBuilder buf, List<DataFormLayoutElement> pageLayout) {
|
||||
for (DataFormLayoutElement object : pageLayout) {
|
||||
buf.append(object.toXML());
|
||||
}
|
||||
}
|
||||
|
||||
public static class Fieldref implements DataFormLayoutElement{
|
||||
|
||||
public static final String ELEMENT = "fieldref";
|
||||
private final String var;
|
||||
|
||||
/**
|
||||
* @param var reference to a field
|
||||
*/
|
||||
public Fieldref(String var) {
|
||||
this.var = var;
|
||||
}
|
||||
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder(this);
|
||||
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 implements DataFormLayoutElement{
|
||||
|
||||
public static final String ELEMENT = "section";
|
||||
private final List<DataFormLayoutElement> sectionLayout = new ArrayList<DataFormLayoutElement>();
|
||||
private final String label;
|
||||
|
||||
/**
|
||||
* @param label
|
||||
*/
|
||||
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 }
|
||||
*/
|
||||
public List<DataFormLayoutElement> getSectionLayout() {
|
||||
return this.sectionLayout;
|
||||
}
|
||||
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder(this);
|
||||
buf.optAttribute("label", getLabel());
|
||||
buf.rightAngleBracket();
|
||||
|
||||
walkList(buf, 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 implements DataFormLayoutElement{
|
||||
|
||||
public static final String ELEMENT = "reportedref";
|
||||
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder(this);
|
||||
buf.closeEmptyElement();
|
||||
return buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getElementName() {
|
||||
return ELEMENT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class Text implements DataFormLayoutElement{
|
||||
public static final String ELEMENT = "text";
|
||||
private final String text;
|
||||
|
||||
/**
|
||||
* @param text reference to a field
|
||||
*/
|
||||
public Text(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public XmlStringBuilder toXML() {
|
||||
XmlStringBuilder buf = new XmlStringBuilder();
|
||||
buf.element(ELEMENT, getText());
|
||||
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 static interface DataFormLayoutElement extends NamedElement {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2014 Anno van Vliet
|
||||
*
|
||||
* 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.provider;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.DataFormLayoutElement;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Reportedref;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
/**
|
||||
* Extension Provider for Page layout of forms.
|
||||
*
|
||||
* @author Anno van Vliet
|
||||
*
|
||||
*/
|
||||
public class DataLayoutProvider extends PacketExtensionProvider<DataLayout> {
|
||||
|
||||
public static final DataLayoutProvider INSTANCE = new DataLayoutProvider();
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.jivesoftware.smack.provider.Provider#parse(org.xmlpull.v1.XmlPullParser, int)
|
||||
*/
|
||||
@Override
|
||||
public DataLayout parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException,
|
||||
SmackException {
|
||||
DataLayout dataLayout = new DataLayout(parser.getAttributeValue("", "label"));
|
||||
parseLayout(dataLayout.getPageLayout(), parser);
|
||||
return dataLayout;
|
||||
}
|
||||
|
||||
private static Section parseSection(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
Section layout = new Section(parser.getAttributeValue("", "label"));
|
||||
parseLayout(layout.getSectionLayout(), parser);
|
||||
return layout;
|
||||
}
|
||||
|
||||
private static void parseLayout(List<DataFormLayoutElement> layout, XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
final int initialDepth = parser.getDepth();
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
switch (eventType) {
|
||||
case XmlPullParser.START_TAG:
|
||||
switch (parser.getName()) {
|
||||
case Text.ELEMENT:
|
||||
layout.add(new Text(parser.nextText()));
|
||||
break;
|
||||
case Section.ELEMENT:
|
||||
layout.add(parseSection(parser));
|
||||
break;
|
||||
case Fieldref.ELEMENT:
|
||||
layout.add(parseFieldref(parser));
|
||||
break;
|
||||
case Reportedref.ELEMENT:
|
||||
layout.add(new Reportedref());
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case XmlPullParser.END_TAG:
|
||||
if (parser.getDepth() == initialDepth) {
|
||||
break outerloop;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static Fieldref parseFieldref(XmlPullParser parser) throws XmlPullParserException, IOException {
|
||||
final int initialDepth = parser.getDepth();
|
||||
Fieldref fieldref = new Fieldref(parser.getAttributeValue("", "var"));
|
||||
outerloop: while (true) {
|
||||
int eventType = parser.next();
|
||||
if (eventType == XmlPullParser.END_TAG && parser.getDepth() == initialDepth) {
|
||||
break outerloop;
|
||||
}
|
||||
}
|
||||
return fieldref;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -481,4 +481,12 @@
|
|||
<namespace>http://jabber.org/protocol/rsm</namespace>
|
||||
<className>org.jivesoftware.smackx.rsm.provider.RSMSetProvider</className>
|
||||
</extensionProvider>
|
||||
|
||||
<!-- XEP-141: Data Forms Layout-->
|
||||
<extensionProvider>
|
||||
<elementName>page</elementName>
|
||||
<namespace>http://jabber.org/protocol/xdata-layout</namespace>
|
||||
<className>org.jivesoftware.smackx.xdatalayout.provider.DataLayoutProvider</className>
|
||||
</extensionProvider>
|
||||
|
||||
</smackProviders>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue