📄 vcard.java
字号:
/* * VCard.java * * Created on November 26, 2001, 10:10 PM */package com.sams.jxta.vcard;import org.jdom.input.SAXBuilder;import javax.xml.parsers.*;import org.xml.sax.*;import org.w3c.dom.*;import org.xml.sax.SAXException;import java.text.SimpleDateFormat;import java.io.*;import java.net.*;import java.lang.reflect.*;import java.util.*;import net.jxta.endpoint.Message;import net.jxta.endpoint.MessageElement;import net.jxta.peer.*;import org.apache.log4j.Category;import net.jxta.impl.id.UUID.UUIDFactory;/** * * @author Daniel Brookshier Turbogeek@cluck.com */public class VCard { private static final Category LOG = Category.getInstance(VCard.class.getName()); protected SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'"); public static final String supporedVersion="3.0"; public static final String versionAttr = "version"; protected String valueOfXML = null; protected boolean changed = true; protected String firstName="first"; protected String lastName="last"; protected String nickName = ""; protected String birthday=""; protected String dtstamp = format.format( new Date(System.currentTimeMillis())); protected Vector phones = new Vector(2); protected Vector addresses = new Vector(2); protected String email="dev@null.gov"; protected String note=""; protected String fullName=""; protected String latitude=""; protected String longitude=""; protected String peerName="";// Custom tag to specify name of peer protected String peerID="";// Custom tag to specify ID of peer protected String groupID="";// Custom tag to specify ID of peer protected String groupName="";// Custom tag to specify ID of peer protected String groupDesc="";// Custom tag to specify ID of peer protected String id=UUIDFactory.newUUID().toString();// Custom tag to specify ID of item /** Creates new VCard */ public static final String phoneType[] ={"HOME","WORK","CELL","FAX","PAGER"}; public static final String addrType[] = {"POSTAL PARCEL WORK","POSTAL PARCEL HOME"}; public static final String phoneTypeAttr = "tel.type"; public static final String addrTypeAttr = "adr.type"; public static final String vCardTag = "vcard"; public static final String fullNameTag = "fn";//full name public static final String nameTag = "n"; public static final String nickNameTag = "nickname"; public static final String familyTag = "family"; public static final String givenTag = "given"; public static final String telTag = "tel"; public static final String adrTag = "adr"; public static final String streetTag = "street"; public static final String poboxTag = "pobox"; public static final String extaddTag = "extadd"; public static final String localityTag = "locality"; public static final String regionTag = "region"; public static final String countryTag = "country"; public static final String pcodeTag = "pcode"; public static final String emailTag = "email"; public static final String noteTag = "note"; public static final String geoTag = "geo"; public static final String latTag = "lat"; public static final String lonTag = "lon"; // Custom tags public static final String peerNameTag = "-jxta-peer-name"; public static final String peerIdTag = "x-jxta-peer-id"; public static final String dtstampTag = "x-jxta-dtstamp"; public static final String idTag = "x-jxta-id"; public static final String groupIdTag = "x-jxta-group-id"; public static final String groupNameTag = "x-jxta-group-name"; public static final String groupDescTag = "x-jxta-group-desc"; public VCard() { } public VCard(String message) { dtstamp = "VCard(String message)"; processMessage(message); } public String toXML(){ if (changed){ // Note that many compilers covert this into a series of appends // to a StringBuffer, thus making this effiecient when written this way. valueOfXML = "<vcard \n" +" version=\"3.0\"" +" prodid=\"-//HandGen//NONSGML vGen v1.0//EN\">\n" +"<"+idTag+">"+id+"</"+idTag+">\n" +"<"+dtstampTag+">"+dtstamp+"</"+dtstampTag+">\n" +"<"+groupNameTag+">"+groupName+"</"+groupNameTag+">\n" +"<"+groupDescTag+">"+groupDesc+"</"+groupDescTag+">\n" +"<"+groupIdTag+">"+groupID+"</"+groupIdTag+">\n" +"<"+peerIdTag+">"+peerID+"</"+peerIdTag+">\n" +"<"+peerNameTag+">"+peerName+"</"+peerNameTag+">\n" +"<fn>"+fullName+"</fn>" +"<nickname>"+fullName+"</nickname>" +"<n><family>"+lastName+"</family> <given>"+firstName+"</given><bday>"+birthday+"</bday><nickname>"+nickName+"</nickname></n>\n"; for (int i = 0;i < phones.size(); i++){ Phone phone = (Phone) phones.elementAt(i); valueOfXML += "<tel tel.type=\""+phone.getType()+"\">"+phone.getNumber()+"</tel>\n"; } for (int i = 0;i < addresses.size(); i++){ Address address = (Address) addresses.elementAt(i); valueOfXML += "<adr del.type=\""+address.getType()+"\">\n" +"<pobox>"+address.getPobox()+"</pobox>\n" +"<extadd>"+address.getExtadd()+"</extadd>\n" +"<street>"+address.getStreet()+"</street>\n" +"<locality>"+address.getCity()+"</locality> <region>"+address.getState()+"</region>\n" +"<pcode>"+address.getPostalCode()+"</pcode> <country>"+address.getCountry()+"</country>\n" +"</adr>\n"; } valueOfXML +="<email email.type=\"INTERNET\">"+email+"</email>\n" +"<note>"+note+"</note>\n" +"<geo><lat>"+latitude+"</lat><lon>"+longitude+"</lon> </geo>\n" +"</vcard>"; }// if changed return valueOfXML; } public org.jdom.Document toJDOM(){ try{ ByteArrayInputStream in = new ByteArrayInputStream(toXML().getBytes()); SAXBuilder builder = new SAXBuilder(); org.jdom.Document doc = builder.build(in); return doc; }catch(org.jdom.JDOMException je){ je.printStackTrace(); return null; } } public void processMessage(Message msg) throws IOException, InvocationTargetException { dtstamp = "vCard processMessage(Message msg)"; try{ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); MessageElement element = msg.getElement(vCardTag); Document responseDoc = builder.parse(element.getStream()); Element root = responseDoc.getDocumentElement(); processMessage(root); }catch(java.io.IOException ioe){ ioe.printStackTrace(); }catch(org.xml.sax.SAXParseException spe){ System.out.println(); System.out.println("line:"+spe.getLineNumber()); System.out.println("Column:"+spe.getColumnNumber()); System.out.println("PublicId:"+spe.getPublicId() ); System.out.println("SystemId:"+spe.getSystemId() ); System.out.println(msg); System.out.println("-------------------------------------------------------"); spe.printStackTrace(System.out); }catch(org.xml.sax.SAXException se){ se.printStackTrace(); }catch(javax.xml.parsers.ParserConfigurationException pce){ pce.printStackTrace(); changed = true; } } public void processMessage(String msg) { // set up variables to track an error if parsing fails id = "error"; dtstamp = "error"; LOG.debug("Processing a VCard string"); LOG.debug(msg); try{ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = docBuilderFactory.newDocumentBuilder(); //MessageElement element = msg.getElement(vCardTag); //Document responseDoc = builder.parse(element.getStream()); ByteArrayInputStream in = new ByteArrayInputStream(msg.getBytes()); Document responseDoc = builder.parse(in); Element root = responseDoc.getDocumentElement(); LOG.debug("End processing a string VCard element"); processMessage(root); LOG.debug("End processing a element VCard element"); }catch(java.io.IOException ioe){ ioe.printStackTrace(); }catch(org.xml.sax.SAXParseException spe){ LOG.error("Error processing XML",spe); LOG.error("line:"+spe.getLineNumber()); LOG.error("Column:"+spe.getColumnNumber()); LOG.error("PublicId:"+spe.getPublicId() ); LOG.error("SystemId:"+spe.getSystemId() ); LOG.error(msg); spe.printStackTrace(System.out); }catch(org.xml.sax.SAXException se){ se.printStackTrace(); }catch(javax.xml.parsers.ParserConfigurationException pce){ pce.printStackTrace(); } changed = true; } public void processMessage(Element root) { if (root == null) { LOG.error("Invalid root==null"); throw new RuntimeException("Invalid root==null"); } if (!root.getTagName().equals(vCardTag)){ LOG.error("Invalid root:"+root.getTagName()); throw new RuntimeException("Invalid root:"+root.getTagName()); } try{ String version = root.getAttributeNode( versionAttr ).getValue(); if(supporedVersion.equals(version)){ LOG.debug("Supported Version!"); }else{ LOG.warn("Error Unsupported vCard document version:"+version); return; } Enumeration enum= null; Element tag,tag1,tag2,tag3 =null; NodeList hitNodes = root.getChildNodes(); for (int i = 0; i < hitNodes.getLength(); i++) { Node hitNode = hitNodes.item(i); LOG.debug("Element reading child nodes:"+hitNode.getNodeName()); if (hitNode.getNodeName().equals(nameTag)){ NodeList childNodesList = hitNode.getChildNodes(); for (int j = 0; j < childNodesList.getLength(); j++) { Node subNode = childNodesList.item(j); //System.out.println("##### Element reading name nodes:"+subNode.getNodeName()); if (familyTag.equals(subNode.getNodeName())){ lastName = getText(subNode); }else if (givenTag.equals(subNode.getNodeName())){ firstName = getText(subNode); } } }else if (idTag.equals(hitNode.getNodeName())){ id = getText(hitNode); }else if (peerIdTag.equals(hitNode.getNodeName())){ peerID = getText(hitNode); }else if (groupIdTag.equals(hitNode.getNodeName())){ groupID = getText(hitNode); }else if (groupDescTag.equals(hitNode.getNodeName())){ groupDesc = getText(hitNode); }else if (peerNameTag.equals(hitNode.getNodeName())){ peerName = getText(hitNode); }else if (groupNameTag.equals(hitNode.getNodeName())){ groupName = getText(hitNode); }else if (dtstampTag.equals(hitNode.getNodeName())){ dtstamp = getText(hitNode); }else if (emailTag.equals(hitNode.getNodeName())){ email = getText(hitNode); }else if (fullNameTag.equals(hitNode.getNodeName())){ fullName = getText(hitNode); }else if (nickNameTag.equals(hitNode.getNodeName())){ nickName = getText(hitNode); }else if (telTag.equals(hitNode.getNodeName())){ Phone phone = new Phone(); phone.setType( root.getAttributeNode( phoneTypeAttr ).getValue()); phone.setNumber(getText(hitNode)); phones.addElement(phone); }else if (adrTag.equals(hitNode.getNodeName())){ Address addr = new Address(); NodeList childNodesList = hitNode.getChildNodes(); addresses.addElement(addr); for (int k = 0; k < childNodesList.getLength(); k++) { LOG.debug("Element addr node"); Node subNode = childNodesList.item(k); addr.setType(((Element)hitNode).getAttributeNode( addrTypeAttr ).getValue()); if (streetTag.equals(subNode.getNodeName())){ addr.setStreet( getText(subNode));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -