001/**
002 *
003 * Copyright the original author or authors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub;
018
019import java.io.IOException;
020
021import javax.xml.namespace.QName;
022
023import org.jivesoftware.smack.packet.ExtensionElement;
024import org.jivesoftware.smack.util.PacketParserUtils;
025import org.jivesoftware.smack.util.ParserUtils;
026import org.jivesoftware.smack.util.StringUtils;
027
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031/**
032 * The default payload representation for {@link PayloadItem#getPayload()}.  It simply
033 * stores the XML payload as a string.
034 *
035 * @author Robin Collier
036 */
037public class SimplePayload implements ExtensionElement {
038    private final String elemName;
039    private final String ns;
040    private final String payload;
041
042    /**
043     * Construct a <tt>SimplePayload</tt> object with the specified element name,
044     * namespace and content.  The content must be well formed XML.
045     *
046     * @param xmlPayload The payload data
047     */
048    public SimplePayload(String xmlPayload) {
049        XmlPullParser parser;
050        try {
051            parser = PacketParserUtils.getParserFor(xmlPayload);
052        }
053        catch (XmlPullParserException | IOException e) {
054            throw new AssertionError(e);
055        }
056        QName qname = ParserUtils.getQName(parser);
057
058        payload = xmlPayload;
059
060        elemName = StringUtils.requireNotNullOrEmpty(qname.getLocalPart(), "Could not determine element name from XML payload");
061        ns = StringUtils.requireNotNullOrEmpty(qname.getNamespaceURI(), "Could not determine namespace from XML payload");
062    }
063
064    /**
065     * Construct a <tt>SimplePayload</tt> object with the specified element name,
066     * namespace and content.  The content must be well formed XML.
067     *
068     * @param elementName The root element name (of the payload)
069     * @param namespace The namespace of the payload, null if there is none
070     * @param xmlPayload The payload data
071     * @deprecated use {@link #SimplePayload(String)} insteas.
072     */
073    // TODO: Remove in Smack 4.5
074    @Deprecated
075    public SimplePayload(String elementName, String namespace, CharSequence xmlPayload) {
076        this(xmlPayload.toString());
077        if (!elementName.equals(this.elemName)) {
078            throw new IllegalArgumentException();
079        }
080        if (!namespace.equals(this.ns)) {
081            throw new IllegalArgumentException();
082        }
083    }
084
085    @Override
086    public String getElementName() {
087        return elemName;
088    }
089
090    @Override
091    public String getNamespace() {
092        return ns;
093    }
094
095    @Override
096    public String toXML(String enclosingNamespace) {
097        return payload;
098    }
099
100    @Override
101    public String toString() {
102        return getClass().getName() + "payload [" + toXML(null) + "]";
103    }
104}