Class XmlCompressor
- java.lang.Object
-
- com.googlecode.htmlcompressor.compressor.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 Summary
Fields Modifier and Type Field Description protected static PatterncdataPatternThe Constant cdataPattern.protected static PatterncommentPatternThe Constant commentPattern.protected static PatternintertagPatternThe Constant intertagPattern.protected static PatternmultispacePatternThe Constant multispacePattern.protected static PatterntagEndSpacePatternThe Constant tagEndSpacePattern.protected static PatterntagPropertyPatternThe Constant tagPropertyPattern.protected static StringTEMP_CD_DATA_BLOCKThe Constant tempCdataBlock.protected static PatterntempCdataPatternThe Constant tempCdataPattern.
-
Constructor Summary
Constructors Constructor Description XmlCompressor()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Stringcompress(String xml)The main method that compresses given XML source and returns compressed result.booleanisEnabled()Returnstrueif compression is enabled.booleanisRemoveComments()Returnstrueif all XML comments will be removed.booleanisRemoveIntertagSpaces()Returnstrueif all inter-tag whitespace characters will be removed.protected StringpreserveBlocks(String xml, List<String> cdataBlocks)Preserve blocks.protected StringprocessXml(String xml)Process xml.protected StringremoveComments(String xml)Removes the comments.protected StringremoveIntertagSpaces(String xml)Removes the intertag spaces.protected StringremoveSpacesInsideTags(String xml)Removes the spaces inside tags.protected StringreturnBlocks(String xml, List<String> cdataBlocks)Return blocks.voidsetEnabled(boolean enabled)If set tofalseall compression will be bypassed.voidsetRemoveComments(boolean removeComments)If set totrueall XML comments will be removed.voidsetRemoveIntertagSpaces(boolean removeIntertagSpaces)If set totrueall inter-tag whitespace characters will be removed.
-
-
-
Field Detail
-
TEMP_CD_DATA_BLOCK
protected static final String TEMP_CD_DATA_BLOCK
The Constant tempCdataBlock.- See Also:
- Constant Field Values
-
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.
-
-
Method Detail
-
compress
public String compress(String xml)
The main method that compresses given XML source and returns compressed result.- Specified by:
compressin interfaceCompressor- Parameters:
xml- XML content to compress- Returns:
- compressed content.
-
preserveBlocks
protected String preserveBlocks(String xml, List<String> cdataBlocks)
Preserve blocks.- Parameters:
xml- the xmlcdataBlocks- the cdata blocks- Returns:
- the string
-
returnBlocks
protected String returnBlocks(String xml, List<String> cdataBlocks)
Return blocks.- Parameters:
xml- the xmlcdataBlocks- 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()
Returnstrueif compression is enabled.- Returns:
trueif compression is enabled.
-
setEnabled
public void setEnabled(boolean enabled)
If set tofalseall compression will be bypassed. Might be useful for testing purposes. Default istrue.- Parameters:
enabled- setfalseto bypass all compression
-
isRemoveComments
public boolean isRemoveComments()
Returnstrueif all XML comments will be removed.- Returns:
trueif all XML comments will be removed
-
setRemoveComments
public void setRemoveComments(boolean removeComments)
If set totrueall XML comments will be removed. Default istrue.- Parameters:
removeComments- settrueto remove all XML comments
-
isRemoveIntertagSpaces
public boolean isRemoveIntertagSpaces()
Returnstrueif all inter-tag whitespace characters will be removed.- Returns:
trueif all inter-tag whitespace characters will be removed.
-
setRemoveIntertagSpaces
public void setRemoveIntertagSpaces(boolean removeIntertagSpaces)
If set totrueall inter-tag whitespace characters will be removed. Default istrue.- Parameters:
removeIntertagSpaces- settrueto remove all inter-tag whitespace characters
-
-