当前位置:网站首页>XML modeling

XML modeling

2022-07-23 11:26:00 _ Leaf1217

Catalog

One 、 Learn to XML modeling

Two 、23 Factory mode of three modeling methods

3、 ... and 、 Practice cases


One 、 Learn to XML modeling

Today we'll see how XML modeling , First of all, we should model it first XML file 【config.xml Put it here :

<?xml version="1.0" encoding="UTF-8"?>

<config>
	<action path="/regAction" type="test.RegAction">
		<forward name="failed" path="/reg.jsp" redirect="false" />
		<forward name="success" path="/login.jsp" redirect="true" />
	</action>

	<action path="/loginAction" type="test.LoginAction">
		<forward name="failed" path="/login.jsp" redirect="false" />
		<forward name="success" path="/main.jsp" redirect="true" />
	</action>
</config>

  Let's first analyze this xml file :

1) Analyze tags

A root tag :config

A sub tag :action

action There are sub tags inside :forward

2) Analysis properties

action:path、type

forward:name、path、redirect

According to the above analysis, we can know that there are three different tags in this configuration file :

config: There are sub tags 、 There is no attribute

action: There are sub tags

forward: No sub tags

So we started our The first step of modeling ,

utilize Object oriented programming idea For different labels Construction entity

ForwardModel:

package com.leaf.mode;
/**
 * forward label 
 * @author Leaf
 *
 * 2022 year 6 month 15 Japan   In the morning 8:40:48
 */
public class ForwardModel {
	//<forward name="success" path="/main.jsp" redirect="true" />
	private String name;
	private String path;
	private boolean redirect;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public boolean isRedirect() {
		return redirect;
	}
	public void setRedirect(boolean redirect) {
		this.redirect = redirect;
	}
	
	public ForwardModel() {

	}
	public ForwardModel(String name, String path, boolean redirect) {
		this.name = name;
		this.path = path;
		this.redirect = redirect;
	}
	
	@Override
	public String toString() {
		return "ForwardMode [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
	}
	
}

ActionModel: There are sub tags , need Two behavior methods

                        1) add to forward label

                        2) Inquire about forward label

package com.leaf.mode;

import java.util.HashMap;
import java.util.Map;

/**
 * action label 
 * @author Leaf
 *
 * 2022 year 6 month 15 Japan   In the morning 8:41:08
 */
public class ActionModel {
	//<action path="/loginAction" type="test.LoginAction">
	private String path;
	private String type;
	private Map<String, ForwardModel> fMap = new HashMap<String, ForwardModel>();
	
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	
	public ActionModel() {
		// TODO Auto-generated constructor stub
	}
	public ActionModel(String path, String type) {
		super();
		this.path = path;
		this.type = type;
	}
	
	/**
	 *  Two behaviors : increase forward object | lookup forward object 
	 * @param forward object 
	 */
	// Will a new forward Label objects are added to the container 
	public void push(ForwardModel f) {
		fMap.put(f.getName(), f);
	}
    // Inquire about forward label 
	public ForwardModel pop(String name) {
		return fMap.get(name);
	}
	
	@Override
	public String toString() {
		return "ActionMode [path=" + path + ", type=" + type + "]";
	}
	
}

ConfigModel: There is no attribute 、 There are sub tags 、 need Two behavior methods

                        1) add to action label

                        2) Inquire about action label

package com.leaf.mode;
/**
 * config label 
 * @author Leaf
 *
 * 2022 year 6 month 15 Japan   In the morning 8:41:23
 */

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	
	private Map<String, ActionModel> aMap = new HashMap<String, ActionModel>();
	
	public void push(ActionModel am) {
		aMap.put(am.getPath(), am);
	}
	
	public ActionModel pop(String path) {
		return aMap.get(path);
	}
	
}

  Two 、23 Factory mode of three modeling methods

But after we build entity classes for each tag and write the corresponding behavior methods ,

Need Create a factory pattern class ConfigModelFactory

First take a look at the comments about this factory schema class :

