Class XMLUtil

java.lang.Object
com.strategyquant.lib.XMLUtil

public class XMLUtil extends Object
The Class XMLUtil.
  • Constructor Details

    • XMLUtil

      public XMLUtil()
  • Method Details

    • getChildElem

      public static org.jdom2.Element getChildElem(org.jdom2.Element parentElem, String childName) throws Exception
      Gets the child elem.
      Parameters:
      parentElem - the parent elem
      childName - the child name
      Returns:
      the child elem
      Throws:
      Exception - the exception
    • getChildElem

      public static org.jdom2.Element getChildElem(org.jdom2.Element parentElem, String childName, String fallback) throws Exception
      Throws:
      Exception
    • hasChildElem

      public static boolean hasChildElem(org.jdom2.Element parentElem, String childName)
    • elementIsNot

      public static boolean elementIsNot(org.jdom2.Element elem, String attribute)
      Element is not.
      Parameters:
      elem - the elem
      attribute - the attribute
      Returns:
      true, if successful
    • elementIs

      public static boolean elementIs(org.jdom2.Element elem, String attribute)
      Element is.
      Parameters:
      elem - the elem
      attribute - the attribute
      Returns:
      true, if successful
    • valueToElement

      public static org.jdom2.Element valueToElement(String key, Object value)
      Value to element.
      Parameters:
      key - the key
      value - the value
      Returns:
      the element
    • elementToValue

      public static Object elementToValue(org.jdom2.Element child)
      Element to value.
      Parameters:
      child - the child
      Returns:
      the object
    • valueHash

      public static int valueHash(int keyHash, Object value)
      Value hash.
      Parameters:
      keyHash - the key hash
      value - the value
      Returns:
      the int
    • stringToElement

      public static org.jdom2.Element stringToElement(String xmlConfig) throws org.jdom2.JDOMException, IOException
      String to element.
      Parameters:
      xmlConfig - the xml config
      Returns:
      the element
      Throws:
      org.jdom2.JDOMException - the JDOM exception
      IOException - Signals that an I/O exception has occurred.
    • elementToString

      public static String elementToString(org.jdom2.Element element)
      Element to string.
      Parameters:
      element - the element
      Returns:
      the string
    • elementLineToString

      public static String elementLineToString(org.jdom2.Element element)
      Element to string.
      Parameters:
      element - the element
      Returns:
      the string
    • elementToString

      public static String elementToString(org.jdom2.Element element, boolean beautify)
      Element to string.
      Parameters:
      element - the element
      beautify - the beautify
      Returns:
      the string
    • getNestedElements

      public static ArrayList<org.jdom2.Element> getNestedElements(org.jdom2.Element rootElem, String elementName)
      Gets the nested elements.
      Parameters:
      rootElem - the root elem
      elementName - the element name
      Returns:
      the nested elements
    • getNestedElements

      public static ArrayList<org.jdom2.Element> getNestedElements(org.jdom2.Element rootElem, String elementName, String attrName, String attrValue)
      Gets the nested elements with attribute value
      Parameters:
      rootElem - the root elem
      elementName - the element name
      attrName - the attribute name
      attrValue - the attribute value
      Returns:
      the nested elements
    • findFirst

      public static org.jdom2.Element findFirst(org.jdom2.Element config, String elementName)
      Find first.
      Parameters:
      config - the config
      elementName - the element name
      Returns:
      the element
    • findAll

      public static void findAll(org.jdom2.Element config, String elementName, ArrayList<org.jdom2.Element> list)
      Find first.
      Parameters:
      config - the config
      elementName - the element name
      list -
    • findAllWithKey

      public static void findAllWithKey(org.jdom2.Element config, String elementName, String key, ArrayList<org.jdom2.Element> list)
      Parameters:
      config -
      elementName -
      key -
      list -
    • findFirstWithKey

      public static org.jdom2.Element findFirstWithKey(org.jdom2.Element config, String elementName, String key)
      Find first with key.
      Parameters:
      config - the config
      elementName - the element name
      key - the key
      Returns:
      the element
    • getAttr

      public static String getAttr(org.jdom2.Element element, String attributeName) throws Exception
      Gets the attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the attr
      Throws:
      Exception - the exception
    • getAttr

      public static String getAttr(org.jdom2.Element element, String attributeName, String defaultValue)
      Gets the attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the attr
    • getStringAttr

      public static String getStringAttr(org.jdom2.Element element, String attributeName, String defaultValue)
      Gets the string attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the string attr
    • getByteAttr

      public static byte getByteAttr(org.jdom2.Element element, String attributeName, byte defaultValue)
      Gets the byte attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the byte attr
    • getIntAttr

      public static int getIntAttr(org.jdom2.Element element, String attributeName, int defaultValue)
      Gets the int attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the int attr
    • getIntAttr

      public static int getIntAttr(org.jdom2.Element element, String attributeName) throws Exception
      Gets the int attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the int attr
      Throws:
      Exception
    • getLongAttr

      public static long getLongAttr(org.jdom2.Element element, String attributeName) throws Exception
      Gets the long attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the int attr
      Throws:
      Exception
    • getBooleanAttr

      public static boolean getBooleanAttr(org.jdom2.Element element, String attributeName) throws Exception
      Gets the boolean attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the int attr
      Throws:
      Exception
    • getIntAttr

      public static byte getIntAttr(org.jdom2.Element element, String attributeName, byte defaultValue)
      Gets the byte attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the byte attr
    • getDoubleAttr

      public static double getDoubleAttr(org.jdom2.Element element, String attributeName, double defaultValue)
      Gets the double attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the double attr
    • getBooleanAttr

      public static boolean getBooleanAttr(org.jdom2.Element element, String attributeName, boolean defaultValue)
      Gets the boolean attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the boolean attr
    • tryGetDoubleAttr

      public static double tryGetDoubleAttr(org.jdom2.Element element, String attributeName)
      Try get double attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the double
    • tryGetAttr

      public static String tryGetAttr(org.jdom2.Element element, String attributeName, String defaultValue)
      Try get attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      defaultValue - the default value
      Returns:
      the string
    • tryGetLongAttr

      public static long tryGetLongAttr(org.jdom2.Element element, String attributeName)
      Try get long attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the long
    • tryGetIntAttr

      public static int tryGetIntAttr(org.jdom2.Element element, String attributeName)
      Try get int attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the int
    • tryGetBoolAttr

      public static boolean tryGetBoolAttr(org.jdom2.Element element, String attributeName)
      Try get bool attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      true, if successful
    • tryGetByteAttr

      public static byte tryGetByteAttr(org.jdom2.Element element, String attributeName)
      Try get byte attr.
      Parameters:
      element - the element
      attributeName - the attribute name
      Returns:
      the byte
    • trySetAttr

      public static void trySetAttr(org.jdom2.Element element, String name, Object value)
      Try set attr.
      Parameters:
      element - the element
      name - the name
      value - the value
    • stringToXml

      public static org.jdom2.Document stringToXml(String text) throws org.jdom2.JDOMException, IOException
      String to xml.
      Parameters:
      text - the text
      Returns:
      the document
      Throws:
      org.jdom2.JDOMException - the JDOM exception
      IOException - Signals that an I/O exception has occurred.
    • stringToXmlElement

      public static org.jdom2.Element stringToXmlElement(String xmlString) throws org.jdom2.JDOMException, IOException
      String to xml element.
      Parameters:
      xmlString - the xml string
      Returns:
      the element
      Throws:
      org.jdom2.JDOMException - the JDOM exception
      IOException - Signals that an I/O exception has occurred.
    • fileToXml

      public static org.jdom2.Document fileToXml(File file) throws org.jdom2.JDOMException, IOException, Exception
      File to xml.
      Parameters:
      file - the file
      Returns:
      the document
      Throws:
      org.jdom2.JDOMException - the JDOM exception
      IOException - Signals that an I/O exception has occurred.
      Exception - the exception
    • fileToXmlElement

      public static org.jdom2.Element fileToXmlElement(File file) throws org.jdom2.JDOMException, IOException, Exception
      File to xml element.
      Parameters:
      file - the file
      Returns:
      the element
      Throws:
      org.jdom2.JDOMException - the JDOM exception
      IOException - Signals that an I/O exception has occurred.
      Exception - the exception
    • xmlToFile

      public static void xmlToFile(org.jdom2.Element xmlElem, File file) throws Exception
      Xml to file.
      Parameters:
      xmlElem - the xml elem
      file - the file
      Throws:
      Exception - the exception
    • xmlToFile

      public static void xmlToFile(org.jdom2.Document doc, File file) throws Exception
      Xml to file.
      Parameters:
      doc - the doc
      file - the file
      Throws:
      Exception - the exception
    • xmlToString

      public static String xmlToString(org.jdom2.Element el)
      Xml to string.
      Parameters:
      el - the el
      Returns:
      the string
    • xmlToStringRaw

      public static String xmlToStringRaw(org.jdom2.Element element)
      Xml to string raw.
      Parameters:
      element - the element
      Returns:
      the string
    • getInt

      public static int getInt(org.jdom2.Element elValues, String name, int defaultValue)
      Gets the int.
      Parameters:
      elValues - the el values
      name - the name
      defaultValue - the default value
      Returns:
      the int
    • getLong

      public static long getLong(org.jdom2.Element elValues, String name, int defaultValue)
      Gets the long.
      Parameters:
      elValues - the el values
      name - the name
      defaultValue - the default value
      Returns:
      the long
    • getFloat

      public static float getFloat(org.jdom2.Element elValues, String name, float defaultValue)
      Gets the float.
      Parameters:
      elValues - the el values
      name - the name
      defaultValue - the default value
      Returns:
      the float
    • getDouble

      public static double getDouble(org.jdom2.Element elValues, String name, double defaultValue)
      Gets the double.
      Parameters:
      elValues - the el values
      name - the name
      defaultValue - the default value
      Returns:
      the double
    • getBoolean

      public static boolean getBoolean(org.jdom2.Element elValues, String name, boolean defaultValue)
      Gets the boolean.
      Parameters:
      elValues - the el values
      name - the name
      defaultValue - the default value
      Returns:
      the boolean
    • tryAddElement

      public static org.jdom2.Element tryAddElement(org.jdom2.Element elParent, String elementName)
      Try add element.
      Parameters:
      elParent - the el parent
      elementName - the element name
      Returns:
      the element
    • tryAddNode

      public static org.jdom2.Element tryAddNode(org.jdom2.Element elParent, String nodeName, String value)
      Try add node.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      value - the value
      Returns:
      the element
    • tryAddBooleanNode

      public static org.jdom2.Element tryAddBooleanNode(org.jdom2.Element elParent, String nodeName, boolean value)
      Try add boolean node.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      value - the value
      Returns:
      the element
    • tryAddIntNode

      public static org.jdom2.Element tryAddIntNode(org.jdom2.Element elParent, String nodeName, int value)
      Try add int node.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      value - the value
      Returns:
      the element
    • tryAddDoubleNode

      public static org.jdom2.Element tryAddDoubleNode(org.jdom2.Element elParent, String nodeName, double value)
      Try add double node.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      value - the value
      Returns:
      the element
    • getNodeValue

      public static String getNodeValue(org.jdom2.Element elParent, String nodeName) throws Exception
      Gets the node value.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      Returns:
      the node value
      Throws:
      Exception - the exception
    • getNodeValue

      public static String getNodeValue(org.jdom2.Element elParent, String nodeName, String defaultValue)
      Gets the node value.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      defaultValue - the default value
      Returns:
      the node value
    • getNodeIntValue

      public static int getNodeIntValue(org.jdom2.Element elParent, String nodeName) throws Exception
      Gets the node int value.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      Returns:
      the node int value
      Throws:
      Exception - the exception
    • getNodeIntValue

      public static int getNodeIntValue(org.jdom2.Element elParent, String nodeName, int defaultValue) throws Exception
      Gets the node int value.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      defaultValue - the default value
      Returns:
      the node int value
      Throws:
      Exception - the exception
    • getNodeBooleanValue

      public static boolean getNodeBooleanValue(org.jdom2.Element elParent, String nodeName, boolean defaultValue) throws Exception
      Gets the node boolean value.
      Parameters:
      elParent - the el parent
      nodeName - the node name
      defaultValue - the default value
      Returns:
      the node boolean value
      Throws:
      Exception - the exception
    • getItemParameterNoException

      public static org.jdom2.Element getItemParameterNoException(org.jdom2.Element elItem, String key)
      Gets the item parameter no exception.
      Parameters:
      elItem - the el item
      key - the key
      Returns:
      the item parameter no exception
    • getItemParameter

      public static org.jdom2.Element getItemParameter(org.jdom2.Element elItem, String key, boolean throwExceptionIfNotFound) throws Exception
      Gets the item parameter.
      Parameters:
      elItem - the el item
      key - the key
      throwExceptionIfNotFound - the throw exception if not found
      Returns:
      the item parameter
      Throws:
      Exception - the exception
    • getOptionParameter

      public static org.jdom2.Element getOptionParameter(org.jdom2.Element elOptions, String key)
      Gets the option parameter.
      Parameters:
      elOptions - the el options
      key - the key
      Returns:
      the option parameter
    • printXml

      public static void printXml(org.jdom2.Element element)
      Prints the xml.
      Parameters:
      element - the element
    • copyElement

      public static org.jdom2.Element copyElement(org.jdom2.Element elSource)
      Copy element.
      Parameters:
      elSource - the el source
      Returns:
      the element
    • copyAttributes

      public static void copyAttributes(org.jdom2.Element elSource, org.jdom2.Element elDesc, boolean replace)
      Copy attributes.
      Parameters:
      elSource - the el source
      elDesc - the el desc
      replace - the replace
    • copySettings

      public static void copySettings(org.jdom2.Element target, org.jdom2.Element template, String elementToReplace) throws Exception
      Copy settings.
      Parameters:
      target - the target
      template - the template
      elementToReplace - the element to replace
      Throws:
      Exception - the exception
    • elementsEqual

      public static boolean elementsEqual(org.jdom2.Element element1, org.jdom2.Element element2)
      Elements equal.
      Parameters:
      element1 - the element 1
      element2 - the element 2
      Returns:
      true, if successful
    • attributesEqual

      public static boolean attributesEqual(org.jdom2.Element element1, org.jdom2.Element element2)
      Attributes equal.
      Parameters:
      element1 - the element 1
      element2 - the element 2
      Returns:
      true, if successful
    • JSValuesEqual

      public static boolean JSValuesEqual(String value1, String value2)
      JS values equal.
      Parameters:
      value1 - the value 1
      value2 - the value 2
      Returns:
      true, if successful
    • readXmlFile

      public static org.jdom2.Element readXmlFile(InputStream is) throws Exception
      Read xml file.
      Parameters:
      is - the is
      Returns:
      the element
      Throws:
      Exception - the exception
    • countElements

      public static int countElements(String elementName, org.jdom2.Element el, int count)
      Count elements.
      Parameters:
      elementName - the element name
      el - the el
      count - the count
      Returns:
      the int
    • findFirstParent

      public static org.jdom2.Element findFirstParent(org.jdom2.Element el, String elementName)
      Find first parent.
      Parameters:
      el - the el
      elementName - the element name
      Returns:
      the element
    • removeAttribute

      public static void removeAttribute(org.jdom2.Element el, String attributeName)
      Removes the attribute.
      Parameters:
      el - the el
      attributeName - the attribute name
    • getItemIntParam

      public static int getItemIntParam(org.jdom2.Element elItem, String key, int defaultValue)
    • setItemIntParam

      public static void setItemIntParam(org.jdom2.Element elItem, String key, int value)
    • getItemDoubleParam

      public static double getItemDoubleParam(org.jdom2.Element elItem, String key, double defaultValue)
    • setItemDoubleParam

      public static void setItemDoubleParam(org.jdom2.Element elItem, String key, double value)
    • getItemBoolParam

      public static boolean getItemBoolParam(org.jdom2.Element elItem, String key, boolean defaultValue)
    • setItemBoolParam

      public static void setItemBoolParam(org.jdom2.Element elItem, String key, boolean value)
    • getSubchildElementEx

      public static org.jdom2.Element getSubchildElementEx(org.jdom2.Element el, String xmlPath, String keyValue) throws Exception
      method looks for the exact path in the XML. It can optionally look for the leaf with given key. Example of use: Element elParamRB = XMLUtil.getSubchildElement(elTaskConfig, "Settings/Options/BuildTradingOptions/Params/Param", "ReservedBars", false); If the path is not found an exception is thrown
      Parameters:
      el -
      xmlPath -
      keyValue -
      Returns:
      Throws:
      Exception
    • getSubchildElement

      public static org.jdom2.Element getSubchildElement(org.jdom2.Element el, String xmlPath, String keyValue) throws Exception
      method looks for the exact path in the XML. It can optionally look for the leaf with given key. Example of use: Element elParamRB = XMLUtil.getSubchildElement(elTaskConfig, "Settings/Options/BuildTradingOptions/Params/Param", "ReservedBars", false); Same as getSubchildElementEx(), but this one doesn't throw exception but returns null if path is incorrect.
      Parameters:
      el -
      xmlPath -
      keyValue -
      Returns:
      Throws:
      Exception
    • getBooleanFromElText

      public static boolean getBooleanFromElText(org.jdom2.Element el, boolean defaultValue)
    • removeXMLCharacters

      public static String removeXMLCharacters(String input)
    • getOrCreateChild

      public static org.jdom2.Element getOrCreateChild(org.jdom2.Element parentElement, String elementName)
    • replaceElementsWithAttribute

      public static void replaceElementsWithAttribute(org.jdom2.Element elParent, String attrName, String attrValue, org.jdom2.Element elReplacement, String ignoreElementWithName)
    • parseDate

      public static long parseDate(String value)
    • attributeValid

      public static boolean attributeValid(org.jdom2.Element element, String attribute)