emoticonmanager.java.svn-base

来自「开源项目openfire的完整源程序」· SVN-BASE 代码 · 共 481 行 · 第 1/2 页

SVN-BASE
481
字号
        try {
            emoticonFile = saxParser.read(plist);
        }
        catch (DocumentException e) {
            Log.error(e);
            return;
        }

        Node root = emoticonFile.selectSingleNode("/plist/dict/dict");

        List keyList = root.selectNodes("key");
        List dictonaryList = root.selectNodes("dict");

        Iterator keys = keyList.iterator();
        Iterator dicts = dictonaryList.iterator();

        while (keys.hasNext()) {
            Element keyEntry = (Element)keys.next();
            String key = keyEntry.getText();

            Element dict = (Element)dicts.next();
            String name = dict.selectSingleNode("string").getText();

            // Load equivilants
            final List<String> equivs = new ArrayList<String>();
            final List equivilants = dict.selectNodes("array/string");
            Iterator iter = equivilants.iterator();
            while (iter.hasNext()) {
                Element equivilant = (Element)iter.next();
                String equivilantString = equivilant.getText();
                equivs.add(equivilantString);
            }

            final Emoticon emoticon = new Emoticon(key, name, equivs, emoticonSet);
            emoticons.add(emoticon);
        }

        emoticonMap.put(packName, emoticons);
    }

    /**
     * Retrieve the URL to an emoticon.
     *
     * @param emoticon the emoticon.
     * @return the URL of the image.
     */
    public URL getEmoticonURL(Emoticon emoticon) {
        final String imageName = emoticon.getImageName();

        File file = new File(emoticon.getEmoticonDirectory(), imageName);
        try {
            return file.toURL();
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Retrieves the associated key emoticon.
     *
     * @param packName the name of the Archive Pack File.
     * @param key      the key.
     * @return the emoticon.
     */
    public Emoticon getEmoticon(String packName, String key) {
        final Collection<Emoticon> emoticons = emoticonMap.get(packName);

        for (Emoticon emoticon : emoticons) {
            for (String string : emoticon.getEquivalants()) {
                if (key.equals(string)) {
                    return emoticon;
                }
            }
        }

        return null;
    }

    /**
     * Returns the <code>Emoticon</code> associated with the given key. Note: This gets the emoticon from
     * the active emoticon pack.
     *
     * @param key the key.
     * @return the Emoticon found. If no emoticon is found, null is returned.
     */
    public Emoticon getEmoticon(String key) {
        final Collection<Emoticon> emoticons = emoticonMap.get(getActiveEmoticonSetName());

        for (Emoticon emoticon : emoticons) {
            for (String string : emoticon.getEquivalants()) {
                if (key.toLowerCase().equals(string.toLowerCase())) {
                    return emoticon;
                }
            }
        }

        return null;
    }

    /**
     * Returns the Icon that is mapped to a given key.
     *
     * @param key the key to search for.
     * @return the Icon representing the key.
     */
    public ImageIcon getEmoticonImage(String key) {
        final Emoticon emoticon = getEmoticon(key);
        if (emoticon != null) {
            ImageIcon icon = imageMap.get(key);
            if (icon == null) {
                URL url = getEmoticonURL(emoticon);
                icon = new ImageIcon(url);
                imageMap.put(key, icon);
            }

            return imageMap.get(key);
        }

        return null;
    }

    /**
     * Returns a list of all available emoticon packs.
     *
     * @return Collection of Emoticon Pack names.
     */
    public Collection<String> getEmoticonPacks() {
        final List<String> emoticonList = new ArrayList<String>();

        File[] dirs = EMOTICON_DIRECTORY.listFiles();
        for (int i = 0; i < dirs.length; i++) {
            File file = dirs[i];
            if (file.isDirectory() && file.getName().toLowerCase().endsWith("adiumemoticonset")) {
                try {
                    String name = URLFileSystem.getName(file.toURL());
                    name = name.replaceAll("adiumemoticonset", "");
                    name = name.replaceAll("AdiumEmoticonset", "");
                    emoticonList.add(name);
                }
                catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        }
        return emoticonList;
    }

    /**
     * Expands any zipped Emoticon Packs.
     */
    private void expandNewPack(File file, File dist) {
        URL url = null;
        try {
            url = file.toURL();
        }
        catch (MalformedURLException e) {
            Log.error(e);
        }
        String name = URLFileSystem.getName(url);
        File directory = new File(dist, name);

        // Unzip contents into directory
        unzipPack(file, directory.getParentFile());
    }

    /**
     * Checks zip file for the Emoticons.plist file. This is Sparks way of detecting a valid file.
     *
     * @param zip the zip file to check.
     * @return true if the EmoticonPlist exists in the archive.
     */
    private boolean containsEmoticonPList(File zip) {
        try {
            ZipFile zipFile = new JarFile(zip);
            for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
                JarEntry entry = (JarEntry)e.nextElement();
                if (entry.getName().contains("Emoticons.plist")) {
                    return true;
                }
            }
        }
        catch (IOException e) {
            Log.error(e);
        }
        return false;
    }

    /**
     * Unzips a theme from a ZIP file into a directory.
     *
     * @param zip the ZIP file
     * @param dir the directory to extract the plugin to.
     * @return the root directory.
     */
    private File unzipPack(File zip, File dir) {
        File rootDirectory = null;
        try {
            ZipFile zipFile = new JarFile(zip);

            dir.mkdir();
            for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
                JarEntry entry = (JarEntry)e.nextElement();
                File entryFile = new File(dir, entry.getName());
                // Ignore any manifest.mf entries.
                if (entry.getName().toLowerCase().endsWith("manifest.mf")) {
                    continue;
                }

                if (entry.isDirectory() && rootDirectory == null) {
                    rootDirectory = entryFile;
                }

                if (!entry.isDirectory()) {
                    entryFile.getParentFile().mkdirs();
                    FileOutputStream out = new FileOutputStream(entryFile);
                    InputStream zin = zipFile.getInputStream(entry);
                    byte[] b = new byte[512];
                    int len = 0;
                    while ((len = zin.read(b)) != -1) {
                        out.write(b, 0, len);
                    }
                    out.flush();
                    out.close();
                    zin.close();
                }
            }
            zipFile.close();
            zipFile = null;
        }
        catch (Exception e) {
            Log.error("Error unzipping emoticon pack", e);
        }

        return rootDirectory;
    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?