2014-02-17 23:58:40 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* 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.pubsub;
|
|
|
|
|
2018-04-23 22:00:12 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.xml.namespace.QName;
|
|
|
|
|
2015-02-26 18:41:17 +01:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2018-04-23 22:00:12 +02:00
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
|
|
|
import org.jivesoftware.smack.util.StringUtils;
|
2019-05-06 22:06:13 +02:00
|
|
|
import org.jivesoftware.smack.xml.XmlPullParser;
|
|
|
|
import org.jivesoftware.smack.xml.XmlPullParserException;
|
2014-02-17 23:58:40 +01:00
|
|
|
|
|
|
|
/**
|
2018-05-09 23:06:12 +02:00
|
|
|
* The default payload representation for {@link PayloadItem#getPayload()}. It simply
|
2014-02-17 23:58:40 +01:00
|
|
|
* stores the XML payload as a string.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-02-17 23:58:40 +01:00
|
|
|
* @author Robin Collier
|
|
|
|
*/
|
2018-04-06 10:21:46 +02:00
|
|
|
public class SimplePayload implements ExtensionElement {
|
2017-02-07 22:02:40 +01:00
|
|
|
private final String elemName;
|
|
|
|
private final String ns;
|
2018-04-23 22:00:12 +02:00
|
|
|
private final String payload;
|
|
|
|
|
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Construct a <code>SimplePayload</code> object with the specified element name,
|
2018-04-23 22:00:12 +02:00
|
|
|
* namespace and content. The content must be well formed XML.
|
|
|
|
*
|
|
|
|
* @param xmlPayload The payload data
|
|
|
|
*/
|
|
|
|
public SimplePayload(String xmlPayload) {
|
|
|
|
XmlPullParser parser;
|
|
|
|
try {
|
|
|
|
parser = PacketParserUtils.getParserFor(xmlPayload);
|
|
|
|
}
|
|
|
|
catch (XmlPullParserException | IOException e) {
|
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2019-06-02 10:44:50 +02:00
|
|
|
QName qname = parser.getQName();
|
2018-04-23 22:00:12 +02:00
|
|
|
|
|
|
|
payload = xmlPayload;
|
|
|
|
|
2018-07-17 15:10:39 +02:00
|
|
|
elemName = StringUtils.requireNotNullNorEmpty(qname.getLocalPart(), "Could not determine element name from XML payload");
|
|
|
|
ns = StringUtils.requireNotNullNorEmpty(qname.getNamespaceURI(), "Could not determine namespace from XML payload");
|
2018-04-23 22:00:12 +02:00
|
|
|
}
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
/**
|
2019-07-19 18:10:36 +02:00
|
|
|
* Construct a <code>SimplePayload</code> object with the specified element name,
|
2017-02-07 22:02:40 +01:00
|
|
|
* namespace and content. The content must be well formed XML.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2017-02-07 22:02:40 +01:00
|
|
|
* @param elementName The root element name (of the payload)
|
|
|
|
* @param namespace The namespace of the payload, null if there is none
|
|
|
|
* @param xmlPayload The payload data
|
2018-04-23 22:00:12 +02:00
|
|
|
* @deprecated use {@link #SimplePayload(String)} insteas.
|
2017-02-07 22:02:40 +01:00
|
|
|
*/
|
2018-04-23 22:00:12 +02:00
|
|
|
// TODO: Remove in Smack 4.5
|
|
|
|
@Deprecated
|
2018-04-06 10:21:46 +02:00
|
|
|
public SimplePayload(String elementName, String namespace, CharSequence xmlPayload) {
|
2018-04-23 22:00:12 +02:00
|
|
|
this(xmlPayload.toString());
|
|
|
|
if (!elementName.equals(this.elemName)) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
if (!namespace.equals(this.ns)) {
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2018-04-06 10:21:46 +02:00
|
|
|
public String getElementName() {
|
2017-02-07 22:02:40 +01:00
|
|
|
return elemName;
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2018-04-06 10:21:46 +02:00
|
|
|
public String getNamespace() {
|
2017-02-07 22:02:40 +01:00
|
|
|
return ns;
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Override
|
2019-02-04 13:27:41 +01:00
|
|
|
public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
|
2017-02-07 22:02:40 +01:00
|
|
|
return payload;
|
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
|
2017-02-07 22:02:40 +01:00
|
|
|
@Override
|
2018-04-06 10:21:46 +02:00
|
|
|
public String toString() {
|
2019-02-04 13:27:41 +01:00
|
|
|
return getClass().getName() + "payload [" + toXML() + "]";
|
2017-02-07 22:02:40 +01:00
|
|
|
}
|
2014-02-17 23:58:40 +01:00
|
|
|
}
|