All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.stripecubeui.App Maven / Gradle / Ivy

Go to download

Css / Javascript Merger and Minimizer integrated with Stripecube Web App Development

The newest version!
package com.clickntap.stripecubeui;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.binary.Base64;
import org.springframework.core.io.Resource;

import com.clickntap.stripecubeui.api.BinaryResource;
import com.clickntap.stripecubeui.api.UiService;
import com.clickntap.stripecubeui.bo.UiResource;
import com.clickntap.stripecubeui.bo.UiSet;
import com.github.sommeri.less4j.LessCompiler;
import com.github.sommeri.less4j.core.ThreadUnsafeLessCompiler;
import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

public class App extends com.clickntap.hub.App implements UiService {

	private Resource resourcesDir;

	public Resource getResourcesDir() {
		return resourcesDir;
	}

	public void setResourcesDir(Resource resourcesDir) {
		this.resourcesDir = resourcesDir;
	}

	public String compressJavascript(String js) throws Exception {
		js = js.replace("/*!", "/*");
		StringReader reader = new StringReader(js);
		JavaScriptCompressor compressor = new JavaScriptCompressor(reader, null);
		StringWriter writer = new StringWriter();
		compressor.compress(writer, 999, true, false, true, true);
		return writer.toString();
	}

	public String compressCss(String css) throws Exception {
		css = css.replace("/*!", "/*");
		StringReader reader = new StringReader(css);
		CssCompressor compressor = new CssCompressor(reader);
		StringWriter writer = new StringWriter();
		compressor.compress(writer, 999);
		return writer.toString();
	}

	public String compressLess(String less) throws Exception {
		LessCompiler compiler = new ThreadUnsafeLessCompiler();
		return compressCss(compiler.compile(less).getCss());
	}

	public List getBinaryResources(String name) throws Exception {
		UiSet set = getUiSet(name);
		List resources = new ArrayList();
		for (UiResource resource : set.getResources()) {
			if ("binary".equals(resource.getType())) {
				BinaryResource binaryResource = new BinaryResource();
				binaryResource.setPath(resource.getPath());
				binaryResource.setBytes(Base64.decodeBase64(resource.getContent().getBytes()));
				resources.add(binaryResource);
			}
		}
		return resources;
	}

	public String getCssSet(String name) throws Exception {
		UiSet set = getUiSet(name);
		String css = "";
		for (UiResource resource : set.getResources()) {
			if ("css".equals(resource.getType())) {
				css += new String((resource.getContent().getBytes()));
				css += "\n\n";
			}
		}
		return css;
	}

	public String getJavascriptSet(String name) throws Exception {
		UiSet set = getUiSet(name);
		String js = "";
		for (UiResource resource : set.getResources()) {
			if ("javascript".equals(resource.getType())) {
				js += new String((resource.getContent().getBytes()));
				js += "\n\n";
			}
		}
		return js;
	}

	private UiSet getUiSet(String name) throws Exception {
		UiSet set = new UiSet();
		int x = name.indexOf(":");
		set.setName(name.substring(0, x));
		set.setVersion(name.substring(x + 1));
		set.setApp(this);
		set.read("set");
		return set;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy