当前位置: 首页>>代码示例>>Java>>正文


Java HtmlCompressor类代码示例

本文整理汇总了Java中com.googlecode.htmlcompressor.compressor.HtmlCompressor的典型用法代码示例。如果您正苦于以下问题:Java HtmlCompressor类的具体用法?Java HtmlCompressor怎么用?Java HtmlCompressor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


HtmlCompressor类属于com.googlecode.htmlcompressor.compressor包,在下文中一共展示了HtmlCompressor类的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: process

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public String process(String src) {
    HtmlCompressor compressor = new HtmlCompressor();
    compressor.setEnabled(true);
    compressor.setCompressCss(false);
    compressor.setCompressJavaScript(false);

    compressor.setGenerateStatistics(false);

    compressor.setRemoveComments(options.isRemoveComments());
    compressor.setRemoveMultiSpaces(options.isRemoveMutliSpaces());
    compressor.setRemoveIntertagSpaces(options.isRemoveIntertagSpaces());
    compressor.setRemoveQuotes(options.isRemoveQuotes());
    compressor.setSimpleDoctype(options.isSimpleDoctype());
    compressor.setRemoveScriptAttributes(options.isRemoveScriptAttributes());
    compressor.setRemoveStyleAttributes(options.isRemoveStyleAttributes());
    compressor.setRemoveLinkAttributes(options.isRemoveLinkAttributes());
    compressor.setRemoveFormAttributes(options.isRemoveFormAttributes());
    compressor.setRemoveInputAttributes(options.isRemoveInputAttributes());
    compressor.setSimpleBooleanAttributes(options.isSimpleBooleanAttributes());
    compressor.setRemoveJavaScriptProtocol(options.isRemoveJavaScriptProtocol());
    compressor.setRemoveHttpProtocol(options.isRemoveHttpProtocol());
    compressor.setRemoveHttpsProtocol(options.isRemoveHttpsProtocol());
    compressor.setPreserveLineBreaks(options.isPreserveLineBreaks());

    return compressor.compress(src);
}
 
开发者ID:instant-solutions,项目名称:gradle-website-optimizer,代码行数:27,代码来源:HtmlProcessor.java

示例2: ContentHandler

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public ContentHandler(String rootPath, String webRootPath) {
    this.rootPath = rootPath;
    indexPath = rootPath + "/index.html";
    this.webRootPath = webRootPath;
    webRootLen = webRootPath.length()-1;
    compressor = new HtmlCompressor();
    cache = CacheBuilder.newBuilder()
            .maximumSize(1000)
            .build(new CacheLoader<String, CachedContent>() {
                @Override
                public CachedContent load(String key) throws IOException {
                    try (InputStream is = this.getClass().getResourceAsStream(key)) {
                        if (is == null) {
                            return null;
                        }
                        return new CachedContent(key, is, compressor);
                    }
                }
            });
}
 
开发者ID:Jukkorsis,项目名称:Hadrian,代码行数:21,代码来源:ContentHandler.java

示例3: getCleanCompressor

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
private HtmlCompressor getCleanCompressor() {
	HtmlCompressor compressor = new HtmlCompressor();
	compressor.setRemoveComments(false);
	compressor.setRemoveMultiSpaces(false);
	
	return compressor;
}
 
开发者ID:ad3n,项目名称:htmlcompressor,代码行数:8,代码来源:HtmlAnalyzer.java

示例4: init

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
@Override
public void init(RuntimeServices rs, InternalContextAdapter context, Node node) throws TemplateInitException {
	super.init(rs, context, node);
	log = rs.getLog();
	
	//set compressor properties
	enabled = rs.getBoolean("userdirective.compressJs.enabled", true);
	jsCompressor = rs.getString("userdirective.compressHtml.jsCompressor", HtmlCompressor.JS_COMPRESSOR_YUI);
	yuiJsNoMunge = rs.getBoolean("userdirective.compressJs.yuiJsNoMunge", false);
	yuiJsPreserveAllSemiColons = rs.getBoolean("userdirective.compressJs.yuiJsPreserveAllSemiColons", false);
	yuiJsLineBreak = rs.getInt("userdirective.compressJs.yuiJsLineBreak", -1);
	closureOptLevel = rs.getString("userdirective.compressHtml.closureOptLevel", ClosureJavaScriptCompressor.COMPILATION_LEVEL_SIMPLE);
}
 
开发者ID:ad3n,项目名称:htmlcompressor,代码行数:14,代码来源:JavaScriptCompressorDirective.java

示例5: init

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
@Override
public void init(WebApplication webApplication) {
	
	HtmlCompressor compressor = new HtmlCompressor();
	setFeatureConfiguration(compressor);
	webApplication.getMarkupSettings().setMarkupFactory(new HtmlCompressingMarkupFactory(compressor));
	
	wicketEndpointRepository.add(new WicketAutoConfig.Builder(this.getClass())
			.withDetail("properties", props)
			.build());
}
 
开发者ID:MarcGiffing,项目名称:wicket-spring-boot,代码行数:12,代码来源:HTMLCompressingConfig.java

示例6: setFeatureConfiguration

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
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());
		}
	}
}
 
