[仗贱天涯]Code-AuthorParser

/* * 创建日期 2005-11-13 * * TODO 要更改此生成的文件的模板,请转至 * 窗口 - 首选项 - Java - 代码样式 - 代码模板 */ package com.ty.parse; import java.util.ArrayList; import java.util.List; import com.ty.cc.Lexer; import com.ty.cfg.TYConfig; import com.ty.cfg.XMLNode; import com.ty.domain.Author; import com.ty.util.Logger; /** * @author Administrator * * 根据配置文档解析用户信息 */ public class AuthorParser { private static AuthorParser self=null; private List fieldList=new ArrayList(); private AuthorParser(){ init(); } public int getFieldCount(){ return fieldList.size(); } public AuthorField getAuthorField(int i){ return (AuthorField)fieldList.get(i); } private void init(){ XMLNode root=TYConfig.getInstance().getNode("/ty/user-detail"); XMLNode fieldNode; AuthorField field; for(int i=0,n=root.getChildNodeCount();i<n;i++){ field=new AuthorField(); fieldNode=root.getChildNode(i); field.index=i; field.name=fieldNode.getName(); field.tag=fieldNode.getChildNode("tag").getValue(); field.skip=Integer.parseInt(fieldNode.getChildNode("next").getValue().trim()); field.re=fieldNode.getChildNode("return").getValue().toLowerCase().trim(); field.initReturnParser();//初始化解析对象 fieldList.add(field); } Logger.log("用户配置字段:"+fieldList); } public synchronized static AuthorParser getInstance(){ if(self==null){ self=new AuthorParser(); } return self; } //配置字段 class AuthorField{ int index; String name; String tag; int skip; String re; private ReturnValueParser vp; void initReturnParser(){ if(re.startsWith("substring")){//substring vp=new DoSubstringReturn(this); }else{ vp=new DoNothingReturn(this); } } public String toString(){ return "<user-field>name="+name+", next="+skip+", return="+re; } public boolean matchTag(String value){ return value.matches(".*"+tag+".*"); } public String pathMatchValue(Lexer lexer,String currentToken){ return vp.pathMatchValue(lexer,currentToken); } } //配置解析执行者 class ReturnValueParser{ protected AuthorField af; public ReturnValueParser(AuthorField af){ this.af=af; } public String pathMatchValue(Lexer lexer,String currentToken){ String ww=null; if(af.skip>0){ lexer.skipToken(af.skip); ww=lexer.nextToken(); }else{ ww=currentToken; } ww=trimAndTurn(trimAndTurn(ww)); return doReturn(ww); } //删除前面空格/t/n并翻转字符串,调用该函数两次 private String trimAndTurn(String stmt){ boolean begined=false; int from=0; char[] chs=stmt.toCharArray(); int m=chs.length; for(int i=0;i<m;i++){ if(begined){ break; } switch(chs[i]){ case ' ': case '/t': case '/n':{ from++; break; } default:{ begined=true; } } } char[] ss=new char[m-from]; for(int i=0,n=ss.length;i<n;i++){ ss[i]=chs[m-i-1]; } return new String(ss,0,ss.length); } public String doReturn(String value){ return value; } } class DoNothingReturn extends ReturnValueParser{ public DoNothingReturn(AuthorField af){ super(af); } } class DoSubstringReturn extends ReturnValueParser{ private int from; private int to; public DoSubstringReturn(AuthorField af){ super(af); String gz=af.re; int i1=gz.indexOf('('); int i2=gz.indexOf(','); int i3=gz.indexOf(')'); from=Integer.parseInt(gz.substring(i1+1,i2)); to=Integer.parseInt(gz.substring(i2+1,i3)); } public String doReturn(String value){ int s1=from; int s2=to; if(s1<0){ s1+=value.length()+1; } if(s2<0){ s2+=value.length()+1; } return value.substring(s1,s2); } } public static Author parseAuthor(String s){ Author author=new Author(); try{ Lexer myLex = new Lexer(new StringBuffer(s)); AuthorParser ap=AuthorParser.getInstance(); AuthorField field; String ww; String value; for(int i=0,n=ap.getFieldCount();i<n;i++){ field=ap.getAuthorField(i); while(!myLex.eof()){ ww=myLex.nextToken(); if(field.matchTag(ww)){ value=field.pathMatchValue(myLex,ww); if("name".equals(field.name)){ author.setName(value); }else if("sex".equals(field.name)){ author.setMale("男".equals(value)); }else if("province".equals(field.name)){ author.setProvince(value); }else if("married".equals(field.name)){ author.setMarried("已婚".equals(value)); } break; } } } }catch(Exception ex){ throw new com.ty.parse.ParseException(ex); } return author; } }

你可能感兴趣的:(java,String,null,Class,文档,import)