private void setFeatureConfiguration(HtmlCompressor compressor) {
   for (Entry<String, Boolean> entrySet : props.getFeatures().entrySet()) {
     Method method = null;
     String capitalizedKey = StringUtils.capitalize(entrySet.getKey());
     String methodname = "set" + capitalizedKey;
     try {
       method = compressor.getClass().getMethod(methodname, boolean.class);
       method.setAccessible(true);
       ReflectionUtils.invokeMethod(method, compressor, entrySet.getValue());
     } catch (Exception e) {
       logger.warn(
           "failed to invoke method: {} with value {}. Exception: {}",
           methodname,
           entrySet.getValue(),
           e.getMessage());
     }
   }
 }
예제 #2
0
  @Override
  public int doEndTag() throws JspException {

    BodyContent bodyContent = getBodyContent();
    String content = bodyContent.getString();

    HtmlCompressor htmlCompressor = new HtmlCompressor();
    htmlCompressor.setEnabled(enabled);
    htmlCompressor.setRemoveComments(removeComments);
    htmlCompressor.setRemoveMultiSpaces(removeMultiSpaces);
    htmlCompressor.setRemoveIntertagSpaces(removeIntertagSpaces);
    htmlCompressor.setRemoveQuotes(removeQuotes);
    htmlCompressor.setPreserveLineBreaks(preserveLineBreaks);
    htmlCompressor.setCompressJavaScript(compressJavaScript);
    htmlCompressor.setCompressCss(compressCss);
    htmlCompressor.setYuiJsNoMunge(yuiJsNoMunge);
    htmlCompressor.setYuiJsPreserveAllSemiColons(yuiJsPreserveAllSemiColons);
    htmlCompressor.setYuiJsDisableOptimizations(yuiJsDisableOptimizations);
    htmlCompressor.setYuiJsLineBreak(yuiJsLineBreak);
    htmlCompressor.setYuiCssLineBreak(yuiCssLineBreak);

    htmlCompressor.setSimpleDoctype(simpleDoctype);
    htmlCompressor.setRemoveScriptAttributes(removeScriptAttributes);
    htmlCompressor.setRemoveStyleAttributes(removeStyleAttributes);
    htmlCompressor.setRemoveLinkAttributes(removeLinkAttributes);
    htmlCompressor.setRemoveFormAttributes(removeFormAttributes);
    htmlCompressor.setRemoveInputAttributes(removeInputAttributes);
    htmlCompressor.setSimpleBooleanAttributes(simpleBooleanAttributes);
    htmlCompressor.setRemoveJavaScriptProtocol(removeJavaScriptProtocol);
    htmlCompressor.setRemoveHttpProtocol(removeHttpProtocol);
    htmlCompressor.setRemoveHttpsProtocol(removeHttpsProtocol);

    if (compressJavaScript && jsCompressor.equalsIgnoreCase(HtmlCompressor.JS_COMPRESSOR_CLOSURE)) {
      ClosureJavaScriptCompressor closureCompressor = new ClosureJavaScriptCompressor();
      if (closureOptLevel.equalsIgnoreCase(
          ClosureJavaScriptCompressor.COMPILATION_LEVEL_ADVANCED)) {
        closureCompressor.setCompilationLevel(CompilationLevel.ADVANCED_OPTIMIZATIONS);
      } else if (closureOptLevel.equalsIgnoreCase(
          ClosureJavaScriptCompressor.COMPILATION_LEVEL_WHITESPACE)) {
        closureCompressor.setCompilationLevel(CompilationLevel.WHITESPACE_ONLY);
      } else {
        closureCompressor.setCompilationLevel(CompilationLevel.SIMPLE_OPTIMIZATIONS);
      }
      htmlCompressor.setJavaScriptCompressor(closureCompressor);
    }

    try {
      bodyContent.clear();
      bodyContent.append(htmlCompressor.compress(content));
      bodyContent.writeOut(pageContext.getOut());
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }

    return super.doEndTag();
  }