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.XmlElement; 024import org.jivesoftware.smack.util.PacketParserUtils; 025import org.jivesoftware.smack.util.StringUtils; 026import org.jivesoftware.smack.xml.XmlPullParser; 027import org.jivesoftware.smack.xml.XmlPullParserException; 028 029/** 030 * The default payload representation for {@link PayloadItem#getPayload()}. It simply 031 * stores the XML payload as a string. 032 * 033 * @author Robin Collier 034 */ 035public class SimplePayload implements XmlElement { 036 private final String elemName; 037 private final String ns; 038 private final String payload; 039 040 /** 041 * Construct a <code>SimplePayload</code> object with the specified element name, 042 * namespace and content. The content must be well formed XML. 043 * 044 * @param xmlPayload The payload data 045 */ 046 public SimplePayload(String xmlPayload) { 047 XmlPullParser parser; 048 try { 049 parser = PacketParserUtils.getParserFor(xmlPayload); 050 } 051 catch (XmlPullParserException | IOException e) { 052 throw new AssertionError(e); 053 } 054 QName qname = parser.getQName(); 055 056 payload = xmlPayload; 057 058 elemName = StringUtils.requireNotNullNorEmpty(qname.getLocalPart(), "Could not determine element name from XML payload"); 059 ns = StringUtils.requireNotNullNorEmpty(qname.getNamespaceURI(), "Could not determine namespace from XML payload"); 060 } 061 062 @Override 063 public String getElementName() { 064 return elemName; 065 } 066 067 @Override 068 public String getNamespace() { 069 return ns; 070 } 071 072 @Override 073 public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 074 return payload; 075 } 076 077 @Override 078 public String toString() { 079 return getClass().getName() + "payload [" + toXML() + "]"; 080 } 081}