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

📄 confbuilder.java

📁 本软件使用Java语言提供的JavaMail包进行开发
💻 JAVA
字号:
package com.lanx.app.mail;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.lanx.app.mail.entity.MailAttache;
import com.lanx.app.mail.entity.MailHost;
import com.lanx.app.mail.entity.MailInfo;
import com.lanx.app.mail.entity.MailItem;
import com.lanx.app.mail.util.AgileMailConstants;
import com.lanx.app.mail.util.AgileMailUtils;

import com.lanx.base.util.Constant;

/**
 * <p>解析xml配置文件的类。</p>
 * 此配置文件由用户自己指定,需要符合本软件提供的schema文件规范。
 * 根据配置文件找到当前需要发送的邮件地址,邮件正文等参数值
 * 
 * @author Ramboo Lan
 * @version 0.5
 * @Date: 2008-10-27
 */
public class ConfBuilder implements Configuration {
    private List<MailItem> itemList = new ArrayList<MailItem>(Constant.Common.INIT_SIZE);

    private InputStream fis = null;
    
	public ConfBuilder(String confFilename) throws FileNotFoundException {
		this(new File(confFilename));
    }
			
	public ConfBuilder(File confFile) throws FileNotFoundException {
		this(new FileInputStream(confFile));
	}
	
	public ConfBuilder(InputStream fis) {
		try {
			this.fis = fis;			
			this.load();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (AgileMailException e) {
			e.printStackTrace();
		} 		
	}
	
	/**
	 * 根据给出的符合snatch.xsd规范的xml配置文件,取得参数值,
	 * 将之以集合方式存储到成员变量的List实例中
	 * */
	@SuppressWarnings("unchecked")
	private void load() throws DocumentException,AgileMailException{
		if (fis == null) {
			throw new AgileMailException("File name needed! But directory found or wrong path!");
		}
		
		//DefaultXPath xpath = new DefaultXPath("/agilemail/item/@priority");
		//Node root = XMLBuilder.newInstance(fis).selectSingleNode("/agilemail");
        SAXReader reader = new SAXReader();
        Document doc = reader.read(fis);
        List list = doc.selectNodes("/agilemail/item", "@priority", false);
		//Node agilemailNode = xpath.selectSingleNode(root);//XMLBuilder.newInstance(fis).selectNodes("/agilemail/item");
		//List list = xpath.selectNodes(root);
		if (list != null) {
			//xpath.sort(list);
			
			MailItem mailItem = null;
			for (int i = 0; i < list.size(); i++) {
				mailItem = new MailItem();

				Node node = (Node) list.get(i);
				Element element = (Element)node;
				
				mailItem.setMailId(node.valueOf("@name"));
				mailItem.setPriority(node.valueOf("@priority"));
				
				Element hostsEle = element.element("hosts");
				//解析item节点的hosts子节点
				parseHostsNode(hostsEle,mailItem);
												
				Element sendEle = element.element("send");				
				//解析item节点的send子节点
				parseSendNode(sendEle,mailItem);				
				
				itemList.add(mailItem);
			}
			
			//Collections.sort(itemList);
		}
	}

	/**
	 * 解析item节点的hosts子节点
	 * 
	 * @param hostsEle 	hosts节点的Element实例
	 * @param mailItem 	item节点的对应vo类实例
	 * */
	@SuppressWarnings("unchecked")
	private void parseHostsNode(Element hostsEle,MailItem mailItem) {
		List hostList = hostsEle.elements("host");
		for (int x = 0; x < hostList.size(); x++) {
			MailHost mailHost = new MailHost();
			Element hostEle = (Element) hostList.get(x);
			
			Element pop3Ele = hostEle.element("pop3");
			mailHost.setPop3Host(pop3Ele.getTextTrim());
			mailHost.setPop3Port(pop3Ele.valueOf("@port"));
			
			Element smtpEle = hostEle.element("smtp");
			mailHost.setSmtpHost(smtpEle.getTextTrim());
			mailHost.setSmtpPort(smtpEle.valueOf("@port"));
			
			Element imapEle = hostEle.element("imap");
			mailHost.setImapHost(imapEle.getTextTrim());
			mailHost.setImapPort(imapEle.attributeValue("port"));

			Element authEle = hostEle.element("auth");
			mailHost.setUsername(authEle.element("username").getTextTrim());
			mailHost.setPassword(authEle.element("password").getTextTrim());
			
			String auth = authEle.valueOf("@auth");
			if(auth != null && "true".equals(auth)){
				mailHost.setAuth(true);	
			}else{
				mailHost.setAuth(false);
			}
			
			mailHost.setMailAddress(hostEle.valueOf("@mailAddress"));
			
			mailItem.addMailHosts(mailHost);
		}
	}
	
	/**
	 * 解析item节点的send子节点
	 * 
	 * @param sendEle 		send节点的Element实例
	 * @param mailItem 	item节点的对应vo类实例
	 * */
	@SuppressWarnings("unchecked")
	private void parseSendNode(Element sendEle,MailItem mailItem) {
		List mailtoList = sendEle.elements("mailto");
		for (int x = 0; x < mailtoList.size(); x++) {
			MailInfo mailInfo = new MailInfo();
			
			Element mailtoEle = (Element) mailtoList.get(x);
			mailInfo.setCc(mailtoEle.valueOf("@cc"));
			mailInfo.setBcc(mailtoEle.valueOf("@bcc"));
			mailInfo.setSubject(mailtoEle.valueOf("@title"));
			
			String htmlPath = mailtoEle.valueOf("@html");
			if(htmlPath != null && !"".equals(htmlPath)){
				mailInfo.setHtmlPath(htmlPath);
				mailInfo.setMailFormat(AgileMailConstants.MailFormat.html);
			}else{
				mailInfo.setMailFormat(AgileMailConstants.MailFormat.txt);
			}
			
			Element contentEle = mailtoEle.element("content");
			mailInfo.setMailContent(contentEle.getTextTrim());
			
			Element emailEle = mailtoEle.element("email");
			mailInfo.setMailTo(emailEle.getTextTrim());
			//mailInfo.setMailFrom(mailItem.getMailAddress());
			
			mailInfo.setMailDateTime(System.currentTimeMillis());

			//查找附件
			parseAttache(contentEle,mailInfo);
			
			mailItem.addMailInfos(mailInfo);
		}				
	}	
	
	/**
	 * 解析send节点的content子节点下的attache附件属性
	 * 
	 * @param contentEle 	content节点的Element实例
	 * @param mailInfo 	send节点的对应vo类实例
	 * */
	private void parseAttache(Element contentEle,MailInfo mailInfo) {
		Attribute attribute = contentEle.attribute("attache");
		if(attribute == null || attribute.getValue() == null 
				|| "".equals(attribute.getValue())){
			return ;
		}
			
		String[] split = AgileMailUtils.split(attribute.getValue());		
		if(split != null){
			for(String cell : split){
				MailAttache attacheInfo = new MailAttache();
				//拆分为数组时,可能会拆分出""
				if(cell != null && !"".equals(cell))
					attacheInfo.setAttacheName(cell);
				
				mailInfo.addAttaches(attacheInfo);
			}			
		}
	}
	
	public List<MailItem> loadItem() {
		return itemList;
	}

	public HashMap<String,MailItem> asHashMap() {
		if(itemList == null || itemList.size() == 0)
			return null;	
		
		HashMap<String, MailItem> hashMap = new HashMap<String, MailItem>(Constant.Common.INIT_SIZE);		
		Iterator<MailItem> iter = itemList.iterator();
		while(iter.hasNext()){
			MailItem mailItem = iter.next();									
			hashMap.put(mailItem.getMailId(), mailItem);
		}
		
		return hashMap;
	}
}

⌨️ 快捷键说明

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