Class XmlUtil


  • public final class XmlUtil
    extends Object
    Some XML-related utilities
    Author:
    kir
    • 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
                                        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 is PARAMETER_ELEMENT_NAME.
        Parameters:
        name - name of parameter
        parentElement - parent element for param tag
        value - 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 into
        xpath - 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 into
        xpath - 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 into
        xpath - 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 into
        xpath - 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 into
        xpath - 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​(boolean validate)
                                         throws SAXException
        Creates new XMLReader with security option enabled and safe entity resolved (see XMLParser.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)