Klasse 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.
Autor:
Andreas Enders
  • Methodendetails

    • getInstance

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

      public String getRootIDAttribute(String aXMLString)
      Helper method to read the name Attribute from the root element of an XML-String.
      Parameter:
      aXMLString -
      Gibt zurück:
      RootIDAttribute
    • replace

      public static String replace(String aString, String aToBeReplacedString, String aReplaceWithString)
      Replaces a String in a String.
      Parameter:
      aString -
      aToBeReplacedString -
      aReplaceWithString -
      Gibt zurück:
      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.
      Parameter:
      aString -
      aReplacements -
      aAttribute -
      Gibt zurück:
      converted String
    • getRootNameAttribute

      public String getRootNameAttribute(String aXMLString)
      Helper method to read the name Attribute from the root element of an XML-String.
      Parameter:
      aXMLString -
      Gibt zurück:
      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.
      Parameter:
      aElement -
      Gibt zurück:
      the name Attribute from the root element of an XML-String
      Löst aus:
      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.
      Parameter:
      aDocument -
      Gibt zurück:
      the name Attribute from the root element of an XML-String
      Löst aus:
      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.
      Parameter:
      aXMLContent -
      Gibt zurück:
      the name Attribute from the root element of an XML-String
      Löst aus:
      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.
      Parameter:
      aElement -
      aDefault -
      Gibt zurück:
      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.
      Parameter:
      aElement -
      aAttributeName -
      Gibt zurück:
      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.
      Parameter:
      aDocument -
      aDefault -
      Gibt zurück:
      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.
      Parameter:
      aXMLString -
      aAttributeName -
      Gibt zurück:
      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.
      Parameter:
      aXMLString -
      aName -
      Gibt zurück:
      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.
      Parameter:
      aElement -
      aName -
      aValue -
      Gibt zurück:
      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.
      Parameter:
      aParentElement -
      aChildName -
      Gibt zurück:
      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.
      Parameter:
      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.
      Parameter:
      tFileIn -
      Gibt zurück:
      the Element
      Löst aus:
      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.
      Parameter:
      tFileIn -
      Gibt zurück:
      the Document
      Löst aus:
      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.
      Parameter:
      aString -
      Gibt zurück:
      the Document
      Löst aus:
      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.
      Parameter:
      aReader -
      Gibt zurück:
      the Element
      Löst aus:
      org.jdom2.JDOMException
      IOException
    • getElementFromFile

      public org.jdom2.Element getElementFromFile(File aFile)
      Parses the content of the file and returns the element.
      Parameter:
      aFile -
      Gibt zurück:
      the Element
    • getElementFromFile

      public org.jdom2.Element getElementFromFile(File aFile, String aEncoding)
      Parses the content of the file and returns the element.
      Parameter:
      aFile -
      aEncoding -
      Gibt zurück:
      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.
      Parameter:
      aElement -
      aAtrributeValue -
      Gibt zurück:
      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.
      Parameter:
      aElement -
      aAtrributeValue -
      Gibt zurück:
      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.
      Parameter:
      aElement -
      aName -
      aDefault -
      Gibt zurück:
      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.
      Typparameter:
      T -
      Parameter:
      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 .
      Gibt zurück:
      parameter value from map as a number
    • isXMLWellFormed

      public boolean isXMLWellFormed(String aXMLString)
      Checks the xml-structure of the given String.
      Parameter:
      aXMLString -
      Gibt zurück:
      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
      Parameter:
      aElement -
      Gibt zurück:
      JDOM-Element transformed to HashMap
    • createParameterElement

      public static org.jdom2.Element createParameterElement(String aParameter, Object aValue)
      create standard parameter Element
      Parameter:
      aParameter -
      aValue -
      Gibt zurück:
      standard parameter Element
    • createTextElement

      public static org.jdom2.Element createTextElement(String aId, String aTitle, String aRequired, String aEnabled, String aDefault)
      create text field Element
      Parameter:
      aId -
      aTitle -
      aRequired -
      aEnabled -
      aDefault -
      Gibt zurück:
      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
      Parameter:
      aId -
      aTitle -
      aRequired -
      aEnabled -
      aType -
      aLocalize -
      aMultiselect -
      aDefault -
      aOptions -
      Gibt zurück:
      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
      Parameter:
      aId -
      aTitle -
      aRequired -
      aEnabled -
      aMin -
      aMax -
      aType -
      aDefault -
      Gibt zurück:
      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
      Parameter:
      aId -
      aTitle -
      aRequired -
      aEnabled -
      aMin -
      aMax -
      aType -
      aDefault -
      Gibt zurück:
      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
      Parameter:
      aName -
      aTitle -
      aId -
      aRequired -
      aEnabled -
      aDefault -
      Gibt zurück:
      standard parameter Element
    • evaluateXPath

      public static List<org.jdom2.Element> evaluateXPath(org.jdom2.Document aDoc, String aExpression)
      create standard field Element
      Parameter:
      aDoc -
      aExpression -
      Gibt zurück:
      standard parameter Element
    • evaluateXPath

      public static List<org.jdom2.Element> evaluateXPath(org.jdom2.Element aElement, String aExpression)
      create standard field Element
      Parameter:
      aExpression -
      aDoc -
      Gibt zurück:
      standard parameter Element
    • evaluateXPathSingleResult

      public static String evaluateXPathSingleResult(org.jdom2.Document aDoc, String aExpression)
      create standard field Element
      Parameter:
      aDoc -
      aExpression -
      Gibt zurück:
      standard parameter Element
    • transformMapToElement

      public static org.jdom2.Element transformMapToElement(Map<String,Object> aParameterMap, String aNameOfRootElement)
      Parameter:
      aParameterMap -
      aNameOfRootElement -
      Gibt zurück:
      Element
    • checkAttributeExists

      public static boolean checkAttributeExists(org.jdom2.Element aVarElement, String aString)
      Parameter:
      aVarElement -
      aString -
      Gibt zurück:
      true if attribute exists
    • checkAttributeNotEmpty

      public static boolean checkAttributeNotEmpty(org.jdom2.Element aVarElement, String aString)
      Parameter:
      aVarElement -
      aString -
      Gibt zurück:
      true if attribute not empty
    • checkValueExists

      public static boolean checkValueExists(org.jdom2.Element aVarElement)
      Parameter:
      aVarElement -
      Gibt zurück:
      true if value exists
    • toIntArray

      public static Integer[] toIntArray(List<org.jdom2.Element> aChildren)
      Parameter:
      aChildren -
      Gibt zurück:
      Array of Integers
    • toDoubleArray

      public static Double[] toDoubleArray(List<org.jdom2.Element> aChildren)
      Parameter:
      aChildren -
      Gibt zurück:
      Array of Doubles
    • toDoubleMatrix

      public static Double[][] toDoubleMatrix(List<org.jdom2.Element> aChildren)
      Parameter:
      aChildren -
      Gibt zurück:
    • generateProjectXML

      public static org.jdom2.Document generateProjectXML(String aProjectDataFile, List<FWSimVariable<?>> aVars, org.apache.commons.collections15.map.LinkedMap<FWSimVariable<?>,String> aSensitivityVarsMap, org.jdom2.Element aOutputsElement) throws org.jdom2.JDOMException, IOException
      Parameter:
      aOutputsElement -
      tSensitivityVarsMap -
      aChildren -
      Gibt zurück:
      Array of Strings
      Löst aus:
      IOException
      org.jdom2.JDOMException
    • toStringArray

      public static String[] toStringArray(List<org.jdom2.Element> aChildren)
      Parameter:
      aChildren -
      Gibt zurück:
      Array of Strings