SimplePayload.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.pubsub;

  18. import java.io.IOException;

  19. import javax.xml.namespace.QName;

  20. import org.jivesoftware.smack.packet.XmlElement;
  21. import org.jivesoftware.smack.util.PacketParserUtils;
  22. import org.jivesoftware.smack.util.StringUtils;
  23. import org.jivesoftware.smack.xml.XmlPullParser;
  24. import org.jivesoftware.smack.xml.XmlPullParserException;

  25. /**
  26.  * The default payload representation for {@link PayloadItem#getPayload()}.  It simply
  27.  * stores the XML payload as a string.
  28.  *
  29.  * @author Robin Collier
  30.  */
  31. public class SimplePayload implements XmlElement {
  32.     private final String elemName;
  33.     private final String ns;
  34.     private final String payload;

  35.     /**
  36.      * Construct a <code>SimplePayload</code> object with the specified element name,
  37.      * namespace and content.  The content must be well formed XML.
  38.      *
  39.      * @param xmlPayload The payload data
  40.      */
  41.     public SimplePayload(String xmlPayload) {
  42.         XmlPullParser parser;
  43.         try {
  44.             parser = PacketParserUtils.getParserFor(xmlPayload);
  45.         }
  46.         catch (XmlPullParserException | IOException e) {
  47.             throw new AssertionError(e);
  48.         }
  49.         QName qname = parser.getQName();

  50.         payload = xmlPayload;

  51.         elemName = StringUtils.requireNotNullNorEmpty(qname.getLocalPart(), "Could not determine element name from XML payload");
  52.         ns = StringUtils.requireNotNullNorEmpty(qname.getNamespaceURI(), "Could not determine namespace from XML payload");
  53.     }

  54.     @Override
  55.     public String getElementName() {
  56.         return elemName;
  57.     }

  58.     @Override
  59.     public String getNamespace() {
  60.         return ns;
  61.     }

  62.     @Override
  63.     public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  64.         return payload;
  65.     }

  66.     @Override
  67.     public String toString() {
  68.         return getClass().getName() + "payload [" + toXML() + "]";
  69.     }
  70. }