packageorg.siqisource.mozo.servlets;
importjava.io.File;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.Reader;
importjava.io.Writer;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.UUID;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.commons.io.FileDeleteStrategy;
importorg.apache.commons.io.FileUtils;
importorg.siqisource.mozo.context.Path;
importcom.yahoo.platform.yui.compressor.CssCompressor;
importcom.yahoo.platform.yui.compressor.JavaScriptCompressor;
publicclassYuiCombineServlet extendsHttpServlet {
privatestaticMap<String, String> cachedResources = newHashMap<String, String>();
privateString cacheContextPath = "uilibrary/yui/cache/";
privateString cacheDir = Path.getPhysicalPath() + cacheContextPath;
intlinebreakpos = -1;
booleanmunge = true;
booleanverbose = false;
booleanpreserveAllSemiColons = false;
booleandisableOptimizations = false;
@Override
publicvoidinit() throwsServletException {
File catchedDir = newFile(cacheDir);
if(catchedDir.exists()) {
FileDeleteStrategy strategy = FileDeleteStrategy.FORCE;
try{
strategy.delete(catchedDir);
} catch(IOException e) {
e.printStackTrace();
}
}
catchedDir.mkdirs();
super.init();
}
@Override
protectedvoiddoGet(HttpServletRequest request,
HttpServletResponse response) throwsServletException, IOException {
String queryString = request.getQueryString();
String resourcePath = cachedResources.get(queryString);
if(resourcePath == null) {
String[] resources = queryString.split("&");
String firstResource = resources[0];
String fileName = UUID.randomUUID().toString();
if(firstResource.endsWith(".js")) {
fileName += ".js";
Writer writer = newFileWriter(cacheDir + fileName);
for(String resource : resources) {
Reader reader = newFileReader(Path.getPhysicalPath()
+ resource);
JavaScriptCompressor compressor = newJavaScriptCompressor(
reader, null);
compressor.compress(writer, linebreakpos, munge, verbose,
preserveAllSemiColons, disableOptimizations);
reader.close();
}
writer.flush();
writer.close();
} elseif(resources[0].endsWith(".css")) {
fileName += ".css";
Writer writer = newFileWriter(cacheDir + fileName);
for(String resource : resources) {
Reader reader = newFileReader(replacedUrlFile(resource));
CssCompressor compressor = newCssCompressor(reader);
compressor.compress(writer, linebreakpos);
reader.close();
}
writer.flush();
writer.close();
}
resourcePath = cacheContextPath + fileName;
cachedResources.put(queryString, resourcePath);
}
request.getRequestDispatcher(resourcePath).forward(request, response);
return;
}
publicString replacedUrlFile(String fileName) throwsIOException {
String cssfilePath = Path.getPhysicalPath() + fileName;
File cssFile = newFile(cssfilePath);
String tempCssFilePath = cacheDir + "tmp-css-"+ cssFile.getName();
File tempCssFile = newFile(tempCssFilePath);
if(tempCssFile.exists()) {
returntempCssFilePath;
}
String css = FileUtils.readFileToString(cssFile);
intmaxIndex = css.length() - 1;
intappendIndex = 0;
Pattern p = Pattern.compile("url\\(\\s*([\"']?)");
if(!p.matcher(css).find()) {
returncssfilePath;
}
Matcher m = p.matcher(css);
String url = fileName.substring(0, fileName.lastIndexOf('/'));
url = Path.getContextPath() + "/"+ url + "/";
StringBuffer replacedUrlCss = newStringBuffer();
while(m.find()) {
intstartIndex = m.start() + 4;
String terminator = m.group(1);
if(terminator.length() == 0) {
terminator = ")";
}
booleanfoundTerminator = false;
intendIndex = m.end() - 1;
while(foundTerminator == false&& endIndex + 1<= maxIndex) {
endIndex = css.indexOf(terminator, endIndex + 1);
if((endIndex > 0) && (css.charAt(endIndex - 1) != '\\')) {
foundTerminator = true;
if(!")".equals(terminator)) {
endIndex = css.indexOf(")", endIndex);
}
}
}
replacedUrlCss.append(css.substring(appendIndex, m.start()));
if(foundTerminator) {
String token = css.substring(startIndex, endIndex);
token = token.replaceAll("\\s+", "");
String preserver = "url('"+ url + token + "')";
replacedUrlCss.append(preserver);
appendIndex = endIndex + 1;
} else{
replacedUrlCss.append(css.substring(m.start(), m.end()));
appendIndex = m.end();
}
}
FileUtils.writeStringToFile(tempCssFile, replacedUrlCss.toString());
returntempCssFilePath;
}
}
|