Class XMLHelper

java.lang.Object
net.simplace.core.service.XMLHelper

public final class XMLHelper extends Object
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 Details

    • getInstance

      public static XMLHelper getInstance()
      Creates a new instance of. Easier to use as constructor.
      Returns:
      the Instance
    • getRootIDAttribute

      public String getRootIDAttribute(String aXMLString)
      Helper method to read the name Attribute from the root element of an XML-String.
      Parameters:
      aXMLString -
      Returns:
      RootIDAttribute
    • replace

      public static String replace(String aString, String aToBeReplacedString, String aReplaceWithString)
      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

      public String getRootNameAttribute(String aXMLString)
      Helper method to read the name Attribute from the root element of an XML-String.
      Parameters:
      aXMLString -
      Returns:
      RootNameAttribute
    • outputToString

      public String outputToString(org.jdom2.Element aElement) throws IOException
      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

      public String outputToString(org.jdom2.Document aDocument) throws IOException
      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

      public String outputToStringWithDefault(org.jdom2.Element aElement, String aDefault)
      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

      public String getCommaSeparatedList(org.jdom2.Element aElement, String aAttributeName)
      Helper method to read the AttributeName from the child Elements of given Element. CommaSeaparated String is returned.
      Parameters:
      aElement -
      aAttributeName -
      Returns:
      CommaSeaparated String
    • outputToStringWithDefault

      public String outputToStringWithDefault(org.jdom2.Document aDocument, String aDefault)
      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

      public String getRootAttribute(String aXMLString, String aAttributeName)
      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

      public String removeChildren(String aXMLString, String aName)
      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

      public org.jdom2.Element getElementFromFile(File aFile)
      Parses the content of the file and returns the element.
      Parameters:
      aFile -
      Returns:
      the Element
    • getElementFromFile

      public org.jdom2.Element getElementFromFile(File aFile, String aEncoding)
      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 of Number)
      aDefault - default value, which is returned if parameter not set or value can not be converted to aNumberType .
      Returns:
      parameter value from map as a number
    • isXMLWellFormed

      public boolean isXMLWellFormed(String aXMLString)
      Checks the xml-structure of the given String.
      Parameters:
      aXMLString -
      Returns:
      success
    • transformElementToMap

      public static Map<String,Object> transformElementToMap(org.jdom2.Element aElement)
      transforms a JDOM-Element to HashMap. Structure needed: modis.map.subXalt modis.map.ndsiresult
      Parameters:
      aElement -
      Returns:
      JDOM-Element transformed to HashMap
    • createParameterElement

      public static org.jdom2.Element createParameterElement(String aParameter, Object aValue)
      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

      public static List<org.jdom2.Element> evaluateXPath(org.jdom2.Document aDoc, String aExpression)
      create standard field Element
      Parameters:
      aDoc -
      aExpression -
      Returns:
      standard parameter Element
    • evaluateXPath

      public static List<org.jdom2.Element> evaluateXPath(org.jdom2.Element aElement, String aExpression)
      create standard field Element
      Parameters:
      aDoc -
      aExpression -
      Returns:
      standard parameter Element
    • evaluateXPathSingleResult

      public static String evaluateXPathSingleResult(org.jdom2.Document aDoc, String aExpression)
      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

      public static boolean checkAttributeExists(org.jdom2.Element aVarElement, String aString)
      Parameters:
      aVarElement -
      aString -
      Returns:
      true if attribute exists
    • checkAttributeNotEmpty

      public static boolean checkAttributeNotEmpty(org.jdom2.Element aVarElement, String aString)
      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

      public static Integer[] toIntArray(List<org.jdom2.Element> aChildren)
      Parameters:
      aChildren -
      Returns:
      Array of Integers
    • toDoubleArray

      public static Double[] toDoubleArray(List<org.jdom2.Element> aChildren)
      Parameters:
      aChildren -
      Returns:
      Array of Doubles
    • toDoubleMatrix

      public static Double[][] toDoubleMatrix(List<org.jdom2.Element> aChildren)
      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

      public static String[] toStringArray(List<org.jdom2.Element> aChildren)
      Parameters:
      aChildren -
      Returns:
      Array of Strings