xml建模

1、对web.xml进行建模

编写一个config.xml文件



	
	
	
	
]>


	
	
		
		
		
	

	
		
		
	

编写一个config.xml里面的forward实体类

package com.likang;

import java.io.InputStream;
import java.util.List;

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

public class ConfigModeFactory {

	public static ConfigMode build() throws Exception {
		return ConfigMode("config.xml");
	}
/**
 * 生产出有内容的实体类ConfigMode
 * @param xmlPath
 * @return
 * @throws Exception 
 */
	private static ConfigMode ConfigMode(String xmlPath) throws Exception {
		// TODO Auto-generated method stub
		ConfigMode configmode=new ConfigMode();
		ActionMode actionmode=null;
		ForwardMode forwardMode=null;
		InputStream ra = ConfigModeFactory.class.getResourceAsStream(xmlPath);
		SAXReader saxReader=new SAXReader();
		Document doc = saxReader.read(ra);
		List list = doc.selectNodes("/config/action");
		for (Element element : list) {
			actionmode=new ActionMode();
			actionmode.setType(element.attributeValue("type"));
			actionmode.setPath(element.attributeValue("path"));
			List list2 = element.selectNodes("forward");
			for (Element element2 : list2) {
				forwardMode=new ForwardMode();
				forwardMode.setName(element2.attributeValue("name"));
				forwardMode.setPath(element2.attributeValue("path"));
				forwardMode.setRedirect(!"false".equals(element2.attribute("redirect")));//默认为true
				actionmode.add(forwardMode);
			}
			configmode.add(actionmode);
		}
		return configmode;
	}
	public static void main(String[] args) throws Exception {
		ConfigMode configMode=ConfigModeFactory.build();
		ActionMode actionMode=configMode.getam("/loginAction");
		System.out.println("config下的第二个action的type值:"+actionMode.getType());
		ForwardMode forwardMode=actionMode.getfm("success");
		System.out.println("config下的第二个action下的第二个forward的path值:"+forwardMode.getPath());
	}
	
}

结果如下
在这里插入图片描述

2、写一个servlet
3、通过url-pattern读取到servlet-class的值

web.xml文件



  
  	jrebelServlet
  	com.zking.xml.JrebelServlet
  
  
  
  	jrebelServlet
  	/jrebelServlet
  
  
  
  	jrebelServlet2
  	com.zking.xml.JrebelServlet2
  
  
  
  	jrebelServlet2
  	/jrebelServlet2
  	/jrebelServlet3
  

ServletClassModel类

package com.likang.xml;

public class ServletClassModel {

	private String clas;

	public String getClas() {
		return clas;
	}

	public void setClas(String clas) {
		this.clas = clas;
	}
	
}

ServletMappingModel类

package com.likang.xml;

import java.util.ArrayList;
import java.util.List;

public class ServletMappingModel {
	private ServletNameMode servletNameModel;
	private List urlPatternModels = new ArrayList<>();
	public ServletNameMode getServletNameModel() {
		return servletNameModel;
	}
	public void setServletNameModel(ServletNameMode servletNameModel) {
		this.servletNameModel = servletNameModel;
	}
	
	public void pushUrlPatternModel(UrlPatternModel urlPatternModel) {
		urlPatternModels.add(urlPatternModel);
	}
	
	public List getUrlPatternModels() {
		return urlPatternModels;
	}
	
}

ServletModel类

package com.likang.xml;

public class ServletModel {
	private ServletNameMode servletNameModel;
	private ServletClassModel servletClassModel;

	public ServletNameMode getServletNameModel() {
		return servletNameModel;
	}

	public void setServletNameModel(ServletNameMode servletNameModel) {
		this.servletNameModel = servletNameModel;
	}

	public ServletClassModel getServletClassModel() {
		return servletClassModel;
	}

	public void setServletClassModel(ServletClassModel servletClassModel) {
		this.servletClassModel = servletClassModel;
	}

}

ServletNameMode类

package com.likang.xml;

public class ServletNameMode {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
}

UrlPatternModel类

package com.likang.xml;