/**
 * 23 Factory mode of design mode
 * ConfigModelFactory It's for production ConfigModel Object's
 * manufactured ConfigModel Object contains Config.xml Configuration content in
 * 
 * Produced here ConfigModel There is configuration information
 * 1、 analysis Config.xml Configuration information in
 * 2、 Load the corresponding configuration information into different model objects
 * @author Leaf
 *
 * 2022 year 6 month 15 Japan In the morning 9:00:42
 */

Then I put it directly Factory class code La :

package com.leaf.mode;

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;

/**
 * 23 Factory mode of design mode 
 * ConfigModelFactory It's for production ConfigModel Object's 
 *  manufactured ConfigModel Object contains Config.xml Configuration content in 
 * 
 *  Produced here ConfigModel There is configuration information 
 * 1、 analysis Config.xml Configuration information in 
 * 2、 Load the corresponding configuration information into different model objects 
 * @author Leaf
 *
 * 2022 year 6 month 15 Japan   In the morning 9:00:42
 */
public class ConfigModelFactory {
	public static ConfigModel bulid(String path) throws Exception {
		InputStream in = ConfigModelFactory.class.getResourceAsStream(path);
		SAXReader sr = new SAXReader();
		Document doc = sr.read(in);
		List<Element> actionEles = doc.selectNodes("/config/action");
		ConfigModel cm = new ConfigModel();
		for (Element actionEle : actionEles) {
			ActionModel am = new ActionModel();
			am.setPath(actionEle.attributeValue("path"));
			am.setType(actionEle.attributeValue("type"));
			// take forwardmodel Assign and add to Actionmodel in 
			List<Element> forwardEle = actionEle.selectNodes("forward");
			for (Element element : forwardEle) {
				ForwardModel forwardModel = new ForwardModel();
				forwardModel.setName(element.attributeValue("name"));
				forwardModel.setPath(element.attributeValue("path"));
				//redirect: Can only be false|true, Allow space , The default value is false
				forwardModel.setRedirect("true".equals(element.attributeValue("redirect")));
				am.push(forwardModel);
			}
			cm.push(am);
		}
		return cm;
	}
	// Only test here , It can be called directly where it is used bulid(" File path ") Of ;
	public static ConfigModel bulid() throws Exception {
		String defaultPath = "config.xml";
		return bulid(defaultPath);
	}
}

  When all these are built, we can create another class Call tests Use it :

    public static void main(String[] args) throws Exception {
        // Call the method of the factory class to get the tag config
        ConfigModel cm = ConfigModelFactory.bulid();
        
        // call config How to query
        ActionModel am = cm.pop("/loginAction");
        // Print test
        System.out.println("Type:\t"+am.getType()+"\n");
        
        // Inquire about
        ForwardModel fm = am.pop("success");
        // Print test
        System.out.println("Path:\t"+fm.getPath());
    }

Running results : 


  3、 ... and 、 Practice cases

  Case study : Yes web.xml Modeling , Write a servlet adopt url-pattern Read servlet-class Value ;

First of all, put XML file :web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
  	<servlet-name>jrebelServlet</servlet-name>
  	<servlet-class>com.leaf.xml.JrebelServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>jrebelServlet</servlet-name>
  	<url-pattern>/jrebelServlet</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>jrebelServlet2</servlet-name>
  	<servlet-class>com.leaf.xml.JrebelServlet2</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>jrebelServlet2</servlet-name>
  	<url-pattern>/jrebelServlet2</url-pattern>
  	<url-pattern>/jrebelServlet3</url-pattern>
  </servlet-mapping>
</web-app>

Then let's go analysis Look at the whole XML file Of structure

1) Analyze tags

A root tag :web-app

Two sub tags :servlet、servlet-mapping

servlet There are sub tags inside :servlet-name、servlet-class

servlet-mapping There are sub tags inside :servlet-name、url-pattern

According to the above analysis, we can know that there are six different tags in this configuration file :

servlet-name、servlet-class、url-pattern、servlet、servlet-mapping、web-app

except servlet、servlet-mapping、web-app outside , Other tags have values ;

therefore , Old rules , Let's give them six different labels first Object oriented idea Build entity classes :

ServletNameModel、ServletClassModel、UrlPatternModel、ServletModel、ServletMappingModel、WebAppModel;

