liurunyu
2025-03-21 fb50c6c8111fffd16091ce25d5d389ea3fcc560f
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package com.dy.common.util;
 
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
 
import java.io.FileWriter;
import java.net.URL;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/8/21 15:27
 * @Description
 */
public class ConfigXml {
 
    /**
     * 创建Document对象
     * @param configFileURL  配置文件路径
     * @return 返回结果  返回结果返回 doc对象
     * @throws Exception  抛出异常 抛出异常
     */
    protected Document doCreateDom(URL configFileURL) throws Exception {
        if (configFileURL == null) {
            throw new Exception("没有得到配置文件!", null);
        }
        Document doc;
        try {
            SAXBuilder sb = new SAXBuilder();
            doc = sb.build(configFileURL);
            if (doc == null) {
                throw new Exception("没有生成配置文件的DOM对象!", null);
            }
        } catch (Exception e) {
            throw new Exception("生成配置文件的DOM对象失败!", e);
        }
        return doc;
    }
 
 
    /**
     * 保存xml文件
     * @param clazz 参考类
     * @param doc doc对象 doc对象
     * @param filePath 配置文件路径
     * @throws Exception  抛出异常 抛出异常
     */
    public void saveXML(Class<?> clazz, Document doc, String filePath) throws Exception {
        if(doc == null){
            throw new Exception("Document对象为空!");
        }
        if(filePath == null || filePath.equals("")){
            throw new Exception("配置文件路径名称为空!");
        }
        if(!filePath.startsWith("/")){
            filePath = "/" + filePath ;
        }
        // 将doc对象输出到文件
        FileWriter writer = null ;
        try {
            // 创建xml文件输出流
            XMLOutputter xmlopt = new XMLOutputter();
            // 创建文件输出流
            URL configFileURL = clazz.getResource(filePath);
            if (configFileURL == null) {
                throw new Exception("没有得到" + filePath + "配置!");
            }
            writer = new FileWriter(configFileURL.getPath());
            // 指定文档格式
            Format fm = Format.getPrettyFormat();
            fm.setEncoding("UTF-8");
            xmlopt.setFormat(fm);
            // 将doc写入到指定的文件中
            xmlopt.output(doc, writer);
        } catch (Exception e) {
            throw new Exception("保存xml文件失败!", e);
        } finally {
            if(writer != null){
                writer.close();
            }
        }
    }
 
    /**
     * 检查元素是否存在
     * @param doc doc对象
     * @param elementName 元素名称
     * @return 返回结果  返回结果
     */
    public Element getElement(Document doc, String elementName){
        if(doc == null){
            return null ;
        }
        if(elementName == null){
            return null ;
        }
        elementName = elementName.trim() ;
        if(elementName.equals("")){
            return null ;
        }
        return this.get_element(doc, elementName) ;
    }
 
