Class XmlCompressor

  • All Implemented Interfaces:
    Compressor

    public class XmlCompressor
    extends java.lang.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
    • Constructor Summary

      Constructors 
      ConstructorDescription
      XmlCompressor() 
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      java.lang.Stringcompress​(java.lang.String xml)
      The main method that compresses given XML source and returns compressed result.
      booleanisEnabled()
      Returns true if compression is enabled.
      booleanisRemoveComments()
      Returns true if all XML comments will be removed.
      booleanisRemoveIntertagSpaces()
      Returns true if all inter-tag whitespace characters will be removed.
      protected java.lang.StringpreserveBlocks​(java.lang.String xml, java.util.List<java.lang.String> cdataBlocks) 
      protected java.lang.StringprocessXml​(java.lang.String xml) 
      protected java.lang.StringremoveComments​(java.lang.String xml) 
      protected java.lang.StringremoveIntertagSpaces​(java.lang.String xml) 
      protected java.lang.StringremoveSpacesInsideTags​(java.lang.String xml) 
      protected java.lang.StringreturnBlocks​(java.lang.String xml, java.util.List<java.lang.String> cdataBlocks) 
      voidsetEnabled​(boolean enabled)
      If set to false all compression will be bypassed.
      voidsetRemoveComments​(boolean removeComments)
      If set to true all XML comments will be removed.
      voidsetRemoveIntertagSpaces​(boolean removeIntertagSpaces)
      If set to true all inter-tag whitespace characters will be removed.
      • Methods inherited from class java.lang.Object

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

      • cdataPattern

        protected static final java.util.regex.Pattern cdataPattern
      • commentPattern

        protected static final java.util.regex.Pattern commentPattern
      • intertagPattern

        protected static final java.util.regex.Pattern intertagPattern
      • tagEndSpacePattern

        protected static final java.util.regex.Pattern tagEndSpacePattern
      • multispacePattern

        protected static final java.util.regex.Pattern multispacePattern
      • tagPropertyPattern

        protected static final java.util.regex.Pattern tagPropertyPattern
      • tempCdataPattern

        protected static final java.util.regex.Pattern tempCdataPattern
    • Constructor Detail

      • XmlCompressor

        public XmlCompressor()
    • Method Detail

      • compress

        public java.lang.String compress​(java.lang.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 java.lang.String preserveBlocks​(java.lang.String xml,
                                                  java.util.List<java.lang.String> cdataBlocks)
      • returnBlocks

        protected java.lang.String returnBlocks​(java.lang.String xml,
                                                java.util.List<java.lang.String> cdataBlocks)
      • processXml

        protected java.lang.String processXml​(java.lang.String xml)
      • removeSpacesInsideTags

        protected java.lang.String removeSpacesInsideTags​(java.lang.String xml)
      • removeIntertagSpaces

        protected java.lang.String removeIntertagSpaces​(java.lang.String xml)
      • removeComments

        protected java.lang.String removeComments​(java.lang.String xml)
      • 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