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

📄 analyzestring.java

📁 使用java rmi编写的一个议程服务系统
💻 JAVA
字号:
package agendaClient;

import java.text.*;
import java.util.Date;
import java.util.regex.*;

/**
 * 检查输入的字符串是否是约束的输入格式
 * @author Crise.Lee
 *
 */
public class AnalyzeString 
{
	static final String format="yyyy/MM/dd/HH:mm";
	static String[] strAnalyze=new String[12];
	public AnalyzeString()
	{
		
	}
	public AnalyzeString(String command) throws Exception
	{
		AnalyzeCommand(command);
	}
	
	
	/**
	 * 将字符串转换成时间
	 * @param strDate
	 * @return
	 * @throws Exception
	 */
	public Date strToDate(String strDate) throws Exception
	{
		SimpleDateFormat dateFormat=new SimpleDateFormat(format);
		Date date=null;
		try {
			date=dateFormat.parse(strDate);
		} catch (ParseException e) {
			System.out.println("错误:输入的日期格式错误");
			//e.printStackTrace();
			throw new Exception("错误:输入的日期格式错误");
		}
		return date;
	}
	
	/**
	 * 判断时间的先后顺序
	 * @param start
	 * @param end
	 * @return
	 */
	public Boolean isOrder(Date start,Date end)
	{
		if(start.before(end))
			return true;
		else 
			return false;
	}
	
	/**
	 * 分析输入的字符串,将其转换称参数。
	 * @param command
	 * @throws Exception 
	 */
	public Object[] AnalyzeCommand(String strCommand) throws Exception
	{
		String readline=null;
		Pattern PCommand=Pattern.compile("[\\w|-|/|:]+");
		Matcher m=PCommand.matcher(strCommand);
		           //初始化命令数组
		iniArray();
		
		int i=0;
		while(m.find())
		{
			readline=m.group();
			System.out.print(readline+" ");
			strAnalyze[i++]=readline;	
		}
		System.out.println();
	/*	for(i=0;i<strAnalyze.length;i++)
		{
			System.out.println("strAnalyze["+i+"]"+strAnalyze[i]);
		}*/
		if(strAnalyze[4]==null)
			throw new CommandFatmatException("错误: 输入的参数太少,请参照一下的命令格式:");
		
		Object[] command=null;
		if(strAnalyze[0].equalsIgnoreCase("java")&&strAnalyze[1].equalsIgnoreCase("client"))
		{
			readline=strAnalyze[3];
			if(!isPort(readline))
			{
//				System.out.println("int the analyze,port error");
				throw new Exception("int the analyze,port error");
			}
            
			readline=strAnalyze[4].toLowerCase();
			if(readline.equals("register"))
			{
				if(strAnalyze[6]==null)
					throw new Exception("错误原因:register 命令参数不足。");
				else if(strAnalyze[7]!=(null))
					throw new Exception("错误原因:register 命令参数过多。");
				command=new Object[5];
				//给命令一个标识
				command[0]=new Integer(1); 
				//hose name
				command[1]=new String(strAnalyze[2]);
				//port number
				command[2]=new Integer(strAnalyze[3]);
				//User name and password
				command[3]=new String (strAnalyze[5]);
				command[4]=new String(strAnalyze[6]);
				
			}else if(readline.equals("add"))
			{
				if(strAnalyze[10]==null)
					throw new Exception("错误原因:add命令参数不足。");
				else if(strAnalyze[11]!=null)
					throw new Exception("错误原因:add命令参数过多。");
				
				Date start =  strToDate(strAnalyze[8]);
				Date end = strToDate(strAnalyze[9]);
				if(isOrder(start,end))
				{
					command=new Object[9];
					//给命令一个标识
					command[0]=new Integer(2); 
					//hose name
					command[1] = new String(strAnalyze[2]);
					//port number
					command[2] = new Integer(strAnalyze[3]);
					//User name and password
					command[3] = new String (strAnalyze[5]);
					command[4] = new String(strAnalyze[6]);
					//other start end title
					command[5] = new String(strAnalyze[7]);
					command[6] = start;
					command[7] = end;
					command[8] = new String (strAnalyze[10]);	
				}
			}else if(readline.equals("query"))
			{
				if(strAnalyze[8]==null)
					throw new Exception("错误原因:query命令参数不足。");
				else if(strAnalyze[9]!=null)
					throw new Exception("错误原因:query命令参数过多。");
				
				Date start =  strToDate(strAnalyze[7]);
				Date end = strToDate(strAnalyze[8]);
				if(isOrder(start,end))
				{
					command=new Object[7];	
					//给命令一个标识
					command[0]=new Integer(3); 
					//hose name
					command[1] = new String(strAnalyze[2]);
					//port number
					command[2] = new Integer(strAnalyze[3]);
					//User name and password
					command[3] = new String (strAnalyze[5]);
					command[4] = new String(strAnalyze[6]);
					// start end title
					command[5] = start;
					command[6] = end;
				}
			}else if(readline.equals("delete"))
			{
				if(strAnalyze[7]==null)
					throw new Exception("错误原因:delete 命令参数不足。");
				else if(strAnalyze[8]!=null)
					throw new Exception("错误原因:delete 命令参数过多。");
					command=new Object[6];
					
					//给命令一个标识
					command[0]=new Integer(4); 
					//hose name
					command[1] = new String(strAnalyze[2]);
					//port number
					command[2] = new Integer(strAnalyze[3]);
					//User name and password
					command[3] = new String (strAnalyze[5]);
					command[4] = new String(strAnalyze[6]);
					//title
					command[5] = new String(strAnalyze[7]);
				
			}else if(readline.equals("clear"))
			{
				if(strAnalyze[6]==null)
					throw new Exception("错误原因:clear 命令参数不足。");
				else if(strAnalyze[7]!=null)
					throw new Exception("错误原因:clear 命令参数过多。");
			//	/
					command=new Object[5];
					//给命令一个标识
					command[0]=new Integer(5); 
					//hose name
					command[1] = new String(strAnalyze[2]);
					//port number
					command[2] = new Integer(strAnalyze[3]);
					//User name and password
					command[3] = new String (strAnalyze[5]);
					command[4] = new String(strAnalyze[6]);
			}else{
				throw new CommandFatmatException("错误原因:输入的参数格式发生错误,请查证!!!");
			}	
		}else{
			System.out.println("This command is not the system's command!");
			throw new CommandFatmatException("错误原因:输入的参数格式发生错误,请查证!!!");
		}
		return command;
		
	}
	/**
	 * 保证数组中没有内容
	 * @param strAnalyze
	 */
	private void iniArray()
	{
		for(int i=0;i<strAnalyze.length;i++)
		{
			strAnalyze[i]=null;
		}
	}
	
	
	/**
	 * 判断是否是一个合法的端口号
	 * @param port
	 * @return
	 * @throws Exception
	 */
	public Boolean isPort(String port) throws Exception
	{
		try
		{
			Integer.valueOf(port);
		}catch (NumberFormatException e)
		{
			throw new Exception("Port format error");
		}	
		return true;
	}
	
	
	/*public Object[] analyze() throws Exception
	{
		
	}*/
	
	
	
	public static void main(String [] args)
	{
		try {
			new AnalyzeString("JAVA  jfsdjf 2008/2/30/14:20 DFSF FSFS");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//a.analyze();
	}
	

}

⌨️ 快捷键说明

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