Smack中IQProvider的一个问题。

在Smack的Readme文档Provider Architecture: Packet Extensions and Custom IQ's 中有一段描述:

QProvider By default, Smack only knows how to process IQ packets with sub-packets that are in a few namespaces such as:

  • jabber:iq:auth
  • jabber:iq:roster
  • jabber:iq:register

Because many more IQ types are part of XMPP and its extensions, a pluggable IQ parsing mechanism is provided. IQ providers are registered programatically or by creating a smack.providers file in the META-INF directory of your JAR file. The file is an XML document that contains one or more iqProvider entries, as in the following example:

 <?xml version="1.0"?>
<smackProviders>
<iqProvider>
<elementName>query</elementName>
<namespace>jabber:iq:time</namespace>
<className>org.jivesoftware.smack.packet.Time</className>
</iqProvider>
</smackProviders>

Each IQ provider is associated with an element name and a namespace. In the example above, the element name is query and the namespace is abber:iq:time . If multiple provider entries attempt to register to handle the same namespace, the first entry loaded from the classpath will take precedence.

The IQ provider class can either implement the IQProvider interface, or extend the IQ class. In the former case, each IQProvider is responsible for parsing the raw XML stream to create an IQ instance. In the latter case, bean introspection is used to try to automatically set properties of the IQ instance using the values found in the IQ packet XML. For example, an XMPP time packet resembles the following:

<iq type='result' to='joe@example.com' from='mary@example.com' id='time_1'>
<query xmlns='jabber:iq:time'>
<utc>20020910T17:58:35</utc>
<tz>MDT</tz>
<display>Tue Sep 10 12:58:35 2002</display>
</query>

</iq>

In order for this packet to be automatically mapped to the Time object listed in the providers file above, it must have the methods setUtc(String), setTz(String), and setDisplay(String). The introspection service will automatically try to convert the String value from the XML into a boolean, int, long, float, double, or Class depending on the type the IQ instance expects.

 

这里只提到了setUtc(String), setTz(String), and setDisplay(String)等写方法,但在实际调试了Smack库后,它还要求有读方法,即必须要有对应的get方法。否则在<query>下如果有子结点时,将不能解析到对应的class。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值