2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package com.landtool.lanbase.common.utils;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
 
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
 
import com.landtool.lanbase.modules.api.utils.DOMUtil;
import com.landtool.lanbase.modules.res.entity.WebService.ParameterInfo;
import com.landtool.lanbase.modules.res.entity.WebService.SchemaDefaultType;
 
/**
 * 第二步,使用DOM解析XML Schema,获取元素名称和类型
 *
 * @author wangleai
 */
public class WADOMUtil {
 
    private static Logger log = Logger.getLogger(WADOMUtil.class);
 
    /**
     * 查找某个操作含有的参数
     *
     * @param inputParamList
     * @param document
     * @param typeName         类型名称
     * @param xPath            到element上个结点的xpath
     * @param parentParam
     * @param isSelfDefinition 是否自己定义
     * @throws Exception
     */
    public static void getInputParams(List<ParameterInfo> inputParamList, Document document, String typeName,
                                      String xPath, ParameterInfo parentParam, boolean isSelfDefinition) throws
            Exception {
        // 是否添加结点
        boolean canAddParam = true;
 
        // 参数
        ParameterInfo param = new ParameterInfo();
        param.setName(typeName);
 
        // 寻找element节点
        String elementXPath = getElementXPath(xPath, typeName);
        Node elementNode = WADOMUtil.findNode(document, elementXPath);
 
        // 判断element是否为空,为空说明该类型名称可能是直接定义在simpleType或complexType中
        // 判断是否是自己定义,防止同名导致的堆栈溢出问题
        if (elementNode != null && isSelfDefinition) {
            // 是否含type属性
            if (DOMUtil.assertNodeAttributeExist(elementNode, "type")) {
                String type = DOMUtil.getNodeType(elementNode);
 
                // 是否是数组,XML Schema中map会定义成List<Object>的形式
                if (DOMUtil.isArray(elementNode)) {
                    param.setType(SchemaDefaultType.type_array.getType());
                    param.setChildType(type);
 
                    // 复杂类型数组
                    if (!DOMUtil.isDefaultType(elementNode)) {
                        getInputParams(inputParamList, document, type, xPath, param, false);
                    } else {
                        param.setValue(WAIXPathConstant.VALUEDEF);
                    }
                } else {
                    param.setType(type);
 
                    // 复杂类型
                    if (!DOMUtil.isDefaultType(elementNode)) {
                        getInputParams(inputParamList, document, type, WAIXPathConstant.SCHEMAXPATH, param, false);
                    } else {
                        param.setValue(WAIXPathConstant.VALUEDEF);
                    }
                }
            } else {
                // 是否是数组,XML Schema中map会定义成List<Object>的形式
                if (DOMUtil.isArray(elementNode)) {
                    param.setType(SchemaDefaultType.type_array.getType());
                }
 
                // 如果type属性不存在,说明该结点的类型在其子结点中定义
                // 判断是simpleType还是complexType
                Node simpleTypeNode = getSimpleTypeNode(document, typeName, elementXPath, true);
                if (simpleTypeNode != null) {
                    String type = DOMUtil.getNodeBase(simpleTypeNode);
                    if (DOMUtil.isArray(elementNode)) {
                        param.setChildType(type);
                    } else {
                        param.setType(type);
                    }
                    param.setValue(WAIXPathConstant.VALUEDEF);
                    // TODO: 2018/2/24
                    // 忽略限定条件
                } else {
                    // 寻找complexType
                    List<Node> nodeList = getComplexTypeSequenceElement(document, typeName, elementXPath, true);
                    if (nodeList.size() != 0) {
                        for (Node tempNode : nodeList) {
                            // 遍历element
                            String elementName = DOMUtil.getNodeName(tempNode);
                            String sequenceXPath = getSequenceXPathByName(document, typeName, elementXPath, true,
                                    elementName);
                            getInputParams(inputParamList, document, elementName, sequenceXPath, param, true);
                        }
                    } else {
                        log.warn("unknown type " + typeName + ",please check your document");
                    }
                }
            }
        } else {
            // 非自己定义的Type添加到父结点
            canAddParam = false;
 
            // 判断是simpleType还是complexType
            Node simpleTypeNode = getSimpleTypeNode(document, typeName, xPath, false);
            if (simpleTypeNode != null) {
                String type = DOMUtil.getNodeBase(simpleTypeNode);
                parentParam.setType(type);
                parentParam.setValue(WAIXPathConstant.VALUEDEF);
                // TODO: 2018/2/24
                // 忽略限定条件
            } else {
                // 寻找complexType
                List<Node> nodeList = getComplexTypeSequenceElement(document, typeName, elementXPath, false);
                if (nodeList.size() != 0) {
                    for (Node tempNode : nodeList) {
                        // 遍历element
                        String elementName = DOMUtil.getNodeName(tempNode);
                        String sequenceXPath = getSequenceXPathByName(document, typeName, elementXPath, false,
                                elementName);
                        getInputParams(inputParamList, document, elementName, sequenceXPath, parentParam, true);
                    }
                } else {
                    log.warn("unknown type " + typeName + ",please check your document");
                }
            }
        }
        if (canAddParam) {
            if (parentParam == null) {
                inputParamList.add(param);
            } else {
                parentParam.addChild(param);
            }
        }
    }
 