开发者ID:MarcGiffing,项目名称:wicket-spring-boot,代码行数:15,代码来源:HTMLCompressingConfig.java

示例7: save

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public void save() throws IOException {
	String content = core.toString();
	if (JRenderConfig.Server.View.useMinified) {
		HtmlCompressor html = new HtmlCompressor();
		html.setRemoveIntertagSpaces(true);
		content = html.compress(content);
	}
	FileUtils.createFile(content, jrenderPath + "/core.js");
}
 
开发者ID:mehah,项目名称:jRender,代码行数:10,代码来源:CoreFileJS.java

示例8: compressHTML

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public static void compressHTML(String inputFilename, String outputFilename, Options o) throws IOException {
    String html = FileUtils.readFileToString(new File(inputFilename));
    HtmlCompressor compressor = new HtmlCompressor();
    FileUtils.writeStringToFile(new File(outputFilename), compressor.compress(html));
}
 
开发者ID:AdityaAnand1,项目名称:Website-Compressor,代码行数:6,代码来源:Compressor.java

示例9: init

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
@Override
public void init(RuntimeServices rs, InternalContextAdapter context, Node node) throws TemplateInitException {
	super.init(rs, context, node);
	log = rs.getLog();
	
	boolean compressJavaScript = rs.getBoolean("userdirective.compressHtml.compressJavaScript", false);
	
	//set compressor properties
	htmlCompressor.setEnabled(rs.getBoolean("userdirective.compressHtml.enabled", true));
	htmlCompressor.setRemoveComments(rs.getBoolean("userdirective.compressHtml.removeComments", true));
	htmlCompressor.setRemoveMultiSpaces(rs.getBoolean("userdirective.compressHtml.removeMultiSpaces", true));
	htmlCompressor.setRemoveIntertagSpaces(rs.getBoolean("userdirective.compressHtml.removeIntertagSpaces", false));
	htmlCompressor.setRemoveQuotes(rs.getBoolean("userdirective.compressHtml.removeQuotes", false));
	htmlCompressor.setPreserveLineBreaks(rs.getBoolean("userdirective.compressHtml.preserveLineBreaks", false));
	htmlCompressor.setCompressJavaScript(compressJavaScript);
	htmlCompressor.setCompressCss(rs.getBoolean("userdirective.compressHtml.compressCss", false));
	htmlCompressor.setYuiJsNoMunge(rs.getBoolean("userdirective.compressHtml.yuiJsNoMunge", false));
	htmlCompressor.setYuiJsPreserveAllSemiColons(rs.getBoolean("userdirective.compressHtml.yuiJsPreserveAllSemiColons", false));
	htmlCompressor.setYuiJsLineBreak(rs.getInt("userdirective.compressHtml.yuiJsLineBreak", -1));
	htmlCompressor.setYuiCssLineBreak(rs.getInt("userdirective.compressHtml.yuiCssLineBreak", -1));
	htmlCompressor.setSimpleDoctype(rs.getBoolean("userdirective.compressHtml.simpleDoctype", false));
	htmlCompressor.setRemoveScriptAttributes(rs.getBoolean("userdirective.compressHtml.removeScriptAttributes", false));
	htmlCompressor.setRemoveStyleAttributes(rs.getBoolean("userdirective.compressHtml.removeStyleAttributes", false));
	htmlCompressor.setRemoveLinkAttributes(rs.getBoolean("userdirective.compressHtml.removeLinkAttributes", false));
	htmlCompressor.setRemoveFormAttributes(rs.getBoolean("userdirective.compressHtml.removeFormAttributes", false));
	htmlCompressor.setRemoveInputAttributes(rs.getBoolean("userdirective.compressHtml.removeInputAttributes", false));
	htmlCompressor.setSimpleBooleanAttributes(rs.getBoolean("userdirective.compressHtml.simpleBooleanAttributes", false));
	htmlCompressor.setRemoveJavaScriptProtocol(rs.getBoolean("userdirective.compressHtml.removeJavaScriptProtocol", false));
	htmlCompressor.setRemoveHttpProtocol(rs.getBoolean("userdirective.compressHtml.removeHttpProtocol", false));
	htmlCompressor.setRemoveHttpsProtocol(rs.getBoolean("userdirective.compressHtml.removeHttpsProtocol", false));
	
	
	if(compressJavaScript && rs.getString("userdirective.compressHtml.jsCompressor", HtmlCompressor.JS_COMPRESSOR_YUI).equalsIgnoreCase(HtmlCompressor.JS_COMPRESSOR_CLOSURE)) {
		String closureOptLevel = rs.getString("userdirective.compressHtml.closureOptLevel", ClosureJavaScriptCompressor.COMPILATION_LEVEL_SIMPLE);
		
		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);
	}
}
 
开发者ID:ad3n,项目名称:htmlcompressor,代码行数:48,代码来源:HtmlCompressorDirective.java

示例10: render

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public boolean render(InternalContextAdapter context, Writer writer, Node node) 
  		throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
  	
  	//render content
  	StringWriter content = new StringWriter();
node.jjtGetChild(0).render(context, content);

