⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 emoticonfilter.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
*  Copyright (c) 2001 Sun Microsystems, Inc.  All rights
*  reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*  1. Redistributions of source code must retain the above copyright
*  notice, this list of conditions and the following disclaimer.
*
*  2. Redistributions in binary form must reproduce the above copyright
*  notice, this list of conditions and the following discalimer in
*  the documentation and/or other materials provided with the
*  distribution.
*
*  3. The end-user documentation included with the redistribution,
*  if any, must include the following acknowledgment:
*  "This product includes software developed by the
*  Sun Microsystems, Inc. for Project JXTA."
*  Alternately, this acknowledgment may appear in the software itself,
*  if and wherever such third-party acknowledgments normally appear.
*
*  4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA"
*  must not be used to endorse or promote products derived from this
*  software without prior written permission. For written
*  permission, please contact Project JXTA at http://www.jxta.org.
*
*  5. Products derived from this software may not be called "JXTA",
*  nor may "JXTA" appear in their name, without prior written
*  permission of Sun.
*
*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
*  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
*  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
*  DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
*  ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
*  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
*  SUCH DAMAGE.
*  ====================================================================
*
*  This software consists of voluntary contributions made by many
*  individuals on behalf of Project JXTA.  For more
*  information on Project JXTA, please see
*  <http://www.jxta.org/>.
*
*  This license is based on the BSD license adopted by the Apache Foundation.
*
*  $Id: EmoticonFilter.java,v 1.7 2007/05/28 22:00:52 nano Exp $
*/

package net.jxta.myjxta.dialog.filter;

import net.jxta.logging.Logging;
import net.jxta.myjxta.dialog.DialogMessage;
import net.jxta.myjxta.dialog.DialogMessageWrapper;
import net.jxta.myjxta.util.Constants;
import net.jxta.myjxta.util.Resources;
import net.sf.p2pim.P2PActivator;

import java.io.*;
import java.net.URL;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.eclipse.ui.internal.util.BundleUtility;

/**
 * Substitues emoticon graphics for emoticon ascii sequences.
 * See net/jxta/myjxta/resources/themes/theme.txt for a list of emoticon sequences.
 * <pattern>
 * No substitution occurs if the emoticon sequence is preceeded by the escape character "/".
 *
 * @author james todd [gonzo at jxta dot org]
 * @author mike mcangus [mcangus at jxta dot org]
 * @version $Id: EmoticonFilter.java,v 1.7 2007/05/28 22:00:52 nano Exp $
 * 20071113 修订取表情图释
 */

public class EmoticonFilter extends AbstractDialogFilter {

    private static final Logger LOG = Logger.getLogger(EmoticonFilter.class.getName());
    private static final String defaultTheme =
            "/net/jxta/myjxta/resources/themes/crystal-gaim/theme.txt";
    private static final String DELIMITER = "/";
    private static final String EMOTICON_ESCAPE_CHAR = "/";
    private static final String ESCAPE_PATTERN_PATTERN = "\\";
    private static final String PREFIX = "<img src=data/embed/faces/";
    private static final String POSTFIX = " width=22 height=22></img>";

    // Small set of only the most often used HTML Character Entities
    private static final Set<String> CHAR_ENTITIES;

    static {
        CHAR_ENTITIES = new HashSet<String>(16);
        CHAR_ENTITIES.add("&amp");
        CHAR_ENTITIES.add("&copy");
        CHAR_ENTITIES.add("&gt");
        CHAR_ENTITIES.add("&lt");
        CHAR_ENTITIES.add("&nbsp");
        CHAR_ENTITIES.add("&quot");
        CHAR_ENTITIES.add("&reg");
    }

    private static Map<String, List<Pattern>> patterns;

    // Loads the patterns map with emoticon graphic URL and a List of the emoticons for them.
    // Keeps the list in the same order as they appear in the theme.txt file.  This allows us to ensure that emoticons
    // that are extensions of other emoticons ( e.g., >:O extends :O ) are always displayed properly if they appear 
    // in the file before the emoticons that they extend.  (clear?)
    static {
        Constants c = Constants.getInstance();
        String theme = c.get(Constants.THEME_RESOURCES, defaultTheme);
        String base = "";//theme.substring(0, theme.lastIndexOf(DELIMITER) + DELIMITER.length());
        InputStream is=null;//=Resources.class.getResourceAsStream(theme);
		try {
			is = BundleUtility.find(P2PActivator.PLUGIN_ID, "icons/faces/theme.txt").openStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
        patterns = new LinkedHashMap<String, List<Pattern>>();

        if (is != null) {
            StreamTokenizer st =
                    new StreamTokenizer(new BufferedReader(new InputStreamReader(is)));

            st.ordinaryChars('0', '9');
            st.wordChars('!', '~');
            st.commentChar('#');
            st.eolIsSignificant(true);

            try {
                String msgText = null;
                String t = null;
                String resource = null;
                boolean newLine = true;
                List<Pattern> emoticonList = null;
                Pattern pattern = null;

                while (st.nextToken() != StreamTokenizer.TT_EOF) {
                    if (st.ttype == StreamTokenizer.TT_WORD) {
                        msgText = st.sval;

                        if (newLine) {
                            newLine = false;
                            resource = base + msgText;
                            if (patterns.containsKey(resource)) {
                                // if the same resource was previously defined in the themes file
                                // we keep adding to its list of ascii emoticons.
                                emoticonList = patterns.get(resource);
                            } else {
                                emoticonList = new ArrayList<Pattern>();
                                patterns.put(resource, emoticonList);
                            }
                        } else {
                            t = escape(msgText);
                            try {
                                pattern = Pattern.compile(t);
                            } catch (PatternSyntaxException pse) {
                                LOG.log(Level.SEVERE,
                                        "Caught unexpected Exception", pse);

⌨️ 快捷键说明

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