There have been examples above , There is no more code for putting entity classes here ;

After we build the entity class, we need to create a factory class , Put it here Factory class code WebAppFactory

package com.leaf.modezuoye;

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

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

/**
 * WebApp Factory 
 * @author Leaf
 *
 * 2022 year 6 month 15 Japan   In the morning 11:34:52
 */
public class WebAppFactory {
	
	/**
	 *  modeling 
	 * @param path xml name 
	 * @return
	 */
	public static WebAppModel build(String path) {
		InputStream in = WebAppFactory.class.getResourceAsStream(path);
		SAXReader sr = new SAXReader();
		WebAppModel wam = new WebAppModel();
		try {
			Document doc = sr.read(in);
			// take servlet The contents of the label are filled in web-app
			List<Element> servletEles = doc.selectNodes("/web-app/servlet");
			for (Element servletEle : servletEles) {
				ServletModel servletModel = new ServletModel();

				// to servlet  fill xml The content of 
				Element servletNameEle = (Element) servletEle.selectSingleNode("servlet-name");
				Element servletClassEle = (Element) servletEle.selectSingleNode("servlet-class");
				ServletNameModel servletNameModel = new ServletNameModel();
				ServletClassModel servletClassModel = new ServletClassModel();
				servletNameModel.setNr(servletNameEle.getText());
				servletClassModel.setNr(servletClassEle.getText());
				
				servletModel.setSnm(servletNameModel);
				servletModel.setScm(servletClassModel);

				wam.push(servletModel);
			}

			/*
			 *  take servlet-mapping The contents of the label are filled in web_app
			 */
			List<Element> servletMappingEles = doc.selectNodes("/web-app/servlet-mapping");
			for (Element servletMappingEle : servletMappingEles) {
				ServletMappingModel servletMappingModel = new ServletMappingModel();

				/*
				 *  to servletmapingmodel fill xml The content of 
				 */
				Element servletNameEle = (Element) servletMappingEle.selectSingleNode("servlet-name");
				ServletNameModel servletNameModel = new ServletNameModel();
				servletNameModel.setNr(servletNameEle.getText());
				servletMappingModel.setSnm(servletNameModel);
				
				List<Element> urlPatternEles = servletMappingEle.selectNodes("url-pattern");
				for (Element urlPatternEle : urlPatternEles) {
					UrlPatternModel urlPatternModel = new UrlPatternModel();
					urlPatternModel.setNr(urlPatternEle.getText());
					servletMappingModel.push(urlPatternModel);
				}

				wam.push(servletMappingModel);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return wam;
	}

	/**
	 *  Automatically find the corresponding background processing class through the web address entered by the browser 
	 * @param webAppModel	 The modeled entity class 
	 * @param url	 The web address visited by the browser 
	 * @return
	 */
	public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
		String servletClass = "";
		/*
		 *  Find the corresponding URL of the browser servlet-name
		 */
		String servletName = "";
		List<ServletMappingModel> servletMappingModels = webAppModel.pop();
		for (ServletMappingModel servletMappingModel : servletMappingModels) {
			List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
			for (UrlPatternModel urlPatternModel : urlPatternModels) {
				if(url.equals(urlPatternModel.getNr())) {
					ServletNameModel servletNameModel = servletMappingModel.getSnm();
					servletName = servletNameModel.getNr();
				}
			}
		}
		
		/*
		 *  find servlet-name Corresponding background processing class 
		 */
		List<ServletModel> servletModels = webAppModel.getServletModels();
		for (ServletModel servletModel : servletModels) {
			ServletNameModel servletNameModel = servletModel.getSnm();
			if(servletName.equals(servletNameModel.getNr())) {
				ServletClassModel servletClassModel = servletModel.getScm();
				servletClass = servletClassModel.getNr();
			}
		}
		return servletClass;
	}
	
	// test 
	public static void main(String[] args) {
		
		WebAppModel webAppModel = WebAppFactory.build("web.xml");
		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);
		
	}
	
}

Running results :


OK, today Leaf That's all for learning notes sharing , See you next time !!!

原网站

版权声明
本文为[_ Leaf1217]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207230538039446.html