问题描述
这是我想免费获赠消息包。
<消息[email protected]/smackchat_id =73392
custom_packet =真的user_id =44MANAGER_ID =39TIME_STAMP =0website_id =0
visitor_name =约翰END_TIME =FALSEXML:LANG =EN-USTYPE =群聊
从[email protected]/39>
<身体GT;你好< /身体GT;
&其中,X的xmlns =http://jabber.org/protocol/muc#user>
<状态的xmlns =code =0/>
< / X>< /消息>
消息包,我Recieving。
<消息[email protected]/Smack
从[email protected]/Visitor1171TYPE =群聊>
<身体GT;你好< /身体GT;
<延迟的xmlns =金塔:XMPP:延迟>< /延迟>
&其中,X的xmlns =胡言乱语:X:延迟邮票=20120917T05:57:19
从[email protected]/4732abb5>
< / X>< /消息>
我有一个咂嘴的来源$ C $ c和这里是包类。谁能帮我做我的自定义数据包。任何帮助修改源代码code是AP preciated。
code:
公共抽象类包{
受保护的静态最后弦乐DEFAULT_LANGUAGE =
。java.util.Locale.getDefault来()使用getLanguage()与toLowerCase()。
私有静态字符串DEFAULT_XML_NS = NULL;
/ **
*用作包标签的常量,表示一个分组具有没有id。为了表明一个包
*没有ID设置这个常量作为数据包的ID。当问其ID的数据包中
*答案将是< TT>空< / TT取代。
* /
公共静态最后弦乐ID_NOT_AVAILABLE =ID_NOT_AVAILABLE;
/ **
*定义在XEP-0082的日期格式 - XMPP协议的日期和时间配置文件。
*时区设置为UTC。
*所述p为H.;
*日期格式不同步。由于多个线程访问格式的同时,
*它必须保持外部同步。
* /
公共静态最后的DateFormat XEP_0082_UTC_FORMAT =新的SimpleDateFormat(
YYYY-MM-dd'T'HH:毫米:ss.SSS'Z');
静态{
XEP_0082_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone(UTC));
}
/ **
*一个preFIX有助于确保ID是跨复式实例是唯一的。
* /
私有静态字符串preFIX = StringUtils.randomString(5)+ - ;
/ **
*跟踪当前增量,它被附加到preFIX到的
*论坛一个唯一的ID。
* /
私有静态长ID = 0;
私人字符串的xmlns = DEFAULT_XML_NS;
/ **
*返回下一个唯一的ID。每个ID由一个短的字母数字的
* preFIX随着一个独特的数值。
*
返回:下一个ID。
* /
公共静态同步串nextID(){
返回preFIX + Long.toString(ID ++);
}
公共静态无效setDefaultXmlns(字符串defaultXmlns){
DEFAULT_XML_NS = defaultXmlns;
}
私人字符串包标签= NULL;
私人字符串= NULL;
从= NULL私人字符串;
私人最终名单,其中,PacketExtension> packetExtensions
=新的CopyOnWriteArrayList< PacketExtension>();
私人最终地图<字符串,对象>属性=新的HashMap<字符串,对象>();
私人XMPPError错误= NULL;
/ **
*返回分组的唯一ID。返回值可以是< TT>空< / TT>什么时候
* ID_NOT_AVAILABLE被设置为分组的id。
*
* @返回数据包的唯一ID或LT; TT>空< / TT>如果数据包的ID不可。
* /
公共字符串getPacketID(){
如果(ID_NOT_AVAILABLE.equals(包标签)){
返回null;
}
如果(包标签== NULL){
包标签= nextID();
}
返回包标签;
}
/ **
*设置数据包的唯一ID。为了表明一个包没有ID
*通恒ID_NOT_AVAILABLE作为数据包的id值。
*
* @参数包标签该分组的唯一ID。
* /
公共无效setPacketID(字符串包标签){
this.packetID =包标签;
}
/ **
*谁的数据包被发送到,或LT返回; TT>空< / TT>如果
*该值未设置。 XMPP协议往往使得到
*属性可选的,因此它并不总是需要设置与所述;差异无显着
*
*该StringUtils类提供了处理几种有用的方法
* XMPP地址,如解析
* {@link StringUtils的#parseBareAddress(字符串)裸地址},
* {@link StringUtils的#PARSENAME(字符串)用户名},
* {@link StringUtils的#parseServer(字符串)服务器},并
* {@link StringUtils的#parseResource(字符串)资源}。
*
* @返回谁的数据包被发送到,或LT; TT>空< / TT>如果
*值尚未设置。
* /
公共字符串会过(){
还给;
}
/ **
*谁的数据包被发送到集。 XMPP协议常使
*目标属性可选的,因此它并不总是需要设置。
*
* @参数到谁该分组被发送到。
* /
公共无效setTo(字符串){
this.to =到;
}
/ **
*谁的数据包被发送,从或LT返回; TT>空< / TT>如果
*该值未设置。 XMPP协议往往使得从
*属性可选的,因此它并不总是需要设置与所述;差异无显着
*
*该StringUtils类提供了处理几种有用的方法
* XMPP地址,如解析
* {@link StringUtils的#parseBareAddress(字符串)裸地址},
* {@link StringUtils的#PARSENAME(字符串)用户名},
* {@link StringUtils的#parseServer(字符串)服务器},并
* {@link StringUtils的#parseResource(字符串)资源}。
*
* @返回谁包正在从,或LT发送; TT>空< / TT>如果
*值尚未设置。
* /
公共字符串GETFROM(){
从回报;
}
/ **
*谁的数据包被发送,从集。 XMPP协议常常
*使得从属性可选的,因此它并不总是需要
*进行设置。
*
*参数从谁的数据包被发送到。
* /
公共无效setFrom(从字符串){
this.from =距离;
}
/ **
*返回与此数据包相关联的错误,或LT; TT>空< / TT>如果有
*没有错误。
*
* @返回错误的子包或LT; TT>空< / TT>如果没有错误。
* /
公共XMPPError getError(){
返回错误;
}
/ **
*设置此数据包中的错误。
*
* @参数错误的错误与该分组相关联。
* /
公共无效SETERROR(XMPPError错误){
this.error =错误;
}
/ **
*返回连接到数据包的包扩展的一个不可修改的集合。
*
返回:数据包的扩展。
* /
公共同步收藏< PacketExtension> getExtensions(){
如果(packetExtensions == NULL){
返回Collections.emptyList();
}
返回Collections.unmodifiableList(新的ArrayList< PacketExtension>(packetExtensions));
}
/ **
*返回此数据包具有给定命名空间的第一部分。
*
*参数命名空间需要扩展的空间。
返回:包扩展与给定的命名空间。
* /
公共PacketExtension getExtension(字符串命名空间){
返回getExtension(NULL,命名空间);
}
/ **
*返回第一个包扩展匹配指定元素名称和
*命名空间,或LT; TT>空< / TT>如果它不存在。如果提供的elementName为null
*比只提供命名空间被试图匹配。包扩展
*是任意XML子文档标准的XMPP包。默认情况下,
* DefaultPacketExtension实例将被为每部分返回。然而,
* PacketExtensionProvider实例可以与登记
* {@link org.jivesoftware.smack.provider.ProviderManager的ProviderManager}
*类来处理自定义分析。对象的在这种情况下,类型
*将由供应商来决定。
*
* @参数的ElementName的包扩展XML元素名称。 (可能为null)
* @参数名称空间的包扩展的XML元素的命名空间。
* @返回的扩展,或LT; TT>空< / TT>如果它不存在。
* /
公共PacketExtension getExtension(字符串的ElementName,串命名空间){
如果(命名空间== NULL){
返回null;
}
对于(PacketExtension分机:packetExtensions){
如果((的ElementName == NULL || elementName.equals(ext.getElementName()))
&功放;&安培; namespace.equals(ext.getNamespace()))
{
返回分机;
}
}
返回null;
}
/ **
*增加了一个包扩展到数据包。
*
*参数扩展包扩展。
* /
公共无效addExtension(PacketExtension扩展){
packetExtensions.add(扩展);
}
/ **
*删除一个包扩展的数据包。
*
*参数扩展包扩展删除。
* /
公共无效removeExtension(PacketExtension扩展){
packetExtensions.remove(扩展);
}
/ **
*返回的数据包特性具有指定名称或LT; TT>空< / TT>如果
*属性不存在。这是属性值orginally元将
*返回为目的等同。例如,一个int属性将
*返回一个整数,双为双等。
*
* @参数名称的属性的名称。
* @返回的财产,或LT; TT>空< / TT>如果属性不存在。
* /
公共同步对象的getProperty(字符串名称){
如果(属性== NULL){
返回null;
}
返回properties.get(名称);
}
/ **
*设置与对象的值的属性。该值必须是可序列化
*或将抛出IllegalArgumentException。
*
* @参数名称的属性的名称。
*参数值的属性的值。
* /
市民同步无效的SetProperty(字符串名称,对象的值){
如果(!(价值的instanceof序列化)){
抛出新抛出:IllegalArgumentException(值必须serialiazble);
}
properties.put(名称,值);
}
/ **
*删除属性。
*
* @参数名删除的属性的名称。
* /
市民同步无效deleteProperty(字符串名称){
如果(属性== NULL){
返回;
}
properties.remove(名称);
}
/ **
*返回所有在设置的属性名称的不可修改的集合。
*
返回:所有属性名。
* /
公共同步收藏<字符串> getPropertyNames(){
如果(属性== NULL){
返回Collections.emptySet();
}
返回Collections.unmodifiableSet(新的HashSet&其中;字符串>(properties.keySet()));
}
/ **
*返回的数据包为XML。包的每一个具体的扩展必须实现
*此方法。除了写出分组特定数据,每个子类
*也应该写出来的错误和扩展数据,如果它们被定义。
*
* @返回的数据包作为一个字符串的XML格式。
* /
公共抽象toxml用于字符串();
/ **
*返回扩展子分组(包括属性数据)作为XML
*字符串,如果没有包分机空字符串。
*
返回:扩展子包为XML或空字符串,如果有
*没有包分机。
* /
保护同步字符串getExtensionsXML(){
StringBuilder的BUF =新的StringBuilder();
//添加在所有标准的扩展子包。
对于(PacketExtension扩展:getExtensions()){
buf.append(extension.toXML());
}
//添加数据包的属性。
如果(属性= NULL和放大器;!&安培;!properties.isEmpty()){
buf.append(<属性的xmlns = \HTTP://www.jivesoftware.com/xmlns/xmpp/properties \>中);
//循环遍历所有属性,并把它们写出来。
对于(字符串名称:getPropertyNames()){
对象值=的getProperty(名称);
buf.append(<性>);
buf.append(<名称>)。追加(StringUtils.escapeForXML(名称))追加。(< /名称>);
buf.append(<值类型= \);
如果(值的instanceof整数){
buf.append(整数\>中)。追加(值).append(< /值GT;);
}
否则,如果(值的instanceof龙){
buf.append(长\>中)。追加(值).append(< /值GT;);
}
否则,如果(值的instanceof浮动){
buf.append(浮动\>中)。追加(值).append(< /值GT;);
}
否则,如果(值的instanceof双人间){
buf.append(双\>中)。追加(值).append(< /值GT;);
}
否则,如果(值的instanceof布尔){
buf.append(布尔\>中)。追加(值).append(< /值GT;);
}
否则,如果(值的instanceof字符串){
buf.append(字符串\>中);
buf.append(StringUtils.escapeForXML((字符串)值));
buf.append(&所述; /值>中);
}
//否则,它是一个通用的序列化对象。序列化对象是在
//为二进制格式,这将不是XML的内部工作。因此,我们基于64位
// EN code添加它之前的二进制数据。
其他 {
ByteArrayOutputStream字节流= NULL;
ObjectOutputStream的出= NULL;
尝试 {
字节流=新ByteArrayOutputStream();
OUT =新的ObjectOutputStream(字节流);
out.writeObject(值);
buf.append(java的对象\>中);
字符串连接codedVal = StringUtils.en codeBase64(byteStream.toByteArray());
buf.append(EN codedVal).append(< /值GT;);
}
赶上(例外五){
e.printStackTrace();
}
最后 {
如果(满分!= NULL){
尝试 {
out.close();
}
赶上(例外五){
// 忽略。
}
}
如果(字节流!= NULL){
尝试 {
byteStream.close();
}
赶上(例外五){
// 忽略。
}
}
}
}
buf.append(< /性>);
}
buf.append(< /性状>);
}
返回buf.toString();
}
公共字符串getXmlns(){
返回this.xmlns;
}
公共静态字符串getDefaultLanguage(){
返回DEFAULT_LANGUAGE;
}
@覆盖
公共布尔等于(对象o){
如果(这种== O)返回true;
如果(O!== NULL ||的getClass()= o.getClass())返回false;
分组包=(数据包)O;
如果(!?!错误= NULL error.equals(packet.error):packet.error = NULL){返回false; }
如果(!?!从= NULL from.equals(packet.from):packet.from = NULL){返回false; }
如果{返回false(packetExtensions.equals(packet.packetExtensions)!); }
如果(!?!包标签= NULL packetID.equals(packet.packetID):packet.packetID = NULL){
返回false;
}
如果(属性!= NULL?!properties.equals(packet.properties)
!packet.properties = NULL){
返回false;
}
如果(!?!来= NULL to.equals(packet.to):packet.to = NULL){返回false; }
返回:;!(的xmlns = NULL xmlns.equals(packet.xmlns)packet.xmlns!= NULL?!)
}
@覆盖
公众诠释哈希code(){
INT结果;
结果=(!?的xmlns = NULL xmlns.hash code():0);
结果= 31 *结果+(!?包标签= NULL packetID.hash code():0);
结果= 31 *结果+(以= NULL to.hash code():?0);
结果= 31 *结果+(从= NULL from.hash code():?0);
结果= 31 *结果+ packetExtensions.hash code();
结果= 31 *结果+ properties.hash code();
结果= 31 *结果+(错误= NULL error.hash code():?0);
返回结果;
}
}
我的包类增加了新的领域,并写有getter和setter。
私有String chat_id = NULL;
私人字符串custom_packet = NULL;
私人字符串USER_ID = NULL;
私人字符串MANAGER_ID = NULL;
私人字符串TIME_STAMP = NULL;
私人字符串website_id = NULL;
私人字符串visitor_name = NULL;
私人字符串END_TIME = NULL;
公共字符串getChat_id(){
返回chat_id;
}
公共无效setChat_id(字符串chat_id){
this.chat_id = chat_id;
}
公共字符串getCustom_packet(){
返回custom_packet;
}
公共无效setCustom_packet(字符串custom_packet){
this.custom_packet = custom_packet;
}
公共字符串getUser_id(){
返回USER_ID;
}
公共无效setUser_id(字符串USER_ID){
this.user_id = user_ID的;
}
公共字符串getManager_id(){
返回MANAGER_ID;
}
公共无效setManager_id(字符串MANAGER_ID){
this.manager_id = MANAGER_ID;
}
公共字符串getTime_stamp(){
返回TIME_STAMP;
}
公共无效setTime_stamp(字符串TIME_STAMP){
this.time_stamp = TIME_STAMP;
}
公共字符串getWebsite_id(){
返回website_id;
}
公共无效setWebsite_id(字符串website_id){
this.website_id = website_id;
}
公共字符串getVisitor_name(){
返回visitor_name;
}
公共无效setVisitor_name(字符串visitor_name){
this.visitor_name = visitor_name;
}
公共字符串getEnd_time(){
返回END_TIME;
}
公共无效setEnd_time(字符串END_TIME){
this.end_time = END_TIME;
}
然后我说这PacketParserUtils类parseMessage()方法。
// ------------------------------自定义属性---- -------------------------
message.setChat_id(parser.getAttributeValue(,chat_id));
message.setCustom_packet(parser.getAttributeValue(,custom_packet));
message.setUser_id(parser.getAttributeValue(,USER_ID));
message.setManager_id(parser.getAttributeValue(,经理标识));
message.setTime_stamp(parser.getAttributeValue(,TIME_STAMP));
message.setWebsite_id(parser.getAttributeValue(,website_id));
message.setVisitor_name(parser.getAttributeValue(,visitor_name));
message.setEnd_time(parser.getAttributeValue(,end_time时间));
// ------------------------------------------------ -----------------------------
然后我更新了消息toxml用于类()方法来此。
// ------------------------------自定义标签---- -------------------------
如果(getChat_id()!= NULL)
{
。buf.append(chat_id = \)追加(StringUtils.escapeForXML(getChat_id()))附加(\);
}
如果(getCustom_packet()!= NULL)
{
。buf.append(custom_packet = \)追加(StringUtils.escapeForXML(getCustom_packet()))附加(\);
}
如果(getUser_id()!= NULL)
{
。buf.append(USER_ID = \)追加(StringUtils.escapeForXML(getUser_id()))附加(\);
}
如果(getManager_id()!= NULL)
{
。buf.append(MANAGER_ID = \)追加(StringUtils.escapeForXML(getManager_id()))附加(\);
}
如果(getTime_stamp()!= NULL)
{
。buf.append(TIME_STAMP = \)追加(StringUtils.escapeForXML(getTime_stamp()))附加(\);
}
如果(getWebsite_id()!= NULL)
{
。buf.append(website_id = \)追加(StringUtils.escapeForXML(getWebsite_id()))附加(\);
}
如果(getVisitor_name()!= NULL)
{
。buf.append(visitor_name = \)追加(StringUtils.escapeForXML(getVisitor_name()))附加(\);
}
如果(getEnd_time()!= NULL)
{
。buf.append(end_time时间= \)追加(StringUtils.escapeForXML(getEnd_time()))附加(\);
}
// ------------------------------------------------ ------------------------------------
这我怎么能接受她的自定义属性的消息标记。
The Message Packet that i want to recieve.
<message to="[email protected]/smack" chat_id="73392"
custom_packet="true" user_id="44" manager_id="39" time_stamp="0" website_id="0"
visitor_name="John" end_time="False" xml:lang="en-us" type="groupchat"
from="[email protected]/39">
<body>Hello</body>
<x xmlns="http://jabber.org/protocol/muc#user">
<status xmlns="" code="0"/>
</x></message>
Message Packet that I am Recieving.
<message to="[email protected]/Smack"
from="[email protected]/Visitor1171" type="groupchat">
<body>Hello</body>
<delay xmlns="urn:xmpp:delay"></delay>
<x xmlns="jabber:x:delay" stamp="20120917T05:57:19"
from="[email protected]/4732abb5">
</x></message>
I have the Source code of a smack and Here is the packet class. Can anyone help me make my custom packet. Any help Modifying the source code is appreciated.
Code :
public abstract class Packet {
protected static final String DEFAULT_LANGUAGE =
java.util.Locale.getDefault().getLanguage().toLowerCase();
private static String DEFAULT_XML_NS = null;
/**
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet
* has no id set this constant as the packet's id. When the packet is asked for its id the
* answer will be <tt>null</tt>.
*/
public static final String ID_NOT_AVAILABLE = "ID_NOT_AVAILABLE";
/**
* Date format as defined in XEP-0082 - XMPP Date and Time Profiles.
* The time zone is set to UTC.
* <p>
* Date formats are not synchronized. Since multiple threads access the format concurrently,
* it must be synchronized externally.
*/
public static final DateFormat XEP_0082_UTC_FORMAT = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
static {
XEP_0082_UTC_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
}
/**
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/
private static String prefix = StringUtils.randomString(5) + "-";
/**
* Keeps track of the current increment, which is appended to the prefix to
* forum a unique ID.
*/
private static long id = 0;
private String xmlns = DEFAULT_XML_NS;
/**
* Returns the next unique id. Each id made up of a short alphanumeric
* prefix along with a unique numeric value.
*
* @return the next id.
*/
public static synchronized String nextID() {
return prefix + Long.toString(id++);
}
public static void setDefaultXmlns(String defaultXmlns) {
DEFAULT_XML_NS = defaultXmlns;
}
private String packetID = null;
private String to = null;
private String from = null;
private final List<PacketExtension> packetExtensions
= new CopyOnWriteArrayList<PacketExtension>();
private final Map<String,Object> properties = new HashMap<String, Object>();
private XMPPError error = null;
/**
* Returns the unique ID of the packet. The returned value could be <tt>null</tt> when
* ID_NOT_AVAILABLE was set as the packet's id.
*
* @return the packet's unique ID or <tt>null</tt> if the packet's id is not available.
*/
public String getPacketID() {
if (ID_NOT_AVAILABLE.equals(packetID)) {
return null;
}
if (packetID == null) {
packetID = nextID();
}
return packetID;
}
/**
* Sets the unique ID of the packet. To indicate that a packet has no id
* pass the constant ID_NOT_AVAILABLE as the packet's id value.
*
* @param packetID the unique ID for the packet.
*/
public void setPacketID(String packetID) {
this.packetID = packetID;
}
/**
* Returns who the packet is being sent "to", or <tt>null</tt> if
* the value is not set. The XMPP protocol often makes the "to"
* attribute optional, so it does not always need to be set.<p>
*
* The StringUtils class provides several useful methods for dealing with
* XMPP addresses such as parsing the
* {@link StringUtils#parseBareAddress(String) bare address},
* {@link StringUtils#parseName(String) user name},
* {@link StringUtils#parseServer(String) server}, and
* {@link StringUtils#parseResource(String) resource}.
*
* @return who the packet is being sent to, or <tt>null</tt> if the
* value has not been set.
*/
public String getTo() {
return to;
}
/**
* Sets who the packet is being sent "to". The XMPP protocol often makes
* the "to" attribute optional, so it does not always need to be set.
*
* @param to who the packet is being sent to.
*/
public void setTo(String to) {
this.to = to;
}
/**
* Returns who the packet is being sent "from" or <tt>null</tt> if
* the value is not set. The XMPP protocol often makes the "from"
* attribute optional, so it does not always need to be set.<p>
*
* The StringUtils class provides several useful methods for dealing with
* XMPP addresses such as parsing the
* {@link StringUtils#parseBareAddress(String) bare address},
* {@link StringUtils#parseName(String) user name},
* {@link StringUtils#parseServer(String) server}, and
* {@link StringUtils#parseResource(String) resource}.
*
* @return who the packet is being sent from, or <tt>null</tt> if the
* value has not been set.
*/
public String getFrom() {
return from;
}
/**
* Sets who the packet is being sent "from". The XMPP protocol often
* makes the "from" attribute optional, so it does not always need to
* be set.
*
* @param from who the packet is being sent to.
*/
public void setFrom(String from) {
this.from = from;
}
/**
* Returns the error associated with this packet, or <tt>null</tt> if there are
* no errors.
*
* @return the error sub-packet or <tt>null</tt> if there isn't an error.
*/
public XMPPError getError() {
return error;
}
/**
* Sets the error for this packet.
*
* @param error the error to associate with this packet.
*/
public void setError(XMPPError error) {
this.error = error;
}
/**
* Returns an unmodifiable collection of the packet extensions attached to the packet.
*
* @return the packet extensions.
*/
public synchronized Collection<PacketExtension> getExtensions() {
if (packetExtensions == null) {
return Collections.emptyList();
}
return Collections.unmodifiableList(new ArrayList<PacketExtension>(packetExtensions));
}
/**
* Returns the first extension of this packet that has the given namespace.
*
* @param namespace the namespace of the extension that is desired.
* @return the packet extension with the given namespace.
*/
public PacketExtension getExtension(String namespace) {
return getExtension(null, namespace);
}
/**
* Returns the first packet extension that matches the specified element name and
* namespace, or <tt>null</tt> if it doesn't exist. If the provided elementName is null
* than only the provided namespace is attempted to be matched. Packet extensions are
* are arbitrary XML sub-documents in standard XMPP packets. By default, a
* DefaultPacketExtension instance will be returned for each extension. However,
* PacketExtensionProvider instances can be registered with the
* {@link org.jivesoftware.smack.provider.ProviderManager ProviderManager}
* class to handle custom parsing. In that case, the type of the Object
* will be determined by the provider.
*
* @param elementName the XML element name of the packet extension. (May be null)
* @param namespace the XML element namespace of the packet extension.
* @return the extension, or <tt>null</tt> if it doesn't exist.
*/
public PacketExtension getExtension(String elementName, String namespace) {
if (namespace == null) {
return null;
}
for (PacketExtension ext : packetExtensions) {
if ((elementName == null || elementName.equals(ext.getElementName()))
&& namespace.equals(ext.getNamespace()))
{
return ext;
}
}
return null;
}
/**
* Adds a packet extension to the packet.
*
* @param extension a packet extension.
*/
public void addExtension(PacketExtension extension) {
packetExtensions.add(extension);
}
/**
* Removes a packet extension from the packet.
*
* @param extension the packet extension to remove.
*/
public void removeExtension(PacketExtension extension) {
packetExtensions.remove(extension);
}
/**
* Returns the packet property with the specified name or <tt>null</tt> if the
* property doesn't exist. Property values that were orginally primitives will
* be returned as their object equivalent. For example, an int property will be
* returned as an Integer, a double as a Double, etc.
*
* @param name the name of the property.
* @return the property, or <tt>null</tt> if the property doesn't exist.
*/
public synchronized Object getProperty(String name) {
if (properties == null) {
return null;
}
return properties.get(name);
}
/**
* Sets a property with an Object as the value. The value must be Serializable
* or an IllegalArgumentException will be thrown.
*
* @param name the name of the property.
* @param value the value of the property.
*/
public synchronized void setProperty(String name, Object value) {
if (!(value instanceof Serializable)) {
throw new IllegalArgumentException("Value must be serialiazble");
}
properties.put(name, value);
}
/**
* Deletes a property.
*
* @param name the name of the property to delete.
*/
public synchronized void deleteProperty(String name) {
if (properties == null) {
return;
}
properties.remove(name);
}
/**
* Returns an unmodifiable collection of all the property names that are set.
*
* @return all property names.
*/
public synchronized Collection<String> getPropertyNames() {
if (properties == null) {
return Collections.emptySet();
}
return Collections.unmodifiableSet(new HashSet<String>(properties.keySet()));
}
/**
* Returns the packet as XML. Every concrete extension of Packet must implement
* this method. In addition to writing out packet-specific data, every sub-class
* should also write out the error and the extensions data if they are defined.
*
* @return the XML format of the packet as a String.
*/
public abstract String toXML();
/**
* Returns the extension sub-packets (including properties data) as an XML
* String, or the Empty String if there are no packet extensions.
*
* @return the extension sub-packets as XML or the Empty String if there
* are no packet extensions.
*/
protected synchronized String getExtensionsXML() {
StringBuilder buf = new StringBuilder();
// Add in all standard extension sub-packets.
for (PacketExtension extension : getExtensions()) {
buf.append(extension.toXML());
}
// Add in packet properties.
if (properties != null && !properties.isEmpty()) {
buf.append("<properties xmlns=\"http://www.jivesoftware.com/xmlns/xmpp/properties\">");
// Loop through all properties and write them out.
for (String name : getPropertyNames()) {
Object value = getProperty(name);
buf.append("<property>");
buf.append("<name>").append(StringUtils.escapeForXML(name)).append("</name>");
buf.append("<value type=\"");
if (value instanceof Integer) {
buf.append("integer\">").append(value).append("</value>");
}
else if (value instanceof Long) {
buf.append("long\">").append(value).append("</value>");
}
else if (value instanceof Float) {
buf.append("float\">").append(value).append("</value>");
}
else if (value instanceof Double) {
buf.append("double\">").append(value).append("</value>");
}
else if (value instanceof Boolean) {
buf.append("boolean\">").append(value).append("</value>");
}
else if (value instanceof String) {
buf.append("string\">");
buf.append(StringUtils.escapeForXML((String)value));
buf.append("</value>");
}
// Otherwise, it's a generic Serializable object. Serialized objects are in
// a binary format, which won't work well inside of XML. Therefore, we base-64
// encode the binary data before adding it.
else {
ByteArrayOutputStream byteStream = null;
ObjectOutputStream out = null;
try {
byteStream = new ByteArrayOutputStream();
out = new ObjectOutputStream(byteStream);
out.writeObject(value);
buf.append("java-object\">");
String encodedVal = StringUtils.encodeBase64(byteStream.toByteArray());
buf.append(encodedVal).append("</value>");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (out != null) {
try {
out.close();
}
catch (Exception e) {
// Ignore.
}
}
if (byteStream != null) {
try {
byteStream.close();
}
catch (Exception e) {
// Ignore.
}
}
}
}
buf.append("</property>");
}
buf.append("</properties>");
}
return buf.toString();
}
public String getXmlns() {
return this.xmlns;
}
public static String getDefaultLanguage() {
return DEFAULT_LANGUAGE;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Packet packet = (Packet) o;
if (error != null ? !error.equals(packet.error) : packet.error != null) { return false; }
if (from != null ? !from.equals(packet.from) : packet.from != null) { return false; }
if (!packetExtensions.equals(packet.packetExtensions)) { return false; }
if (packetID != null ? !packetID.equals(packet.packetID) : packet.packetID != null) {
return false;
}
if (properties != null ? !properties.equals(packet.properties)
: packet.properties != null) {
return false;
}
if (to != null ? !to.equals(packet.to) : packet.to != null) { return false; }
return !(xmlns != null ? !xmlns.equals(packet.xmlns) : packet.xmlns != null);
}
@Override
public int hashCode() {
int result;
result = (xmlns != null ? xmlns.hashCode() : 0);
result = 31 * result + (packetID != null ? packetID.hashCode() : 0);
result = 31 * result + (to != null ? to.hashCode() : 0);
result = 31 * result + (from != null ? from.hashCode() : 0);
result = 31 * result + packetExtensions.hashCode();
result = 31 * result + properties.hashCode();
result = 31 * result + (error != null ? error.hashCode() : 0);
return result;
}
}
I added new Fields in Packet Class and wrote there Getter and Setter.
private String chat_id = null;
private String custom_packet = null;
private String user_id=null;
private String manager_id=null;
private String time_stamp=null;
private String website_id=null;
private String visitor_name=null;
private String end_time=null;
public String getChat_id() {
return chat_id;
}
public void setChat_id(String chat_id) {
this.chat_id = chat_id;
}
public String getCustom_packet() {
return custom_packet;
}
public void setCustom_packet(String custom_packet) {
this.custom_packet = custom_packet;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getManager_id() {
return manager_id;
}
public void setManager_id(String manager_id) {
this.manager_id = manager_id;
}
public String getTime_stamp() {
return time_stamp;
}
public void setTime_stamp(String time_stamp) {
this.time_stamp = time_stamp;
}
public String getWebsite_id() {
return website_id;
}
public void setWebsite_id(String website_id) {
this.website_id = website_id;
}
public String getVisitor_name() {
return visitor_name;
}
public void setVisitor_name(String visitor_name) {
this.visitor_name = visitor_name;
}
public String getEnd_time() {
return end_time;
}
public void setEnd_time(String end_time) {
this.end_time = end_time;
}
Then i added this to PacketParserUtils Class parseMessage() method.
// ------------------------------CUSTOM ATTRIBUTES-----------------------------
message.setChat_id(parser.getAttributeValue("", "chat_id"));
message.setCustom_packet(parser.getAttributeValue("", "custom_packet"));
message.setUser_id(parser.getAttributeValue("", "user_id"));
message.setManager_id(parser.getAttributeValue("", "manager_id"));
message.setTime_stamp(parser.getAttributeValue("", "time_stamp"));
message.setWebsite_id(parser.getAttributeValue("", "website_id"));
message.setVisitor_name(parser.getAttributeValue("", "visitor_name"));
message.setEnd_time(parser.getAttributeValue("", "end_time"));
// -----------------------------------------------------------------------------
Then i updated the Message Class toXML() method to this.
// ------------------------------CUSTOM TAGS-----------------------------
if(getChat_id()!=null)
{
buf.append(" chat_id=\"").append(StringUtils.escapeForXML(getChat_id())).append("\"");
}
if(getCustom_packet()!=null)
{
buf.append(" custom_packet=\"").append(StringUtils.escapeForXML(getCustom_packet())).append("\"");
}
if(getUser_id()!=null)
{
buf.append(" user_id=\"").append(StringUtils.escapeForXML(getUser_id())).append("\"");
}
if(getManager_id()!=null)
{
buf.append(" manager_id=\"").append(StringUtils.escapeForXML(getManager_id())).append("\"");
}
if(getTime_stamp()!=null)
{
buf.append(" time_stamp=\"").append(StringUtils.escapeForXML(getTime_stamp())).append("\"");
}
if(getWebsite_id()!=null)
{
buf.append(" website_id=\"").append(StringUtils.escapeForXML(getWebsite_id())).append("\"");
}
if(getVisitor_name()!=null)
{
buf.append(" visitor_name=\"").append(StringUtils.escapeForXML(getVisitor_name())).append("\"");
}
if(getEnd_time()!=null)
{
buf.append(" end_time=\"").append(StringUtils.escapeForXML(getEnd_time())).append("\"");
}
//------------------------------------------------------------------------------------
This how i was able to Recieve custom attributes in MESSAGE TAG.
这篇关于添加自定义属性的邮件标签的XMPP包使用asmack为Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!