    /**
     * 获取element的xpath
     *
     * @param xPath    父结点xpath
     * @param typeName 名称
     * @return
     */
    private static String getElementXPath(String xPath, String typeName) {
        String elementXPath = xPath + "/*[local-name()='element' and @name='" + typeName + "']";
        return elementXPath;
    }
 
    /**
     * 获取simpleType结点
     *
     * @param document
     * @param simpleTypeName   simpleType名称,当为自己定义时可以为空
     * @param elementXPath     XPath路径,当非自己定义时可以为空
     * @param isSelfDefinition 是否为自己定义
     * @return
     * @throws Exception
     */
    private static Node getSimpleTypeNode(Document document, String simpleTypeName, String elementXPath,
                                          boolean isSelfDefinition) throws Exception {
        String simpleTypeXPath = isSelfDefinition
                ? elementXPath + "/*[local-name()='simpleType']/*[local-name()='restriction']"
                : WAIXPathConstant.SCHEMAXPATH + "/*[local-name()='simpleType' and @name='" + simpleTypeName
                + "']/*[local-name()='restriction']";
        Node node = WADOMUtil.findNode(document, simpleTypeXPath);
        return node;
    }
 
    /**
     * 获取complexType中element
     *
     * @param document
     * @param complexTypeName  complexType名称,当为自己定义时可以为空
     * @param elementXPath     XPath路径,当非自己定义时可以为空
     * @param isSelfDefinition 是否为自己定义
     * @return
     * @throws Exception
     */
    private static List<Node> getComplexTypeSequenceElement(Document document, String complexTypeName,
                                                            String elementXPath, boolean isSelfDefinition) throws
            Exception {
        List<Node> nodeList = new ArrayList<Node>();
        // 判断是否有继承
        String extensionXpath = isSelfDefinition
                ? elementXPath + "/*[local-name()='complexType']/*[local-name()='complexContent']/*[local-name()"
                + "='extension']"
                : WAIXPathConstant.SCHEMAXPATH + "/*[local-name()='complexType' and @name='" + complexTypeName
                + "']/*[local-name()='complexContent']/*[local-name()='extension']";
        Node extension = WADOMUtil.findNode(document, extensionXpath);
        if (extension != null) {
            // 存在继承
            // 添加父类
            String parentTypeName = DOMUtil.getAttributeValue(extension, "base").split(":")[1];
            List<Node> parentElements = getComplexTypeSequenceElement(document, parentTypeName, elementXPath, false);
            if (parentElements != null && parentElements.size() > 0) {
                nodeList.addAll(parentElements);
            }
            // 查找自己
            String selfXpath = extensionXpath + "/*[local-name()='sequence']/*[local-name()" + "='element']";
            NodeList selfList = WADOMUtil.findNodeList(document, selfXpath);
            if (selfList != null && selfList.getLength() > 0) {
                nodeList.addAll(DOMUtil.covertNodeListToList(selfList));
            }
        } else {
            // 查找自己的属性
            String elementsOfSequenceXpath = isSelfDefinition
                    ? elementXPath + "/*[local-name()='complexType']/*[local-name()='sequence']/*[local-name()"
                    + "='element']"
                    : WAIXPathConstant.SCHEMAXPATH + "/*[local-name()='complexType' and @name='" + complexTypeName
                    + "']/*[local-name()='sequence']/*[local-name()='element']";
            NodeList elementsOfSequence = WADOMUtil.findNodeList(document, elementsOfSequenceXpath);
            nodeList = DOMUtil.covertNodeListToList(elementsOfSequence);
        }
        return nodeList;
    }
 
