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.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 ExtensionElement { 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 /** 063 * Construct a <code>SimplePayload</code> object with the specified element name, 064 * namespace and content. The content must be well formed XML. 065 * 066 * @param elementName The root element name (of the payload) 067 * @param namespace The namespace of the payload, null if there is none 068 * @param xmlPayload The payload data 069 * @deprecated use {@link #SimplePayload(String)} insteas. 070 */ 071 // TODO: Remove in Smack 4.5 072 @Deprecated 073 public SimplePayload(String elementName, String namespace, CharSequence xmlPayload) { 074 this(xmlPayload.toString()); 075 if (!elementName.equals(this.elemName)) { 076 throw new IllegalArgumentException(); 077 } 078 if (!namespace.equals(this.ns)) { 079 throw new IllegalArgumentException(); 080 } 081 } 082 083 @Override 084 public String getElementName() { 085 return elemName; 086 } 087 088 @Override 089 public String getNamespace() { 090 return ns; 091 } 092 093 @Override 094 public String toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) { 095 return payload; 096 } 097 098 @Override 099 public String toString() { 100 return getClass().getName() + "payload [" + toXML() + "]"; 101 } 102}