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

📄 xmlprocesserimpl.java

📁 类似struts2的mvc框架
💻 JAVA
字号:
package pp.mvc.core.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import pp.mvc.core.XmlProcesser;
import pp.mvc.domain.ActionDefine;
import pp.mvc.domain.Result;

/**
 * 
 * @author panhuizi
 * 
 */
public class XmlProcesserImpl implements XmlProcesser {

	private static final String PP_XML_FILE = "/ppmvc.xml";

	private Map<String, ActionDefine> actionDefines = new HashMap<String, ActionDefine>();

	/**
	 * 获取所有的<packageNameSpace+actionName,ActionDefine>映射
	 */
	public Map<String, ActionDefine> getAllActionDefineMapsByPpXmlFile()
			throws Exception {
		this.doParse(PP_XML_FILE);
		return actionDefines;
	}

	/**
	 * 读取所有xml文件,以便添加所有文件的映射
	 * 
	 * @param xmlFile
	 * @throws Exception
	 */
	private void doParse(String xmlFile) throws Exception {

		Map<String, ActionDefine> actionDefines = this.getAXmlMaps(xmlFile);
		if (actionDefines != null) {
			this.actionDefines.putAll(actionDefines);
		}
		List<String> files = this.getAXMLIncludeFileName(xmlFile);
		if (files != null && files.size() > 0) {
			for (String f : files) {
				/* 递归调用自身以便添加所有ActionPackage到list */
				try {
					this.doParse("/" + f);
				} catch (Exception e) {
					throw new RuntimeException("找不到文件:" + f);
				}
			}
		}
	}

	/**
	 * 获取一个xml文件中所有的<packageNameSpace+actionName,ActionDefine>映射
	 * 
	 * @param xmlFile
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private Map<String, ActionDefine> getAXmlMaps(String xmlFile)
			throws Exception {

		Map<String, ActionDefine> actionDefines = new HashMap<String, ActionDefine>();

		/* 读取PP_XML_FILE文件 */
		InputStream is = this.getClass().getResourceAsStream(xmlFile);
		SAXReader xmlReader = new SAXReader();
		Document doc = xmlReader.read(is);
		/* 获取package节点 */
		Element root = doc.getRootElement();
		List<Element> ps = root.elements("package");
		if (ps != null && ps.size() > 0) {
			for (Element p : ps) {
				/* 获取package节点内名为name的元素 */
				Attribute packageName = p.attribute("name");
				Attribute packageNamespace = p.attribute("namespace");

				List<Element> as = p.elements("action");
				if (as != null && as.size() > 0) {
					for (Element a : as) {
						ActionDefine actionDefine = new ActionDefine();
						Attribute actionName = a.attribute("name");
						Attribute actionMethod = a.attribute("method");
						Attribute actionClass = a.attribute("class");
						Map<String, Result> results = new HashMap<String, Result>();
						List<Element> rs = a.elements("result");
						if (rs != null && rs.size() > 0) {
							for (Element r : rs) {
								Result result = new Result();
								Attribute resultName = r.attribute("name");
								Attribute resultType = r.attribute("type");
								/* 获取result的值 */
								Object resultData = r.getData();
								result.setLoaction((String) resultData);
								result.setName(resultName.getText());
								if (resultType != null) {
									result.setType(resultType.getText());
								}
								results.put(resultName.getText(), result);
							}
						} else {
							throw new RuntimeException("名为:["
									+ actionName.getText()
									+ "]的action不存在result");
						}
						if (actionMethod != null) {
							actionDefine.setMethod(actionMethod.getText());
						}
						actionDefine.setName(actionName.getText());
						actionDefine.setType(actionClass.getText());
						actionDefine.setResults(results);

						if (packageNamespace == null
								|| packageNamespace.getText().equals("/")) {
							actionDefines.put("/" + actionName.getText(),
									actionDefine);
						} else {
							actionDefines.put(packageNamespace.getText() + "/"
									+ actionName.getText(), actionDefine);
						}
					}

				} else {
					throw new RuntimeException("名为:[" + packageName.getText()
							+ "]的package不存在action");
				}
			}
			return actionDefines;
		}
		return null;

	}

	/**
	 * 获取一个xml文件中所有包含的文件的文件名
	 * 
	 * @param xmlFile
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private List<String> getAXMLIncludeFileName(String xmlFile)
			throws Exception {

		List<String> files = new ArrayList<String>();

		InputStream is = this.getClass().getResourceAsStream(xmlFile);
		SAXReader xmlReader = new SAXReader();
		Document doc = xmlReader.read(is);
		Element root = doc.getRootElement();
		List<Element> includes = root.elements("include");
		if (includes != null && includes.size() > 0) {
			for (Element include : includes) {
				Attribute file = include.attribute("file");
				files.add(file.getText());
			}
			return files;
		}
		return null;
	}
}

⌨️ 快捷键说明

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