Class XMLTools


  • public final class XMLTools
    extends Object
    A utility class for working with XML.
    Author:
    Curtis Rueden ctrueden at wisc.edu, Chris Allan callan at blackcat.ca, Melissa Linkert melissa at glencoesoftware.com
    • Method Detail

      • dumpXML

        public static String dumpXML​(String schemaLocation,
                                     Document doc,
                                     Element r)
        Dumps the given OME-XML DOM tree to a string.
        Parameters:
        schemaLocation - if null, no xmlns attribute will be added.
        doc - the Document representing the XML to be written
        r - the Element to use for adding the schema location
        Returns:
        OME-XML as a string.
      • dumpXML

        public static String dumpXML​(String schemaLocation,
                                     Document doc,
                                     Element r,
                                     boolean includeXMLDeclaration)
        Dumps the given OME-XML DOM tree to a string.
        Parameters:
        schemaLocation - if null, no xmlns attribute will be added.
        doc - the Document representing the XML to be written
        r - the Element to use for adding the schema location
        includeXMLDeclaration - false if the XML declaration should be omitted, see OutputKeys.OMIT_XML_DECLARATION
        Returns:
        OME-XML as a string.
      • escapeXML

        public static String escapeXML​(String s)
        Escape special characters. Replaces quotes, ampersands, and angle brackets with the escaped equivalent.
        Parameters:
        s - input string containing characters to escape
        Returns:
        copy of the input string with characters escaped as described above
      • sanitizeXML

        public static String sanitizeXML​(String s)
        Remove invalid characters from an XML string.
        Parameters:
        s - the input string from which to remove invalid characters
        Returns:
        a copy of the input string with CR, LF, tab, and control characters replaced by a single space
      • indentXML

        public static String indentXML​(String xml)
        Indents XML to be more readable. Uses 3 spaces to indent, and may indent CDATA.
        Parameters:
        xml - the XML string to format
        Returns:
        the formatted XML string
        See Also:
        indentXML(String, int, boolean)
      • indentXML

        public static String indentXML​(String xml,
                                       int spacing)
        Indents XML by the given spacing to be more readable. CDATA may be indented.
        Parameters:
        xml - the XML string to format
        spacing - the number of spaces by which to indent
        Returns:
        the formatted XML string
        See Also:
        indentXML(String, int, boolean)
      • indentXML

        public static String indentXML​(String xml,
                                       boolean preserveCData)
        Indents XML to be more readable, avoiding any whitespace injection into CDATA if the preserveCData flag is set. Uses 3 spaces to indent.
        Parameters:
        xml - the XML string to format
        preserveCData - true if CDATA nodes should be preserved with no indenting
        Returns:
        the formatted XML string
        See Also:
        indentXML(String, int, boolean)
      • indentXML

        public static String indentXML​(String xml,
                                       int spacing,
                                       boolean preserveCData)
        Indents XML by the given spacing to be more readable, avoiding any whitespace injection into CDATA if the preserveCData flag is set.
        Parameters:
        xml - the XML string to format
        spacing - the number of spaces by which to indent
        preserveCData - true if CDATA nodes should be preserved with no indenting
        Returns:
        the formatted XML string
      • parseXML

        public static Hashtable<String,​String> parseXML​(String xml)
                                                       throws IOException
        Parses the given XML string into a list of key/value pairs.
        Parameters:
        xml - the InputStream representing the XML
        Returns:
        a hashtable of key/value pairs representing the XML
        Throws:
        IOException - if there is an error during parsing
        See Also:
        MetadataHandler
      • parseXML

        public static void parseXML​(String xml,
                                    DefaultHandler handler)
                             throws IOException
        Parses the given XML string using the specified XML handler.
        Parameters:
        xml - the string representing the XML
        handler - the DefaultHandler to use for parsing
        Throws:
        IOException - if there is an error during parsing
      • parseXML

        public static void parseXML​(byte[] xml,
                                    DefaultHandler handler)
                             throws IOException
        Parses the XML contained in the given byte array into using the specified XML handler.
        Parameters:
        xml - the byte array representing the XML
        handler - the DefaultHandler to use for parsing
        Throws:
        IOException - if there is an error during parsing
      • getStylesheet

        public static Templates getStylesheet​(String resourcePath,
                                              Class<?> sourceClass)
        Gets an XSLT template from the given resource location.
        Parameters:
        resourcePath - the name of the stylesheet resource
        sourceClass - the class to use when searching for the resource
        Returns:
        a Templates object representing the stylesheet
      • avoidUndeclaredNamespaces

        public static String avoidUndeclaredNamespaces​(String xml)
        Replaces NS:tag with NS_tag for undeclared namespaces
        Parameters:
        xml - the XML string whose namespaces need to be replaced
        Returns:
        a copy of the input string with NS:tag replaced by NS_tag for any undeclared namespaces
      • transformXML

        public static String transformXML​(String xml,
                                          Templates xslt)
                                   throws IOException
        Transforms the given XML string using the specified XSLT stylesheet.
        Parameters:
        xml - the XML string to be transformed
        xslt - the Templates object representing an XSLT stylesheet
        Returns:
        the XML string that results from applying the stylesheet
        Throws:
        IOException - if there is an error parsing the XML
        See Also:
        getStylesheet(String, Class)
      • transformXML

        public static String transformXML​(Source xmlSource,
                                          Templates xslt)
                                   throws IOException
        Transforms the given XML data using the specified XSLT stylesheet.
        Parameters:
        xmlSource - the Source object representing the XML to be transformed
        xslt - the Templates object representing an XSLT stylesheet
        Returns:
        the XML string that results from applying the stylesheet
        Throws:
        IOException - if there is an error parsing the XML
        See Also:
        getStylesheet(String, Class)
      • validateXML

        public static boolean validateXML​(String xml)
        Attempts to validate the given XML string using Java's XML validation facility.
        Parameters:
        xml - The XML string to validate.
        Returns:
        whether or not validation was successful.
      • validateXML

        public static boolean validateXML​(String xml,
                                          String label)
        Attempts to validate the given XML string using Java's XML validation facility.
        Parameters:
        xml - The XML string to validate.
        label - String describing the type of XML being validated.
        Returns:
        whether or not validation was successful.
      • validateXML

        public static boolean validateXML​(String xml,
                                          String label,
                                          XMLTools.SchemaReader schemaReader)
        Attempts to validate the given XML string using Java's XML validation facility.
        Parameters:
        xml - The XML string to validate.
        label - String describing the type of XML being validated.
        schemaReader - turns schema system IDs into input streams, may be null
        Returns:
        whether or not validation was successful.