    /**
     * 检查元素是否存在
     * @param doc doc对象
     * @param elementName 元素名称
     * @return 返回结果  返回结果
     */
    public boolean existElement(Document doc, String elementName){
        if(doc == null){
            return false ;
        }
        if(elementName == null){
            return false ;
        }
        elementName = elementName.trim() ;
        if(elementName.equals("")){
            return false ;
        }
        Element e = this.get_element(doc, elementName) ;
        return e != null;
    }
 
 
    /**
     * 得到并设置元素的属性的布尔值
     * @param doc doc对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public boolean getSetAttrBoolean(Document doc, String elementName, String attrName, String attPropertyNameFix, String setValue) throws Exception{
        String txt = this.getSetAttrTxt(doc, elementName, attrName, attPropertyNameFix, false, setValue) ;
        return txt.equalsIgnoreCase("true");
    }
 
    /**
     * 得到并设置元素的属性的布尔值
     * @param e 元素对象
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public boolean getSetAttrBoolean(Element e, String attrName, String attPropertyNameFix, String setValue) throws Exception{
        String txt = this.getSetAttrTxt(e, attrName, attPropertyNameFix, false, setValue) ;
        return txt.equalsIgnoreCase("true");
    }
 
    /**
     * 得到并设置元素的属性的字符串值
     * @param doc doc对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param canBlank 是否可为空
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public String getSetAttrTxt(Document doc, String elementName, String attrName, String attPropertyNameFix, Boolean canBlank, String setValue) throws Exception{
        Element e = this.check_and_get_element(doc, elementName);
        return this.getSetAttrTxt(e, attrName, attPropertyNameFix, canBlank, setValue) ;
    }
 
    /**
     * 得到并设置元素的属性的字符串值
     * @param e 元素对象
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param canBlank 是否可为空
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public String getSetAttrTxt(Element e, String attrName, String attPropertyNameFix, Boolean canBlank, String setValue) throws Exception{
        String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
        if(!canBlank){
            if(txt.trim().equals("")){
                throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值为空字符串!");
            }
        }
        return txt.trim() ;
    }
    /**
     * 得到并设置元素的属性的正整数值
     * @param doc doc对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Integer getSetAttrPlusInt(Document doc, String elementName, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
        Element e = this.check_and_get_element(doc, elementName);
        return this.getSetAttrPlusInt(e, attrName, attPropertyNameFix, min, max, setValue) ;
    }
 
    /**
     * 得到并设置元素的属性的正整数值
     * @param e 元素对象
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Integer getSetAttrPlusInt(Element e, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
        int v ;
        String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
        if(!NumUtil.isPlusIntNumber(txt)){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正整数!");
        }
        v = Integer.parseInt(txt);
        if(min != null && v < min){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
        }
        if(max != null && v > max){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
        }
        return v ;
    }
 
    /**
     * 得到并设置元素的属性的整数值
     * @param doc doc对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Integer getSetAttrInt(Document doc, String elementName, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
        Element e = this.check_and_get_element(doc, elementName);
        return this.getSetAttrInt(e, attrName, attPropertyNameFix, min, max, setValue) ;
    }
 
    /**
     * 得到并设置元素的属性的整数值
     * @param e 元素对象
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Integer getSetAttrInt(Element e, String attrName, String attPropertyNameFix, Integer min, Integer max, String setValue) throws Exception{
        int v ;
        String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
        if(!NumUtil.isIntNumber(txt)){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正整数!");
        }
        v = Integer.parseInt(txt) ;
        if(min != null && v < min){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
        }
        if(max != null && v > max){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
        }
        return v ;
    }
 
    /**
     * 得到并设置元素的属性的浮点数值
     * @param doc doc对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Double getSetAttrPlusDouble(Document doc, String elementName, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
        Element e = this.check_and_get_element(doc, elementName);
        return this.getSetAttrPlusDouble(e, attrName, attPropertyNameFix, min, max, setValue) ;
    }
 
    /**
     * 得到并设置元素的属性的正浮点数值
     * @param e 元素对象
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Double getSetAttrPlusDouble(Element e, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
        double v ;
        String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
        if(!NumUtil.isPlusDoubleNumber(txt)){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正浮点数!");
        }
        v = Double.parseDouble(txt) ;
        if(min != null && v < min){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
        }
        if(max != null && v > max){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
        }
        return v ;
    }
 
    /**
     * 得到并设置元素的属性的浮点数值
     * @param doc doc对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Double getSetAttrDouble(Document doc, String elementName, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
        Element e = this.check_and_get_element(doc, elementName);
        return this.getSetAttrDouble(e, attrName, attPropertyNameFix, min, max, setValue) ;
    }
 
    /**
     * 得到并设置元素的属性的浮点数值
     * @param e 元素对象
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param min 最小值
     * @param max 最大值
     * @param setValue 同时设置值
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    public Double getSetAttrDouble(Element e, String attrName, String attPropertyNameFix, Double min, Double max, String setValue) throws Exception{
        double v ;
        String txt = this.check_get_set_Attribute(e, e.getName(), attrName, attPropertyNameFix, setValue) ;
        if(!NumUtil.isDoubleNumber(txt)){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值不是正浮点数!");
        }
        v = Double.parseDouble(txt) ;
        if(min != null && v < min){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值小于" + min + "!");
        }
        if(max != null && v > max){
            throw new Exception("元素" + e.getName() + "的属性" + attrName + "的值大于" + max + "!");
        }
        return v ;
    }
    /**
     * 得到元素
     * @param doc doc对象
     * @param elementName 元素名称 元素名,形如"config.base.test"
     * @return 返回结果
     */
    private Element get_element(Document doc, String elementName){
        Element e = null ;
        Element root ;
        String nm ;
        String[] eNames = elementName.split("\\.") ;
        if(eNames.length > 0){
            root = doc.getRootElement() ;
            nm = root.getName() ;
            if(nm.equals(eNames[0].trim())){
                if(eNames.length == 1){
                    e = root ;
                }else{
                    e = this.get_next_element(root, eNames, 1) ;
                }
            }
        }
        return e ;
    }
 
