Xml Xml analytical work and modeling andXml modeling jobs

  Job: config.xml resolved

1, get the value of all the action in the type of

public static void main(String[] args) throws Exception {
        InputStream in=XmlDemo.class.getResourceAsStream("config.xml");        
        SAXReader sax= new SAXReader();
        Document doc=sax.read(in);
                            //获取所有action中的type的值
        List<Element> stuEles= doc.selectNodes("/config/action");
        for (Element stuEle : stuEles) {
            String type=stuEle.attributeValue("type");
            System.out.println(type);
                
        }

2, to obtain the value of the second type of action

List<Element> stuEles= doc.selectNodes("/config/action");
        for (Element stuEle : stuEles) {
            if("/loginAction".equals(stuEle.attributeValue("path"))) {
                String type=stuEle.attributeValue("type");
                System.out.println(type);
            }
        }

 3, to get all of the forward path of the second action

List<Element> stuEles= doc.selectNodes("/config/action");
        for (Element stuEle : stuEles) {
            if("/loginAction".equals(stuEle.attributeValue("path"))) {
                List<Element> ford=(List<Element>) stuEle.selectNodes("forward");
                for (Element element : ford) {
                    String path=element.attributeValue("path");
                    System.out.println(path);
                }               
            }
        }

4, obtaining a second forward path of the second action

List<Element> stuEles= doc.selectNodes("/config/action");
        for (Element stuEle : stuEles) {
            if("/loginAction".equals(stuEle.attributeValue("path"))) {
                List<Element> ford=(List<Element>) stuEle.selectNodes("forward");
                for (Element element : ford) {
                    if("success".equals(element.attributeValue("name"))) {
                    String path=element.attributeValue("path");
                    System.out.println(path);
                    }
                }
                                
            }
        }

Modeling Xml

1. What is modeling Xml

                   Convert XML configuration file elements, attributes, text information into objects called XML Process Modeling

Modeling 2.XML

                    1. Create an element node, the entity classes based on XML configuration file element nodes
                     ConfigModel ActionModel ForwardModel

                     2. The use of XML technology dom4j + xpath modeling ConfigModelFactory

2. XML建模类
                      ConfigModel
                      ActionModel
                      ForwardModel
                      ConfigModelFactory

Modeling ideas

 

                     1, the analysis needs to be modeled in the file that has several objects
                     2, each object has behavior and attributes
                     3, the definition of small to large objects (inside and out)
                     4, by 23 kinds of design patterns factory pattern parse xml produce the specified object
                     benefits:
                                          improve code reusability

4. Use the engineering mode + dom4j + xpath parse XML configuration file

Config.xml file

 

<? XML Version = "1.0" encoding = "UTF-. 8"?> 
<config> 
    <- Action Tags:! 0 ~ N th can be full forward tag path: in / at the beginning of the string and a non-null value must be unique type: string, a non-empty -> 
    <action path = "/ regAction" type = "test.RegAction"> 
        <- Forward tags: no sub-labels; name:! string, forward under the same label name tag action value can not be the same; path: in / string beginning 
            redirect: only to false | to true , allowing empty, the default value to false -> 
        <Forward name = "failed" path = "/ reg.jsp" = the redirect "to false "/> 
        <name = Forward" Success "path =" / the login.jsp "= the redirect" to 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>

 

3. Map set storage element child nodes, wherein the unique Key attribute child node, Value for the whole child node object.

Defined object entity class

package com.zking.model;

public class FowardModel {
/*
 * 添加节点属性
 */
    private String name;
    private String path;
    private Boolean reairect;
    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 getReairect() {
        return reairect;
    }
    public void setReairect(Boolean reairect) {
        this.reairect = reairect;
    }
}

Behavior and attributes 

package com.zking.model;

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

public class ActionModel {

    private String path;
    private String type;
    private Map<String, FowardModel> fmap=new HashMap<>();
    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;
    }
    
    //存放到Map集合
    public void push(FowardModel fowardModel) {
        fmap.put(fowardModel.getName(), fowardModel);
    }
    //获取 返回FowardModel
    public FowardModel pop(String name) {
    return    fmap.get(name);

    }
}
package com.zking.model;

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

public class ConfigModel {

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

4. Use the engineering mode + dom4j + xpath parse XML configuration file

Import package dom4j

Package com.zking.model; 

Import a java.io.InputStream;
 Import java.util.List; 

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

/ * 
 * 
 * one of 23 design patterns: the factory pattern is used to produce a resource file designated entity class 
 * deal with some specific problems encountered in Java 
 * 
 * benefits: 
 * increase code reusability 
 * / 
public  class {ConfigModelFactory 

    public  static ConfigModel Build () throws DocumentException {
         return ConfigModel ( "the config.xml"  );
    }
/*
 * 生产出有类容的实体类ConfigModel
 */
    private static ConfigModel ConfigModel(String xmlPath) throws DocumentException {
        // TODO Auto-generated method stub
        // 创建ConfigModel对象
        ConfigModel    configModel=new ConfigModel();
        ActionModel actionModel=null;
        FowardModel fowardModel=null;
        InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath);
        SAXReader saxRead=new SAXReader();
        Document doc = saxRead.read(in);
        
