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

📄 anyincomingpacketlistener.java

📁 网站即时通讯系统
💻 JAVA
字号:
/* Copyright (C) 2003 Adam Olsen This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother.jabber.smack;import java.io.StringReader;import java.io.StringWriter;import java.util.Date;import java.util.ResourceBundle;import java.util.Locale;import javax.swing.SwingUtilities;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.stream.StreamResult;import javax.xml.transform.stream.StreamSource;import org.jivesoftware.smack.PacketListener;import org.jivesoftware.smack.packet.Packet;import com.valhalla.jbother.BuddyList;import com.valhalla.jbother.ConversationText;import com.valhalla.jbother.jabber.BuddyStatus;import com.valhalla.jbother.jabber.ParsedBuddyInfo;/** * Listens for all incoming packets, and sends it to the XML Console window (if * exists) * * @author Andrey Zakirov * @created March 2, 2005 * @version 0.1 */public class AnyIncomingPacketListener implements PacketListener {    private ResourceBundle resources = ResourceBundle.getBundle(            "JBotherBundle", Locale.getDefault());    /**     * Constructor for the packet listener     */    public AnyIncomingPacketListener() {    }    /**     * Processes the message packet     *     * @param packet     *            packet to process     */    public void processPacket(Packet packet) {        final String tempMessage;        String parsed = formatXML(packet.toXML());        String text = ConversationText.replaceText(parsed, false, false);        String lines[] = text.split("\n");        text = "";        for (int i = 0; i < lines.length; i++) {            text += "<font color='#16569e'>" + lines[i] + "</font>\n";        }        text = text.replaceAll("\n$", "");        tempMessage = text;        String from = resources.getString("xmlConsole");        ParsedBuddyInfo info = new ParsedBuddyInfo(from);        String userId = info.getUserId().toLowerCase();        final BuddyStatus buddyStatus = BuddyList.getInstance().getBuddyStatus(                userId);        if (buddyStatus.getConversation() == null) {            return;        }        SwingUtilities.invokeLater(new Runnable() {            public void run() {                buddyStatus.getConversation().receiveMessage("", "", tempMessage,                        "", new Date(), false, false);            }        });    }    /**     * Description of the Method     *     * @param str     *            Description of the Parameter     * @return Description of the Return Value     */    public static String formatXML(String str) {        try {            // Use a Transformer for output            TransformerFactory tFactory = TransformerFactory.newInstance();            // Surround this setting in a try/catch for compatibility with Java            // 1.4. This setting is required            // for Java 1.5            try {                tFactory.setAttribute("indent-number", new Integer(8));            } catch (IllegalArgumentException e) {            }            Transformer transformer = tFactory.newTransformer();            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,                    "yes");            transformer.setOutputProperty(OutputKeys.INDENT, "yes");            transformer.setOutputProperty(                    "{http://xml.apache.org/xalan}indent-amount", "8");            transformer.setOutputProperty(                    "{http://xml.apache.org/xslt}indent-amount", "8");            // Transform the requested string into a nice formatted XML string            StreamSource source = new StreamSource(new StringReader(str));            StringWriter sw = new StringWriter();            StreamResult result = new StreamResult(sw);            transformer.transform(source, result);            return sw.toString().replaceAll("\\s*$", "");        } catch (TransformerConfigurationException tce) {            // Use the contained exception, if any            Throwable x = tce;            if (tce.getException() != null) {                x = tce.getException();            }        } catch (TransformerException te) {            // Use the contained exception, if any            Throwable x = te;            if (te.getException() != null) {                x = te.getException();            }        }        return str.replaceAll("\\s*$", "");    }}

⌨️ 快捷键说明

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