//compress
if(enabled) {
	try {
		String result = content.toString();
		
		if(jsCompressor.equalsIgnoreCase(HtmlCompressor.JS_COMPRESSOR_CLOSURE)) {
			//call Closure compressor
			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);
			}
			
			result = closureCompressor.compress(result);
			
		} else {
			//call YUICompressor
			YuiJavaScriptCompressor yuiCompressor = new YuiJavaScriptCompressor();
			yuiCompressor.setDisableOptimizations(yuiJsDisableOptimizations);
			yuiCompressor.setLineBreak(yuiJsLineBreak);
			yuiCompressor.setNoMunge(yuiJsNoMunge);
			yuiCompressor.setPreserveAllSemiColons(yuiJsPreserveAllSemiColons);
			
			result = yuiCompressor.compress(result);
		}
		
		writer.write(result);
	} catch (Exception e) {
		writer.write(content.toString());
		String msg = "Failed to compress content: "+content.toString();
           log.error(msg, e);
           throw new RuntimeException(msg, e);
           
	}
} else {
	writer.write(content.toString());
}

return true;
  	
  }
 
开发者ID:ad3n,项目名称:htmlcompressor,代码行数:52,代码来源:JavaScriptCompressorDirective.java

示例11: close

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public void close() throws IOException {
	if (closed) {
		throw new IOException(
				"This output stream has already been closed");
	}

	String contentType = response.getContentType();
	if (contentType != null && contentType.startsWith("text/html")) {
		String html = baos.toString();

		// { extract JSON-LD before compression, HtmlCompressor cannot
		// handle them
		ArrayList<String> jsonLd = new ArrayList<String>();

		while (true) {
			Matcher m = JSON_LD.matcher(html);
			if (!m.find())
				break;

			jsonLd.add(m.group(2).replace("\n", "").replace("\r", ""));
			html = m.replaceFirst("");
		}
		// }

		HtmlCompressor compressor = new HtmlCompressor();
		compressor.setCompressJavaScript(true);
		compressor.setCompressCss(true);
		html = compressor.compress(html);

		// insert JSON-LD back to before </head> or </body>
		if (jsonLd.size() > 0) {
			StringBuffer sb = new StringBuffer();
			for (String str : jsonLd)
				sb.append("<script type=\"application/ld+json\">")
						.append(str).append("</script>");

			String lower = html.toLowerCase();
			int idx = lower.indexOf("</head>");
			if (idx <= 0)
				idx = lower.indexOf("</body>");
			if (idx > 0)
				html = html.substring(0, idx) + sb.toString()
						+ html.substring(idx);
		}

		output.write(html.getBytes());
	} else
		output.write(baos.toByteArray());

	output.flush();
	output.close();
	closed = true;
}
 
开发者ID:yangjm,项目名称:winlet,代码行数:54,代码来源:CompressFilter.java

示例12: HtmlCompressorMinifier

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
public HtmlCompressorMinifier() {
    super(new HtmlCompressor());
}
 
开发者ID:trimou,项目名称:trimou,代码行数:4,代码来源:HtmlCompressorMinifier.java

示例13: testCustomizedHtmlListener

import com.googlecode.htmlcompressor.compressor.HtmlCompressor; //导入依赖的package包/类
@Test
public void testCustomizedHtmlListener() {

    String contents = "<html><body><!-- My comment --></body></html>";

    MustacheEngine engine = MustacheEngineBuilder
            .newBuilder()
            .addMustacheListener(
                    Minify.customListener(new HtmlCompressorMinifier() {
                        @Override
                        protected void initCompressor(
                                HtmlCompressor compressor,
                                Configuration configuration) {
                            compressor.setEnabled(false);
                        }
                    })).build();
    // Compressor is disabled
    assertEquals(contents,
            engine.compileMustache("minify_html_customized", contents)
                    .render(null));

    engine = MustacheEngineBuilder.newBuilder()
            .addMustacheListener(
                    Minify.customListener(new HtmlCompressorMinifier(
                            mustacheName -> mustacheName.endsWith("html"))))
            .build();
    // Mustache name does not match
    assertEquals(contents,
            engine.compileMustache("minify_html_customized", contents)
                    .render(null));

    // Skip lambdas
    engine = MustacheEngineBuilder.newBuilder()
            .addMustacheListener(
                    Minify.customListener(new HtmlCompressorMinifier(
                            mustacheName -> !mustacheName.startsWith(
                                    Lambda.ONEOFF_LAMBDA_TEMPLATE_PREFIX))))
            .build();
    assertEquals(contents, engine
            .compileMustache("minify_html_customized_skip_lambda",
                    "<html><body>{{{lambda}}}</body></html>")
            .render(ImmutableMap.of("lambda", new InputLiteralLambda() {

                @Override
                public boolean isReturnValueInterpolated() {
                    return true;
                }

                @Override
                public String invoke(String text) {
                    return "<!-- My comment -->";
                }
            })));
}
 
开发者ID:trimou,项目名称:trimou,代码行数:55,代码来源:MinifyListenerTest.java


注:本文中的com.googlecode.htmlcompressor.compressor.HtmlCompressor类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。