📄 emailaddress.java
字号:
package net.meybo.mail.client;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import com.easyjf.web.tools.*;
import net.meybo.mail.Config;
import net.meybo.util.CommUtil;
import net.meybo.util.XmlFileTools;
public class EmailAddress {
private String userName;
private String domain;
private String basePath;
public EmailAddress()
{
this("","");
}
public EmailAddress(String userName,String domain)
{
this.userName=userName;
this.domain=domain;
this.basePath=Config.getMailPath()+"/"+domain+"/"+userName+"/address.xml";
if(!checkFormat())create();
}
private boolean checkFormat()
{
boolean ret=false;
Document doc;
try{
doc=XmlFileTools.load(basePath);
Node node=doc.selectSingleNode("/root");
if(node!=null)ret=true;
}
catch(Exception e)
{
System.out.println("文件格式错误:"+e);
}
return ret;
}
private void create()
{
try{
Document doc=DocumentHelper.parseText("<root/>");
XmlFileTools.write(basePath,doc);
}
catch(Exception e)
{
}
}
public MailAddress get(String id)
{
MailAddress addr=null;
Document doc;
try{
doc=XmlFileTools.load(basePath);
Element el=(Element)doc.selectSingleNode("/root/address[@cid='"+id+"']");
if(el!=null)
{
addr=new MailAddress();
addr.setCid(el.attributeValue("cid"));
addr.setName(el.attributeValue("name"));
addr.setEmail(el.attributeValue("email"));
addr.setUserName(el.attributeValue("userName"));
}
}
catch(Exception e)
{
}
return addr;
}
public MailAddress readByMail(String mail)
{
MailAddress addr=null;
Document doc;
try{
doc=XmlFileTools.load(basePath);
Element el=(Element)doc.selectSingleNode("/root/address[@email='"+mail+"']");
if(el!=null)
{
addr=new MailAddress();
addr.setCid(el.attributeValue("cid"));
addr.setName(el.attributeValue("name"));
addr.setEmail(el.attributeValue("email"));
addr.setUserName(el.attributeValue("userName"));
}
}
catch(Exception e)
{
}
return addr;
}
public int add(MailAddress addr)
{
int ret=-1;
Document doc;
try{
doc=XmlFileTools.load(basePath);
Element el=(Element)doc.selectSingleNode("/root/address[@cid='"+addr.getCid()+"']");
Node node=doc.selectSingleNode("/root/address[@name='"+addr.getName()+"']");
if(node!=null)
{
System.out.println("用户已经存在!");
}
else if(el==null)
{
el=DocumentHelper.createElement("address");
el.addAttribute("cid",addr.getCid());
el.addAttribute("name",addr.getName());
el.addAttribute("email",addr.getEmail());
el.addAttribute("userName",addr.getUserName());
((Element)doc.selectSingleNode("/root")).add(el);
if(XmlFileTools.write(basePath,doc))ret=1;
}
}
catch(Exception e)
{
}
return ret;
}
public int del(MailAddress addr)
{
int ret=-1;
Document doc;
try{
doc=XmlFileTools.load(basePath);
Element el=(Element)doc.selectSingleNode("/root/address[@cid='"+addr.getCid()+"']");
if(el!=null)
{
el.detach();
if(XmlFileTools.write(basePath,doc))ret=1;
}
}
catch(Exception e)
{
}
return ret;
}
public int update(MailAddress addr)
{
int ret=-1;
Document doc;
try{
doc=XmlFileTools.load(basePath);
Element el=(Element)doc.selectSingleNode("/root/address[@cid='"+addr.getCid()+"']");
if(el!=null)
{
el.addAttribute("name",addr.getName());
el.addAttribute("email",addr.getEmail());
el.addAttribute("userName",addr.getUserName());
if(XmlFileTools.write(basePath,doc))ret=1;
}
}
catch(Exception e)
{
}
return ret;
}
public IPageList list()
{
IPageList pList=null;
List list=null;
Document doc;
try{
doc=XmlFileTools.load(basePath);
list=doc.selectNodes("/root/address");
pList=new PageList(new ListQuery(list));
}
catch(Exception e)
{
}
return pList;
}
public IPageList search(String condition)
{
IPageList pList=null;
return pList;
}
public static List domList2ObjList(List domList)
{
List list=new ArrayList();
if(domList!=null)
{
for(int i=0;i<domList.size();i++)
{
Element el=(Element)domList.get(i);
MailAddress addr=new MailAddress();
addr.setCid(el.attributeValue("cid"));
addr.setName(el.attributeValue("name"));
addr.setEmail(el.attributeValue("email"));
addr.setUserName(el.attributeValue("userName"));
list.add(addr);
}
}
return list;
}
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
/**
* @param args
*/
public static void main(String[] args) {
EmailAddress eadd=new EmailAddress("csy","cai");
MailAddress addr=new MailAddress();
addr.setCid(CommUtil.getOnlyID());
addr.setName("蔡世友");
addr.setEmail("caishiyoU@21cn.com");
eadd.add(addr);
IPageList pList=eadd.list();
if(pList!=null)
{
pList.doList(10,1,"","");
System.out.println(pList.getRowCount());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -