emoticonmanager.java.svn-base
来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 481 行 · 第 1/2 页
SVN-BASE
481 行
/**
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Lesser Public License (LGPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.sparkimpl.plugin.emoticons;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.jivesoftware.Spark;
import org.jivesoftware.spark.util.URLFileSystem;
import org.jivesoftware.spark.util.log.Log;
import org.jivesoftware.sparkimpl.settings.local.LocalPreferences;
import org.jivesoftware.sparkimpl.settings.local.SettingsManager;
import org.xml.sax.SAXException;
import javax.swing.ImageIcon;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;
/**
* Responsible for the handling of all Emoticon packs. Using the EmoticonManager, you can specify
* any defined Emoticon Pack, retrieve any emoticon based on its text equivalant, and retrieve its
* associated image url.
*
* @author Derek DeMoro
*/
public class EmoticonManager {
private static EmoticonManager singleton;
private static final Object LOCK = new Object();
private Map<String, Collection<Emoticon>> emoticonMap = new HashMap<String, Collection<Emoticon>>();
private Map<String, ImageIcon> imageMap = new HashMap<String, ImageIcon>();
/**
* The root emoticon directory.
*/
public static File EMOTICON_DIRECTORY;
/**
* Returns the singleton instance of <CODE>EmoticonManager</CODE>,
* creating it if necessary.
* <p/>
*
* @return the singleton instance of <Code>EmoticonManager</CODE>
*/
public static EmoticonManager getInstance() {
// Synchronize on LOCK to ensure that we don't end up creating
// two singletons.
synchronized (LOCK) {
if (null == singleton) {
EmoticonManager controller = new EmoticonManager();
singleton = controller;
return controller;
}
}
return singleton;
}
/**
* Initialize the EmoticonManager
*/
private EmoticonManager() {
EMOTICON_DIRECTORY = new File(Spark.getBinDirectory().getParent(), "xtra/emoticons").getAbsoluteFile();
// Copy over to allow for non-admins to extract.
copyFiles();
final LocalPreferences pref = SettingsManager.getLocalPreferences();
String emoticonPack = pref.getEmoticonPack();
try {
addEmoticonPack(emoticonPack);
}
catch (Exception e) {
Log.error(e);
}
}
/**
* Copy the files directly over to an accepted permissions directory.
*/
private void copyFiles() {
// Current Plugin directory
File newEmoticonDir = new File(Spark.getLogDirectory().getParentFile(), "xtra/emoticons").getAbsoluteFile();
newEmoticonDir.mkdirs();
File[] files = EMOTICON_DIRECTORY.listFiles();
final int no = files != null ? files.length : 0;
for (int i = 0; i < no; i++) {
File file = files[i];
if (file.isFile()) {
try {
// Copy over
File newFile = new File(newEmoticonDir, file.getName());
// Check timestamps
long installerFile = file.lastModified();
long copiedFile = newFile.lastModified();
if (installerFile > copiedFile) {
// Copy over and expand :)
URLFileSystem.copy(file.toURL(), newFile);
expandNewPack(newFile, newEmoticonDir);
}
}
catch (IOException e) {
Log.error(e);
}
}
}
EMOTICON_DIRECTORY = newEmoticonDir;
}
/**
* Returns the active emoticon set within Spark.
*
* @return the active set of emoticons.
*/
public Collection<Emoticon> getActiveEmoticonSet() {
final LocalPreferences pref = SettingsManager.getLocalPreferences();
String emoticonPack = pref.getEmoticonPack();
return emoticonMap.get(emoticonPack);
}
/**
* Returns the name of the active emoticon set.
*
* @return the name of the active emoticon set.
*/
public String getActiveEmoticonSetName() {
final LocalPreferences pref = SettingsManager.getLocalPreferences();
return pref.getEmoticonPack();
}
/**
* Sets the active emoticon set.
*
* @param pack the archive containing the emotiocon pack.
*/
public void setActivePack(String pack) {
final LocalPreferences pref = SettingsManager.getLocalPreferences();
pref.setEmoticonPack(pack);
imageMap.clear();
}
/**
* Installs a new Adium style emoticon pack into Spark.
*
* @param pack the emotiocn pack (contains Emotiocons.plist)
* @return the name of the newly installed emoticon set.
*/
public String installPack(File pack) {
if (!containsEmoticonPList(pack)) {
return null;
}
String name = null;
// Copy to the emoticon area
try {
URLFileSystem.copy(pack.toURL(), new File(EMOTICON_DIRECTORY, pack.getName()));
File rootDirectory = unzipPack(pack, EMOTICON_DIRECTORY);
name = URLFileSystem.getName(rootDirectory.toURL());
addEmoticonPack(name);
}
catch (IOException e) {
Log.error(e);
}
return name;
}
/**
* Loads an emoticon set.
*
* @param packName the name of the pack.
*/
public void addEmoticonPack(String packName) {
File emoticonSet = new File(EMOTICON_DIRECTORY, packName + ".adiumemoticonset");
if (!emoticonSet.exists()) {
emoticonSet = new File(EMOTICON_DIRECTORY, packName + ".AdiumEmoticonset");
}
if (!emoticonSet.exists()) {
emoticonSet = new File(EMOTICON_DIRECTORY, "Default.adiumemoticonset");
packName = "Default";
setActivePack("Default");
}
List<Emoticon> emoticons = new ArrayList<Emoticon>();
final File plist = new File(emoticonSet, "Emoticons.plist");
// Create SaxReader and set to non-validating parser.
// This will allow for non-http problems to not break spark :)
final SAXReader saxParser = new SAXReader();
saxParser.setValidation(false);
try {
saxParser.setFeature("http://xml.org/sax/features/validation", false);
saxParser.setFeature("http://xml.org/sax/features/namespaces", false);
saxParser.setFeature("http://apache.org/xml/features/validation/schema", false);
saxParser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", false);
saxParser.setFeature("http://apache.org/xml/features/validation/dynamic", false);
saxParser.setFeature("http://apache.org/xml/features/allow-java-encodings", true);
saxParser.setFeature("http://apache.org/xml/features/continue-after-fatal-error", true);
saxParser.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
saxParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
}
catch (SAXException e) {
e.printStackTrace();
}
Document emoticonFile = null;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?