public class UrlPatternModel {

	private String url;

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
}

WebappMode类

package com.likang.xml;

import java.util.List;
import java.util.ArrayList;

public class WebappMode {
	private List servletModels = new ArrayList<>();
	private List servletMappingModels = new ArrayList<>();

	public void pushServletModel(ServletModel servletModel) {
		servletModels.add(servletModel);
	}
	
	public List getServletModels() {
		return servletModels;
	}
	
	public void pushServletMappingModel(ServletMappingModel servletMappingModel) {
		servletMappingModels.add(servletMappingModel);
	}
	
	public List getServletMappingModels() {
		return servletMappingModels;
	}
}

WebAppModelFactory工厂

package com.likang.xml;

import java.io.InputStream;
import java.util.List;

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



public class WebAppModelFactory {
	public static WebappMode buildWebAppModel() {
		String xmlPath = "web.xml";
		return builc(xmlPath);
	}


	public static WebappMode builc(String xmlPath) {
		InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
		SAXReader saxReader = new SAXReader();
		WebappMode webAppModel = new WebappMode();
		try {
			Document doc = saxReader.read(in);

			List servletEles = doc.selectNodes("/web-app/servlet");
			for (Element servletEle : servletEles) {
				ServletModel servletModel = new ServletModel();


				Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
				Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
				ServletNameMode servletNameModel = new ServletNameMode();
				ServletClassModel servletClassModel = new ServletClassModel();
				servletNameModel.setName(servletNameEle.getText());
				servletClassModel.setClas(servletClassEle.getText());
				
				servletModel.setServletNameModel(servletNameModel);
				servletModel.setServletClassModel(servletClassModel);

				webAppModel.pushServletModel(servletModel);
			}

			List servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
			for (Element servletMappingEle : servletMappingEles) {
				ServletMappingModel servletMappingModel = new ServletMappingModel();

	
				Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
				ServletNameMode servletNameModel = new ServletNameMode();
				servletNameModel.setName(servletNameEle.getText());
				servletMappingModel.setServletNameModel(servletNameModel);
				
				List urlPatternEles = servletMappingEle.selectNodes("url-pattern");
				for (Element urlPatternEle : urlPatternEles) {
					UrlPatternModel urlPatternModel = new UrlPatternModel();
					urlPatternModel.setUrl(urlPatternEle.getText());
					servletMappingModel.pushUrlPatternModel(urlPatternModel);
				}

				webAppModel.pushServletMappingModel(servletMappingModel);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return webAppModel;
	}
	

	public static String getServletClassByUrl(WebappMode webAppModel, String url) {
		String servletClass = "";
	
		String servletName = "";
		List servletMappingModels = webAppModel.getServletMappingModels();
		for (ServletMappingModel servletMappingModel : servletMappingModels) {
			List urlPatternModels = servletMappingModel.getUrlPatternModels();
			for (UrlPatternModel urlPatternModel : urlPatternModels) {
				if(url.equals(urlPatternModel.getUrl())) {
					ServletNameMode servletNameModel = servletMappingModel.getServletNameModel();
					servletName = servletNameModel.getName();
				}
			}
		}
		

		List servletModels = webAppModel.getServletModels();
		for (ServletModel servletModel : servletModels) {
			ServletNameMode servletNameModel = servletModel.getServletNameModel();
			if(servletName.equals(servletNameModel.getName())){
				ServletClassModel servletClassModel = servletModel.getServletClassModel();
				servletClass = servletClassModel.getClas();
			}
		}
		return servletClass;
	}
	
	public static void main(String[] args) {
		WebappMode webAppModel = WebAppModelFactory.buildWebAppModel();
		String res = getServletClassByUrl(webAppModel, "/jrebelServlet");
		String res2 = getServletClassByUrl(webAppModel, "/jrebelServlet2");
		String res3 = getServletClassByUrl(webAppModel, "/jrebelServlet3");
		System.out.println(res);
		System.out.println(res2);
		System.out.println(res3);
		
	}
}

结果如下

在这里插入图片描述

你可能感兴趣的:(xml建模)