Package jetbrains.buildServer.util
Class XmlUtil
- java.lang.Object
-
- jetbrains.buildServer.util.XmlUtil
-
public final class XmlUtil extends Object
Some XML-related utilities- Author:
- kir
-
-
Field Summary
Fields Modifier and Type Field Description static String
PARAMETER_ELEMENT_NAME
Name of element that is used to save parameter bywriteParameter(org.jdom.Element, String, String)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.jdom.Element
addTextChild(org.jdom.Element element, String childName, String childText)
static <C extends Collection<String>>
CcollectValuesByXPath(org.jdom.Element element, String xpath, C collection)
static <C extends Collection<String>>
CcollectValuesByXPath(org.jdom.Element element, org.jdom.xpath.XPath xpath, C collection)
static <C extends Collection<String>>
CcollectValuesFromFileByXPath(File xmlFile, String xpath, C collection)
static org.jdom.xpath.XPath
compileXPath(String xpath)
static XMLReader
createXMLReader()
Creates new XMLReader with security option enabled and safe entity resolved (seeXMLParser.SafeEntityResolver
for details) Also reader is not validating.static XMLReader
createXMLReader(boolean validate)
Creates new XMLReader with security option enabled and safe entity resolved (seeXMLParser.SafeEntityResolver
for details) Also reader could be validating or not.static String
formatXml(String xmlString)
Reformats given valid xml string with pretty formatterstatic org.jdom.Element
from_s(String xmlString)
Simple helper method to parse XML and return root JDOM element.static org.jdom.Element
getElement(org.jdom.Element element, String xpath)
Looks for the first element specified by the given XPath.static boolean
isSecureParameter(String propertyName)
Returns true if the property is secure, i.e.static org.jdom.Element
makeElement(String name, String... attributes)
static Pair<String,String>
readParameter(org.jdom.Element paramElement)
Reads single parameter from specified parameter elementstatic Map<String,String>
readParameters(org.jdom.Element parametersElement)
static byte[]
saveDocument(org.jdom.Document document)
static void
saveDocument(org.jdom.Document document, OutputStream os)
static void
saveDocument(org.jdom.Document document, Writer writer)
static void
saveElement(org.jdom.Element element, Writer writer)
static org.jdom.Element
selectElement(org.jdom.Element element, String xpath)
Looks for the first element specified by the given XPath.static org.jdom.Element
selectElement(org.jdom.Element element, org.jdom.xpath.XPath xpath)
Looks for the first element specified by the given XPath.static List<org.jdom.Element>
selectElements(org.jdom.Element element, String xpath)
Looks for the all elements specified by the given XPath.static List<org.jdom.Element>
selectElements(org.jdom.Element element, org.jdom.xpath.XPath xpath)
Looks for the all elements specified by the given XPath.static String
selectValue(org.jdom.Element element, String xpath)
static String
selectValue(org.jdom.Element element, org.jdom.xpath.XPath xpath)
protected static void
setJavaEncodingFeature(XMLReader reader)
protected static void
setSecurityFeatures(XMLReader reader)
protected static void
setValidationFeature(XMLReader reader, boolean validate)
static String
to_s(org.jdom.Element el)
Simple helper method to format JDOM element as a Stringstatic String
toString(org.jdom.Document document)
static String
toString(org.jdom.Element el)
static org.jdom.Element
writeParameter(org.jdom.Element parentElement, String name, String value)
Write param tag under parentElement with name 'name' and value 'value' Parameter element name isPARAMETER_ELEMENT_NAME
.static void
writeParameters(org.jdom.Element parentElement, Map<String,String> params)
-
-
-
Field Detail
-
PARAMETER_ELEMENT_NAME
public static final String PARAMETER_ELEMENT_NAME
Name of element that is used to save parameter bywriteParameter(org.jdom.Element, String, String)
- Since:
- 6.0
- See Also:
- Constant Field Values
-
-
Method Detail
-
saveDocument
public static byte[] saveDocument(@NotNull org.jdom.Document document) throws IOException
- Throws:
IOException
-
saveDocument
public static void saveDocument(@NotNull org.jdom.Document document, @NotNull OutputStream os) throws IOException
- Throws:
IOException
-
saveDocument
public static void saveDocument(@NotNull org.jdom.Document document, @NotNull Writer writer) throws IOException
- Throws:
IOException
-
saveElement
public static void saveElement(@NotNull org.jdom.Element element, @NotNull Writer writer) throws IOException
- Throws:
IOException
-
isSecureParameter
public static boolean isSecureParameter(String propertyName)
Returns true if the property is secure, i.e. its value should be scrambled when property persisted.- Parameters:
propertyName
- property name.- Returns:
true
if property value should be scrambled.
-
writeParameter
public static org.jdom.Element writeParameter(@NotNull org.jdom.Element parentElement, String name, String value)
Write param tag under parentElement with name 'name' and value 'value' Parameter element name isPARAMETER_ELEMENT_NAME
.- Parameters:
name
- name of parameterparentElement
- parent element for param tagvalue
- value to be saved in param tag. If contains LF, value will be saved as CDATA- Returns:
- created parameter element
-
readParameter
@NotNull public static Pair<String,String> readParameter(@NotNull org.jdom.Element paramElement)
Reads single parameter from specified parameter element- Parameters:
paramElement
- parameter element, must have name attribute- Returns:
- name and value
-
writeParameters
public static void writeParameters(@NotNull org.jdom.Element parentElement, @NotNull Map<String,String> params)
-
readParameters
@NotNull public static Map<String,String> readParameters(@NotNull org.jdom.Element parametersElement)
-
to_s
public static String to_s(@NotNull org.jdom.Element el)
Simple helper method to format JDOM element as a String- Parameters:
el
- element to format- Returns:
- formatted XML
- Since:
- 4.5
-
toString
public static String toString(@NotNull org.jdom.Element el)
-
toString
public static String toString(@NotNull org.jdom.Document document)
-
from_s
public static org.jdom.Element from_s(String xmlString)
Simple helper method to parse XML and return root JDOM element. Upon any error, throws RuntimeException.- Parameters:
xmlString
- XML to parse- Returns:
- root JDOM element for the XML string
- Since:
- 5.0
-
formatXml
@NotNull public static String formatXml(@NotNull String xmlString)
Reformats given valid xml string with pretty formatter- Parameters:
xmlString
- valid xml text- Returns:
- formatted xml
- Since:
- 8.0
-
selectElement
@Nullable public static org.jdom.Element selectElement(@NotNull org.jdom.Element element, @NotNull org.jdom.xpath.XPath xpath)
Looks for the first element specified by the given XPath.- Parameters:
element
- a particular element to look intoxpath
- XPath denoting Element (not an attribute).- Returns:
- first found element, or null if not found.
- Since:
- 8.0
-
selectElement
@Nullable public static org.jdom.Element selectElement(@NotNull org.jdom.Element element, @NotNull String xpath)
Looks for the first element specified by the given XPath.- Parameters:
element
- a particular element to look intoxpath
- XPath denoting Element (not an attribute).- Returns:
- first found element, or null if not found.
- Since:
- 8.0
- See Also:
getElement(org.jdom.Element, String)
-
getElement
@NotNull public static org.jdom.Element getElement(@NotNull org.jdom.Element element, @NotNull String xpath) throws NoSuchElementException
Looks for the first element specified by the given XPath. The element MUST exist.- Parameters:
element
- a particular element to look intoxpath
- XPath denoting Element (not an attribute).- Returns:
- found element. Always not null.
- Throws:
NoSuchElementException
- when the requested element is not found.- Since:
- 8.0
- See Also:
selectElement(org.jdom.Element, String)
-
selectValue
@Nullable public static String selectValue(@NotNull org.jdom.Element element, @NotNull String xpath)
- Since:
- 8.0
-
selectValue
@Nullable public static String selectValue(@NotNull org.jdom.Element element, @NotNull org.jdom.xpath.XPath xpath)
- Since:
- 8.0
-
selectElements
@NotNull public static List<org.jdom.Element> selectElements(@NotNull org.jdom.Element element, @NotNull org.jdom.xpath.XPath xpath)
Looks for the all elements specified by the given XPath.- Parameters:
element
- a particular element to look intoxpath
- XPath denoting Element (not an attribute).- Returns:
- found elements in the order occurred in the given XML.
- Since:
- 8.0
-
selectElements
@NotNull public static List<org.jdom.Element> selectElements(@NotNull org.jdom.Element element, @NotNull String xpath)
Looks for the all elements specified by the given XPath.- Parameters:
element
- a particular element to look intoxpath
- XPath denoting Element (not an attribute).- Returns:
- found elements in the order occurred in the given XML.
- Since:
- 8.0
-
collectValuesFromFileByXPath
@NotNull public static <C extends Collection<String>> C collectValuesFromFileByXPath(@NotNull File xmlFile, @NotNull String xpath, @NotNull C collection)
- Since:
- 8.0
-
collectValuesByXPath
@NotNull public static <C extends Collection<String>> C collectValuesByXPath(@NotNull org.jdom.Element element, @NotNull String xpath, @NotNull C collection) throws org.jdom.JDOMException
- Throws:
org.jdom.JDOMException
- Since:
- 8.0
-
collectValuesByXPath
@NotNull public static <C extends Collection<String>> C collectValuesByXPath(@NotNull org.jdom.Element element, @NotNull org.jdom.xpath.XPath xpath, @NotNull C collection) throws org.jdom.JDOMException
- Throws:
org.jdom.JDOMException
- Since:
- 8.0
-
compileXPath
@NotNull public static org.jdom.xpath.XPath compileXPath(String xpath)
- Since:
- 8.0
-
addTextChild
@NotNull public static org.jdom.Element addTextChild(@NotNull org.jdom.Element element, @NotNull String childName, @Nullable String childText)
-
makeElement
@NotNull public static org.jdom.Element makeElement(@NotNull String name, String... attributes)
-
createXMLReader
@NotNull public static XMLReader createXMLReader() throws SAXException
Creates new XMLReader with security option enabled and safe entity resolved (seeXMLParser.SafeEntityResolver
for details) Also reader is not validating.- Returns:
- configured XMLReader with enabled security features
- Throws:
SAXException
- if SAXException occurs- Since:
- 9.0.2
- See Also:
createXMLReader(boolean)
-
createXMLReader
@NotNull public static XMLReader createXMLReader(boolean validate) throws SAXException
Creates new XMLReader with security option enabled and safe entity resolved (seeXMLParser.SafeEntityResolver
for details) Also reader could be validating or not.- Parameters:
validate
- selects validating feature- Returns:
- configured XMLReader with enabled security features
- Throws:
SAXException
- if SAXException occurs- Since:
- 9.0.2
-
setJavaEncodingFeature
protected static void setJavaEncodingFeature(XMLReader reader)
-
setSecurityFeatures
protected static void setSecurityFeatures(@NotNull XMLReader reader)
-
setValidationFeature
protected static void setValidationFeature(@NotNull XMLReader reader, boolean validate)
-
-