From: rnayabed Date: Sat, 26 Dec 2020 01:23:48 +0530 Subject: Added java.xml parser helpers --- Added java.xml parser helpers --- --- /dev/null +++ b/src/main/java/com/StreamPi/Util/XMLConfigHelper/XMLConfigHelper.java @@ -0,0 +1,101 @@ +package com.StreamPi.Util.XMLConfigHelper; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +public class XMLConfigHelper { + + public XMLConfigHelper() + { + + } + + + public String getStringProperty(Element parentElement, String propertyName) throws Exception + { + return getProperty(parentElement, propertyName); + } + + public int getIntProperty(Element parentElement, String propertyName) throws Exception + { + return Integer.parseInt(getProperty(parentElement, propertyName)); + } + + public boolean getBooleanProperty(Element parentElement, String propertyName) throws Exception + { + return getProperty(parentElement, propertyName).equals("true"); + } + + public String getProperty(Element parentElement, String propertyName) throws Exception + { + return parentElement.getElementsByTagName(propertyName).item(0).getTextContent(); + } + + + public String getStringProperty(Element parentElement, String propertyName, String ifNotPresent, boolean printStackTrace) + { + String tbr = ifNotPresent; + + try + { + tbr = getProperty(parentElement, propertyName); + } + catch(Exception e) + { + if(printStackTrace) + e.printStackTrace(); + } + + + return tbr; + } + + public String getStringProperty(Element parentElement, String propertyName, String ifNotPresent) + { + return getStringProperty(parentElement, propertyName, ifNotPresent, true); + } + + public int getIntProperty(Element parentElement, String propertyName, int ifNotPresent) + { + int tbr = ifNotPresent; + + try + { + tbr = Integer.parseInt(getProperty(parentElement, propertyName)); + } + catch(Exception e) + { + e.printStackTrace(); + } + + + return tbr; + } + + public boolean getBooleanProperty(Element parentElement, String propertyName, boolean ifNotPresent) + { + boolean tbr = ifNotPresent; + + try + { + tbr = getProperty(parentElement, propertyName).equals("true"); + } + catch(Exception e) + { + e.printStackTrace(); + } + + return tbr; + } + + public boolean doesElementExist(Element parent, String nameOfElement) + { + return parent.getElementsByTagName(nameOfElement).getLength() > 0; + } + + public boolean doesElementExist(Document document, String nameOfElement) + { + return document.getElementsByTagName(nameOfElement).getLength() > 0; + } + +} --- 'a/src/main/java/module-info.java' +++ b/src/main/java/module-info.java @@ -5,6 +5,8 @@ module com.StreamPi.Util { requires transitive javafx.base; requires transitive javafx.controls; + requires transitive java.xml; + exports com.StreamPi.Util.Version; exports com.StreamPi.Util.Exception; exports com.StreamPi.Util.Platform; @@ -12,4 +14,5 @@ module com.StreamPi.Util { exports com.StreamPi.Util.StartAtBoot; exports com.StreamPi.Util.Alert; exports com.StreamPi.Util.ComboBox; + exports com.StreamPi.Util.XMLConfigHelper; } \ No newline at end of file