YUI Compressor 压缩大的js文件出现问题,请高手指点!

李球 发布于 2012/03/28 13:27
阅读 4K+
收藏 0
  利用YUI Compressor压缩文件,但是出现了一些蛋疼的问题,请高手指点,代码贴上:
package com.jee;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;

import com.yahoo.platform.yui.compressor.CssCompressor;
import com.yahoo.platform.yui.compressor.JavaScriptCompressor;

public class Compressor {

	// 要处理的目录
	File result = new File("E:\\compressed\\");

	int linebreakpos = -1;
	boolean munge = true;
	boolean verbose = true;
	boolean preserveAllSemiColons = true;
	boolean disableOptimizations = false;
	File compressedDir = null;
	File compressDir = null;
	int count = 0;

	public Compressor(String path) {
		this.compressDir = new File(path);
		this.handler(compressDir);
	}

	public static void main(String[] args) {
		new Compressor("E:\\project\\mercury\\web\\resources");
	}

	public void handler(File dir) {
		if (!result.exists()) {
			result.mkdir();
		}
		this.handler(result.getAbsolutePath() + "\\", dir);
		System.out.println("压缩了" + count + "个文件...");
	}

	public int handler(String basePath, File file) {
		try {
			if (this.check(file.getName())) {
				this.compress(file);
			} else if (file.isDirectory() && !file.getName().endsWith(".svn")) {
				this.compressedDir = new File(basePath + file.getName()); //根据压缩的文件夹创建存放压缩好的文件的文件夹
				this.compressedDir.mkdir();
				for (File sub : file.listFiles()) {
					this.handler(basePath + file.getName() + "\\", sub);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		return 1;
	}

	public boolean check(String fileName) {
		return (fileName.endsWith(".js") || fileName.endsWith(".css"));
	}

	public void compress(File file) throws Exception {
		count++;
		String fileName = file.getName();
		Reader in = new FileReader(file);
		File compressed = new File(this.compressedDir.getAbsolutePath() + "\\" + file.getName()); //将压缩好的文件放到另外一个文件夹中去
		Writer out = new FileWriter(compressed);
		if (fileName.endsWith(".js")) {
			System.out.println("正在压缩JS文件:" + file.getPath() + ".....");
			JavaScriptCompressor compressor = new JavaScriptCompressor(in, null);
			compressor.compress(out, linebreakpos, munge, verbose,
					preserveAllSemiColons, disableOptimizations);
		} else if (fileName.endsWith(".css")) {
			System.out.println("正在压缩CSS文件:" + file.getPath() + ".....");
			CssCompressor compressor = new CssCompressor(in);
			compressor.compress(out, linebreakpos);
		}
		out.close();
		in.close();
	}
}

错误信息:

 

正在压缩JS文件:E:\project\mercury\web\resources\lib\ext4\module\util\Memento.js.....
java.lang.StringIndexOutOfBoundsException: String index out of range: 1345
	at java.lang.String.substring(String.java:1935)
	at com.yahoo.platform.yui.compressor.JavaScriptCompressor.printSourceString(JavaScriptCompressor.java:267)
	at com.yahoo.platform.yui.compressor.JavaScriptCompressor.parse(JavaScriptCompressor.java:330)
	at com.yahoo.platform.yui.compressor.JavaScriptCompressor.<init>(JavaScriptCompressor.java:533)
	at com.jee.Compressor.compress(Compressor.java:73)
	at com.jee.Compressor.handler(Compressor.java:46)
	at com.jee.Compressor.handler(Compressor.java:51)
	at com.jee.Compressor.handler(Compressor.java:51)
	at com.jee.Compressor.handler(Compressor.java:51)
	at com.jee.Compressor.handler(Compressor.java:51)
	at com.jee.Compressor.handler(Compressor.java:51)
	at com.jee.Compressor.handler(Compressor.java:39)
	at com.jee.Compressor.<init>(Compressor.java:28)
	at com.jee.Compressor.main(Compressor.java:32)

压缩css文件没有问题,压缩1kb的js文件也没有问题,压缩大的js文件就出现问题了,请高手指定迷津!!

加载中
0
大东哥
大东哥
看异常像是String下标问题,但也有可能是你的js文件有语法错误.
0
abcijkxyz
abcijkxyz
http://stackoverflow.com/questions/6652550/yui-compressor-stringindexoutofboundsexception-on-jboss
0
linapex
linapex
/** 


 * 项目名称:YUI_Compressor 
 *  
 * 类名称:Compressor 
 *  
 * 创建人:LinApex 
 *  
 * 创建时间:2013-8-2 下午02:46:39 
 *  
 */ 


import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.io.Reader; 
import java.io.Writer; 
import org.mozilla.javascript.ErrorReporter; 
import org.mozilla.javascript.EvaluatorException; 
import com.yahoo.platform.yui.compressor.CssCompressor; 
import com.yahoo.platform.yui.compressor.JavaScriptCompressor; 


public class TestJsZip 



static File dir = new File("D:\\Tools\\MyEclipse 8.5\\workspace\\YUI_Compressor\\WebRoot\\WEB-INF\\lib"); 
static File out = new File("d:\\resources\\"); 


static int linebreakpos = -1; 
static boolean munge = true; 
static boolean verbose = false; 
static boolean preserveAllSemiColons = false; 
static boolean disableOptimizations = false; 


public static void main(String[] args) throws Exception 

listFiles(dir); 



public static void listFiles(File file) throws Exception 

if (file.isDirectory() && !file.getName().startsWith("_")) 

File[] files = file.listFiles(); 
for (int i = 0; i < files.length; i++) 

listFiles(files[i]); 

} else if (file.isFile()) 

if (!file.getName().startsWith(".") && file.getName().endsWith(".js") || file.getName().endsWith(".css")) 

File desFile = buildFilePath(file); 
compress(file, desFile); 





public static File buildFilePath(File file) throws Exception 

String filePath = file.getAbsolutePath(); 


String rootDir = dir.getAbsolutePath(); 
rootDir = rootDir.substring(rootDir.lastIndexOf("\\") + 1); 


filePath = filePath.substring(filePath.indexOf(rootDir)); 


String builderPath = out.getAbsolutePath() + File.separator + filePath; 
File tf = new File(builderPath); 
if (!tf.getParentFile().exists()) 

tf.getParentFile().mkdirs(); 

return tf; 



public static void compress(final File srcFile, final File desFile) throws Exception 

Reader in = new InputStreamReader(new FileInputStream(srcFile), "utf-8"); 
Writer out = new OutputStreamWriter(new FileOutputStream(desFile), "utf-8"); 


String fileName = srcFile.getName(); 
if (fileName.endsWith(".js")) 

JavaScriptCompressor jscompressor = new JavaScriptCompressor(in, new ErrorReporter() 

public void warning(String message, String sourceName, int line, String lineSource, int lineOffset) 

if (line < 0) 

System.err.println("\n[WARNING] " + message); 
} else 

System.err.println("\n[WARNING] " + line + ':' + lineOffset + ':' + message); 




public void error(String message, String sourceName, int line, String lineSource, int lineOffset) 

// if (line < 0) 
// { 
// System.err.println("\n[ERROR] " + message); 
// } else 
// { 
// System.err.println("\n[ERROR] " + line + ':' + lineOffset 
// + ':' + message); 
// } 
System.out.println("---压缩出错文件:" + srcFile.getAbsoluteFile()); 



public EvaluatorException runtimeError(String message, String sourceName, int line, String lineSource, int lineOffset) 

error(message, sourceName, line, lineSource, lineOffset); 
return new EvaluatorException(message); 

}); 


jscompressor.compress(out, linebreakpos, munge, verbose, preserveAllSemiColons, disableOptimizations); 
} else if (fileName.endsWith(".css")) 

CssCompressor csscompressor = new CssCompressor(in); 
csscompressor.compress(out, linebreakpos); 



out.close(); 
in.close(); 


返回顶部
顶部