    /**
     * 获取complexType中element上一级sequence的xpath
     *
     * @param document
     * @param complexTypeName  complexType名称,当为自己定义时可以为空
     * @param elementXPath     XPath路径,当非自己定义时可以为空
     * @param isSelfDefinition 是否为自己定义
     * @param elementName      结点名称
     * @return
     * @throws Exception
     */
    private static String getSequenceXPathByName(Document document, String complexTypeName, String elementXPath,
                                                 boolean isSelfDefinition, String elementName) throws Exception {
        String result = "";
        // 判断是否有继承
        String extensionXpath = isSelfDefinition
                ? elementXPath + "/*[local-name()='complexType']/*[local-name()='complexContent']/*[local-name()"
                + "='extension']"
                : WAIXPathConstant.SCHEMAXPATH + "/*[local-name()='complexType' and @name='" + complexTypeName
                + "']/*[local-name()='complexContent']/*[local-name()='extension']";
        Node extension = WADOMUtil.findNode(document, extensionXpath);
        if (extension != null) {
            // 存在继承
            // 在父类查找
            String parentTypeName = DOMUtil.getAttributeValue(extension, "base").split(":")[1];
            result = getSequenceXPathByName(document, parentTypeName, elementXPath, false, elementName);
 
            // 在自己查找
            String sequenceXPath = extensionXpath + "/*[local-name()='sequence']";
            String eleXPath = sequenceXPath + "/*[local-name()='element' and @name='" + elementName + "']";
            Node selfNode = WADOMUtil.findNode(document, eleXPath);
            if (selfNode != null) {
                result = sequenceXPath;
            }
        } else {
            // 在自己查找
            String sequenceXPath = isSelfDefinition
                    ? elementXPath + "/*[local-name()='complexType']/*[local-name()='sequence']"
                    : WAIXPathConstant.SCHEMAXPATH + "/*[local-name()='complexType' and @name='" + complexTypeName
                    + "']/*[local-name()='sequence']";
            String eleXpath = sequenceXPath + "/*[local-name()='element' and @name='" + elementName + "']";
            Node selfNode = WADOMUtil.findNode(document, eleXpath);
            if (selfNode != null) {
                result = sequenceXPath;
            }
        }
        return result;
    }
 
    /**
     * 在document中查找结点,如果查找不到,则进入import结点中递归查找
     *
     * @param document
     * @param xpathStr
     * @return
     * @throws Exception
     */
    public static Node findNode(Document document, String xpathStr) throws Exception {
        XPath xpath = WAWsdlUtil.getXPath();
        Node node = (Node) xpath.evaluate(xpathStr, document, XPathConstants.NODE);
        if (node == null) {
            List<Document> importDocumentList = WAWsdlUtil.getImportDocumentList(document, xpath);
            for (Document importDoucment : importDocumentList) {
                node = findNode(importDoucment, xpathStr);
                if (node != null) {
                    return node;
                }
            }
        }
        return node;
    }
 
    /**
     * 在document中查找结点,如果查找不到,则进入import结点中递归查找
     *
     * @param document
     * @param xpathStr
     * @return
     * @throws Exception
     */
    public static NodeList findNodeList(Document document, String xpathStr) throws Exception {
        XPath xpath = WAWsdlUtil.getXPath();
        NodeList nodeList = (NodeList) xpath.evaluate(xpathStr, document, XPathConstants.NODESET);
        if (nodeList.getLength() == 0) {
            List<Document> importDocumentList = WAWsdlUtil.getImportDocumentList(document, xpath);
            for (Document importDoucment : importDocumentList) {
                nodeList = findNodeList(importDoucment, xpathStr);
                if (nodeList.getLength() > 0) {
                    return nodeList;
                }
            }
        }
        return nodeList;
    }
}