    /**
     * 得到子元素
     * @param base 上级元素对象
     * @param eNames 元素名称
     * @param index 数组下标
     * @return 返回结果
     */
    private Element get_next_element(Element base, String[] eNames, int index){
        Element e = null ;
        String nm ;
        List<Element> list = base.getChildren() ;
        if(list != null && list.size() >0){
            for(Element el: list){
                nm = el.getName() ;
                if(nm.equals(eNames[index].trim())){
                    if(eNames.length == index + 1){
                        e = el ;
                    }else{
                        e = this.get_next_element(el, eNames, index + 1) ;
                    }
                    break ;
                }
            }
        }
        return e ;
    }
    /**
     * 检查并获元素
     * @param doc doc对象
     * @param elementName 元素名称
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    private Element check_and_get_element(Document doc, String elementName)throws Exception{
        if(doc == null){
            throw new Exception("配置文件的DOM对象为空!");
        }
        if(elementName == null){
            throw new Exception("元素名为空!");
        }
        elementName = elementName.trim() ;
        if(elementName.equals("")){
            throw new Exception("元素名为空!");
        }
        Element e = this.get_element(doc, elementName) ;
        if(e == null){
            throw new Exception("未得到名称为" + elementName + "的元素!");
        }
        return e ;
    }
 
    /**
     * 检查并获得并设置属性值
     * @param e 元素对象
     * @param elementName 元素名称
     * @param attrName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @return 返回结果
     * @throws Exception  抛出异常
     */
    private String check_get_set_Attribute(Element e, String elementName, String attrName, String attPropertyNameFix, String setValue)throws Exception{
        if(attrName == null){
            throw new Exception("属性名为空!");
        }
        attrName = attrName.trim() ;
        if(attrName.equals("")){
            throw new Exception("属性名为空!");
        }
        String txt = this.get_set_AttributTxt(e, attrName, attPropertyNameFix, setValue) ;
        if(txt == null){
            throw new Exception("未得到元素" + elementName + "的属性" + attrName + "的值!");
        }
        return txt ;
    }
    /**
     * 得到并设置属性配置值
     * @param e 元素对象
     * @param attName 属性名称
     * @param attPropertyNameFix 属性名称前缀
     * @param setValue 同时设置值
     * @return 返回结果
     */
    private String get_set_AttributTxt(Element e, String attName, String attPropertyNameFix, String setValue){
        String txt = null ;
        if(e != null && attName != null){
            attName = attName.trim() ;
            if(!attName.equals("")){
                txt = e.getAttributeValue(attName) ;
                if(setValue != null){
                    e.setAttribute(attName, setValue) ;
                }
            }
        }
        if(txt != null && txt.startsWith("${") && txt.endsWith("}")){
            txt = txt.substring(2, txt.length() - 1) ;
            if(attPropertyNameFix != null){
                txt = txt + attPropertyNameFix ;
            }
            txt = ConfigProperties.getConfig(txt) ;
        }
        return txt ;
    }
 
}