com.github.wuic.engine.htmlcompressor.HtmlCompressorEngine Maven / Gradle / Ivy
/*
* "Copyright (c) 2014 Capgemini Technology Services (hereinafter "Capgemini")
*
* License/Terms of Use
* Permission is hereby granted, free of charge and for the term of intellectual
* property rights on the Software, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to use, copy, modify and
* propagate free of charge, anywhere in the world, all or part of the Software
* subject to the following mandatory conditions:
*
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Any failure to comply with the above shall automatically terminate the license
* and be construed as a breach of these Terms of Use causing significant harm to
* Capgemini.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, PEACEFUL ENJOYMENT,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Capgemini shall not be used in
* advertising or otherwise to promote the use or other dealings in this Software
* without prior written authorization from Capgemini.
*
* These Terms of Use are subject to French law.
*
* IMPORTANT NOTICE: The WUIC software implements software components governed by
* open source software licenses (BSD and Apache) of which CAPGEMINI is not the
* author or the editor. The rights granted on the said software components are
* governed by the specific terms and conditions specified by Apache 2.0 and BSD
* licenses."
*/
package com.github.wuic.engine.htmlcompressor;
import com.github.wuic.ApplicationConfig;
import com.github.wuic.NutType;
import com.github.wuic.config.BooleanConfigParam;
import com.github.wuic.config.ConfigConstructor;
import com.github.wuic.engine.EngineRequest;
import com.github.wuic.engine.EngineService;
import com.github.wuic.engine.EngineType;
import com.github.wuic.engine.NodeEngine;
import com.github.wuic.exception.WuicException;
import com.github.wuic.exception.wrapper.StreamException;
import com.github.wuic.nut.ByteArrayNut;
import com.github.wuic.nut.ConvertibleNut;
import com.github.wuic.util.IOUtils;
import com.googlecode.htmlcompressor.compressor.HtmlCompressor;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* This engine is based on HTML compressor to be able to compress HTML code.
*
*
* @author Guillaume DROUET
* @version 1.0
* @since 0.5.0
*/
@EngineService(injectDefaultToWorkflow = true)
public class HtmlCompressorEngine extends NodeEngine {
/**
* Do compression or not.
*/
private final Boolean work;
/**
* Compressor.
*/
private final HtmlCompressor compressor;
/**
*
* Builds a new instance.
*
*
* @param compress the instance to build
* @param preserveLb preserve the line break points or not
*/
@ConfigConstructor
public HtmlCompressorEngine(
@BooleanConfigParam(propertyKey = ApplicationConfig.COMPRESS, defaultValue = true) Boolean compress,
@BooleanConfigParam(propertyKey = ApplicationConfig.PRESERVE_LINE_BREAK, defaultValue = false) Boolean preserveLb) {
work = compress;
compressor = new HtmlCompressor();
compressor.setPreserveLineBreaks(preserveLb);
}
/**
* {@inheritDoc}
*/
@Override
public List getNutTypes() {
return Arrays.asList(NutType.HTML);
}
/**
* {@inheritDoc}
*/
@Override
public EngineType getEngineType() {
return EngineType.MINIFICATION;
}
/**
* {@inheritDoc}
*/
@Override
protected List internalParse(final EngineRequest request) throws WuicException {
if (!works()) {
return request.getNuts();
} else {
final List retval = new ArrayList(request.getNuts().size());
try {
for (final ConvertibleNut nut : request.getNuts()) {
final String compressString = compressor.compress(IOUtils.readString(new InputStreamReader(nut.openStream())));
final ConvertibleNut compress = new ByteArrayNut(compressString.getBytes(), nut.getName(), NutType.HTML, nut);
retval.add(compress);
if (nut.getReferencedNuts() != null) {
// Also add all the referenced nuts
for (final ConvertibleNut ref : nut.getReferencedNuts()) {
compress.addReferencedNut(ref);
}
}
}
if (getNext() != null) {
return getNext().parse(new EngineRequest(retval, request));
} else {
return retval;
}
} catch (IOException ioe) {
throw new StreamException(ioe);
}
}
}
/**
* {@inheritDoc}
*/
@Override
public Boolean works() {
return work;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy