📄 vcardiqprovider.java
字号:
package com.valhalla.jbother.jabber.smack.provider;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.provider.*;
import com.valhalla.jbother.jabber.smack.*;
/** An IQ Provider for vcards. </p>
* <p> Licence: GPL.</p>
* @author Pheet
* @version 0.2
* @see VCard */
public final class VCardIQProvider implements IQProvider{
public VCardIQProvider(){
// nothing to do
}
/** Installs the IQ provider.
* Call this method once at the beginning of your code to programatically
* register the IQ provider.*/
public static void Install(){
ProviderManager.addIQProvider("vCard","vcard-temp",new VCardIQProvider());
}
private static boolean tagPresent(String text, String tag){
if( text == null ) return false;
int i=text.indexOf("<"+tag+">");
if (i==-1){
return false;
}
return (text.indexOf("</"+tag+">",i)!=-1);
}
/**Gets the content of the tag from the text.
* The tag itelf is removed from the result.
* @return The contents of the tag or <b>null</b> if the tag is not present.*/
private static String getTag(String text, String tag){
if(!tagPresent(text,tag)){
return null;
}
String result=text.substring(text.indexOf("<"+tag+">")+2+tag.length(),text.indexOf("</"+tag+">"));
if(result.equals("")){
return null;
}
return result;
}
/** Pares method used internally by the SMACK library.*/
public IQ parseIQ(org.xmlpull.v1.XmlPullParser parser) {
VCard iq=new VCard();
try {
if (parser.isEmptyElementTag()){
return iq;
}
StringBuffer buff=new StringBuffer();
int event=parser.getEventType();
// get the content
while (!(event==parser.END_TAG && parser.getName().equals("vCard"))){
if(event==parser.TEXT){
buff.append(parser.getText());
}
if(event==parser.START_TAG){
buff.append("<"+parser.getName()+">");
}
if(event==parser.END_TAG){
buff.append("</"+parser.getName()+">");
}
event=parser.next();
}
// easier to parse the content this way than using the xml parser.
// It's not elegant, but neither is the vCard DTD....
String content=buff.toString();
iq.setFullName(getTag(content,"FN"));
String name=getTag(content,"N");
if(name!=null){
iq.setName_Given(getTag(name,"GIVEN"));
iq.setName_Middle(getTag(name,"MIDDLE"));
iq.setName_Family(getTag(name,"FAMILY"));
iq.setName_Prefix(getTag(name,"PREFIX"));
iq.setName_Suffix(getTag(name,"SUFFIX"));
}
iq.setNickname(getTag(content,"NICKNAME"));
iq.setURL(getTag(content,"URL"));
iq.setBirthday(getTag(content,"BDAY"));
iq.setTitle(getTag(content,"TITLE"));
iq.setRole(getTag(content,"ROLE"));
String org=getTag(content,"ORG");
if(org!=null){
iq.setOrg_Name(getTag(org,"ORGNAME"));
iq.setOrg_Unit(getTag(org,"ORGUNIT"));
}
iq.setDescription(getTag(content,"DESC"));
iq.setJabberID(getTag(content,"JABBERID"));
// now get email(s)
String section=new String(content);
String subsection=getTag(section,"EMAIL");
iq.setEmail(getTag(subsection,"USERID"));
// Now get address(s)
section=content;
subsection=getTag(section,"ADR");
while (subsection!=null){ // while theres still ADR tags,
if(subsection.indexOf("<WORK/>")>-1){ // its for work
iq.setAddress_Work_House(getTag(subsection,"EXTADD"));
iq.setAddress_Work_Street(getTag(subsection,"STREET"));
iq.setAddress_Work_Locality(getTag(subsection,"LOCALITY"));
iq.setAddress_Work_Region(getTag(subsection,"REGION"));
iq.setAddress_Work_PCode(getTag(subsection,"PCODE"));
iq.setAddress_Work_Country(getTag(subsection,"CTRY"));
}
else{ // its not for work
if(subsection.indexOf("<HOME/>")>-1){ // its for home
iq.setAddress_Home_House(getTag(subsection,"EXTADD"));
iq.setAddress_Home_Street(getTag(subsection,"STREET"));
iq.setAddress_Home_Locality(getTag(subsection,"LOCALITY"));
iq.setAddress_Home_Region(getTag(subsection,"REGION"));
iq.setAddress_Home_PCode(getTag(subsection,"PCODE"));
iq.setAddress_Home_Country(getTag(subsection,"CTRY"));
}
else{ // its not specified, so...
if(!iq.hasWorkAddress()){ // work not set yet so put it there
iq.setAddress_Work_House(getTag(subsection,"EXTADD"));
iq.setAddress_Work_Street(getTag(subsection,"STREET"));
iq.setAddress_Work_Locality(getTag(subsection,"LOCALITY"));
iq.setAddress_Work_Region(getTag(subsection,"REGION"));
iq.setAddress_Work_PCode(getTag(subsection,"PCODE"));
iq.setAddress_Work_Country(getTag(subsection,"CTRY"));
}
else{ //work set already, but..
if(!iq.hasHomeAddress()){ // home not so put there
iq.setAddress_Home_House(getTag(subsection,"EXTADD"));
iq.setAddress_Home_Street(getTag(subsection,"STREET"));
iq.setAddress_Home_Locality(getTag(subsection,"LOCALITY"));
iq.setAddress_Home_Region(getTag(subsection,"REGION"));
iq.setAddress_Home_PCode(getTag(subsection,"PCODE"));
iq.setAddress_Home_Country(getTag(subsection,"CTRY"));
}
}
} // end of its not specified
} // end of else its not for work
section=section.substring(section.indexOf("</ADR>")+6); // search space is after the email tag we just processed
subsection=getTag(section,"ADR");
} //end while
// Now telephone numbers
section=content;
subsection=getTag(section,"TEL");
iq.setTel_Home_Voice(getTag(section, "NUMBER" ) );
/*String data;
while (subsection!=null){ // while theres still TEL tags,
data=getTag(subsection,"NUMBER");
if(data!=null){ // make sure the number tag is there
if(subsection.indexOf("<HOME/>")>-1){ // its for home
if(subsection.indexOf("<FAX/>")>-1){ // its fax
iq.setTel_Home_Fax(data);
}
else{ // not fax
if(subsection.indexOf("<MSG/>")>-1){ // its ansa machine
iq.setTel_Home_Msg(data);
}
else{ // its voice
iq.setTel_Home_Voice(data);
} // end else its voice
} // end else not fax
}
else{ // its not for home, so assume work
if(subsection.indexOf("<FAX/>")>-1){ // its fax
//iq.setTel_Work_Fax(data);
}
else{ // not fax
if(subsection.indexOf("<MSG/>")>-1){ // its ansa machine
//iq.setTel_Work_Msg(data);
}
else{ // its voice
//iq.setTel_Work_Voice(data);
} // end else its voice
} // end else not fax
}// end else is not for home
} // end if data!=null
section=section.substring(section.indexOf("</TEL>")+6); // search space is after the email tag we just processed
subsection=getTag(section,"TEL");
} //end while*/
}
catch( Exception ex )
{
com.valhalla.Logger.logException( ex );
}
// finally return the VCard IQ
return iq;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -