首页>代码>java抓取特定网站图片并下载>/搜图/SouTu/src/com/ksfzhaohui/config/WebConfig.java
package com.ksfzhaohui.config;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.ksfzhaohui.SouTuException;
import com.ksfzhaohui.download.Download;

/**
 * 协议网址配置文件加载
 * 
 * @author ksfzhaohui
 * 
 */
public class WebConfig {

	private static Log logger = LogFactory.getLog(WebConfig.class);
	/** 网址参数配置文件 */
	private static final String webSitePath = "/webSite.xml";

	private Map<String, WebSite> webSiteMap = new HashMap<String, WebSite>();

	private static WebConfig webConfig = new WebConfig();

	private WebConfig() {
		readXML(webSitePath);
	}

	public static WebConfig getWebConfig() {
		return webConfig;
	}

	/**
	 * 读取XMl
	 * 
	 * @param fileName
	 *            文件名
	 * @throws GameException
	 */
	private void readXML(String fileName) {
		try {
			URL url = getClass().getResource(webSitePath);
			if (url == null) {
				throw new SouTuException("类路径下无法找到" + webSitePath + "配置文件");
			}
			parse(url);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

	private void parse(URL url) throws SouTuException {
		StringBuffer sb = new StringBuffer("读取网站参数配置文件--->网站分别是:");
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(url.toExternalForm());

			NodeList typeNodes = doc.getElementsByTagName("webSite");
			if (typeNodes == null || typeNodes.getLength() == 0) {
				throw new SouTuException("不存在webSite标签");
			}

			for (int i = 0; i < typeNodes.getLength(); i++) {
				Node typeNode = typeNodes.item(i);
				NamedNodeMap webSiteAttr = typeNode.getAttributes();
				NodeList elements = typeNode.getChildNodes();
				WebSite webSite = new WebSite();
				for (int j = 0; j < elements.getLength(); j++) {
					Node element = elements.item(j);
					if (element instanceof Element) {
						Chapter chapter = new Chapter();
						NamedNodeMap chapterAttr = element.getAttributes();
						for (int k = 0; k < chapterAttr.getLength(); k++) {
							Node attribute = chapterAttr.item(k);
							String attName = attribute.getNodeName();
							if (attName.equalsIgnoreCase("name")) {
								chapter.setName(attribute.getNodeValue());
							} else if (attName.equalsIgnoreCase("tag")) {
								chapter.setTag(attribute.getNodeValue());
							} else if (attName.equalsIgnoreCase("page")) {
								chapter.setPage(Integer.valueOf(attribute
										.getNodeValue()));
							} else if (attName.equalsIgnoreCase("downloadPage")) {
								chapter.setDownloadPage(Integer.valueOf(attribute
										.getNodeValue()));
							} else {
								throw new SouTuException("不存在" + attName
										+ "此属性");
							}
						}
						webSite.addChapter(chapter);
					}
				}

				for (int k = 0; k < webSiteAttr.getLength(); k++) {
					Node attribute = webSiteAttr.item(k);
					String attName = attribute.getNodeName();
					if (attName.equalsIgnoreCase("name")) {
						webSite.setName(attribute.getNodeValue());
						webSiteMap.put(attribute.getNodeValue(), webSite);
						sb.append("[").append(attribute.getNodeValue())
								.append("]");
					} else if (attName.equalsIgnoreCase("class")) {
						webSite.setDownload((Download) Class.forName(
								attribute.getNodeValue()).newInstance());
					} else {
						throw new SouTuException("不存在" + attName + "此属性");
					}
				}
			}

			logger.info(sb.toString());
		} catch (Exception e) {
			throw new SouTuException(e.getMessage(), e);
		}
	}

	public Map<String, WebSite> getWebSiteMap() {
		return webSiteMap;
	}
}
最近下载更多
dafeiyu  LV10 2020年7月4日
相思入骨  LV11 2020年4月9日
wei112233  LV15 2020年2月2日
dmx12345  LV2 2019年7月1日
再回首丶白了头  LV1 2019年6月6日
dvd速度  LV9 2019年6月2日
skystory  LV11 2019年4月27日
飞翔的企鹅嗯嗯  LV11 2019年1月30日
chinafjfzlj  LV31 2018年12月26日
lris_luanling  LV11 2018年11月28日
最近浏览更多
adminLi  LV1 1月10日
ljt289917726  LV3 2023年12月18日
xingxing1234  LV10 2023年3月22日
best2018  LV46 2022年10月24日
mq13947193109  LV19 2022年7月5日
SZEPEZS  LV8 2022年6月10日
zfbbnp  LV1 2022年6月6日
qiaofu22  LV4 2022年6月3日
yangsha868  LV9 2021年8月13日
°Freedom 2021年4月21日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友