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

📄 messageparser.java

📁 JAVA平台下优秀的CHART开源代码,可以实现类似EXCEL中的二维或三维的饼图/椎图功能.
💻 JAVA
字号:
/**
 * Copyright (C) 2003  Manfred Andres
 * 
 * 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 2
 * of the License, 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
/**
 * the command set is inside here
 */
package freecs.core;

import freecs.*;
import freecs.layout.*;
import freecs.commands.CommandSet;
import freecs.content.*;
import freecs.interfaces.*;

import java.nio.charset.Charset;
import java.nio.charset.UnmappableCharacterException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.HashMap;

/**
 * constructed with a sender (possible null) and a raw message, this
 * message-container is responsible for parsing the message. if the message
 * was parsed successfully, the apropriate flags get set and the parsing
 * thread decides what to do with this message.
 */
public class MessageParser implements IContainer {
	private MessageState	msgState;
	private ByteBuffer		bBuff = null;
	private HashMap			renderCache;
	private boolean			isHTTP11 = true;
	private CommandSet		cs;
	private RequestReader	req;

	public MessageParser () {
		renderCache = new HashMap ();
		msgState = new MessageState(this);
		cs = CommandSet.getCommandSet ();
		clear ();
	}
   public MessageParser (RequestReader r) {
      renderCache = new HashMap ();
      msgState = new MessageState(this);
      cs = CommandSet.getCommandSet ();
      clear ();
      req=r;
   }

	/**
	 * clear the state of this messagparser
	 */
	public void clear () {
		msgState.clear();
		renderCache.clear ();
		isHTTP11 = true;
	}

	/**
	 * set the http/1.1-capability for this message
	 * @param b if true HTTP1.1 is enabled, if false it will be disabled
	 */
   public void setHTTP11 (boolean b) {
      isHTTP11 = b;
   }

	public void setConnectionBuffer (ConnectionBuffer cb) {
		msgState.cb = cb;
	}

   /**
    * set the target to the given String
    * @param target the target
    */
   public void setTarget (String target) {
      msgState.target = target;
   }

   /**
    * set the source of this message to the given String
    * @param source the source
    */
   public void setSource (String source) {
      msgState.source = source;
   }

   /**
    * set the sender of this message
    * @param s the user which sent this message
    */
   public void setSender (User s) {
      msgState.sender = s;
      msgState.reciever = msgState.sender.getGroup ();
   }
   
   public void setUsercontext (User s) {
	  msgState.usercontext = s;
   }

	/**
	 * set the message
	 * @param msg the raw message
	 */
   public void setMessage (String msg) {
      msgState.msg = msg.trim ();
   }

	/**
	 * return the sender of this message
	 * @return the sender of this message
	 */
   public User getSender () {
      return msgState.sender;
   }

	/**
	 * interface contentcontainer deffines the following
	 * @return the ByteBuffer containing the bytes to send
	 */
   public ByteBuffer getByteBuffer () {
      if (bBuff != null) bBuff.rewind ();
      return (bBuff);
   }

   /**
    * returns always false for this, because the message-window doesn't get closed
    * except the user loggs out
    */
   public boolean closeSocket () {
      return false;
   }

	/**
	 * checks if there is actual some content to send
	 * @return true if there is content to send, false if not
	 */
   public boolean hasContent () {
      return (bBuff != null && bBuff.limit () > 0);
   }

	/**
	 * set the messagetemplate which should be used
	 * @param tplName the name of the message-template
	 */
   public void setMessageTemplate (String tplName) {
      msgState.msgTemplate = tplName;
   }

