Package com.strategyquant.lib
Class XMLUtil
java.lang.Object
com.strategyquant.lib.XMLUtil
The Class XMLUtil.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
attributesEqual(org.jdom2.Element element1, org.jdom2.Element element2)
Attributes equal.static boolean
attributeValid(org.jdom2.Element element, String attribute)
static void
copyAttributes(org.jdom2.Element elSource, org.jdom2.Element elDesc, boolean replace)
Copy attributes.static org.jdom2.Element
copyElement(org.jdom2.Element elSource)
Copy element.static void
copySettings(org.jdom2.Element target, org.jdom2.Element template, String elementToReplace)
Copy settings.static int
countElements(String elementName, org.jdom2.Element el, int count)
Count elements.static boolean
Element is.static boolean
elementIsNot(org.jdom2.Element elem, String attribute)
Element is not.static String
elementLineToString(org.jdom2.Element element)
Element to string.static boolean
elementsEqual(org.jdom2.Element element1, org.jdom2.Element element2)
Elements equal.static String
elementToString(org.jdom2.Element element)
Element to string.static String
elementToString(org.jdom2.Element element, boolean beautify)
Element to string.static Object
elementToValue(org.jdom2.Element child)
Element to value.static org.jdom2.Document
File to xml.static org.jdom2.Element
fileToXmlElement(File file)
File to xml element.static void
Find first.static void
findAllWithKey(org.jdom2.Element config, String elementName, String key, ArrayList<org.jdom2.Element> list)
static org.jdom2.Element
Find first.static org.jdom2.Element
findFirstParent(org.jdom2.Element el, String elementName)
Find first parent.static org.jdom2.Element
findFirstWithKey(org.jdom2.Element config, String elementName, String key)
Find first with key.static String
Gets the attr.static String
Gets the attr.static boolean
getBoolean(org.jdom2.Element elValues, String name, boolean defaultValue)
Gets the boolean.static boolean
getBooleanAttr(org.jdom2.Element element, String attributeName)
Gets the boolean attr.static boolean
getBooleanAttr(org.jdom2.Element element, String attributeName, boolean defaultValue)
Gets the boolean attr.static boolean
getBooleanFromElText(org.jdom2.Element el, boolean defaultValue)
static byte
getByteAttr(org.jdom2.Element element, String attributeName, byte defaultValue)
Gets the byte attr.static org.jdom2.Element
getChildElem(org.jdom2.Element parentElem, String childName)
Gets the child elem.static org.jdom2.Element
getChildElem(org.jdom2.Element parentElem, String childName, String fallback)
static double
Gets the double.static double
getDoubleAttr(org.jdom2.Element element, String attributeName, double defaultValue)
Gets the double attr.static float
Gets the float.static int
Gets the int.static int
getIntAttr(org.jdom2.Element element, String attributeName)
Gets the int attr.static byte
getIntAttr(org.jdom2.Element element, String attributeName, byte defaultValue)
Gets the byte attr.static int
getIntAttr(org.jdom2.Element element, String attributeName, int defaultValue)
Gets the int attr.static boolean
getItemBoolParam(org.jdom2.Element elItem, String key, boolean defaultValue)
static double
getItemDoubleParam(org.jdom2.Element elItem, String key, double defaultValue)
static int
getItemIntParam(org.jdom2.Element elItem, String key, int defaultValue)
static org.jdom2.Element
getItemParameter(org.jdom2.Element elItem, String key, boolean throwExceptionIfNotFound)
Gets the item parameter.static org.jdom2.Element
getItemParameterNoException(org.jdom2.Element elItem, String key)
Gets the item parameter no exception.static long
Gets the long.static long
getLongAttr(org.jdom2.Element element, String attributeName)
Gets the long attr.static ArrayList<org.jdom2.Element>
getNestedElements(org.jdom2.Element rootElem, String elementName)
Gets the nested elements.static ArrayList<org.jdom2.Element>
getNestedElements(org.jdom2.Element rootElem, String elementName, String attrName, String attrValue)
Gets the nested elements with attribute valuestatic boolean
getNodeBooleanValue(org.jdom2.Element elParent, String nodeName, boolean defaultValue)
Gets the node boolean value.static int
getNodeIntValue(org.jdom2.Element elParent, String nodeName)
Gets the node int value.static int
getNodeIntValue(org.jdom2.Element elParent, String nodeName, int defaultValue)
Gets the node int value.static String
getNodeValue(org.jdom2.Element elParent, String nodeName)
Gets the node value.static String
getNodeValue(org.jdom2.Element elParent, String nodeName, String defaultValue)
Gets the node value.static org.jdom2.Element
getOptionParameter(org.jdom2.Element elOptions, String key)
Gets the option parameter.static org.jdom2.Element
getOrCreateChild(org.jdom2.Element parentElement, String elementName)
static String
getStringAttr(org.jdom2.Element element, String attributeName, String defaultValue)
Gets the string attr.static org.jdom2.Element
getSubchildElement(org.jdom2.Element el, String xmlPath, String keyValue)
method looks for the exact path in the XML.static org.jdom2.Element
getSubchildElementEx(org.jdom2.Element el, String xmlPath, String keyValue)
method looks for the exact path in the XML.static boolean
hasChildElem(org.jdom2.Element parentElem, String childName)
static boolean
JSValuesEqual(String value1, String value2)
JS values equal.static long
static void
printXml(org.jdom2.Element element)
Prints the xml.static org.jdom2.Element
readXmlFile(InputStream is)
Read xml file.static void
removeAttribute(org.jdom2.Element el, String attributeName)
Removes the attribute.static String
removeXMLCharacters(String input)
static void
replaceElementsWithAttribute(org.jdom2.Element elParent, String attrName, String attrValue, org.jdom2.Element elReplacement, String ignoreElementWithName)
static void
setItemBoolParam(org.jdom2.Element elItem, String key, boolean value)
static void
setItemDoubleParam(org.jdom2.Element elItem, String key, double value)
static void
setItemIntParam(org.jdom2.Element elItem, String key, int value)
static org.jdom2.Element
stringToElement(String xmlConfig)
String to element.static org.jdom2.Document
stringToXml(String text)
String to xml.static org.jdom2.Element
stringToXmlElement(String xmlString)
String to xml element.static org.jdom2.Element
tryAddBooleanNode(org.jdom2.Element elParent, String nodeName, boolean value)
Try add boolean node.static org.jdom2.Element
tryAddDoubleNode(org.jdom2.Element elParent, String nodeName, double value)
Try add double node.static org.jdom2.Element
tryAddElement(org.jdom2.Element elParent, String elementName)
Try add element.static org.jdom2.Element
tryAddIntNode(org.jdom2.Element elParent, String nodeName, int value)
Try add int node.static org.jdom2.Element
tryAddNode(org.jdom2.Element elParent, String nodeName, String value)
Try add node.static String
tryGetAttr(org.jdom2.Element element, String attributeName, String defaultValue)
Try get attr.static boolean
tryGetBoolAttr(org.jdom2.Element element, String attributeName)
Try get bool attr.static byte
tryGetByteAttr(org.jdom2.Element element, String attributeName)
Try get byte attr.static double
tryGetDoubleAttr(org.jdom2.Element element, String attributeName)
Try get double attr.static int
tryGetIntAttr(org.jdom2.Element element, String attributeName)
Try get int attr.static long
tryGetLongAttr(org.jdom2.Element element, String attributeName)
Try get long attr.static void
trySetAttr(org.jdom2.Element element, String name, Object value)
Try set attr.static int
Value hash.static org.jdom2.Element
valueToElement(String key, Object value)
Value to element.static void
Xml to file.static void
Xml to file.static String
xmlToString(org.jdom2.Element el)
Xml to string.static String
xmlToStringRaw(org.jdom2.Element element)
Xml to string raw.
-
Constructor Details
-
XMLUtil
public XMLUtil()
-
-
Method Details
-
getChildElem
public static org.jdom2.Element getChildElem(org.jdom2.Element parentElem, String childName) throws ExceptionGets the child elem.- Parameters:
parentElem
- the parent elemchildName
- 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
-
elementIsNot
Element is not.- Parameters:
elem
- the elemattribute
- the attribute- Returns:
- true, if successful
-
elementIs
Element is.- Parameters:
elem
- the elemattribute
- the attribute- Returns:
- true, if successful
-
valueToElement
Value to element.- Parameters:
key
- the keyvalue
- the value- Returns:
- the element
-
elementToValue
Element to value.- Parameters:
child
- the child- Returns:
- the object
-
valueHash
Value hash.- Parameters:
keyHash
- the key hashvalue
- the value- Returns:
- the int
-
stringToElement
public static org.jdom2.Element stringToElement(String xmlConfig) throws org.jdom2.JDOMException, IOExceptionString to element.- Parameters:
xmlConfig
- the xml config- Returns:
- the element
- Throws:
org.jdom2.JDOMException
- the JDOM exceptionIOException
- Signals that an I/O exception has occurred.
-
elementToString
Element to string.- Parameters:
element
- the element- Returns:
- the string
-
elementLineToString
Element to string.- Parameters:
element
- the element- Returns:
- the string
-
elementToString
Element to string.- Parameters:
element
- the elementbeautify
- 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 elemelementName
- 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 elemelementName
- the element nameattrName
- the attribute nameattrValue
- the attribute value- Returns:
- the nested elements
-
findFirst
Find first.- Parameters:
config
- the configelementName
- 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 configelementName
- the element namelist
-
-
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 configelementName
- the element namekey
- the key- Returns:
- the element
-
getAttr
Gets the attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the attr
- Throws:
Exception
- the exception
-
getAttr
Gets the attr.- Parameters:
element
- the elementattributeName
- the attribute namedefaultValue
- 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 elementattributeName
- the attribute namedefaultValue
- the default value- Returns:
- the string attr
-
getByteAttr
Gets the byte attr.- Parameters:
element
- the elementattributeName
- the attribute namedefaultValue
- the default value- Returns:
- the byte attr
-
getIntAttr
Gets the int attr.- Parameters:
element
- the elementattributeName
- the attribute namedefaultValue
- the default value- Returns:
- the int attr
-
getIntAttr
Gets the int attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the int attr
- Throws:
Exception
-
getLongAttr
Gets the long attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the int attr
- Throws:
Exception
-
getBooleanAttr
public static boolean getBooleanAttr(org.jdom2.Element element, String attributeName) throws ExceptionGets the boolean attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the int attr
- Throws:
Exception
-
getIntAttr
Gets the byte attr.- Parameters:
element
- the elementattributeName
- the attribute namedefaultValue
- 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 elementattributeName
- the attribute namedefaultValue
- 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 elementattributeName
- the attribute namedefaultValue
- the default value- Returns:
- the boolean attr
-
tryGetDoubleAttr
Try get double attr.- Parameters:
element
- the elementattributeName
- 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 elementattributeName
- the attribute namedefaultValue
- the default value- Returns:
- the string
-
tryGetLongAttr
Try get long attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the long
-
tryGetIntAttr
Try get int attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the int
-
tryGetBoolAttr
Try get bool attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- true, if successful
-
tryGetByteAttr
Try get byte attr.- Parameters:
element
- the elementattributeName
- the attribute name- Returns:
- the byte
-
trySetAttr
Try set attr.- Parameters:
element
- the elementname
- the namevalue
- the value
-
stringToXml
public static org.jdom2.Document stringToXml(String text) throws org.jdom2.JDOMException, IOExceptionString to xml.- Parameters:
text
- the text- Returns:
- the document
- Throws:
org.jdom2.JDOMException
- the JDOM exceptionIOException
- Signals that an I/O exception has occurred.
-
stringToXmlElement
public static org.jdom2.Element stringToXmlElement(String xmlString) throws org.jdom2.JDOMException, IOExceptionString to xml element.- Parameters:
xmlString
- the xml string- Returns:
- the element
- Throws:
org.jdom2.JDOMException
- the JDOM exceptionIOException
- Signals that an I/O exception has occurred.
-
fileToXml
public static org.jdom2.Document fileToXml(File file) throws org.jdom2.JDOMException, IOException, ExceptionFile to xml.- Parameters:
file
- the file- Returns:
- the document
- Throws:
org.jdom2.JDOMException
- the JDOM exceptionIOException
- 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, ExceptionFile to xml element.- Parameters:
file
- the file- Returns:
- the element
- Throws:
org.jdom2.JDOMException
- the JDOM exceptionIOException
- Signals that an I/O exception has occurred.Exception
- the exception
-
xmlToFile
Xml to file.- Parameters:
xmlElem
- the xml elemfile
- the file- Throws:
Exception
- the exception
-
xmlToFile
Xml to file.- Parameters:
doc
- the docfile
- the file- Throws:
Exception
- the exception
-
xmlToString
Xml to string.- Parameters:
el
- the el- Returns:
- the string
-
xmlToStringRaw
Xml to string raw.- Parameters:
element
- the element- Returns:
- the string
-
getInt
Gets the int.- Parameters:
elValues
- the el valuesname
- the namedefaultValue
- the default value- Returns:
- the int
-
getLong
Gets the long.- Parameters:
elValues
- the el valuesname
- the namedefaultValue
- the default value- Returns:
- the long
-
getFloat
Gets the float.- Parameters:
elValues
- the el valuesname
- the namedefaultValue
- the default value- Returns:
- the float
-
getDouble
Gets the double.- Parameters:
elValues
- the el valuesname
- the namedefaultValue
- the default value- Returns:
- the double
-
getBoolean
Gets the boolean.- Parameters:
elValues
- the el valuesname
- the namedefaultValue
- the default value- Returns:
- the boolean
-
tryAddElement
Try add element.- Parameters:
elParent
- the el parentelementName
- 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 parentnodeName
- the node namevalue
- 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 parentnodeName
- the node namevalue
- 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 parentnodeName
- the node namevalue
- 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 parentnodeName
- the node namevalue
- the value- Returns:
- the element
-
getNodeValue
Gets the node value.- Parameters:
elParent
- the el parentnodeName
- 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 parentnodeName
- the node namedefaultValue
- the default value- Returns:
- the node value
-
getNodeIntValue
Gets the node int value.- Parameters:
elParent
- the el parentnodeName
- 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 ExceptionGets the node int value.- Parameters:
elParent
- the el parentnodeName
- the node namedefaultValue
- 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 ExceptionGets the node boolean value.- Parameters:
elParent
- the el parentnodeName
- the node namedefaultValue
- the default value- Returns:
- the node boolean value
- Throws:
Exception
- the exception
-
getItemParameterNoException
Gets the item parameter no exception.- Parameters:
elItem
- the el itemkey
- the key- Returns:
- the item parameter no exception
-
getItemParameter
public static org.jdom2.Element getItemParameter(org.jdom2.Element elItem, String key, boolean throwExceptionIfNotFound) throws ExceptionGets the item parameter.- Parameters:
elItem
- the el itemkey
- the keythrowExceptionIfNotFound
- the throw exception if not found- Returns:
- the item parameter
- Throws:
Exception
- the exception
-
getOptionParameter
Gets the option parameter.- Parameters:
elOptions
- the el optionskey
- 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 sourceelDesc
- the el descreplace
- the replace
-
copySettings
public static void copySettings(org.jdom2.Element target, org.jdom2.Element template, String elementToReplace) throws ExceptionCopy settings.- Parameters:
target
- the targettemplate
- the templateelementToReplace
- 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 1element2
- 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 1element2
- the element 2- Returns:
- true, if successful
-
JSValuesEqual
JS values equal.- Parameters:
value1
- the value 1value2
- the value 2- Returns:
- true, if successful
-
readXmlFile
Read xml file.- Parameters:
is
- the is- Returns:
- the element
- Throws:
Exception
- the exception
-
countElements
Count elements.- Parameters:
elementName
- the element nameel
- the elcount
- the count- Returns:
- the int
-
findFirstParent
Find first parent.- Parameters:
el
- the elelementName
- the element name- Returns:
- the element
-
removeAttribute
Removes the attribute.- Parameters:
el
- the elattributeName
- the attribute name
-
getItemIntParam
-
setItemIntParam
-
getItemDoubleParam
-
setItemDoubleParam
-
getItemBoolParam
-
setItemBoolParam
-
getSubchildElementEx
public static org.jdom2.Element getSubchildElementEx(org.jdom2.Element el, String xmlPath, String keyValue) throws Exceptionmethod 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 Exceptionmethod 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
-
getOrCreateChild
public static org.jdom2.Element getOrCreateChild(org.jdom2.Element parentElement, String elementName) -
replaceElementsWithAttribute
-
parseDate
-
attributeValid
-