Package ghidra.util.xml
Class XmlUtilities
java.lang.Object
ghidra.util.xml.XmlUtilities
A set of utility methods for working with XML.
-
Nested Class Summary
Nested ClassesChangeModifier and TypeClassDescriptionstatic class -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
ChangeModifier and TypeMethodDescriptionMODIFIEDstatic org.jdom2.ElementbyteArrayToXml(byte[] bytes) Converts the specified byte array into an XML element.MODIFIEDstatic org.jdom2.input.SAXBuildercreateSecureSAXBuilder(boolean validate, boolean needsDTD) Create aSAXBuilderthat is not susceptible to XXE.static SAXParserFactorycreateSecureSAXParserFactory(boolean needsDTD) Create aSAXParserFactorythat is not susceptible to XXE.static StringConverts any special or reserved characters in the specified XML string into the equivalent Unicode encoding.MODIFIEDstatic org.jdom2.ElementfromString(String s) Convert a String into a JDOMElement.NEWstatic List<org.jdom2.Element> getChildren(org.jdom2.Element ele, String childName) Type-safe way of getting a list ofElements from JDom.static booleanTests a string for characters that would cause a problem if added to an xml attribute or element.static booleanparseBoolean(String boolStr) Parses the given string into a boolean value.static intparseBoundedInt(String intStr, int minValue, int maxValue) Parses the specified string as a decimal number, returning its integer value.NEWstatic intparseBoundedIntAttr(org.jdom2.Element ele, String attrName, int minValue, int maxValue) Parses the required attribute as a decimal number, returning its integer value.static longparseBoundedLong(String longStr, long minValue, long maxValue) Parses the specified string as a decimal number, returning its long integer value.NEWstatic longparseBoundedLongAttr(org.jdom2.Element ele, String attrName, long minValue, long maxValue) Parses the required attribute as a decimal number, returning its long integer value.static intParse the given string as either a hex number (if it starts with 0x) or a decimal number.static intParses the optional specified string as a decimal number, returning its integer value.static longParse the given string as either a hex number (if it starts with 0x) or a decimal number.NEWstatic booleanparseOptionalBooleanAttr(org.jdom2.Element ele, String attrName, boolean defaultValue) Parses the optional attribute as a boolean value, returning its value or the specified defaultValue if missing.static intparseOptionalBoundedInt(String intStr, int defaultValue, int minValue, int maxValue) Parses the optional specified string as a decimal number, returning its integer value, or defaultValue if the string is null.NEWstatic intparseOptionalBoundedIntAttr(org.jdom2.Element ele, String attrName, int defaultValue, int minValue, int maxValue) Parses an optional attribute as a decimal number, returning its integer value, or the defaultValue if the attribute is null.NEWstatic longparseOptionalBoundedLongAttr(org.jdom2.Element ele, String attrName, long defaultValue, long minValue, long maxValue) Parses the required attribute as a decimal number, returning its long integer value.static StringparseOverlayName(String addrStr) Parses the overlay name from the specified address string.MODIFIEDstatic org.jdom2.DocumentRead a File and convert to jdom xml doc.MODIFIEDstatic org.jdom2.DocumentRead a File and convert to jdom xml doc.NEWstatic StringrequireStringAttr(org.jdom2.Element ele, String attrName) Throws anIOExceptionwith a verbose explanation if the requested attribute is not present or is empty.NEWstatic voidsetIntAttr(org.jdom2.Element ele, String attrName, int attrValue) Sets an integer attribute on the specified element.NEWstatic voidsetStringAttr(org.jdom2.Element ele, String attrName, String attrValue) Sets a string attribute on the specified element.NEWstatic StringtoString(org.jdom2.Element root) Converts the specified XML element into a String.static StringunEscapeElementEntities(String escapedXMLString) Converts any escaped character entities into their unescaped character equivalents.NEWstatic voidwriteDocToFile(org.jdom2.Document doc, File dest) Writes a JDOM XMLDocumentto aFile.NEWstatic voidwritePrettyDocToFile(org.jdom2.Document doc, File dest) NEWstatic byte[]xmlToByteArray(org.jdom2.Element root) Converts the specified XML element into a byte array.REMOVEDbyte[]xmlToByteArray(Element root) RemovedREMOVEDStringtoString(Element root) RemovedREMOVEDvoidwriteDocToFile(Document doc, File dest) RemovedREMOVEDvoidwritePrettyDocToFile(Document doc, File dest) RemovedREMOVEDintparseBoundedIntAttr(Element ele, String attrName, int minValue, int maxValue) RemovedREMOVEDintparseOptionalBoundedIntAttr(Element ele, String attrName, int defaultValue, int minValue, int maxValue) RemovedREMOVEDlongparseBoundedLongAttr(Element ele, String attrName, long minValue, long maxValue) RemovedREMOVEDlongparseOptionalBoundedLongAttr(Element ele, String attrName, long defaultValue, long minValue, long maxValue) RemovedREMOVEDbooleanparseOptionalBooleanAttr(Element ele, String attrName, boolean defaultValue) RemovedREMOVEDStringrequireStringAttr(Element ele, String attrName) RemovedREMOVEDvoidsetStringAttr(Element ele, String attrName, String attrValue) RemovedREMOVEDvoidsetIntAttr(Element ele, String attrName, int attrValue) RemovedREMOVEDList<Element>getChildren(Element ele, String childName) Removed
-
Field Details
-
FEATURE_DISALLOW_DTD
- See Also:
-
FEATURE_EXTERNAL_GENERAL_ENTITIES
- See Also:
-
FEATURE_EXTERNAL_PARAMETER_ENTITIES
- See Also:
-
-
Constructor Details
-
XmlUtilities
public XmlUtilities()
-
-
Method Details
-
escapeElementEntities
Converts any special or reserved characters in the specified XML string into the equivalent Unicode encoding.- Parameters:
xml- the XML string- Returns:
- the encoded XML string
-
unEscapeElementEntities
Converts any escaped character entities into their unescaped character equivalents. This method is designed to be compatible with the output ofescapeElementEntities(String).- Parameters:
escapedXMLString- The string with escaped data- Returns:
- the unescaped string
-
xmlToByteArray NEW
public static byte[] xmlToByteArray(org.jdom2.Element root) Converts the specified XML element into a byte array.- Parameters:
root- the root element- Returns:
- the byte array translation of the given element
-
toString NEW
Converts the specified XML element into a String.- Parameters:
root- the root element- Returns:
- String translation of the given element
-
fromString MODIFIED
return type: Element → Element; added throws JDOMException; removed throws JDOMException-static org.jdom.Element fromString(java.lang.String s) throws JDOMException, IOException+static org.jdom2.Element fromString(java.lang.String s) throws JDOMException, IOExceptionConvert a String into a JDOMElement.- Parameters:
s-- Returns:
- Throws:
org.jdom2.JDOMExceptionIOExceptionJDOMException(removed)
-
writeDocToFile NEW
Writes a JDOM XMLDocumentto aFile.- Parameters:
doc- JDOM XMLDocumentto write.dest-Fileto write to.- Throws:
IOException- if error when writing file.
-
writePrettyDocToFile NEW
- Parameters:
doc- JDOM XMLDocumentto write.dest-Fileto write to.- Throws:
IOException- if error when writing file.
-
readDocFromFile MODIFIED
public static org.jdom2.Document readDocFromFile(File f) throws org.jdom2.JDOMException, IOException return type: Document → Document; added throws JDOMException; removed throws JDOMException-static org.jdom.Document readDocFromFile(java.io.File f) throws JDOMException, IOException+static org.jdom2.Document readDocFromFile(java.io.File f) throws JDOMException, IOExceptionRead a File and convert to jdom xml doc.- Parameters:
f-Fileto read- Returns:
- JDOM
Document - Throws:
org.jdom2.JDOMException- if text in file isn't valid XMLIOException- if IO error when reading file.JDOMException(removed)
-
readDocFromFile MODIFIED
public static org.jdom2.Document readDocFromFile(ResourceFile f) throws org.jdom2.JDOMException, IOException return type: Document → Document; added throws JDOMException; removed throws JDOMException-static org.jdom.Document readDocFromFile(generic.jar.ResourceFile f) throws JDOMException, IOException+static org.jdom2.Document readDocFromFile(generic.jar.ResourceFile f) throws JDOMException, IOExceptionRead a File and convert to jdom xml doc.- Parameters:
f-ResourceFileto read- Returns:
- JDOM
Document - Throws:
org.jdom2.JDOMException- if text in file isn't valid XMLIOException- if IO error when reading file.JDOMException(removed)
-
byteArrayToXml MODIFIED
public static org.jdom2.Element byteArrayToXml(byte[] bytes) return type: Element → Element-static org.jdom.Element byteArrayToXml(byte[] bytes)+static org.jdom2.Element byteArrayToXml(byte[] bytes)Converts the specified byte array into an XML element.- Parameters:
bytes- the XML bytes- Returns:
- an XML element
-
parseOverlayName
Parses the overlay name from the specified address string. Returns null if the address string does appear to represent an overlay.- Parameters:
addrStr- the address string- Returns:
- the overlay name or null
-
parseInt
Parse the given string as either a hex number (if it starts with 0x) or a decimal number.- Parameters:
intStr- the string to parse into an integer- Returns:
- the parsed integer.
- Throws:
NumberFormatException- if the given string does not represent a valid integer.
-
parseInt
Parses the optional specified string as a decimal number, returning its integer value.- Parameters:
intStr- string with integer digits, or empty or nulldefaultValue- value to return if intStr is missing- Returns:
- integer value of the intStr
- Throws:
NumberFormatException- if intStr could not be parsed or the string specifies a value outside the range of a signed 32 bit integer.
-
parseOptionalBoundedInt
public static int parseOptionalBoundedInt(String intStr, int defaultValue, int minValue, int maxValue) throws NumberFormatException Parses the optional specified string as a decimal number, returning its integer value, or defaultValue if the string is null.- Parameters:
intStr- string with integer digits, or null.defaultValue- value to return if intStr is null.minValue- minimum value allowed (inclusive).maxValue- maximum value allowed (inclusive).- Returns:
- integer value of the intStr.
- Throws:
NumberFormatException- if intStr could not be parsed or is out of range.
-
parseBoundedInt
public static int parseBoundedInt(String intStr, int minValue, int maxValue) throws NumberFormatException Parses the specified string as a decimal number, returning its integer value.- Parameters:
intStr- String with integer digitsminValue- minimum value allowed (inclusive)maxValue- maximum value allowed (inclusive)- Returns:
- integer value of the intStr
- Throws:
NumberFormatException- if intStr is null or empty or could not be parsed or is out of range.
-
parseBoundedIntAttr NEW
public static int parseBoundedIntAttr(org.jdom2.Element ele, String attrName, int minValue, int maxValue) throws NumberFormatException Parses the required attribute as a decimal number, returning its integer value.- Parameters:
ele- JDom element that contains the attributeattrName- the name of the xml attribute to parseminValue- minimum value allowed (inclusive)maxValue- maximum value allowed (inclusive)- Returns:
- integer value of the attribute
- Throws:
NumberFormatException- if intStr could not be parsed or is out of range.
-
parseOptionalBoundedIntAttr NEW
public static int parseOptionalBoundedIntAttr(org.jdom2.Element ele, String attrName, int defaultValue, int minValue, int maxValue) throws NumberFormatException Parses an optional attribute as a decimal number, returning its integer value, or the defaultValue if the attribute is null.- Parameters:
ele- JDOM element that contains the attribute.attrName- the name of the xml attribute to parse.defaultValue- the default value to return if attribute is missing.minValue- minimum value allowed (inclusive).maxValue- maximum value allowed (inclusive).- Returns:
- integer value of the attribute.
- Throws:
NumberFormatException- if the attribute value could not be parsed or is out of range.
-
parseLong
Parse the given string as either a hex number (if it starts with 0x) or a decimal number.- Parameters:
longStr- the string to parse into an long- Returns:
- the parsed long.
- Throws:
NumberFormatException- if the given string does not represent a valid long.
-
parseBoundedLong
public static long parseBoundedLong(String longStr, long minValue, long maxValue) throws NumberFormatException Parses the specified string as a decimal number, returning its long integer value.Note, using
Long.MIN_VALUEand/orLong.MAX_VALUEas lower and upper bounds is problematic and should be avoided as the range check will become a NO-OP and always succeed.- Parameters:
longStr- String with integer digitsminValue- minimum value allowed (inclusive)maxValue- maximum value allowed (inclusive)- Returns:
- long integer value of the longStr
- Throws:
NumberFormatException- if intStr is null or empty or could not be parsed or is out of range.
-
parseBoundedLongAttr NEW
public static long parseBoundedLongAttr(org.jdom2.Element ele, String attrName, long minValue, long maxValue) throws NumberFormatException Parses the required attribute as a decimal number, returning its long integer value.Note, using
Long.MIN_VALUEand/orLong.MAX_VALUEas lower and upper bounds is problematic and should be avoided as the range check will become a NO-OP and always succeed.- Parameters:
ele- JDom element that contains the attributeattrName- the name of the xml attribute to parseminValue- minimum value allowed (inclusive)maxValue- maximum value allowed (inclusive)- Returns:
- long integer value of the attribute
- Throws:
NumberFormatException- if intStr could not be parsed or is out of range.
-
parseOptionalBoundedLongAttr NEW
public static long parseOptionalBoundedLongAttr(org.jdom2.Element ele, String attrName, long defaultValue, long minValue, long maxValue) throws NumberFormatException Parses the required attribute as a decimal number, returning its long integer value.Note, using
Long.MIN_VALUEand/orLong.MAX_VALUEas lower and upper bounds is problematic and should be avoided as the range check will become a NO-OP and always succeed.- Parameters:
ele- JDom element that contains the attribute.attrName- the name of the xml attribute to parse.defaultValue- the default value to return if attribute is missing.minValue- minimum value allowed (inclusive).maxValue- maximum value allowed (inclusive).- Returns:
- long integer value of the attribute.
- Throws:
NumberFormatException- if intStr could not be parsed or is out of range.
-
parseBoolean
Parses the given string into a boolean value. Acceptable inputs are y,n,true,fase. A null input string will return false (useful if optional boolean attribute is false by default)- Parameters:
boolStr- the string to parse into a boolean value- Returns:
- the boolean result.
- Throws:
XmlAttributeException- if the string in not one of y,n,true,false or null.
-
parseOptionalBooleanAttr NEW
public static boolean parseOptionalBooleanAttr(org.jdom2.Element ele, String attrName, boolean defaultValue) throws IOException Parses the optional attribute as a boolean value, returning its value or the specified defaultValue if missing.- Parameters:
ele- JDom element that contains the attributeattrName- the name of the xml attribute to parsedefaultValue- boolean value to return if the attribute is not defined- Returns:
- boolean equiv of the attribute string value ("y", "true"/"n", "false")
- Throws:
IOException- if attribute value is not valid boolean string
-
requireStringAttr NEW
Throws anIOExceptionwith a verbose explanation if the requested attribute is not present or is empty.- Parameters:
ele- JDOMElementthat contains the attributeattrName- the attribute name- Returns:
- String value of the attribute (never null or empty)
- Throws:
IOException- if attribute is missing or empty
-
setStringAttr NEW
Sets a string attribute on the specified element.- Parameters:
ele- JDom elementattrName- name of attributeattrValue- value of attribute, null ok
-
setIntAttr NEW
Sets an integer attribute on the specified element.- Parameters:
ele- JDom elementattrName- name of attributeattrValue- value of attribute
-
getChildren NEW
Type-safe way of getting a list ofElements from JDom.- Parameters:
ele- the parent elementchildName- the name of the children elements to return- Returns:
- List<Element> of elements
-
hasInvalidXMLCharacters
Tests a string for characters that would cause a problem if added to an xml attribute or element.- Parameters:
s- a string- Returns:
- boolean true if the string will cause a problem if added to an xml attribute or element.
-
createSecureSAXBuilder MODIFIED
public static org.jdom2.input.SAXBuilder createSecureSAXBuilder(boolean validate, boolean needsDTD) return type: SAXBuilder → SAXBuilder-static org.jdom.input.SAXBuilder createSecureSAXBuilder(boolean validate, boolean needsDTD)+static org.jdom2.input.SAXBuilder createSecureSAXBuilder(boolean validate, boolean needsDTD)Create aSAXBuilderthat is not susceptible to XXE. This configures the builder to ignore external entities.- Parameters:
validate- indicates whether validation should occurneedsDTD- false to disable doctype declarations altogether- Returns:
- the configured builder
-
createSecureSAXParserFactory
Create aSAXParserFactorythat is not susceptible to XXE. This configures the factory to ignore external entities.- Parameters:
needsDTD- false to disable doctype declarations altogether- Returns:
- the configured factory
-