	/**
	 * get the personalized message for this user
	 * @param u the user to personalize the message for
	 * @return the IContainer containing the personalized message
	 */
   public IContainer getPersonalizedMessage (User u) {
      StringBuffer result = new StringBuffer ();
      TemplateSet ts = u.getTemplateSet ();
      StringBuffer tsb = new StringBuffer (ts.getName ()).append ("/").append (msgState.msgTemplate).append ("/").append (isHTTP11);
      String rcKey = tsb.toString ();
      // check if the wanted message is contained within the render-cache
      if (!msgState.useRenderCache || !renderCache.containsKey (rcKey)) {
         String msgTpl = ts.getMessageTemplate (msgState.msgTemplate);
         if (msgTpl == null || msgTpl.length () < 1) {
            tsb = new StringBuffer ("Message-Template ").append (msgState.msgTemplate).append (" was not found");
            Server.log (tsb.toString (), Server.MSG_ERROR, Server.LVL_MAJOR);
            if (Server.srv.DEBUG_TEMPLATESET) msgTpl = tsb.toString ();
            else return null;
         }
         String showTime = ts.getMessageTemplate ("status.showtime");
         if (showTime != null && (showTime.equals ("true") || showTime.equals("1"))) {
         	String tf = ts.getMessageTemplate ("status.showtime.timeformat");
         	if (tf != null) result.append (MessageRenderer.renderTemplate (msgState, tf, false));
         	else {
         	   result.append ("[");
         	   result.append (Server.srv.getFormatedTime ("HH:mm"));
         	   result.append ("] ");
         	}
         }
         result.append (MessageRenderer.renderTemplate (msgState, msgTpl));
         renderCache.put (rcKey, result);
      } else {
         result.append (renderCache.get(rcKey));
      }
      if (result.length() < 1) return null;
      CharBuffer cb = CharBuffer.wrap (result);
      try {
      	if (cb.length() < 2) {
      		if (msgState.msgTemplate.equals ("message.q") || msgState.msgTemplate.equals ("message.server.shutdown")) msgState.sender.removeNow(false);
      		return null;
      	}
         return new PersonalizedMessage (Charset.forName (Server.srv.DEFAULT_CHARSET).newEncoder ().encode (cb), (msgState.msgTemplate.equals ("message.q") || msgState.msgTemplate.equals ("message.server.shutdown")));
      } catch (UnmappableCharacterException uce) {
         Server.debug ("MessageParser.getPersonalizedMessage: ", uce, Server.MSG_ERROR, Server.LVL_MINOR);
         byte[] b = result.toString ().getBytes ();
         ByteBuffer bb = ByteBuffer.wrap (b);
         return new PersonalizedMessage (bb, (msgState.msgTemplate.equals ("message.q") || msgState.msgTemplate.equals ("message.server.shutdown")));
      } catch (Exception e) {
         Server.debug ("MessageParser.getPersonalizedMessage: ", e, Server.MSG_ERROR, Server.LVL_MAJOR);
         return this;
      }
   }

   public boolean prepareForSending () {
      if (this.bBuff != null) return true;
      return false;
   }

/*   public void finalize () {
      Server.log ("MessageParser FINALIZED*******************************", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
   } */

   /**
    *--------------------- MESSAGE-PARSING -------------------
    * this is where the parsing of the message happens.
    */
	public void parseAndSendMessage () {
		if (req!=null) req.currPosition = RequestReader.PARSE_MSG;
		if (msgState.msg == null 
			|| msgState.msg.length () < 1
			|| msgState.sender == null
			|| !msgState.cb.isValid()) {
			clear ();
			return;
		}
		// check if user was in away-state
		if (msgState.sender.isAway ()) {
			msgState.msgTemplate="message.away.off";
			msgState.message=msgState.sender.getAwayMessage();
			Group sg = msgState.sender.getGroup ();
			if (sg != null)
				sg.sendMessage (this);
			else
				msgState.sender.sendMessage (this);
			msgState.sender.setAway (false);
      	}

		if (!msgState.msg.startsWith ("/") || msgState.sender == null) {
			if (cs.isPunished (msgState)) {
				clear ();
				return;
			}
			msgState.msgTemplate = "message.send";
			msgState.message = msgState.msg;
			if (msgState.reciever == null) 
				msgState.sender.sendMessage (this);
			else 
				msgState.reciever.sendMessage (this);
			clear ();
			return;
		}

		if (req!=null) req.currPosition = RequestReader.EVALUATE_COMMAND;
		// retrieve the command-token
		int pos = msgState.msg.indexOf (" ");
		String cmd, param;
		if (pos > -1) {
			cmd = msgState.msg.substring(0,pos).toLowerCase();
			param = msgState.msg.substring (pos).trim ();
		} else {
			cmd = msgState.msg;
			param = "";
		}
		byte result = cs.evaluate(cmd, msgState, param); 
		if (result == CommandSet.UNKNOWN_COMMAND) {
			cs.evaluate("/m", msgState, msgState.msg.substring (1));
		} else if (result==CommandSet.INTERRUPTED) {
			msgState.cb.logError("ConnectionBuffer was invalidated");
		}
		clear ();
	}
}

⌨️ 快捷键说明

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