        List<Element> actionEles = doc.selectNodes("/config/action");
        for (Element actionEle : actionEles) {
            actionModel=new ActionModel();
            
            //给actionModel对象填充Xml中的action标签的类容
            actionModel.setPath(actionEle.attributeValue("path"));
            actionModel.setType(actionEle.attributeValue("type"));
            
            List<Element> fowardEles = actionEle.selectNodes("foward");
            for (Element fowardEle : fowardEles) {
                fowardModel=new new FowardModel (); 
                
                to fowardModel object fill in the foward tag Xml class content 
                fowardModel.setName (fowardEle.attributeValue ( "name" )); 
                fowardModel.setPath (fowardEle.attributeValue ( "path" )); 
                fowardModel.setReairect ( ! "to false" .equals (fowardEle.attributeValue ( "the redirect" )));
                 // <Forward name = "failed" path = "/ the login.jsp" = the redirect "to false" />
 //                the redirect default redirection true
                 // only forward is false
                 // fowardEle.attributeValue ( "redirect") is the value you get filled in the xml
                 //// not fill redirect! "false" .equals (fowardEle.
                attributeValue("redirect")) false
                //填 true 重定向   !"false".equals(fowardEle.attributeValue("redirect")) false
                //填false 转发   !"false".equals(fowardEle.attributeValue("redirect")) true
                
                
                actionModel.push(fowardModel);
            }
            
            configModel.push(actionModel);
        }
        return configModel;
    }
    public static void main(String[] args) throws DocumentException {
        ConfigModel configModel=ConfigModelFactory.build();
        ActionModel actionModel= configModel.pop("/loginAction");
        System.out.println(actionModel.getType());
    }
    
}

Console input results:

 

Xml modeling job

ServletClassModel

 

package com.zking.xml.webModel;

public class ServletClassModel {
    private String context;

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }
}

 

ServletMappingModel

 

package com.zking.xml.webModel;

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

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

 

ServletModel

package com.zking.xml.webModel;

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

    public ServletNameModel getServletNameModel() {
        return servletNameModel;
    }

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

    public ServletClassModel getServletClassModel() {
        return servletClassModel;
    }

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

}

ServletNameModel

package com.zking.xml.webModel;

public class ServletNameModel {
    private String context;

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }
}

UrlPatternModel 

 

package com.zking.xml.webModel;

public class UrlPatternModel {
    private String context;

    public String getContext() {
        return context;
    }

    public void setContext(String context) {
        this.context = context;
    }

}

 

WebAppModel

package com.zking.xml.webModel;

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

public class WebAppModel {
    private List<ServletModel> servletModels = new ArrayList<>();
    private List<ServletMappingModel> servletMappingModels = new ArrayList<>();

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

}

WebAppModelFactory

package com.zking.xml.webModel;

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 WebAppModel buildWebAppModel() {
        String xmlPath = "/web.xml";
        return buildWebAppModel(xmlPath);
    }

    
    public static WebAppModel buildWebAppModel(String xmlPath) {
        InputStream in = WebAppModelFactory.class.getResourceAsStream(xmlPath);
        SAXReader saxReader = new SAXReader();
        WebAppModel webAppModel = new WebAppModel();
        try {
            Document doc = saxReader.read(in);
            
            List<Element> 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");
                ServletNameModel servletNameModel = new ServletNameModel();
                ServletClassModel servletClassModel = new ServletClassModel();
                servletNameModel.setContext(servletNameEle.getText());
                servletClassModel.setContext(servletClassEle.getText());
                
                servletModel.setServletNameModel(servletNameModel);
                servletModel.setServletClassModel(servletClassModel);

                webAppModel.pushServletModel(servletModel);
            }

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

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

                webAppModel.pushServletMappingModel(servletMappingModel);
            }

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

    public static String getServletClassByUrl(WebAppModel webAppModel, String url) {
        String servletClass = "";
    
        String servletName = "";
        List<ServletMappingModel> servletMappingModels = webAppModel.getServletMappingModels();
        for (ServletMappingModel servletMappingModel : servletMappingModels) {
            List<UrlPatternModel> urlPatternModels = servletMappingModel.getUrlPatternModels();
            for (UrlPatternModel urlPatternModel : urlPatternModels) {
                if(url.equals(urlPatternModel.getContext())) {
                    ServletNameModel servletNameModel = servletMappingModel.getServletNameModel();
                    servletName = servletNameModel.getContext();
                }
            }
        }
        

        List<ServletModel> servletModels = webAppModel.getServletModels();
        for (ServletModel servletModel : servletModels) {
            ServletNameModel servletNameModel = servletModel.getServletNameModel();
            if(servletName.equals(servletNameModel.getContext())) {
                ServletClassModel servletClassModel = servletModel.getServletClassModel();
                servletClass = servletClassModel.getContext();
            }
        }
        return servletClass;
    }
    
    public static void main(String[] args) {
        WebAppModel 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);
        
    }
}

                                                                                                                                                                                                                                                                                                 2019-06-1119:22:01

 

Guess you like

Origin www.cnblogs.com/xcn123/p/11005515.html