/htmlcompressor/src/main/java/com/googlecode/htmlcompressor/analyzer/HtmlAnalyzer.java

https://github.com/pfugiel/dm-proxy2 · Java · 249 lines · 163 code · 46 blank · 40 comment · 2 complexity · e632a95e39eb903e5122774bef7a912e MD5 · raw file

  1. package com.googlecode.htmlcompressor.analyzer;
  2. /*
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import java.text.NumberFormat;
  16. import java.util.Formatter;
  17. import com.googlecode.htmlcompressor.compressor.ClosureJavaScriptCompressor;
  18. import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
  19. /**
  20. * Class that compresses provided source with different compression
  21. * settings and displays page size gains in a report
  22. *
  23. * @author <a href="mailto:serg472@gmail.com">Sergiy Kovalchuk</a>
  24. */
  25. public class HtmlAnalyzer {
  26. private String jsCompressor = HtmlCompressor.JS_COMPRESSOR_YUI;
  27. public HtmlAnalyzer() {
  28. }
  29. public HtmlAnalyzer(String jsCompressor) {
  30. this.jsCompressor = jsCompressor;
  31. }
  32. public void analyze(String source) {
  33. int originalSize = source.length();
  34. HtmlCompressor compressor = getCleanCompressor();
  35. String compResult = compressor.compress(source);
  36. printHeader();
  37. System.out.println(formatLine("Compression disabled", originalSize, originalSize, originalSize));
  38. int prevSize = originalSize;
  39. //spaces inside tags
  40. System.out.println(formatLine("All settings disabled", originalSize, compResult.length(), prevSize));
  41. prevSize = compResult.length();
  42. //remove comments
  43. compressor.setRemoveComments(true);
  44. compResult = compressor.compress(source);
  45. System.out.println(formatLine("Comments removed", originalSize, compResult.length(), prevSize));
  46. prevSize = compResult.length();
  47. //remove mulispaces
  48. compressor.setRemoveMultiSpaces(true);
  49. compResult = compressor.compress(source);
  50. System.out.println(formatLine("Multiple spaces removed", originalSize, compResult.length(), prevSize));
  51. prevSize = compResult.length();
  52. //remove intertag spaces
  53. compressor.setRemoveIntertagSpaces(true);
  54. compResult = compressor.compress(source);
  55. System.out.println(formatLine("No spaces between tags", originalSize, compResult.length(), prevSize));
  56. prevSize = compResult.length();
  57. //remove min surrounding spaces
  58. compressor.setRemoveSurroundingSpaces(HtmlCompressor.BLOCK_TAGS_MIN);
  59. compResult = compressor.compress(source);
  60. System.out.println(formatLine("No surround spaces (min)", originalSize, compResult.length(), prevSize));
  61. prevSize = compResult.length();
  62. //remove max surrounding spaces
  63. compressor.setRemoveSurroundingSpaces(HtmlCompressor.BLOCK_TAGS_MAX);
  64. compResult = compressor.compress(source);
  65. System.out.println(formatLine("No surround spaces (max)", originalSize, compResult.length(), prevSize));
  66. prevSize = compResult.length();
  67. //remove all surrounding spaces
  68. compressor.setRemoveSurroundingSpaces(HtmlCompressor.ALL_TAGS);
  69. compResult = compressor.compress(source);
  70. System.out.println(formatLine("No surround spaces (all)", originalSize, compResult.length(), prevSize));
  71. prevSize = compResult.length();
  72. //remove quotes
  73. compressor.setRemoveQuotes(true);
  74. compResult = compressor.compress(source);
  75. System.out.println(formatLine("Quotes removed from tags", originalSize, compResult.length(), prevSize));
  76. prevSize = compResult.length();
  77. //link attrib
  78. compressor.setRemoveLinkAttributes(true);
  79. compResult = compressor.compress(source);
  80. System.out.println(formatLine("<link> attr. removed", originalSize, compResult.length(), prevSize));
  81. prevSize = compResult.length();
  82. //style attrib
  83. compressor.setRemoveStyleAttributes(true);
  84. compResult = compressor.compress(source);
  85. System.out.println(formatLine("<style> attr. removed", originalSize, compResult.length(), prevSize));
  86. prevSize = compResult.length();
  87. //script attrib
  88. compressor.setRemoveScriptAttributes(true);
  89. compResult = compressor.compress(source);
  90. System.out.println(formatLine("<script> attr. removed", originalSize, compResult.length(), prevSize));
  91. prevSize = compResult.length();
  92. //form attrib
  93. compressor.setRemoveFormAttributes(true);
  94. compResult = compressor.compress(source);
  95. System.out.println(formatLine("<form> attr. removed", originalSize, compResult.length(), prevSize));
  96. prevSize = compResult.length();
  97. //input attrib
  98. compressor.setRemoveInputAttributes(true);
  99. compResult = compressor.compress(source);
  100. System.out.println(formatLine("<input> attr. removed", originalSize, compResult.length(), prevSize));
  101. prevSize = compResult.length();
  102. //simple bool
  103. compressor.setSimpleBooleanAttributes(true);
  104. compResult = compressor.compress(source);
  105. System.out.println(formatLine("Simple boolean attributes", originalSize, compResult.length(), prevSize));
  106. prevSize = compResult.length();
  107. //simple doctype
  108. compressor.setSimpleDoctype(true);
  109. compResult = compressor.compress(source);
  110. System.out.println(formatLine("Simple doctype", originalSize, compResult.length(), prevSize));
  111. prevSize = compResult.length();
  112. //js protocol
  113. compressor.setRemoveJavaScriptProtocol(true);
  114. compResult = compressor.compress(source);
  115. System.out.println(formatLine("Remove js pseudo-protocol", originalSize, compResult.length(), prevSize));
  116. prevSize = compResult.length();
  117. //http protocol
  118. compressor.setRemoveHttpProtocol(true);
  119. compResult = compressor.compress(source);
  120. System.out.println(formatLine("Remove http protocol", originalSize, compResult.length(), prevSize));
  121. prevSize = compResult.length();
  122. //https protocol
  123. compressor.setRemoveHttpsProtocol(true);
  124. compResult = compressor.compress(source);
  125. System.out.println(formatLine("Remove https protocol", originalSize, compResult.length(), prevSize));
  126. prevSize = compResult.length();
  127. //inline css
  128. try {
  129. compressor.setCompressCss(true);
  130. compResult = compressor.compress(source);
  131. System.out.println(formatLine("Compress inline CSS (YUI)", originalSize, compResult.length(), prevSize));
  132. prevSize = compResult.length();
  133. } catch (NoClassDefFoundError e){
  134. System.out.println(formatEmptyLine("Compress inline CSS (YUI)"));
  135. }
  136. if(jsCompressor.equals(HtmlCompressor.JS_COMPRESSOR_YUI)) {
  137. //inline js yui
  138. try {
  139. compressor.setCompressJavaScript(true);
  140. compResult = compressor.compress(source);
  141. System.out.println(formatLine("Compress inline JS (YUI)", originalSize, compResult.length(), prevSize));
  142. prevSize = compResult.length();
  143. } catch (NoClassDefFoundError e){
  144. System.out.println(formatEmptyLine("Compress inline JS (YUI)"));
  145. }
  146. } else {
  147. //inline js yui
  148. try {
  149. compressor.setCompressJavaScript(true);
  150. compressor.setJavaScriptCompressor(new ClosureJavaScriptCompressor());
  151. compResult = compressor.compress(source);
  152. System.out.println(formatLine("Compress JS (Closure)", originalSize, compResult.length(), prevSize));
  153. prevSize = compResult.length();
  154. } catch (NoClassDefFoundError e){
  155. System.out.println(formatEmptyLine("Compress JS (Closure)"));
  156. }
  157. }
  158. printFooter();
  159. }
  160. private HtmlCompressor getCleanCompressor() {
  161. HtmlCompressor compressor = new HtmlCompressor();
  162. compressor.setRemoveComments(false);
  163. compressor.setRemoveMultiSpaces(false);
  164. return compressor;
  165. }
  166. private String formatLine(String descr, int originalSize, int compressedSize, int prevSize) {
  167. Formatter fmt = new Formatter();
  168. fmt.format("%-25s | %16s | %16s | %12s |", descr, formatDecrease(prevSize, compressedSize), formatDecrease(originalSize, compressedSize), formatSize(compressedSize));
  169. return fmt.toString();
  170. }
  171. private String formatEmptyLine(String descr) {
  172. Formatter fmt = new Formatter();
  173. fmt.format("%-25s | %16s | %16s | %12s |", descr, "-", "-", "-");
  174. return fmt.toString();
  175. }
  176. private void printHeader() {
  177. System.out.println();
  178. System.out.println("================================================================================");
  179. System.out.format("%-25s | %-16s | %-16s | %-12s |", " Setting", "Incremental Gain", " Total Gain", " Page Size");
  180. System.out.println();
  181. System.out.println("================================================================================");
  182. }
  183. private void printFooter() {
  184. System.out.println("================================================================================");
  185. System.out.println();
  186. System.out.println("Each consecutive compressor setting is applied on top of previous ones.");
  187. System.out.println("In order to see JS and CSS compression results, YUI jar file must be present.");
  188. System.out.println("All sizes are in bytes.");
  189. }
  190. private String formatDecrease(int originalSize, int compressedSize) {
  191. NumberFormat nf = NumberFormat.getPercentInstance();
  192. nf.setGroupingUsed(true);
  193. nf.setMinimumFractionDigits(1);
  194. nf.setMaximumFractionDigits(1);
  195. return formatSize(originalSize - compressedSize) + " (" + nf.format(1 - (double) compressedSize / originalSize) + ")";
  196. }
  197. private String formatSize(int size) {
  198. NumberFormat nf = NumberFormat.getInstance();
  199. nf.setGroupingUsed(true);
  200. nf.setParseIntegerOnly(true);
  201. return nf.format(size);
  202. }
  203. }