?? analyzestring.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 + -