Package lumis.util

Class XmlUtil


  • @StableMinor(version="17.0",
                 sinceVersion="4.0")
    public class XmlUtil
    extends java.lang.Object
    Utility file for xml
    Since:
    4.0.0
    Version:
    $Revision: 26587 $ $Date: 2024-08-26 21:09:17 -0300 (Mon, 26 Aug 2024) $
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String XML_HEADER  
    • Constructor Summary

      Constructors 
      Constructor Description
      XmlUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static org.w3c.dom.Node addNode​(org.w3c.dom.Node xmlParentNode, java.lang.String newNodeName)  
      static org.w3c.dom.Node addNode​(org.w3c.dom.Node xmlParentNode, java.lang.String xpathExpressionParentNode, java.lang.String newNodeName)  
      static org.w3c.dom.Attr addNodeAttr​(org.w3c.dom.Node xmlParentNode, java.lang.String attributeName)  
      static org.w3c.dom.Attr addNodeAttr​(org.w3c.dom.Node xmlParentNode, java.lang.String attributeName, java.lang.String value)  
      static org.w3c.dom.Node addNodeFromXmlString​(org.w3c.dom.Node node, java.lang.String xmlString)  
      static org.w3c.dom.Element addTextNode​(org.w3c.dom.Node xmlParentNode, java.lang.String newNodeName, java.lang.String newNodeText)  
      static org.w3c.dom.Element addTextNode​(org.w3c.dom.Node xmlParentNode, java.lang.String xpathExpressionParentNode, java.lang.String newNodeName, java.lang.String newNodeText)  
      static org.w3c.dom.Node[] addTextNodes​(org.w3c.dom.Node xmlParentNode, java.lang.String newNodesName, java.lang.String[] newNodesValue)  
      static org.w3c.dom.Node[] addTextNodes​(org.w3c.dom.Node xmlParentNode, java.lang.String xpathExpressionParentNode, java.lang.String newNodesName, java.lang.String[] newNodesValue)  
      protected static boolean checkCondition​(java.lang.String leftValue, java.lang.String rightValue, java.lang.String conditionOperator)  
      static boolean checkCondition​(org.w3c.dom.Node conditionNode, java.lang.String condition)  
      static boolean checkSingleCondition​(org.w3c.dom.Node conditionNode, java.lang.String condition)  
      static byte[] convertToUTFBytes​(byte[] content)
      Deprecated.
      Since 4.2.0, this method is no longer used and may be removed in the future.
      static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom, java.lang.String xpathExpressionFrom, org.w3c.dom.Node xmlNodeTo)  
      static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom, java.lang.String xpathExpressionFrom, org.w3c.dom.Node xmlNodeTo, java.lang.String xpathExpressionTo)  
      static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom, org.w3c.dom.Node xmlNodeTo)  
      static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom, org.w3c.dom.Node xmlNodeTo, java.lang.String xpathExpressionTo)  
      static PortalException createPortalException​(org.xml.sax.SAXException e)
      Creates a PortalException based on the given SAXException.
      static java.lang.String encodeXml​(java.lang.String pStr)
      Encodes a string to be used inside a XML document.
      static org.w3c.dom.Document getDocument​(java.io.File xmlFile)  
      static org.w3c.dom.Document getDocument​(java.io.InputStream inputStream)
      Parses a XML document from an input stream.
      static org.w3c.dom.Document getDocument​(java.lang.String xmlString)  
      static org.w3c.dom.Document getDocument​(HttpServletRequest request)  
      static org.w3c.dom.Document getDocument​(IFile xmlFile)
      Returns the Document associated with the given file.
      static org.w3c.dom.Document getDocumentFromFile​(java.lang.String filePath)  
      static org.w3c.dom.Node getOrCreateNode​(java.lang.String path, org.w3c.dom.Node parentNode)  
      static java.lang.String getTextContent​(org.w3c.dom.Node node)  
      static javax.xml.parsers.DocumentBuilder getXmlDocumentBuilder()  
      static org.w3c.dom.Document getXMLDocumentFromString​(java.lang.String xml)
      Deprecated.
      since 4.2.0 replaced by getDocument(String).
      static java.lang.String getXmlEncoding​(java.lang.String xmlString)
      Returns the XML encoding for the xml with the given String.
      static java.lang.String getXmlString​(org.w3c.dom.Document document)  
      static java.lang.String getXmlString​(org.w3c.dom.Element element)  
      static java.lang.String getXmlString​(org.w3c.dom.Node node)
      Returns the xml string representation for a node.
      static org.w3c.dom.Node importIntoNewDocument​(org.w3c.dom.Node originalNode)
      Clones a node, and adopts it into a new document.
      static void initializeDocumentBuilderPool​(int minSize, int maxSize, long timeBetweenEvictionRunsMillis, int numTestsPerEvictionRun, int minEvictableIdleTimeMillis)
      Internal use only.
      static boolean isValidXmlString​(java.lang.String xml)  
      static void mergeAttributes​(org.w3c.dom.Node destinationNode, org.w3c.dom.Node sourceNode, boolean replace)  
      static void mergeElements​(org.w3c.dom.Node destinationNode, org.w3c.dom.Node sourceNode, boolean replace)
      Merges the children elements from a node to another.
      static boolean nodeExists​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode)  
      static boolean readAttributeBoolean​(java.lang.String attributeName, org.w3c.dom.Node node)  
      static boolean readAttributeBoolean​(java.lang.String attributeName, org.w3c.dom.Node node, boolean valueForNull)  
      static int readAttributeInt​(java.lang.String attributeName, org.w3c.dom.Node node, int valueForNull)
      Returns the attribute integer value in the given node.
      static java.lang.String readAttributeOrNodeString​(org.w3c.dom.Node node, java.lang.String fieldName)  
      static java.lang.String readAttributeOrNodeString​(org.w3c.dom.Node node, java.lang.String fieldName, java.lang.String valueForNull)  
      static java.lang.String readAttributeString​(java.lang.String attributeName, org.w3c.dom.Node node)  
      static java.lang.String readAttributeString​(java.lang.String attributeName, org.w3c.dom.Node node, java.lang.String valueForNull)  
      static boolean readNodeBoolean​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode, boolean valueForNull)  
      static int readNodeInt​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode, int valueForNull)  
      static java.lang.Integer readNodeInteger​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode, java.lang.Integer valueForNull)  
      static java.lang.String[] readNodesString​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode)  
      static java.lang.String readNodeString​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode)  
      static java.lang.String readNodeString​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode, java.lang.String valueForNull)  
      static void replaceChildNode​(java.lang.String childNodeName, org.w3c.dom.Node node, java.lang.String xmlString)  
      static void replaceChildNode​(org.w3c.dom.Node parentNode, org.w3c.dom.Node newChildNode)  
      static org.w3c.dom.Node[] selectAncestorNodes​(java.lang.String ancestorNameQuery, org.w3c.dom.Node childNode)  
      static org.w3c.dom.Node[] selectNodes​(java.lang.String childNameQuery, org.w3c.dom.Node parentNode)  
      static org.w3c.dom.Node selectSingleAncestorNode​(java.lang.String ancestorNameQuery, org.w3c.dom.Node childNode)  
      static org.w3c.dom.Node selectSingleNode​(java.lang.String childNameQuery, org.w3c.dom.Node parentNode)  
      protected static org.w3c.dom.Node selectSingleNodeRecursiveByName​(java.lang.String nodeName, org.w3c.dom.Node parentNode)  
      static void setTextContent​(org.w3c.dom.Node parentNode, java.lang.String textValue)  
      static org.w3c.dom.Node writeNodeString​(java.lang.String xpathExpression, org.w3c.dom.Node sourceNode, java.lang.String nodeValue)  
      • Methods inherited from class java.lang.Object

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

      • XmlUtil

        public XmlUtil()
    • Method Detail

      • initializeDocumentBuilderPool

        public static void initializeDocumentBuilderPool​(int minSize,
                                                         int maxSize,
                                                         long timeBetweenEvictionRunsMillis,
                                                         int numTestsPerEvictionRun,
                                                         int minEvictableIdleTimeMillis)
        Internal use only.

        This method may be incompatible in future versions.

        Parameters:
        minSize - minimum pool size.
        maxSize - maximum pool size.
        timeBetweenEvictionRunsMillis - the number of milliseconds to sleep between runs of the idle object evictor thread. When non-positive, no idle object evictor thread will be run.
        numTestsPerEvictionRun - the number of objects to examine during each run of the idle object evictor thread (if any).
        minEvictableIdleTimeMillis - The minimum amount of time an object may sit idle in the pool before it is eligable for eviction by the idle object evictor (if any).
        Since:
        5.5.0
      • getDocument

        public static org.w3c.dom.Document getDocument​(IFile xmlFile)
                                                throws PortalException
        Returns the Document associated with the given file.
        Parameters:
        xmlFile - the XML file.
        Returns:
        the Document associated with the given file.
        Throws:
        PortalException
        Since:
        8.0.0
      • getDocument

        public static org.w3c.dom.Document getDocument​(HttpServletRequest request)
                                                throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getDocument

        public static org.w3c.dom.Document getDocument​(java.io.InputStream inputStream)
                                                throws org.xml.sax.SAXException,
                                                       java.io.IOException
        Parses a XML document from an input stream.

        This method will read the input stream's data, but will not close it.

        Parameters:
        inputStream - the input stream.
        Returns:
        the parsed XML document.
        Throws:
        org.xml.sax.SAXException - if any parse errors occur.
        java.io.IOException - if any I/O errors occur.
        java.lang.IllegalArgumentException - if inputStream is null.
        Since:
        8.0.0
      • createPortalException

        public static PortalException createPortalException​(org.xml.sax.SAXException e)
        Creates a PortalException based on the given SAXException. The PortalException created contains a message describing the error, according to the information in the SAXException.
        Parameters:
        e - the SAXException
        Returns:
        the PortalException created.
        Since:
        4.0.8
      • getXmlString

        public static java.lang.String getXmlString​(org.w3c.dom.Node node)
                                             throws PortalException
        Returns the xml string representation for a node.
        Parameters:
        node - the node. Must be of type Document, Element, Text or Comment.
        Returns:
        the xml string.
        Throws:
        PortalException
        Since:
        4.2.0
      • getXmlString

        public static java.lang.String getXmlString​(org.w3c.dom.Document document)
                                             throws java.lang.Exception
        Throws:
        java.lang.Exception
      • readNodeBoolean

        public static boolean readNodeBoolean​(java.lang.String xpathExpression,
                                              org.w3c.dom.Node sourceNode,
                                              boolean valueForNull)
                                       throws PortalException
        Throws:
        PortalException
      • readAttributeBoolean

        public static boolean readAttributeBoolean​(java.lang.String attributeName,
                                                   org.w3c.dom.Node node)
                                            throws PortalException
        Throws:
        PortalException
      • readAttributeBoolean

        public static boolean readAttributeBoolean​(java.lang.String attributeName,
                                                   org.w3c.dom.Node node,
                                                   boolean valueForNull)
                                            throws PortalException
        Throws:
        PortalException
      • readNodeInt

        public static int readNodeInt​(java.lang.String xpathExpression,
                                      org.w3c.dom.Node sourceNode,
                                      int valueForNull)
                               throws PortalException
        Throws:
        PortalException
      • readNodeInteger

        public static java.lang.Integer readNodeInteger​(java.lang.String xpathExpression,
                                                        org.w3c.dom.Node sourceNode,
                                                        java.lang.Integer valueForNull)
                                                 throws PortalException
        Throws:
        PortalException
      • readAttributeString

        public static java.lang.String readAttributeString​(java.lang.String attributeName,
                                                           org.w3c.dom.Node node)
                                                    throws PortalException
        Throws:
        PortalException
      • readAttributeInt

        public static int readAttributeInt​(java.lang.String attributeName,
                                           org.w3c.dom.Node node,
                                           int valueForNull)
                                    throws PortalException
        Returns the attribute integer value in the given node.
        Parameters:
        attributeName -
        node -
        valueForNull -
        Returns:
        Throws:
        PortalException
      • readAttributeString

        public static java.lang.String readAttributeString​(java.lang.String attributeName,
                                                           org.w3c.dom.Node node,
                                                           java.lang.String valueForNull)
                                                    throws PortalException
        Throws:
        PortalException
      • getTextContent

        public static java.lang.String getTextContent​(org.w3c.dom.Node node)
      • setTextContent

        public static void setTextContent​(org.w3c.dom.Node parentNode,
                                          java.lang.String textValue)
      • readNodeString

        public static java.lang.String readNodeString​(java.lang.String xpathExpression,
                                                      org.w3c.dom.Node sourceNode)
                                               throws PortalException
        Throws:
        PortalException
      • readNodeString

        public static java.lang.String readNodeString​(java.lang.String xpathExpression,
                                                      org.w3c.dom.Node sourceNode,
                                                      java.lang.String valueForNull)
                                               throws PortalException
        Throws:
        PortalException
      • readNodesString

        public static java.lang.String[] readNodesString​(java.lang.String xpathExpression,
                                                         org.w3c.dom.Node sourceNode)
                                                  throws PortalException
        Throws:
        PortalException
      • nodeExists

        public static boolean nodeExists​(java.lang.String xpathExpression,
                                         org.w3c.dom.Node sourceNode)
                                  throws PortalException
        Throws:
        PortalException
      • addTextNode

        public static org.w3c.dom.Element addTextNode​(org.w3c.dom.Node xmlParentNode,
                                                      java.lang.String newNodeName,
                                                      java.lang.String newNodeText)
                                               throws PortalException
        Throws:
        PortalException
      • addTextNode

        public static org.w3c.dom.Element addTextNode​(org.w3c.dom.Node xmlParentNode,
                                                      java.lang.String xpathExpressionParentNode,
                                                      java.lang.String newNodeName,
                                                      java.lang.String newNodeText)
                                               throws PortalException
        Throws:
        PortalException
      • addTextNodes

        public static org.w3c.dom.Node[] addTextNodes​(org.w3c.dom.Node xmlParentNode,
                                                      java.lang.String newNodesName,
                                                      java.lang.String[] newNodesValue)
                                               throws PortalException
        Throws:
        PortalException
      • addTextNodes

        public static org.w3c.dom.Node[] addTextNodes​(org.w3c.dom.Node xmlParentNode,
                                                      java.lang.String xpathExpressionParentNode,
                                                      java.lang.String newNodesName,
                                                      java.lang.String[] newNodesValue)
                                               throws java.lang.Exception
        Throws:
        java.lang.Exception
      • addNode

        public static org.w3c.dom.Node addNode​(org.w3c.dom.Node xmlParentNode,
                                               java.lang.String newNodeName)
      • addNode

        public static org.w3c.dom.Node addNode​(org.w3c.dom.Node xmlParentNode,
                                               java.lang.String xpathExpressionParentNode,
                                               java.lang.String newNodeName)
                                        throws PortalException
        Throws:
        PortalException
      • addNodeAttr

        public static org.w3c.dom.Attr addNodeAttr​(org.w3c.dom.Node xmlParentNode,
                                                   java.lang.String attributeName)
      • addNodeAttr

        public static org.w3c.dom.Attr addNodeAttr​(org.w3c.dom.Node xmlParentNode,
                                                   java.lang.String attributeName,
                                                   java.lang.String value)
      • copyNode

        public static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom,
                                                org.w3c.dom.Node xmlNodeTo)
      • copyNode

        public static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom,
                                                java.lang.String xpathExpressionFrom,
                                                org.w3c.dom.Node xmlNodeTo)
                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • copyNode

        public static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom,
                                                org.w3c.dom.Node xmlNodeTo,
                                                java.lang.String xpathExpressionTo)
                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • copyNode

        public static org.w3c.dom.Node copyNode​(org.w3c.dom.Node xmlNodeFrom,
                                                java.lang.String xpathExpressionFrom,
                                                org.w3c.dom.Node xmlNodeTo,
                                                java.lang.String xpathExpressionTo)
                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • getDocumentFromFile

        public static org.w3c.dom.Document getDocumentFromFile​(java.lang.String filePath)
                                                        throws java.lang.Exception
        Throws:
        java.lang.Exception
      • selectSingleAncestorNode

        public static org.w3c.dom.Node selectSingleAncestorNode​(java.lang.String ancestorNameQuery,
                                                                org.w3c.dom.Node childNode)
                                                         throws java.lang.Exception
        Throws:
        java.lang.Exception
      • selectAncestorNodes

        public static org.w3c.dom.Node[] selectAncestorNodes​(java.lang.String ancestorNameQuery,
                                                             org.w3c.dom.Node childNode)
                                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • importIntoNewDocument

        public static org.w3c.dom.Node importIntoNewDocument​(org.w3c.dom.Node originalNode)
                                                      throws PortalException
        Clones a node, and adopts it into a new document.
        Parameters:
        originalNode -
        Returns:
        Throws:
        PortalException
        Since:
        4.0.6
      • selectSingleNode

        public static org.w3c.dom.Node selectSingleNode​(java.lang.String childNameQuery,
                                                        org.w3c.dom.Node parentNode)
                                                 throws PortalException
        Throws:
        PortalException
      • selectNodes

        public static org.w3c.dom.Node[] selectNodes​(java.lang.String childNameQuery,
                                                     org.w3c.dom.Node parentNode)
                                              throws PortalException
        Throws:
        PortalException
      • checkCondition

        public static boolean checkCondition​(org.w3c.dom.Node conditionNode,
                                             java.lang.String condition)
                                      throws java.lang.Exception
        Throws:
        java.lang.Exception
      • checkSingleCondition

        public static boolean checkSingleCondition​(org.w3c.dom.Node conditionNode,
                                                   java.lang.String condition)
                                            throws java.lang.Exception
        Throws:
        java.lang.Exception
      • checkCondition

        protected static boolean checkCondition​(java.lang.String leftValue,
                                                java.lang.String rightValue,
                                                java.lang.String conditionOperator)
      • selectSingleNodeRecursiveByName

        protected static org.w3c.dom.Node selectSingleNodeRecursiveByName​(java.lang.String nodeName,
                                                                          org.w3c.dom.Node parentNode)
                                                                   throws java.lang.Exception
        Throws:
        java.lang.Exception
      • convertToUTFBytes

        @Deprecated
        public static byte[] convertToUTFBytes​(byte[] content)
        Deprecated.
        Since 4.2.0, this method is no longer used and may be removed in the future.
      • getOrCreateNode

        public static org.w3c.dom.Node getOrCreateNode​(java.lang.String path,
                                                       org.w3c.dom.Node parentNode)
                                                throws PortalException
        Throws:
        PortalException
      • writeNodeString

        public static org.w3c.dom.Node writeNodeString​(java.lang.String xpathExpression,
                                                       org.w3c.dom.Node sourceNode,
                                                       java.lang.String nodeValue)
                                                throws PortalException
        Throws:
        PortalException
      • readAttributeOrNodeString

        public static java.lang.String readAttributeOrNodeString​(org.w3c.dom.Node node,
                                                                 java.lang.String fieldName)
                                                          throws PortalException
        Throws:
        PortalException
      • readAttributeOrNodeString

        public static java.lang.String readAttributeOrNodeString​(org.w3c.dom.Node node,
                                                                 java.lang.String fieldName,
                                                                 java.lang.String valueForNull)
                                                          throws PortalException
        Throws:
        PortalException
      • replaceChildNode

        public static void replaceChildNode​(org.w3c.dom.Node parentNode,
                                            org.w3c.dom.Node newChildNode)
                                     throws java.lang.Exception
        Throws:
        java.lang.Exception
      • replaceChildNode

        public static void replaceChildNode​(java.lang.String childNodeName,
                                            org.w3c.dom.Node node,
                                            java.lang.String xmlString)
                                     throws PortalException
        Throws:
        PortalException
      • addNodeFromXmlString

        public static org.w3c.dom.Node addNodeFromXmlString​(org.w3c.dom.Node node,
                                                            java.lang.String xmlString)
                                                     throws PortalException
        Throws:
        PortalException
      • mergeAttributes

        public static void mergeAttributes​(org.w3c.dom.Node destinationNode,
                                           org.w3c.dom.Node sourceNode,
                                           boolean replace)
                                    throws PortalException
        Throws:
        PortalException
      • mergeElements

        public static void mergeElements​(org.w3c.dom.Node destinationNode,
                                         org.w3c.dom.Node sourceNode,
                                         boolean replace)
                                  throws PortalException
        Merges the children elements from a node to another.
        Parameters:
        destinationNode - the destination node, where the elements will be appended or replaced.
        sourceNode - the source node, where the elements will be copied from.
        replace - if true, elements with the same name will be replaced; if false they will not be copied if an element in the destinationNode with the same name already exists.
        Throws:
        PortalException
        Since:
        4.0.10
      • encodeXml

        public static java.lang.String encodeXml​(java.lang.String pStr)
        Encodes a string to be used inside a XML document. Special XML characters are converted to their corresponding entities. Characters that are invalid in XML (even as entity) are eliminated.
        Returns:
        the encoded string, or an empty string if pStr is null.
      • getXmlEncoding

        public static java.lang.String getXmlEncoding​(java.lang.String xmlString)
        Returns the XML encoding for the xml with the given String.

        The encoding is read from the XML declaration.

        Parameters:
        xmlString - the xml.
        Returns:
        the encoding or null if it was not found.
      • isValidXmlString

        public static boolean isValidXmlString​(java.lang.String xml)