Class XmlCompressor

  • All Implemented Interfaces:
    Compressor

    public class XmlCompressor
    extends Object
    implements Compressor
    Class that compresses given XML source by removing comments, extra spaces and line breaks while preserving content within CDATA blocks.
    Author:
    Sergiy Kovalchuk
    • Field Detail

      • cdataPattern

        protected static final Pattern cdataPattern
        The Constant cdataPattern.
      • commentPattern

        protected static final Pattern commentPattern
        The Constant commentPattern.
      • intertagPattern

        protected static final Pattern intertagPattern
        The Constant intertagPattern.
      • tagEndSpacePattern

        protected static final Pattern tagEndSpacePattern
        The Constant tagEndSpacePattern.
      • multispacePattern

        protected static final Pattern multispacePattern
        The Constant multispacePattern.
      • tagPropertyPattern

        protected static final Pattern tagPropertyPattern
        The Constant tagPropertyPattern.
      • tempCdataPattern

        protected static final Pattern tempCdataPattern
        The Constant tempCdataPattern.
    • Constructor Detail

      • XmlCompressor

        public XmlCompressor()
    • Method Detail

      • compress

        public String compress​(String xml)
        The main method that compresses given XML source and returns compressed result.
        Specified by:
        compress in interface Compressor
        Parameters:
        xml - XML content to compress
        Returns:
        compressed content.
      • preserveBlocks

        protected String preserveBlocks​(String xml,
                                        List<String> cdataBlocks)
        Preserve blocks.
        Parameters:
        xml - the xml
        cdataBlocks - the cdata blocks
        Returns:
        the string
      • returnBlocks

        protected String returnBlocks​(String xml,
                                      List<String> cdataBlocks)
        Return blocks.
        Parameters:
        xml - the xml
        cdataBlocks - the cdata blocks
        Returns:
        the string
      • processXml

        protected String processXml​(String xml)
        Process xml.
        Parameters:
        xml - the xml
        Returns:
        the string
      • removeSpacesInsideTags

        protected String removeSpacesInsideTags​(String xml)
        Removes the spaces inside tags.
        Parameters:
        xml - the xml
        Returns:
        the string
      • removeIntertagSpaces

        protected String removeIntertagSpaces​(String xml)
        Removes the intertag spaces.
        Parameters:
        xml - the xml
        Returns:
        the string
      • removeComments

        protected String removeComments​(String xml)
        Removes the comments.
        Parameters:
        xml - the xml
        Returns:
        the string
      • isEnabled

        public boolean isEnabled()
        Returns true if compression is enabled.
        Returns:
        true if compression is enabled.
      • setEnabled

        public void setEnabled​(boolean enabled)
        If set to false all compression will be bypassed. Might be useful for testing purposes. Default is true.
        Parameters:
        enabled - set false to bypass all compression
      • isRemoveComments

        public boolean isRemoveComments()
        Returns true if all XML comments will be removed.
        Returns:
        true if all XML comments will be removed
      • setRemoveComments

        public void setRemoveComments​(boolean removeComments)
        If set to true all XML comments will be removed. Default is true.
        Parameters:
        removeComments - set true to remove all XML comments
      • isRemoveIntertagSpaces

        public boolean isRemoveIntertagSpaces()
        Returns true if all inter-tag whitespace characters will be removed.
        Returns:
        true if all inter-tag whitespace characters will be removed.
      • setRemoveIntertagSpaces

        public void setRemoveIntertagSpaces​(boolean removeIntertagSpaces)
        If set to true all inter-tag whitespace characters will be removed. Default is true.
        Parameters:
        removeIntertagSpaces - set true to remove all inter-tag whitespace characters