Class XmlUtil


  • public final class XmlUtil
    extends java.lang.Object
    Some XML-related utilities
    Author:
    kir
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static org.jdom.Element addTextChild​(org.jdom.Element element, java.lang.String childName, java.lang.String childText)  
      static <C extends java.util.Collection<java.lang.String>>
      C
      collectValuesByXPath​(org.jdom.Element element, java.lang.String xpath, C collection)  
      static <C extends java.util.Collection<java.lang.String>>
      C
      collectValuesByXPath​(org.jdom.Element element, org.jdom.xpath.XPath xpath, C collection)  
      static <C extends java.util.Collection<java.lang.String>>
      C
      collectValuesFromFileByXPath​(java.io.File xmlFile, java.lang.String xpath, C collection)  
      static org.jdom.xpath.XPath compileXPath​(java.lang.String xpath)  
      static org.xml.sax.XMLReader createXMLReader()
      Creates new XMLReader with security option enabled and safe entity resolved (see XMLParser.SafeEntityResolver for details) Also reader is not validating.
      static org.xml.sax.XMLReader createXMLReader​(boolean validate)
      Creates new XMLReader with security option enabled and safe entity resolved (see XMLParser.SafeEntityResolver for details) Also reader could be validating or not.
      static java.lang.String formatXml​(java.lang.String xmlString)
      Reformats given valid xml string with pretty formatter
      static org.jdom.Element from_s​(java.lang.String xmlString)
      Simple helper method to parse XML and return root JDOM element.
      static org.jdom.Element getElement​(org.jdom.Element element, java.lang.String xpath)
      Looks for the first element specified by the given XPath.
      static boolean isSecureParameter​(java.lang.String propertyName)
      Returns true if the property is secure, i.e.
      static org.jdom.Element makeElement​(java.lang.String name, java.lang.String... attributes)  
      static com.intellij.openapi.util.Pair<java.lang.String,​java.lang.String> readParameter​(org.jdom.Element paramElement)
      Reads single parameter from specified parameter element
      static java.util.Map<java.lang.String,​java.lang.String> readParameters​(org.jdom.Element parametersElement)  
      static byte[] saveDocument​(org.jdom.Document document)  
      static void saveDocument​(org.jdom.Document document, java.io.OutputStream os)  
      static void saveDocument​(org.jdom.Document document, java.io.Writer writer)  
      static void saveElement​(org.jdom.Element element, java.io.Writer writer)  
      static org.jdom.Element selectElement​(org.jdom.Element element, java.lang.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 java.util.List<org.jdom.Element> selectElements​(org.jdom.Element element, java.lang.String xpath)
      Looks for the all elements specified by the given XPath.
      static java.util.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 java.lang.String selectValue​(org.jdom.Element element, java.lang.String xpath)  
      static java.lang.String selectValue​(org.jdom.Element element, org.jdom.xpath.XPath xpath)  
      protected static void setJavaEncodingFeature​(org.xml.sax.XMLReader reader)  
      protected static void setSecurityFeatures​(org.xml.sax.XMLReader reader)  
      protected static void setValidationFeature​(org.xml.sax.XMLReader reader, boolean validate)  
      static java.lang.String to_s​(org.jdom.Element el)
      Simple helper method to format JDOM element as a String
      static java.lang.String toString​(org.jdom.Document document)  
      static java.lang.String toString​(org.jdom.Element el)  
      static org.jdom.Element writeParameter​(org.jdom.Element parentElement, java.lang.String name, java.lang.String value)
      Write param tag under parentElement with name 'name' and value 'value' Parameter element name is PARAMETER_ELEMENT_NAME.
      static void writeParameters​(org.jdom.Element parentElement, java.util.Map<java.lang.String,​java.lang.String> params)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • saveDocument

        public static byte[] saveDocument​(@NotNull
                                          org.jdom.Document document)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • saveDocument

        public static void saveDocument​(@NotNull
                                        org.jdom.Document document,
                                        @NotNull
                                        java.io.OutputStream os)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • saveDocument

        public static void saveDocument​(@NotNull
                                        org.jdom.Document document,
                                        @NotNull
                                        java.io.Writer writer)
                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • saveElement

        public static void saveElement​(@NotNull
                                       org.jdom.Element element,
                                       @NotNull
                                       java.io.Writer writer)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • isSecureParameter

        public static boolean isSecureParameter​(java.lang.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,
                                                      java.lang.String name,
                                                      java.lang.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 com.intellij.openapi.util.Pair<java.lang.String,​java.lang.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
                                           java.util.Map<java.lang.String,​java.lang.String> params)
      • readParameters

        @NotNull
        public static java.util.Map<java.lang.String,​java.lang.String> readParameters​(@NotNull
                                                                                            org.jdom.Element parametersElement)
      • to_s

        public static java.lang.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 java.lang.String toString​(@NotNull
                                                org.jdom.Element el)
      • toString

        public static java.lang.String toString​(@NotNull
                                                org.jdom.Document document)
      • from_s

        public static org.jdom.Element from_s​(java.lang.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 java.lang.String formatXml​(@NotNull
                                                 java.lang.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
                                                     java.lang.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
                                                  java.lang.String xpath)
                                           throws java.util.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:
        java.util.NoSuchElementException - when the requested element is not found.
        Since:
        8.0
        See Also:
        selectElement(org.jdom.Element, String)
      • selectValue

        @Nullable
        public static java.lang.String selectValue​(@NotNull
                                                   org.jdom.Element element,
                                                   @NotNull
                                                   java.lang.String xpath)
        Since:
        8.0
      • selectValue

        @Nullable
        public static java.lang.String selectValue​(@NotNull
                                                   org.jdom.Element element,
                                                   @NotNull
                                                   org.jdom.xpath.XPath xpath)
        Since:
        8.0
      • selectElements

        @NotNull
        public static java.util.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 java.util.List<org.jdom.Element> selectElements​(@NotNull
                                                                      org.jdom.Element element,
                                                                      @NotNull
                                                                      java.lang.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 java.util.Collection<java.lang.String>> C collectValuesFromFileByXPath​(@NotNull
                                                                                                        java.io.File xmlFile,
                                                                                                        @NotNull
                                                                                                        java.lang.String xpath,
                                                                                                        @NotNull
                                                                                                        C collection)
        Since:
        8.0
      • collectValuesByXPath

        @NotNull
        public static <C extends java.util.Collection<java.lang.String>> C collectValuesByXPath​(@NotNull
                                                                                                org.jdom.Element element,
                                                                                                @NotNull
                                                                                                java.lang.String xpath,
                                                                                                @NotNull
                                                                                                C collection)
                                                                                         throws org.jdom.JDOMException
        Throws:
        org.jdom.JDOMException
        Since:
        8.0
      • collectValuesByXPath

        @NotNull
        public static <C extends java.util.Collection<java.lang.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​(java.lang.String xpath)
        Since:
        8.0
      • addTextChild

        @NotNull
        public static org.jdom.Element addTextChild​(@NotNull
                                                    org.jdom.Element element,
                                                    @NotNull
                                                    java.lang.String childName,
                                                    @Nullable
                                                    java.lang.String childText)
      • makeElement

        @NotNull
        public static org.jdom.Element makeElement​(@NotNull
                                                   java.lang.String name,
                                                   java.lang.String... attributes)
      • createXMLReader

        @NotNull
        public static org.xml.sax.XMLReader createXMLReader()
                                                     throws org.xml.sax.SAXException
        Creates new XMLReader with security option enabled and safe entity resolved (see XMLParser.SafeEntityResolver for details) Also reader is not validating.
        Returns:
        configured XMLReader with enabled security features
        Throws:
        org.xml.sax.SAXException - if SAXException occurs
        Since:
        9.0.2
        See Also:
        createXMLReader(boolean)
      • createXMLReader

        @NotNull
        public static org.xml.sax.XMLReader createXMLReader​(boolean validate)
                                                     throws org.xml.sax.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:
        org.xml.sax.SAXException - if SAXException occurs
        Since:
        9.0.2
      • setJavaEncodingFeature

        protected static void setJavaEncodingFeature​(org.xml.sax.XMLReader reader)
      • setSecurityFeatures

        protected static void setSecurityFeatures​(@NotNull
                                                  org.xml.sax.XMLReader reader)
      • setValidationFeature

        protected static void setValidationFeature​(@NotNull
                                                   org.xml.sax.XMLReader reader,
                                                   boolean validate)