Class XMLUtils


  • public class XMLUtils
    extends java.lang.Object
    This class provides additional utilities that can be applied to the XML.
    Since:
    MicroStrategy Web 7.3.1 or earlier
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static boolean allowDoctype()
      Indicates whether the doctype is allowed or not.
      static void appendResolvedSessionId​(java.lang.String sessionID, XMLBuilder builder)  
      static java.lang.String compareNodes​(org.w3c.dom.Node base, org.w3c.dom.Node other, boolean ignoreTextNodes)
      This method compares two Node instances (including all their attributes and children) and returns a string with the differences between them.
      static java.lang.String encodeXMLAttribute​(java.lang.String strToEncode)
      Replaces HTML unfriendly characters with their escaped equivalents.
      static java.lang.String escapeString​(java.lang.String s)
      Given a String containing markup, escape the markup so it can be put into xml as normal text.
      static java.lang.String formatXML​(java.lang.String xmlIn)
      This constructs a nicely formatted XML document, typically used for printing in a user readable format.
      static XMLUtils.EnumDoctypeAllowance getDoctypePolicy()  
      static java.lang.String getResolvedSessionId​(java.lang.String sessionID)
      Deprecated.
      static boolean isValidGUID​(java.lang.String str)
      Check whether the string is a valid GUID
      static boolean isValidNodeKey​(java.lang.String str)
      Check whether the string is a valid Nodekey, a loose check on whether it is consists of alphabet or digits.
      static boolean isValidXML​(java.lang.String str)
      Equivalent to parseValidateXML(String, DefaultHandler) with null for the DefaultHandler parameter.
      static java.lang.String loadXMLFile​(java.lang.String name)
      Load XML from a file.
      static javax.xml.parsers.DocumentBuilderFactory newDocumentBuilderFactory()
      Returns DocumentBuilderFactory with doctype disabled by default to avoid XXE attack.
      static javax.xml.parsers.DocumentBuilderFactory newDocumentBuilderFactory​(ParserParameters parameters)
      Instantiate an instance of DocumentBuilderFactory based on the given parameters.
      static XMLUtils newInstance()  
      static java.lang.String nodeValue​(org.w3c.dom.Node contextNode)
      This method returns the "naive" node value of a node, that is, the concatenation of node values from all its child nodes.
      static org.w3c.dom.Document parse​(java.io.File f, ParserParameters parameters)  
      static org.w3c.dom.Document parse​(java.io.InputStream stream, ParserParameters parameters)  
      static org.w3c.dom.Document parse​(java.lang.String xmlString, ParserParameters parameters)  
      static org.w3c.dom.Document parse​(org.xml.sax.InputSource source, ParserParameters parameters)  
      static org.w3c.dom.Document parseFile​(java.lang.String uri)  
      static boolean parseValidateXML​(java.lang.String str, org.xml.sax.helpers.DefaultHandler handler)
      Returns whether the given string is valid XML.
      static java.lang.String processElement​(int level, org.w3c.dom.Node objXmlElem)
      ProcessElement method returns a string with tabs and newlines inserted in the appropriate spots.
      void transform​(org.w3c.dom.Node xmlDoc, javax.xml.transform.Result outputTarget)
      Transform the XML source to a Result.
      void transform​(org.w3c.dom.Node xmlDoc, javax.xml.transform.Result outputTarget, TransformerParameters transformerParameters)
      Transform the XML source to a Result.
      void transform​(org.w3c.dom.Node xmlDoc, org.w3c.dom.Node xslDoc, javax.xml.transform.Result outputTarget)
      Transform the XML source to a Result.
      void transform​(org.w3c.dom.Node xmlDoc, org.w3c.dom.Node xslDoc, javax.xml.transform.Result outputTarget, TransformerParameters transformerParameters)
      Transform the XML source to a Result, the transformer behavior can be configured through the transformerParameters parameter.
      • Methods inherited from class java.lang.Object

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

      • FEATURE_DISALLOW_DOCTYPE_DECL

        public static final java.lang.String FEATURE_DISALLOW_DOCTYPE_DECL
        See Also:
        Constant Field Values
      • FEATURE_EXTERNAL_GENERAL_ENTITIES

        public static final java.lang.String FEATURE_EXTERNAL_GENERAL_ENTITIES
        See Also:
        Constant Field Values
      • FEATURE_EXTERNAL_PARAMETER_ENTITIES

        public static final java.lang.String FEATURE_EXTERNAL_PARAMETER_ENTITIES
        See Also:
        Constant Field Values
      • FEATURE_LOAD_EXTERNAL_DTD

        public static final java.lang.String FEATURE_LOAD_EXTERNAL_DTD
        See Also:
        Constant Field Values
    • Method Detail

      • newInstance

        public static XMLUtils newInstance()
      • allowDoctype

        public static boolean allowDoctype()
        Indicates whether the doctype is allowed or not.
      • newDocumentBuilderFactory

        public static javax.xml.parsers.DocumentBuilderFactory newDocumentBuilderFactory()
        Returns DocumentBuilderFactory with doctype disabled by default to avoid XXE attack.
        Returns:
        DocumentBuilderFactory
      • newDocumentBuilderFactory

        public static javax.xml.parsers.DocumentBuilderFactory newDocumentBuilderFactory​(ParserParameters parameters)
        Instantiate an instance of DocumentBuilderFactory based on the given parameters.
        Parameters:
        parameters - The ParserParameters class with the parameter values
        Returns:
        The DocumentBuilderFactory instance.
      • parseFile

        public static org.w3c.dom.Document parseFile​(java.lang.String uri)
                                              throws org.xml.sax.SAXException,
                                                     java.io.IOException,
                                                     javax.xml.parsers.ParserConfigurationException
        Throws:
        org.xml.sax.SAXException
        java.io.IOException
        javax.xml.parsers.ParserConfigurationException
      • transform

        public void transform​(org.w3c.dom.Node xmlDoc,
                              javax.xml.transform.Result outputTarget)
                       throws javax.xml.transform.TransformerConfigurationException,
                              javax.xml.transform.TransformerException
        Transform the XML source to a Result.
        Parameters:
        xmlDoc - The XML document to transform
        outputTarget - The Result of transforming the source.
        Throws:
        javax.xml.transform.TransformerConfigurationException
        javax.xml.transform.TransformerException
      • transform

        public void transform​(org.w3c.dom.Node xmlDoc,
                              javax.xml.transform.Result outputTarget,
                              TransformerParameters transformerParameters)
                       throws javax.xml.transform.TransformerConfigurationException,
                              javax.xml.transform.TransformerException
        Transform the XML source to a Result.
        Parameters:
        xmlDoc - The XML document to transform
        outputTarget - The Result of transforming the source.
        transformerParameters - The transformer parameters
        Throws:
        javax.xml.transform.TransformerConfigurationException
        javax.xml.transform.TransformerException
      • transform

        public void transform​(org.w3c.dom.Node xmlDoc,
                              org.w3c.dom.Node xslDoc,
                              javax.xml.transform.Result outputTarget)
                       throws javax.xml.transform.TransformerConfigurationException,
                              javax.xml.transform.TransformerException
        Transform the XML source to a Result.
        Parameters:
        xmlDoc - The XML document to transform
        xslDoc - The XSLT document used to create Transformer
        outputTarget - The Result of transforming the source.
        Throws:
        javax.xml.transform.TransformerConfigurationException
        javax.xml.transform.TransformerException
      • transform

        public void transform​(org.w3c.dom.Node xmlDoc,
                              org.w3c.dom.Node xslDoc,
                              javax.xml.transform.Result outputTarget,
                              TransformerParameters transformerParameters)
                       throws javax.xml.transform.TransformerConfigurationException,
                              javax.xml.transform.TransformerException
        Transform the XML source to a Result, the transformer behavior can be configured through the transformerParameters parameter.
        Parameters:
        xmlDoc - The XML document to transform
        xslDoc - The XSLT document used to create Transformer
        outputTarget - The Result of transforming the source.
        transformerParameters - The transformer parameters
        Throws:
        javax.xml.transform.TransformerConfigurationException
        javax.xml.transform.TransformerException
      • encodeXMLAttribute

        public static java.lang.String encodeXMLAttribute​(java.lang.String strToEncode)
        Replaces HTML unfriendly characters with their escaped equivalents. Used to store XML string inside hidden input fields or other HTML attribute
        Parameters:
        strToEncode - the source string to encode.
        Returns:
        The encoded string.
      • processElement

        public static java.lang.String processElement​(int level,
                                                      org.w3c.dom.Node objXmlElem)
                                               throws XMLSupportException
        ProcessElement method returns a string with tabs and newlines inserted in the appropriate spots.
        Parameters:
        level - int, level from which we want to process
        objXmlElem - Node, node we want to process
        Returns:
        The String, with adequate format
        Throws:
        XMLSupportException - Signals an error when parsing the XML.
      • escapeString

        public static java.lang.String escapeString​(java.lang.String s)
        Given a String containing markup, escape the markup so it can be put into xml as normal text.
        Parameters:
        s - String to escape
        Returns:
        The escaped string.
      • formatXML

        public static java.lang.String formatXML​(java.lang.String xmlIn)
                                          throws XMLSupportException

        This constructs a nicely formatted XML document, typically used for printing in a user readable format.

        Parameters:
        xmlIn - The xml string to be formatted.
        Returns:
        The nicely formatted XML String.
        Throws:
        XMLSupportException - Signals an error when format the XML.
      • loadXMLFile

        public static java.lang.String loadXMLFile​(java.lang.String name)
                                            throws java.io.IOException
        Load XML from a file.
        Parameters:
        name - The path to the file.
        Returns:
        The XML string in the file.
        Throws:
        java.io.IOException - When error occurs during retrieving the XML from the file.
        Since:
        MicroStrategy Web 8.0.0
      • compareNodes

        public static java.lang.String compareNodes​(org.w3c.dom.Node base,
                                                    org.w3c.dom.Node other,
                                                    boolean ignoreTextNodes)
        This method compares two Node instances (including all their attributes and children) and returns a string with the differences between them. If the nodes are equal, it returns null.
        Parameters:
        base - The base node to compare.
        other - The other node to compare.
        ignoreTextNodes - if true, this method only compare Element child nodes, that is, all text nodes are ignored.
        Returns:
        A description of the nodes differences; null if equal.
        Since:
        MicroStrategy Web 9.0.0
      • nodeValue

        public static java.lang.String nodeValue​(org.w3c.dom.Node contextNode)
        This method returns the "naive" node value of a node, that is, the concatenation of node values from all its child nodes.
        Since:
        MicroStrategy Web 9.0.0
      • parseValidateXML

        public static boolean parseValidateXML​(java.lang.String str,
                                               org.xml.sax.helpers.DefaultHandler handler)
        Returns whether the given string is valid XML. The supplied parser is also used to parse the string.
        Parameters:
        str - string to check
        handler - handler attempt parsing with, if null, an empty DefaultHandler is used
        Returns:
        whether string is XML, false if any exception occurs during parsing
        Since:
        MicroStrategy Web 9.0.0
      • isValidXML

        public static boolean isValidXML​(java.lang.String str)
        Equivalent to parseValidateXML(String, DefaultHandler) with null for the DefaultHandler parameter.
        Parameters:
        str - string to check
        Returns:
        whether the string is XML
        Since:
        MicroStrategy Web 9.0.0
      • isValidGUID

        public static boolean isValidGUID​(java.lang.String str)
        Check whether the string is a valid GUID
        Parameters:
        str - string to check
        Returns:
        whether the string is a valid GUID
      • isValidNodeKey

        public static boolean isValidNodeKey​(java.lang.String str)
        Check whether the string is a valid Nodekey, a loose check on whether it is consists of alphabet or digits.
        Parameters:
        str - string to check
        Returns:
        whether the string is a valid Nodekey
      • appendResolvedSessionId

        public static void appendResolvedSessionId​(java.lang.String sessionID,
                                                   XMLBuilder builder)
        Parameters:
        sessionID -
        builder -
      • getResolvedSessionId

        @Deprecated
        public static java.lang.String getResolvedSessionId​(java.lang.String sessionID)
        Deprecated.