From 4513ef24bf9b188c2a77d6ce94f1a6b7e9ebf0e6 Mon Sep 17 00:00:00 2001 From: zuoxiao <470321431@qq.com> Date: 星期日, 27 四月 2025 20:40:19 +0800 Subject: [PATCH] fix(irrigatePlan): 修正灌溉计划开始时间逻辑 --- pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ConfigXml.java | 495 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 495 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ConfigXml.java b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ConfigXml.java new file mode 100644 index 0000000..9bffe3c --- /dev/null +++ b/pipIrr-platform/pipIrr-common/src/main/java/com/dy/common/util/ConfigXml.java @@ -0,0 +1,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("娌℃湁鐢熸垚閰嶇疆鏂囦欢鐨凞OM瀵硅薄!", null); + } + } catch (Exception e) { + throw new Exception("鐢熸垚閰嶇疆鏂囦欢鐨凞OM瀵硅薄澶辫触!", 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 ; + } + // 灏哾oc瀵硅薄杈撳嚭鍒版枃浠� + 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); + // 灏哾oc鍐欏叆鍒版寚瀹氱殑鏂囦欢涓� + 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() ; + } + /** + * 寰楀埌骞惰缃厓绱犵殑灞炴�х殑姝f暣鏁板�� + * @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) ; + } + + /** + * 寰楀埌骞惰缃厓绱犵殑灞炴�х殑姝f暣鏁板�� + * @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) ; + } + + /** + * 寰楀埌骞惰缃厓绱犵殑灞炴�х殑姝f诞鐐规暟鍊� + * @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("閰嶇疆鏂囦欢鐨凞OM瀵硅薄涓虹┖!"); + } + 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 ; + } + +} -- Gitblit v1.8.0