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