Package net.simplace.core.service
Class XMLHelper
java.lang.Object
net.simplace.core.service.XMLHelper
Provides some methods for performing xml operations. It's not implemented as static methods to share some resources.
It's not implemented as singleton to avoid synchronization problems.
- Author:
- Andreas Enders
-
Method Summary
Modifier and TypeMethodDescriptionstatic final org.jdom2.Element
addElement
(org.jdom2.Element aElement, String aName, String aValue) Adds a new element to the given element with the same namespace.static void
addTextToChildThatMayExist
(org.jdom2.Element aParentElement, String aChildName, String aValue) Adds content to the child of aParent with the name aChildName if the child doesn't exist a new child is created an attached.static boolean
checkAttributeExists
(org.jdom2.Element aVarElement, String aString) static boolean
checkAttributeNotEmpty
(org.jdom2.Element aVarElement, String aString) static boolean
checkValueExists
(org.jdom2.Element aVarElement) static org.jdom2.Element
createChoiceElement
(String aId, String aTitle, String aRequired, String aEnabled, String aType, String aLocalize, String aMultiselect, String aDefault, Map<String, String> aOptions) create integer field Elementstatic org.jdom2.Element
createDoubleElement
(String aId, String aTitle, String aRequired, String aEnabled, String aMin, String aMax, String aType, String aDefault) create integer field Elementstatic org.jdom2.Element
createFieldElement
(String aName, String aTitle, String aId, String aRequired, String aEnabled, String aDefault) create standard field Elementstatic org.jdom2.Element
createIntegerElement
(String aId, String aTitle, String aRequired, String aEnabled, String aMin, String aMax, String aType, String aDefault) create integer field Elementstatic org.jdom2.Element
createParameterElement
(String aParameter, Object aValue) create standard parameter Elementstatic org.jdom2.Element
create text field Elementstatic List<org.jdom2.Element>
evaluateXPath
(org.jdom2.Document aDoc, String aExpression) create standard field Elementstatic List<org.jdom2.Element>
evaluateXPath
(org.jdom2.Element aElement, String aExpression) create standard field Elementstatic String
evaluateXPathSingleResult
(org.jdom2.Document aDoc, String aExpression) create standard field Elementstatic org.jdom2.Document
generateProjectXML
(String aProjectDataFile, List<FWSimVariable<?>> aVars) static final String
getAttributeValueOrEmptyString
(org.jdom2.Element aElement, String aAtrributeValue) Returns the value of the attribute specified be aAttributeName or returns "" if the it doesn't exists.static final String
getAttributeValueOrNull
(org.jdom2.Element aElement, String aAtrributeValue) Returns the value of the attribute specified by aAttributeName or returns null if the it doesn't exists or equals "" empty string.static final boolean
getBooleanAttributeValue
(org.jdom2.Element aElement, String aName, boolean aDefault) Returns the value of the attribute specified be aAttributeName or returns the given default.getCommaSeparatedList
(org.jdom2.Element aElement, String aAttributeName) Helper method to read the AttributeName from the child Elements of given Element.org.jdom2.Document
getDocumentFromStream
(InputStream tFileIn) Parses the content of the Stream and returns the document.org.jdom2.Document
getDocumentFromString
(String aString) Parses the given XML string and returns the document.org.jdom2.Element
getElementFromFile
(File aFile) Parses the content of the file and returns the element.org.jdom2.Element
getElementFromFile
(File aFile, String aEncoding) Parses the content of the file and returns the element.org.jdom2.Element
getElementFromReader
(Reader aReader) Parses the content of the Reader and returns the element.org.jdom2.Element
getElementFromStream
(InputStream tFileIn) Parses the content of the Stream and returns the element.org.jdom2.Element
getElementFromString
(String aXMLContent) Helper method to read the name Attribute from the root element of an XML-String.static XMLHelper
Creates a new instance of.static <T extends Number>
TgetNumberAttributeValue
(org.jdom2.Element aElement, String aAttr, Class<T> aNumberType, T aDefault) Getting parameter value from map as a number.static org.jdom2.Element
getOrCreateChild
(org.jdom2.Element aParentElement, String aChildName) Returns the Child-Element of aParentElement with the given name, if it doesn't exist the child element is created and attached to aParentElement.getRootAttribute
(String aXMLString, String aAttributeName) Helper method to read the name Attribute from the root element of an XML-String.getRootIDAttribute
(String aXMLString) Helper method to read the name Attribute from the root element of an XML-String.getRootNameAttribute
(String aXMLString) Helper method to read the name Attribute from the root element of an XML-String.boolean
isXMLWellFormed
(String aXMLString) Checks the xml-structure of the given String.outputToString
(org.jdom2.Document aDocument) Helper method to read the name Attribute from the root element of an XML-String.outputToString
(org.jdom2.Element aElement) Helper method to read the name Attribute from the root element of an XML-String.outputToStringWithDefault
(org.jdom2.Document aDocument, String aDefault) Helper method to read the name Attribute from the root element of an XML-String.outputToStringWithDefault
(org.jdom2.Element aElement, String aDefault) Helper method to read the name Attribute from the root element of an XML-String.removeChildren
(String aXMLString, String aName) Helper method to remove all toplevel children of the XML String with a certain name.static String
Replaces a String in a String.static String
Replaces in the string all key in the given Map by the values.static Double[]
toDoubleArray
(List<org.jdom2.Element> aChildren) static Double[][]
toDoubleMatrix
(List<org.jdom2.Element> aChildren) static Integer[]
toIntArray
(List<org.jdom2.Element> aChildren) static String[]
toStringArray
(List<org.jdom2.Element> aChildren) transformElementToMap
(org.jdom2.Element aElement) transforms a JDOM-Element to HashMap.static org.jdom2.Element
transformMapToElement
(Map<String, Object> aParameterMap, String aNameOfRootElement)
-
Method Details
-
getInstance
Creates a new instance of. Easier to use as constructor.- Returns:
- the Instance
-
getRootIDAttribute
Helper method to read the name Attribute from the root element of an XML-String.- Parameters:
aXMLString
-- Returns:
- RootIDAttribute
-
replace
Replaces a String in a String.- Parameters:
aString
-aToBeReplacedString
-aReplaceWithString
-- Returns:
- converted String
-
replaceAttribute
public static String replaceAttribute(String aString, Map<String, Object> aReplacements, String aAttribute) Replaces in the string all key in the given Map by the values.- Parameters:
aString
-aReplacements
-aAttribute
-- Returns:
- converted String
-
getRootNameAttribute
Helper method to read the name Attribute from the root element of an XML-String.- Parameters:
aXMLString
-- Returns:
- RootNameAttribute
-
outputToString
Helper method to read the name Attribute from the root element of an XML-String.- Parameters:
aElement
-- Returns:
- the name Attribute from the root element of an XML-String
- Throws:
IOException
-
outputToString
Helper method to read the name Attribute from the root element of an XML-String.- Parameters:
aDocument
-- Returns:
- the name Attribute from the root element of an XML-String
- Throws:
IOException
-
getElementFromString
public org.jdom2.Element getElementFromString(String aXMLContent) throws org.jdom2.JDOMException, IOException Helper method to read the name Attribute from the root element of an XML-String.- Parameters:
aXMLContent
-- Returns:
- the name Attribute from the root element of an XML-String
- Throws:
org.jdom2.JDOMException
IOException
-
outputToStringWithDefault
Helper method to read the name Attribute from the root element of an XML-String. If an error occurs or if the element is null a default string is returned.- Parameters:
aElement
-aDefault
-- Returns:
- the name Attribute from the root element of an XML-String or a default string
-
getCommaSeparatedList
Helper method to read the AttributeName from the child Elements of given Element. CommaSeaparated String is returned.- Parameters:
aElement
-aAttributeName
-- Returns:
- CommaSeaparated String
-
outputToStringWithDefault
Helper method to read the name Attribute from the root element of an XML-String. If an error occurs or if the element is null a default string is returned.- Parameters:
aDocument
-aDefault
-- Returns:
- the name Attribute from the root element of an XML-String or a default string
-
getRootAttribute
Helper method to read the name Attribute from the root element of an XML-String.- Parameters:
aXMLString
-aAttributeName
-- Returns:
- the name Attribute from the root element of an XML-String
-
removeChildren
Helper method to remove all toplevel children of the XML String with a certain name.- Parameters:
aXMLString
-aName
-- Returns:
- converted String
-
addElement
public static final org.jdom2.Element addElement(org.jdom2.Element aElement, String aName, String aValue) Adds a new element to the given element with the same namespace.- Parameters:
aElement
-aName
-aValue
-- Returns:
- a new element
-
getOrCreateChild
public static org.jdom2.Element getOrCreateChild(org.jdom2.Element aParentElement, String aChildName) Returns the Child-Element of aParentElement with the given name, if it doesn't exist the child element is created and attached to aParentElement.- Parameters:
aParentElement
-aChildName
-- Returns:
- the Child-Element of aParentElement with the given name
-
addTextToChildThatMayExist
public static void addTextToChildThatMayExist(org.jdom2.Element aParentElement, String aChildName, String aValue) Adds content to the child of aParent with the name aChildName if the child doesn't exist a new child is created an attached.- Parameters:
aParentElement
-aChildName
-aValue
-
-
getElementFromStream
public org.jdom2.Element getElementFromStream(InputStream tFileIn) throws org.jdom2.JDOMException, IOException Parses the content of the Stream and returns the element.- Parameters:
tFileIn
-- Returns:
- the Element
- Throws:
org.jdom2.JDOMException
IOException
-
getDocumentFromStream
public org.jdom2.Document getDocumentFromStream(InputStream tFileIn) throws org.jdom2.JDOMException, IOException Parses the content of the Stream and returns the document.- Parameters:
tFileIn
-- Returns:
- the Document
- Throws:
org.jdom2.JDOMException
IOException
-
getDocumentFromString
public org.jdom2.Document getDocumentFromString(String aString) throws org.jdom2.JDOMException, IOException Parses the given XML string and returns the document.- Parameters:
aString
-- Returns:
- the Document
- Throws:
org.jdom2.JDOMException
IOException
-
getElementFromReader
public org.jdom2.Element getElementFromReader(Reader aReader) throws org.jdom2.JDOMException, IOException Parses the content of the Reader and returns the element.- Parameters:
aReader
-- Returns:
- the Element
- Throws:
org.jdom2.JDOMException
IOException
-
getElementFromFile
Parses the content of the file and returns the element.- Parameters:
aFile
-- Returns:
- the Element
-
getElementFromFile
Parses the content of the file and returns the element.- Parameters:
aFile
-aEncoding
-- Returns:
- the Element
-
getAttributeValueOrEmptyString
public static final String getAttributeValueOrEmptyString(org.jdom2.Element aElement, String aAtrributeValue) Returns the value of the attribute specified be aAttributeName or returns "" if the it doesn't exists.- Parameters:
aElement
-aAtrributeValue
-- Returns:
- the value of the attribute specified be aAttributeName or returns "" if the it doesn't exist
-
getAttributeValueOrNull
public static final String getAttributeValueOrNull(org.jdom2.Element aElement, String aAtrributeValue) Returns the value of the attribute specified by aAttributeName or returns null if the it doesn't exists or equals "" empty string.- Parameters:
aElement
-aAtrributeValue
-- Returns:
- the value of the attribute specified by aAttributeName or returns null if the it doesn't exist
-
getBooleanAttributeValue
public static final boolean getBooleanAttributeValue(org.jdom2.Element aElement, String aName, boolean aDefault) Returns the value of the attribute specified be aAttributeName or returns the given default.- Parameters:
aElement
-aName
-aDefault
-- Returns:
- the value of the attribute specified be aAttributeName or the given default
-
getNumberAttributeValue
public static <T extends Number> T getNumberAttributeValue(org.jdom2.Element aElement, String aAttr, Class<T> aNumberType, T aDefault) Getting parameter value from map as a number.- Type Parameters:
T
-- Parameters:
aElement
-aAttr
-aNumberType
- number type, the determined value is converted to (subclass ofNumber
)aDefault
- default value, which is returned if parameter not set or value can not be converted toaNumberType
.- Returns:
- parameter value from map as a number
-
isXMLWellFormed
Checks the xml-structure of the given String.- Parameters:
aXMLString
-- Returns:
- success
-
transformElementToMap
transforms a JDOM-Element to HashMap. Structure needed:modis.map.subXalt modis.map.ndsiresult - Parameters:
aElement
-- Returns:
- JDOM-Element transformed to HashMap
-
createParameterElement
create standard parameter Element- Parameters:
aParameter
-aValue
-- Returns:
- standard parameter Element
-
createTextElement
public static org.jdom2.Element createTextElement(String aId, String aTitle, String aRequired, String aEnabled, String aDefault) create text field Element- Parameters:
aId
-aTitle
-aRequired
-aEnabled
-aDefault
-- Returns:
- standard parameter Element
-
createChoiceElement
public static org.jdom2.Element createChoiceElement(String aId, String aTitle, String aRequired, String aEnabled, String aType, String aLocalize, String aMultiselect, String aDefault, Map<String, String> aOptions) create integer field Element- Parameters:
aId
-aTitle
-aRequired
-aEnabled
-aType
-aLocalize
-aMultiselect
-aDefault
-aOptions
-- Returns:
- standard parameter Element
-
createDoubleElement
public static org.jdom2.Element createDoubleElement(String aId, String aTitle, String aRequired, String aEnabled, String aMin, String aMax, String aType, String aDefault) create integer field Element- Parameters:
aId
-aTitle
-aRequired
-aEnabled
-aMin
-aMax
-aType
-aDefault
-- Returns:
- standard parameter Element
-
createIntegerElement
public static org.jdom2.Element createIntegerElement(String aId, String aTitle, String aRequired, String aEnabled, String aMin, String aMax, String aType, String aDefault) create integer field Element- Parameters:
aId
-aTitle
-aRequired
-aEnabled
-aMin
-aMax
-aType
-aDefault
-- Returns:
- standard parameter Element
-
createFieldElement
public static org.jdom2.Element createFieldElement(String aName, String aTitle, String aId, String aRequired, String aEnabled, String aDefault) create standard field Element- Parameters:
aName
-aTitle
-aId
-aRequired
-aEnabled
-aDefault
-- Returns:
- standard parameter Element
-
evaluateXPath
create standard field Element- Parameters:
aDoc
-aExpression
-- Returns:
- standard parameter Element
-
evaluateXPath
create standard field Element- Parameters:
aDoc
-aExpression
-- Returns:
- standard parameter Element
-
evaluateXPathSingleResult
create standard field Element- Parameters:
aDoc
-aExpression
-- Returns:
- standard parameter Element
-
transformMapToElement
public static org.jdom2.Element transformMapToElement(Map<String, Object> aParameterMap, String aNameOfRootElement) - Parameters:
aParameterMap
-aNameOfRootElement
-- Returns:
- Element
-
checkAttributeExists
- Parameters:
aVarElement
-aString
-- Returns:
- true if attribute exists
-
checkAttributeNotEmpty
- Parameters:
aVarElement
-aString
-- Returns:
- true if attribute not empty
-
checkValueExists
public static boolean checkValueExists(org.jdom2.Element aVarElement) - Parameters:
aVarElement
-- Returns:
- true if value exists
-
toIntArray
- Parameters:
aChildren
-- Returns:
- Array of Integers
-
toDoubleArray
- Parameters:
aChildren
-- Returns:
- Array of Doubles
-
toDoubleMatrix
- Parameters:
aChildren
-- Returns:
-
generateProjectXML
public static org.jdom2.Document generateProjectXML(String aProjectDataFile, List<FWSimVariable<?>> aVars) throws org.jdom2.JDOMException, IOException - Parameters:
aChildren
-- Returns:
- Array of Strings
- Throws:
IOException
org.jdom2.JDOMException
-
toStringArray
- Parameters:
aChildren
-- Returns:
- Array of Strings
-