mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 14:22:05 +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
|
@ -49,6 +49,7 @@ Smack Extensions and currently supported XEPs by Smack (smack-extensions)
|
||||||
| [SI File Transfer](filetransfer.html) | [XEP-0096](http://xmpp.org/extensions/xep-0096.html) | Transfer files between two users over XMPP. |
|
| [SI File Transfer](filetransfer.html) | [XEP-0096](http://xmpp.org/extensions/xep-0096.html) | Transfer files between two users over XMPP. |
|
||||||
| [Entity Capabilities](caps.html) | [XEP-0115](http://xmpp.org/extensions/xep-0115.html) | Broadcasting and dynamic discovery of entity capabilities. |
|
| [Entity Capabilities](caps.html) | [XEP-0115](http://xmpp.org/extensions/xep-0115.html) | Broadcasting and dynamic discovery of entity capabilities. |
|
||||||
| Stream Compression | [XEP-0138](http://xmpp.org/extensions/xep-0138.html) | Support for optional compression of the XMPP stream.
|
| Stream Compression | [XEP-0138](http://xmpp.org/extensions/xep-0138.html) | Support for optional compression of the XMPP stream.
|
||||||
|
| Data Forms Layout | [XEP-0141](http://xmpp.org/extensions/xep-0141.html) | Enables an application to specify form layouts. |
|
||||||
| Personal Eventing Protocol | [XEP-0163](http://xmpp.org/extensions/xep-0163.html) | Using the XMPP publish-subscribe protocol to broadcast state change events associated with a XMPP account. |
|
| Personal Eventing Protocol | [XEP-0163](http://xmpp.org/extensions/xep-0163.html) | Using the XMPP publish-subscribe protocol to broadcast state change events associated with a XMPP account. |
|
||||||
| Message Delivery Receipts | [XEP-0184](http://xmpp.org/extensions/xep-0184.html) | Extension for message delivery receipts. The sender can request notification that the message has been delivered. |
|
| Message Delivery Receipts | [XEP-0184](http://xmpp.org/extensions/xep-0184.html) | Extension for message delivery receipts. The sender can request notification that the message has been delivered. |
|
||||||
| XMPP Ping | [XEP-0199](http://xmpp.org/extensions/xep-0199.html) | Sending application-level pings over XML streams.
|
| XMPP Ping | [XEP-0199](http://xmpp.org/extensions/xep-0199.html) | Sending application-level pings over XML streams.
|
||||||
|
|
|
@ -23,6 +23,8 @@ import org.jivesoftware.smack.provider.PacketExtensionProvider;
|
||||||
import org.jivesoftware.smack.provider.RosterPacketProvider;
|
import org.jivesoftware.smack.provider.RosterPacketProvider;
|
||||||
import org.jivesoftware.smackx.xdata.FormField;
|
import org.jivesoftware.smackx.xdata.FormField;
|
||||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
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.XmlPullParser;
|
||||||
import org.xmlpull.v1.XmlPullParserException;
|
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)) {
|
else if (parser.getName().equals(RosterPacket.ELEMENT) && parser.getNamespace().equals(RosterPacket.NAMESPACE)) {
|
||||||
dataForm.addExtensionElement(RosterPacketProvider.INSTANCE.parse(parser));
|
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) {
|
} else if (eventType == XmlPullParser.END_TAG) {
|
||||||
if (parser.getName().equals(dataForm.getElementName())) {
|
if (parser.getName().equals(dataForm.getElementName())) {
|
||||||
done = true;
|
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>
|
<namespace>http://jabber.org/protocol/rsm</namespace>
|
||||||
<className>org.jivesoftware.smackx.rsm.provider.RSMSetProvider</className>
|
<className>org.jivesoftware.smackx.rsm.provider.RSMSetProvider</className>
|
||||||
</extensionProvider>
|
</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>
|
</smackProviders>
|
||||||
|
|
|
@ -0,0 +1,146 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* 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.xdata.packet;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.packet.Element;
|
||||||
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
import org.jivesoftware.smackx.xdata.FormField;
|
||||||
|
import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
|
||||||
|
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
||||||
|
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref;
|
||||||
|
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section;
|
||||||
|
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for DataForm reading and parsing.
|
||||||
|
*
|
||||||
|
* @author Anno van Vliet
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DataFormTest {
|
||||||
|
private static final String TEST_OUTPUT_1 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'></field></x>";
|
||||||
|
private static final String TEST_OUTPUT_2 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'></field><page xmlns='http://jabber.org/protocol/xdata-layout' label='Label'><fieldref var='testField1'/><section label='section Label'><text>SectionText</text></section><text>PageText</text></page></x>";
|
||||||
|
private static Logger logger = Logger.getLogger(DataFormTest.class.getName());
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws XmlPullParserException, IOException, SmackException {
|
||||||
|
|
||||||
|
//Build a Form
|
||||||
|
DataForm df = new DataForm("SUBMIT");
|
||||||
|
String instruction = "InstructionTest1";
|
||||||
|
df.addInstruction(instruction);
|
||||||
|
FormField field = new FormField("testField1");
|
||||||
|
df.addField(field);
|
||||||
|
|
||||||
|
assertNotNull( df.toXML());
|
||||||
|
String output = df.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_1, output);
|
||||||
|
|
||||||
|
DataFormProvider pr = new DataFormProvider();
|
||||||
|
|
||||||
|
XmlPullParser parser = getParser(output);
|
||||||
|
|
||||||
|
df = pr.parse(parser);
|
||||||
|
|
||||||
|
assertNotNull(df);
|
||||||
|
assertNotNull(df.getFields());
|
||||||
|
assertEquals(1 , df.getFields().size() );
|
||||||
|
assertEquals(1 , df.getInstructions().size());
|
||||||
|
|
||||||
|
assertNotNull( df.toXML());
|
||||||
|
output = df.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_1, output);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLayout() throws XmlPullParserException, IOException, SmackException {
|
||||||
|
|
||||||
|
//Build a Form
|
||||||
|
DataForm df = new DataForm("SUBMIT");
|
||||||
|
String instruction = "InstructionTest1";
|
||||||
|
df.addInstruction(instruction);
|
||||||
|
FormField field = new FormField("testField1");
|
||||||
|
df.addField(field);
|
||||||
|
|
||||||
|
DataLayout layout = new DataLayout("Label");
|
||||||
|
Fieldref reffield = new Fieldref("testField1");
|
||||||
|
layout.getPageLayout().add(reffield);
|
||||||
|
Section section = new Section("section Label");
|
||||||
|
section.getSectionLayout().add(new Text("SectionText"));
|
||||||
|
layout.getPageLayout().add(section);
|
||||||
|
layout.getPageLayout().add(new Text("PageText"));
|
||||||
|
|
||||||
|
df.addExtensionElement(layout);
|
||||||
|
|
||||||
|
|
||||||
|
assertNotNull( df.toXML());
|
||||||
|
String output = df.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_2, output);
|
||||||
|
|
||||||
|
DataFormProvider pr = new DataFormProvider();
|
||||||
|
|
||||||
|
XmlPullParser parser = getParser(output);
|
||||||
|
|
||||||
|
df = pr.parse(parser);
|
||||||
|
|
||||||
|
assertNotNull(df);
|
||||||
|
assertNotNull(df.getExtensionElements());
|
||||||
|
assertEquals(1 , df.getExtensionElements().size() );
|
||||||
|
Element element = df.getExtensionElements().get(0);
|
||||||
|
assertNotNull(element);
|
||||||
|
layout = (DataLayout) element;
|
||||||
|
|
||||||
|
assertEquals(3 , layout.getPageLayout().size());
|
||||||
|
|
||||||
|
assertNotNull( df.toXML());
|
||||||
|
output = df.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_2, output);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param output
|
||||||
|
* @return
|
||||||
|
* @throws XmlPullParserException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private XmlPullParser getParser(String output) throws XmlPullParserException, IOException {
|
||||||
|
logger.finest("getParser");
|
||||||
|
XmlPullParser parser = PacketParserUtils.newXmppParser();
|
||||||
|
parser.setInput(new StringReader(output));
|
||||||
|
parser.next();
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,185 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Copyright 2014 Anno van Vliet, All rights reserved.
|
||||||
|
*
|
||||||
|
* 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 static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
import org.jivesoftware.smack.SmackException;
|
||||||
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||||
|
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||||
|
import org.jivesoftware.smackx.xdata.provider.DataFormProvider;
|
||||||
|
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.jivesoftware.smackx.xdatalayout.provider.DataLayoutProvider;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for DataForm reading and parsing.
|
||||||
|
*
|
||||||
|
* @author Anno van Vliet
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class DataLayoutTest {
|
||||||
|
private static final String TEST_OUTPUT_2 = "<page xmlns='http://jabber.org/protocol/xdata-layout' label='Label'><fieldref var='testField1'/><section label='section Label'><text>SectionText</text></section><text>PageText</text></page>";
|
||||||
|
private static final String TEST_OUTPUT_SPECIAL = "<page xmlns='http://jabber.org/protocol/xdata-layout' label='Label - & \u00E9 \u00E1 '><fieldref var='testField1'/><section label='section Label - & \u00E9 \u00E1 '><text>SectionText - & \u00E9 \u00E1 </text></section><text>PageText - & \u00E9 \u00E1 </text><section label='<html>Number of Persons by<br/> Nationality and Status</html>'><reportedref/></section><text><html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html></text></page>";
|
||||||
|
private static final String TEST_INPUT_1 = "xdata-layout-sample.xml";
|
||||||
|
private static Logger logger = Logger.getLogger(DataLayoutTest.class.getName());
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLayout() throws XmlPullParserException, IOException, SmackException {
|
||||||
|
|
||||||
|
DataLayout layout = new DataLayout("Label");
|
||||||
|
Fieldref reffield = new Fieldref("testField1");
|
||||||
|
layout.getPageLayout().add(reffield);
|
||||||
|
Section section = new Section("section Label");
|
||||||
|
section.getSectionLayout().add(new Text("SectionText"));
|
||||||
|
layout.getPageLayout().add(section);
|
||||||
|
layout.getPageLayout().add(new Text( "PageText"));
|
||||||
|
|
||||||
|
assertNotNull( layout.toXML());
|
||||||
|
String output = layout.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_2, output);
|
||||||
|
|
||||||
|
DataLayoutProvider pr = DataLayoutProvider.INSTANCE;
|
||||||
|
|
||||||
|
XmlPullParser parser = getParser(output);
|
||||||
|
|
||||||
|
layout = pr.parse(parser);
|
||||||
|
|
||||||
|
assertEquals(3 , layout.getPageLayout().size());
|
||||||
|
assertEquals("Label", layout.getLabel());
|
||||||
|
|
||||||
|
assertNotNull( layout.toXML());
|
||||||
|
output = layout.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_2, output);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLayoutSpecialCharacters() throws XmlPullParserException, IOException, SmackException {
|
||||||
|
|
||||||
|
DataLayout layout = new DataLayout("Label - & \u00E9 \u00E1 ");
|
||||||
|
Fieldref reffield = new Fieldref("testField1");
|
||||||
|
layout.getPageLayout().add(reffield);
|
||||||
|
Section section = new Section("section Label - & \u00E9 \u00E1 ");
|
||||||
|
section.getSectionLayout().add(new Text( "SectionText - & \u00E9 \u00E1 "));
|
||||||
|
layout.getPageLayout().add(section);
|
||||||
|
layout.getPageLayout().add(new Text( "PageText - & \u00E9 \u00E1 "));
|
||||||
|
|
||||||
|
section = new Section("<html>Number of Persons by<br/> Nationality and Status</html>");
|
||||||
|
section.getSectionLayout().add(new Reportedref());
|
||||||
|
layout.getPageLayout().add(section);
|
||||||
|
|
||||||
|
layout.getPageLayout().add(new Text( "<html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html>"));
|
||||||
|
|
||||||
|
|
||||||
|
assertNotNull( layout.toXML());
|
||||||
|
String output = layout.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_SPECIAL, output);
|
||||||
|
|
||||||
|
DataLayoutProvider pr = DataLayoutProvider.INSTANCE;
|
||||||
|
|
||||||
|
XmlPullParser parser = getParser(output);
|
||||||
|
|
||||||
|
layout = pr.parse(parser);
|
||||||
|
|
||||||
|
assertEquals(5 , layout.getPageLayout().size());
|
||||||
|
assertEquals("Label - & \u00E9 \u00E1 ", layout.getLabel());
|
||||||
|
section = (Section) layout.getPageLayout().get(1);
|
||||||
|
assertEquals("section Label - & \u00E9 \u00E1 ", section.getLabel());
|
||||||
|
Text text = (Text)layout.getPageLayout().get(2);
|
||||||
|
assertEquals("PageText - & \u00E9 \u00E1 ", text.getText());
|
||||||
|
section = (Section) layout.getPageLayout().get(3);
|
||||||
|
assertEquals("<html>Number of Persons by<br/> Nationality and Status</html>", section.getLabel());
|
||||||
|
text = (Text) layout.getPageLayout().get(4);
|
||||||
|
assertEquals("<html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html>", text.getText());
|
||||||
|
|
||||||
|
|
||||||
|
assertNotNull( layout.toXML());
|
||||||
|
output = layout.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_SPECIAL, output);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLayoutFromFile() throws XmlPullParserException, IOException, SmackException {
|
||||||
|
|
||||||
|
|
||||||
|
DataFormProvider pr = new DataFormProvider();
|
||||||
|
|
||||||
|
XmlPullParser parser = PacketParserUtils.newXmppParser();
|
||||||
|
parser.setInput(new InputStreamReader(this.getClass().getResourceAsStream(TEST_INPUT_1), "UTF-8"));
|
||||||
|
parser.next();
|
||||||
|
|
||||||
|
DataForm form = pr.parse(parser);
|
||||||
|
assertNotNull( form);
|
||||||
|
assertEquals(1 , form.getExtensionElements().size());
|
||||||
|
|
||||||
|
DataLayout layout = (DataLayout) form.getExtensionElements().get(0);
|
||||||
|
|
||||||
|
assertEquals(5 , layout.getPageLayout().size());
|
||||||
|
assertEquals("Label - & \u00E9 \u00E1 ", layout.getLabel());
|
||||||
|
Section section = (Section) layout.getPageLayout().get(1);
|
||||||
|
assertEquals("section Label - & \u00E9 \u00E1 ", section.getLabel());
|
||||||
|
Text text = (Text)layout.getPageLayout().get(2);
|
||||||
|
assertEquals("PageText - & \u00E9 \u00E1 ", text.getText());
|
||||||
|
section = (Section) layout.getPageLayout().get(3);
|
||||||
|
assertEquals("<html>Number of Persons by<br/> Nationality and Status</html>", section.getLabel());
|
||||||
|
text = (Text) layout.getPageLayout().get(4);
|
||||||
|
assertEquals("<html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html>", text.getText());
|
||||||
|
|
||||||
|
|
||||||
|
assertNotNull( layout.toXML());
|
||||||
|
String output = layout.toXML().toString();
|
||||||
|
logger.finest(output);
|
||||||
|
assertEquals(TEST_OUTPUT_SPECIAL, output);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param output
|
||||||
|
* @return
|
||||||
|
* @throws XmlPullParserException
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
private XmlPullParser getParser(String output) throws XmlPullParserException, IOException {
|
||||||
|
logger.finest("getParser");
|
||||||
|
XmlPullParser parser = PacketParserUtils.newXmppParser();
|
||||||
|
parser.setInput(new StringReader(output));
|
||||||
|
parser.next();
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<x xmlns='jabber:x:data' type='form'>
|
||||||
|
<page xmlns='http://jabber.org/protocol/xdata-layout' label='Label - & é á '>
|
||||||
|
<fieldref var='testField1' />
|
||||||
|
<section label='section Label - & é á '>
|
||||||
|
<text>SectionText - & é á </text>
|
||||||
|
</section>
|
||||||
|
<text>PageText - & é á </text>
|
||||||
|
<section
|
||||||
|
label='<html>Number of Persons by<br/> Nationality and Status</html>'>
|
||||||
|
<reportedref />
|
||||||
|
</section>
|
||||||
|
<text><![CDATA[<html><font color='red'><em>DO NOT DELAY</em></font><br/>supply further information</html>]]></text>
|
||||||
|
</page>
|
||||||
|
</x>
|
Loading…
Reference in a new issue