fwq
昨天 1dd767dcc9919e3a0068835cd0dbca57f94debd5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package com.hx.auto;
 
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.*;
 
/**
 * 自动生成Service and ServiceImpl
 * 
 * @author chenjiahe 2020-06-28
 * 
 */
public class GeneratorReadXmlUtil {
 
    /**
     *
     * @param targetFile 文件路径
     * @return 返回自定义的sql
     */
    public static String readMapperXml(String  targetFile) throws ParserConfigurationException, IOException, SAXException {
        String temp = "";
 
        //映射文件的文件夹
        File file = new File(targetFile);
        if(!file.exists()){
            return "false";
        }
 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(file);
        Node Node= doc.getLastChild();
        NodeList nodeList = Node.getChildNodes();
 
        String comment = "";
        for(int i=0;i<nodeList.getLength();i++) {
            //从节点集中获取i个book节点
            Node childNode = nodeList.item(i);
            //获取子节点内的文本内容
            String content = childNode.getTextContent();
            //获取节点名
            String name = childNode.getNodeName();
            //System.out.println("content:"+content+";name:"+name);
            //System.out.println("name....:"+name);
            if(!"#text".equals(name)&&!"#comment".equals(name)){
                //获取第一个节点内的所有属性
                NamedNodeMap nameNodeMap = childNode.getAttributes();
                //获取节点内名为id的属性的值
                String id = nameNodeMap.getNamedItem("id").getTextContent();
                if(isCustom(nameNodeMap.getNamedItem("id").getTextContent())){
                    String con = NodetoString(childNode);
                    //去掉附带的内容
                    con = con.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","");
                    String con2 = con;
                    con = con.replaceAll("\t","").replaceAll("\n","");
                    if(con.length()>3){
                        con= con2;
                    }
                    temp+=comment+"\n\t"+con;
                }
                comment = "";
            }else{
                String con = NodetoString(childNode);
                //去掉附带的内容
                con = con.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>","");
                String con2 = con;
                //去掉多余的
                con = con.replaceAll("\t","").replaceAll("\n","");
                if(con.length()>5){
                    comment+= con2;
                }
            }
        }
        System.out.println("temp:...:"+temp);
        return temp;
    }
 
    public static boolean isCustom(String id){
        boolean custom = true;
        Set<String> noCustomIds = new HashSet<String>();
        noCustomIds.add("BaseResultMap");
        noCustomIds.add("Entity_Id");
        noCustomIds.add("Table_Id");
        noCustomIds.add("Table_Name");
        noCustomIds.add("Base_Column_List");
        noCustomIds.add("Blob_Column_List");
        noCustomIds.add("Insert_Column_All");
        noCustomIds.add("Update_Column_All");
        noCustomIds.add("Update_Column_NoNull");
        noCustomIds.add("keyFind");
        noCustomIds.add("insert");
        noCustomIds.add("insertById");
        noCustomIds.add("selectList");
        noCustomIds.add("selectListBlob");
        noCustomIds.add("selectOne");
        noCustomIds.add("selectOneBlob");
        noCustomIds.add("selectOneByKey");
        noCustomIds.add("selectOneByKeyBlob");
        noCustomIds.add("updateWhere");
        noCustomIds.add("updateAll");
        noCustomIds.add("updateByNoNull");
        noCustomIds.add("deleteWhere");
        noCustomIds.add("deleteById");
        noCustomIds.add("selectCount");
        noCustomIds.add("Insert_Values_All");
        noCustomIds.add("selectListMap");
        noCustomIds.add("selectOneMap");
        noCustomIds.add("selectCountSql");
        if (noCustomIds.contains(id)){
            custom = false;
        }
        return custom;
    }
 
 
 
    /*
     * 把dom文件转换为xml字符串
     */
    public static String toStringFromDoc(Document document) throws IOException {
        String result = null;
 
        if (document != null) {
            StringWriter strWtr = new StringWriter();
            StreamResult strResult = new StreamResult(strWtr);
            TransformerFactory tfac = TransformerFactory.newInstance();
            try {
                javax.xml.transform.Transformer t = tfac.newTransformer();
                t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                t.setOutputProperty(OutputKeys.INDENT, "yes");
                t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html,
// text
                t.setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount", "4");
                t.transform(new DOMSource(document.getDocumentElement()),
                        strResult);
            } catch (Exception e) {
                System.err.println("XML.toString(Document): " + e);
            }
            result = strResult.getWriter().toString();
            strWtr.close();
        }
        return result;
    }
 
 
    /**
     * 将传入的一个DOM Node对象输出成字符串。如果失败则返回一个空字符串""。
     *
     * @param node
     *            DOM Node 对象。
     * @return a XML String from node
     */
    public static String NodetoString(Node node) {
        Transformer transformer = null;
        String result = null;
        if (node == null) {
            throw new IllegalArgumentException();
        }
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        if (transformer != null) {
            try {
                StringWriter sw = new StringWriter();
                transformer.transform(new DOMSource(node), new StreamResult(sw));
                return sw.toString();
            } catch (TransformerException te) {
                throw new RuntimeException(te.getMessage());
            }
        }
        return result;
    }
 
 
}