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

📄 emoticonfilter.java

📁 mywork是rcp开发的很好的例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                                continue;
                            }
                            if (!exists(pattern)) {
                                emoticonList.add(pattern);
                            }
                        }
                    } else if (st.ttype == StreamTokenizer.TT_NUMBER) {
                        //ignore numbers}
                    } else if (st.ttype == StreamTokenizer.TT_EOL) {
                        newLine = true;
                    }
                }
            } catch (IOException ioe) {
                LOG.log(Level.SEVERE, "Caught unexpected Exception", ioe);
            }
        }
        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("Map:patterns = [");
            for (Iterator<String> pit = patterns.keySet().iterator(); pit.hasNext();) {
                for (Iterator lit = patterns.get(pit.next()).iterator(); lit.hasNext();) {
                    LOG.fine("                " +
                            ((Pattern) lit.next()).pattern());
                }
            }
            LOG.fine("               ]");
        }
    }

    /**
     * Finds emoticon sequences in the message text, and substitutes the URL of the corresponding image.
     * Uses {@link java.util.regex.Pattern regex Pattern} and {@link java.util.regex.Matcher regex Matcher}
     * to find emoticon sequences.
     * <p/>
     * No substitution occurs if the emoticon sequence is preceeded by the escape character "/".
     * <p/>
     * Also, certain HTML Character Entities are check for to ensure that if they occur near a paren
     * that they don't cause extraneous emoticon images to be generated.
     *
     * @param msg the newly received DialogMessage object
     * @return the newly modified DialogMessage object.
     * @see net.jxta.myjxta.dialog.DialogFilter#filter(net.jxta.myjxta.dialog.DialogMessage)
     */
    public DialogMessage filter(DialogMessage msg) {
        if (!processMessage((DialogMessageWrapper) msg)) {
            return msg;
        }

        String msgText = msg.getHtmlMessage() != null ? msg.getHtmlMessage().trim() : "";
        LOG.fine("Begin filter(DialogMessage)");
        LOG.fine("msgText = " + msgText);

        if (msgText != null && msgText.length() > 0) {
            Pattern pattern = null;
            List patternList = null;
            Matcher matcher = null;
            String resource = null;
            URL emoticonUrl = null;
            StringBuffer workingBuff = new StringBuffer();

            for (Iterator<String> resources = patterns.keySet().iterator(); resources.hasNext();) {
                resource = resources.next();
                patternList = patterns.get(resource);
                for (Iterator patternIt = patternList.iterator(); patternIt.hasNext();) {
                    pattern = (Pattern) patternIt.next();
                    matcher = pattern.matcher(msgText);
                    if (matcher.find()) {
                        String match = matcher.group();
                        String[] nonMatchingText = pattern.split(msgText);

                        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
                            LOG.fine("Found matching text \"" + match +
                                    "\" for pattern \"" + pattern.pattern() +
                                    "\"");
                            StringBuffer nmtString = new StringBuffer("[").append(Constants.CRLF).append("\"").append(nonMatchingText[0]).append("\"");

                            for (int i = 1; i < nonMatchingText.length; i++) {
                                nmtString.append(",").append(Constants.CRLF).append("\"").append(nonMatchingText[i]).append("\"");
                            }
                            nmtString.append(Constants.CRLF).append("]");
                            LOG.fine("nonMatchingText = " + nmtString.toString());
                            LOG.fine("nonMatchingText.length = " +
                                    nonMatchingText.length);
                        }
//                        emoticonUrl = Resources.class.getResource(resource);
                        String imageTag = PREFIX + resource + POSTFIX;
                        int textIndex = 0;

                        while (textIndex < nonMatchingText.length - 1) {
                            if (nonMatchingText[textIndex].endsWith(EMOTICON_ESCAPE_CHAR)) {
                                LOG.fine("Escaping emoticon");
                                workingBuff.append(nonMatchingText[textIndex].substring(0,
                                        nonMatchingText[textIndex].length() -
                                                1));
                                workingBuff.append(match);
                            } else {
                                int ampX = nonMatchingText[textIndex].lastIndexOf('&');

                                if (match.startsWith(";") && ampX >= 0) {
                                    String testEntityChar = nonMatchingText[textIndex].substring(ampX);

                                    if (CHAR_ENTITIES.contains(testEntityChar)) {
                                        LOG.fine("Character Entity, not real emoticon");
                                        workingBuff.append(nonMatchingText[textIndex]);
                                        workingBuff.append(match);
                                    } else {
                                        LOG.fine("Substituting emoticon");
                                        workingBuff.append(nonMatchingText[textIndex]).append(imageTag).append(" ");
                                    }
                                } else {
                                    LOG.fine("Substituting emoticon");
                                    workingBuff.append(nonMatchingText[textIndex]).append(imageTag).append(" ");
                                }
                            }
                            textIndex++;
                        }
                        if(textIndex < nonMatchingText.length)
                        	workingBuff.append(nonMatchingText[textIndex]);
                        if(workingBuff.indexOf(imageTag) == -1)
                        	workingBuff.append(imageTag);
                        // update the msgText and reset the working text for the next emoticon
                        msgText = workingBuff.toString();
                        workingBuff.setLength(0);
                    }
                }
            }

            msg.setHtmlMessage(msgText);
        }

        if (Logging.SHOW_FINE && LOG.isLoggable(Level.FINE)) {
            LOG.fine("returning: " + msgText);
            LOG.fine("End   filter(DialogMessage)");
        }
        return msg;
    }

    private static boolean exists(Pattern msgPat) {
        for (String key : patterns.keySet()) {
            Set<Pattern> patternSet = new HashSet<Pattern>(patterns.get(key));
            if (patternSet.contains(msgPat)) {
                return true;
            }
        }

        return false;
    }

    private static String escape(String s) {
        if (s == null) {
            return null;
        }

        char[] c = s.toCharArray();
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < c.length; i++) {
            if (!Character.isLetterOrDigit(c[i])) {
                sb.append(ESCAPE_PATTERN_PATTERN);
            }

            sb.append(c[i]);
        }

//        return c != null ? (ESCAPE_EMOTICON_PATTERN + "(" + sb.toString() + ")") : s;
//        return c != null ? (ESCAPE_EMOTICON_PATTERN + "(" + CHAR_ENTITIES_PATTERN + sb.toString() + ")") : s;
        return sb.toString();
    }
}

⌨️ 快捷键说明

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