Java 类org.jivesoftware.smackx.provider.VCardProvider 实例源码

项目:spark-svn-mirror    文件:VCardManager.java   
/**
 * Attempts to load
 *
 * @param jid the jid of the user.
 * @return the VCard if found, otherwise null.
 */
private VCard loadFromFileSystem(String jid) {
    if (jid == null || jid.trim().isEmpty()) {
        return null;
    }

    // Unescape JID
    String fileName = Base64.encodeBytes(jid.getBytes());

    // remove tab
    fileName   = fileName.replaceAll("\t", "");
    // remove new line (Unix)
    fileName          = fileName.replaceAll("\n", "");
    // remove new line (Windows)
    fileName          = fileName.replaceAll("\r", "");

    final File vcardFile = new File(vcardStorageDirectory, fileName);
    if (!vcardFile.exists()) {
        return null;
    }

    try {
        // Otherwise load from file system.
        BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(vcardFile), "UTF-8"));
        VCardProvider provider = new VCardProvider();
        parser.setInput(in);
        VCard vcard = (VCard)provider.parseIQ(parser);

        // Check to see if the file is older 10 minutes. If so, reload.
        String timestamp = vcard.getField("timestamp");
        if (timestamp != null) {
            long time = Long.parseLong(timestamp);
            long now = System.currentTimeMillis();


            long hour = (1000 * 60) * 60;
            if (now - time > hour) {
                addToQueue(jid);
            }
        }

        addVCard(jid, vcard);
        return vcard;
    }
    catch (Exception e) {
        Log.warning("Unable to load vCard for " + jid, e);
    }

    return null;
}