增加测试类
This commit is contained in:
@@ -4,13 +4,13 @@
|
||||
*/
|
||||
package com.jeesite.common.idgen;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.UUID;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* 封装各种生成唯一性ID算法的工具类.
|
||||
* @author ThinkGem
|
||||
@@ -87,38 +87,5 @@ public class IdGen {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// System.out.println(nextCode("8") + " = 9");
|
||||
// System.out.println(nextCode("09") + " = 10");
|
||||
// System.out.println(nextCode("009") + " = 010");
|
||||
// System.out.println(nextCode("T09") + " = T10");
|
||||
// System.out.println(nextCode("TG09") + " = TG10");
|
||||
// System.out.println(nextCode("TG0101") + " = TG0102");
|
||||
// System.out.println(nextCode("TG0109") + " = TG0110");
|
||||
// System.out.println(nextCode("TG02T03") + " = TG02T04");
|
||||
// System.out.println(nextCode("TG02T099") + " = TG02T100");
|
||||
// System.out.println(nextCode("TG02T100") + " = TG02T101");
|
||||
// System.out.println(nextCode("TG02T10A") + " = TG02T10A001");
|
||||
// System.out.println(nextCode("1123117153417957377") + " = 1123117153417957379");
|
||||
// System.out.println(nextCode("0040009") + " = 0040010");
|
||||
// System.out.println(uuid());
|
||||
// System.out.println(nextId());
|
||||
// // 数值型ID重复验证测试
|
||||
// Set<String> set = SetUtils.newHashSet();
|
||||
// try{
|
||||
// for (int i=0; i<100; i++){
|
||||
// String id = String.valueOf(nextId());
|
||||
// if (set.contains(id)){
|
||||
// throw new Exception(id + " exists");
|
||||
// }
|
||||
// set.add(id);
|
||||
// System.out.println(id);
|
||||
// Thread.sleep(100);
|
||||
// }
|
||||
// }catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
*/
|
||||
package com.jeesite.common.idgen;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* 来自于twitter项目snowflake的id产生方案,全局唯一,时间有序。
|
||||
* 64位ID (42(毫秒)+5(机器ID)+5(业务编码)+12(重复累加))
|
||||
@@ -124,51 +124,5 @@ public class IdWorker {
|
||||
private long timeGen() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
// //////////// test ////////////
|
||||
//
|
||||
// public static void main(String[] args) throws Exception {
|
||||
// final Set<Long> set = SetUtils.newHashSet();
|
||||
//
|
||||
// final IdWorker w1 = new IdWorker(-1, -1);
|
||||
// final IdWorker w2 = new IdWorker(-1, -1);
|
||||
// final CyclicBarrier cdl = new CyclicBarrier(100);
|
||||
//
|
||||
// for (int i = 0; i < 1000; i++) {
|
||||
// new Thread(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// try {
|
||||
// cdl.await();
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// } catch (BrokenBarrierException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
// // id
|
||||
// Long id = w1.nextId();
|
||||
// if (set.contains(id)){
|
||||
// System.out.println(id + " exists");
|
||||
// }
|
||||
// set.add(id);
|
||||
// System.out.println(id);
|
||||
//
|
||||
// // id2
|
||||
// Long id2 = w2.nextId();
|
||||
// if (set.contains(id2)){
|
||||
// System.out.println(id2 + " exists");
|
||||
// }
|
||||
// set.add(id2);
|
||||
// System.out.println(id2);
|
||||
// }
|
||||
// }).start();
|
||||
// }
|
||||
// try {
|
||||
// TimeUnit.SECONDS.sleep(5);
|
||||
// } catch (InterruptedException e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
@@ -4,20 +4,17 @@
|
||||
*/
|
||||
package com.jeesite.common.image;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.jeesite.common.io.FileUtils;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import net.coobird.thumbnailator.Thumbnails.Builder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.jeesite.common.io.FileUtils;
|
||||
import com.jeesite.common.lang.StringUtils;
|
||||
|
||||
import net.coobird.thumbnailator.Thumbnails;
|
||||
import net.coobird.thumbnailator.Thumbnails.Builder;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* 图片处理工具类
|
||||
@@ -26,7 +23,7 @@ import net.coobird.thumbnailator.Thumbnails.Builder;
|
||||
*/
|
||||
public class ImageUtils {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(ImageUtils.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class);
|
||||
|
||||
/**
|
||||
* 缩略图生成,处理一些较大的图片,防止占用太多的网络资源
|
||||
|
||||
@@ -4,26 +4,18 @@
|
||||
*/
|
||||
package com.jeesite.common.image;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.Hashtable;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.BinaryBitmap;
|
||||
import com.google.zxing.DecodeHintType;
|
||||
import com.google.zxing.EncodeHintType;
|
||||
import com.google.zxing.LuminanceSource;
|
||||
import com.google.zxing.MultiFormatReader;
|
||||
import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.Result;
|
||||
import com.google.zxing.*;
|
||||
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
|
||||
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.HybridBinarizer;
|
||||
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* 条形码和二维码编码解码
|
||||
* @author ThinkGem
|
||||
@@ -33,7 +25,7 @@ public class ZxingUtils {
|
||||
|
||||
/**
|
||||
* 条形码编码
|
||||
*
|
||||
*
|
||||
* @param contents
|
||||
* @param width
|
||||
* @param height
|
||||
@@ -56,7 +48,7 @@ public class ZxingUtils {
|
||||
|
||||
/**
|
||||
* 条形码解码
|
||||
*
|
||||
*
|
||||
* @param imgPath
|
||||
* @return String
|
||||
*/
|
||||
@@ -80,7 +72,7 @@ public class ZxingUtils {
|
||||
|
||||
/**
|
||||
* 二维码编码
|
||||
*
|
||||
*
|
||||
* @param contents
|
||||
* @param width
|
||||
* @param height
|
||||
@@ -102,7 +94,7 @@ public class ZxingUtils {
|
||||
|
||||
/**
|
||||
* 二维码解码
|
||||
*
|
||||
*
|
||||
* @param imgPath
|
||||
* @return String
|
||||
*/
|
||||
@@ -126,32 +118,4 @@ public class ZxingUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//
|
||||
// // 条形码
|
||||
// String imgPath = "target\\zxing_EAN13.png";
|
||||
// String contents = "6923450657713";
|
||||
// int width = 105, height = 50;
|
||||
//
|
||||
// ZxingUtils.encode(contents, width, height, imgPath);
|
||||
// System.out.println("finished zxing EAN-13 encode.");
|
||||
//
|
||||
// String decodeContent = ZxingUtils.decode(imgPath);
|
||||
// System.out.println("解码内容如下:" + decodeContent);
|
||||
// System.out.println("finished zxing EAN-13 decode.");
|
||||
//
|
||||
// // 二维码
|
||||
// String imgPath2 = "target\\zxing.png";
|
||||
// String contents2 = "Hello Gem, welcome to Zxing!" + "\nBlog [ http://thinkgem.iteye.com ]" + "\nEMail [ thinkgem@163.com ]";
|
||||
// int width2 = 300, height2 = 300;
|
||||
//
|
||||
// ZxingUtils.encode2(contents2, width2, height2, imgPath2);
|
||||
// System.out.println("finished zxing encode.");
|
||||
//
|
||||
// String decodeContent2 = ZxingUtils.decode2(imgPath2);
|
||||
// System.out.println("解码内容如下:" + decodeContent2);
|
||||
// System.out.println("finished zxing decode.");
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -829,7 +829,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
|
||||
|
||||
/**
|
||||
* 获取文件扩展名(返回小写)
|
||||
* @param pathname 文件名
|
||||
* @param fileName 文件名
|
||||
* @return 例如:test.jpg 返回: jpg
|
||||
*/
|
||||
public static String getFileExtension(String fileName) {
|
||||
|
||||
@@ -20,13 +20,13 @@ public class IOUtils extends org.apache.commons.io.IOUtils {
|
||||
|
||||
/**
|
||||
* 根据文件路径创建文件输入流处理 以字节为单位(非 unicode )
|
||||
* @param path
|
||||
* @param filePath
|
||||
* @return
|
||||
*/
|
||||
public static FileInputStream getFileInputStream(String filepath) {
|
||||
public static FileInputStream getFileInputStream(String filePath) {
|
||||
FileInputStream fileInputStream = null;
|
||||
try {
|
||||
fileInputStream = new FileInputStream(filepath);
|
||||
fileInputStream = new FileInputStream(filePath);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("错误信息:文件不存在");
|
||||
e.printStackTrace();
|
||||
@@ -36,7 +36,7 @@ public class IOUtils extends org.apache.commons.io.IOUtils {
|
||||
|
||||
/**
|
||||
* 根据文件对象创建文件输入流处理 以字节为单位(非 unicode )
|
||||
* @param path
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
public static FileInputStream getFileInputStream(File file) {
|
||||
@@ -69,14 +69,14 @@ public class IOUtils extends org.apache.commons.io.IOUtils {
|
||||
|
||||
/**
|
||||
* 根据文件路径创建文件输出流处理 以字节为单位(非 unicode )
|
||||
* @param path
|
||||
* @param filePath
|
||||
* @param append true:文件以追加方式打开,false:则覆盖原文件的内容
|
||||
* @return
|
||||
*/
|
||||
public static FileOutputStream getFileOutputStream(String filepath, boolean append) {
|
||||
public static FileOutputStream getFileOutputStream(String filePath, boolean append) {
|
||||
FileOutputStream fileOutputStream = null;
|
||||
try {
|
||||
fileOutputStream = new FileOutputStream(filepath, append);
|
||||
fileOutputStream = new FileOutputStream(filePath, append);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("错误信息:文件不存在");
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -26,8 +26,8 @@ import java.util.Properties;
|
||||
public class PropertyLoader implements org.springframework.boot.env.PropertySourceLoader, Ordered{
|
||||
|
||||
private static boolean isLoadJeeSitePropertySource = false;
|
||||
private PropertiesPropertySourceLoader propertiesPropertySourceLoader = new PropertiesPropertySourceLoader();
|
||||
private YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
|
||||
private final PropertiesPropertySourceLoader propertiesPropertySourceLoader = new PropertiesPropertySourceLoader();
|
||||
private final YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();
|
||||
|
||||
@Override
|
||||
public String[] getFileExtensions() {
|
||||
|
||||
@@ -4,17 +4,16 @@
|
||||
*/
|
||||
package com.jeesite.common.io;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
import com.jeesite.common.lang.ExceptionUtils;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.core.io.support.ResourcePatternResolver;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
import com.jeesite.common.lang.ExceptionUtils;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 资源供给类
|
||||
@@ -23,8 +22,8 @@ import com.jeesite.common.lang.ExceptionUtils;
|
||||
*/
|
||||
public class ResourceUtils extends org.springframework.util.ResourceUtils {
|
||||
|
||||
private static ResourceLoader resourceLoader;
|
||||
private static ResourcePatternResolver resourceResolver;
|
||||
private static final ResourceLoader resourceLoader;
|
||||
private static final ResourcePatternResolver resourceResolver;
|
||||
static{
|
||||
resourceLoader = new DefaultResourceLoader();
|
||||
resourceResolver = new PathMatchingResourcePatternResolver(resourceLoader);
|
||||
|
||||
@@ -18,7 +18,7 @@ import java.util.Date;
|
||||
*/
|
||||
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
|
||||
private static String[] parsePatterns = {
|
||||
private static final String[] parsePatterns = {
|
||||
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH", "yyyy-MM",
|
||||
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd HH", "yyyy/MM",
|
||||
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM.dd HH", "yyyy.MM",
|
||||
@@ -316,14 +316,4 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
|
||||
return new Date[]{beginDate, endDate};
|
||||
}
|
||||
|
||||
// public static void main(String[] args) throws ParseException {
|
||||
// System.out.println(formatDate(parseDate("2010/3/6")));
|
||||
// System.out.println(getDate("yyyy年MM月dd日 E"));
|
||||
// long time = new Date().getTime()-parseDate("2012-11-19").getTime();
|
||||
// System.out.println(time/(24*60*60*1000));
|
||||
// System.out.println(getWeekOfYear(new Date()));
|
||||
// System.out.println(formatDate(getOfDayFirst(parseDate("2015/3/6")),"yyyy-MM-dd HH:mm:ss.sss"));
|
||||
// System.out.println(formatDate(getOfDayLast(parseDate("2015/6/6")),"yyyy-MM-dd HH:mm:ss.sss"));
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -406,16 +406,4 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
|
||||
}
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 测试代码
|
||||
// * @param args
|
||||
// */
|
||||
// public static void main(String[] args) {
|
||||
// String html = "<p style=\"text-align: left; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体, tahoma, arial, sans-serif; white-space: normal; color: rgb(0,0,0); word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px\">__eol__ 科技日报讯 (通讯员辛志向 记者王建高)日前,青岛高新区国家级生态工业示范园区通过了由国家环保部及科技部、商务部三部门联合组织的国家正式验收。</p>__eol__<p style=\"text-align: left; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体, tahoma, arial, sans-serif; white-space: normal; color: rgb(0,0,0); word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px\">__eol__ 青岛市委常委、红岛经济区工委书记、管委主任陈飞介绍,建立国家生态工业示范园区是青岛高新区深化和拓展生态创建成果,加快结构调整,推进转型发展,进而辐射带动红岛经济区生态建设的重要抓手。</p>__eol__<p style=\"text-align: left; padding-bottom: 0px; text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; margin: 0px; padding-left: 0px; letter-spacing: normal; padding-right: 0px; font: 14px/25px 宋体, tahoma, arial, sans-serif; white-space: normal; color: rgb(0,0,0); word-spacing: 0px; padding-top: 0px; -webkit-text-stroke-width: 0px\">__eol__ 自2007年经国家三部委批准创建以来,青岛高新区高度重视,强化措施,始终按照创建工作要求,坚持生态、生活、生产“三生共融”理念,以生态环境的优化带动经济的发展。创建过程中,坚持“科学性规划,高水平建设,生态化发展”的原则,实施“国际化、高端化、低碳化”三大战略,逐步建立起适应循环经济发展的产业体系和政策体系,形成了完善的生态工业园建设框架和循环经济发展机制,实现了经济高速发展与环境持续改善的良性循环。随着发展环境和核心竞争力的不断提升,通过了ISO14001环境管理体系认证,获得了中国最具投资潜力经济园区、国家科技兴贸创新基地、山东省海外高层次人才创新创业基地等称号,在国家级高新区综合评价中连续多年跻身前列,已经成为山东半岛乃至环渤海区域快速成长、极具创新活力和竞争力的蓝色经济特色高科技产业园区。</p>";
|
||||
// html += "<div style=\"text-indent: 32pt\">__eol__ <span style=\"line-height: 20px; font-size: 20px\"><span style=\"font-family: 宋体\">我市积极支持和引导企业、高校院所“走出去”开展国际科技合作,在国外集聚和利用各种创新资源,增强我市企业和科研院所竞争实力。</span></span></div>__eol__<div align=\"left\" style=\"text-align: left; text-indent: 32pt; text-autospace: \">__eol__ <span style=\"line-height: 20px; font-size: 20px\"><span style=\"font-family: 宋体\">一是在国外设立研发中心。通过合资、并购、控股等方式,在国外设立研发中心和产业园,传播中国文化,提升青岛声誉。如,软控在斯洛伐克设立的研发中心成为我国与中东欧国家合作的成功样板。南车青岛四方积极筹建与泰国等高铁联合研发中心,将成为我国高端成套装备出口的重要研发基地。<span style=\"color: black\">2013</span><span style=\"color: black\">年,我市企业与大学科研机构与国外共建7家联合研发中心。 </span></span></span></div>__eol__<div style=\"text-indent: 32pt\">__eol__ <span style=\"line-height: 20px; font-size: 20px\"><span style=\"font-family: 宋体\"><span style=\"color: black\">二是利用科技资源和渠道助推中小科技企业“走出去”开发新兴市场。</span><span style=\"color: black\">利用尼日利亚总统访华契机,积极向尼方政府代表团推介我市现代农业技术和先进制造业企业。引导我市科技型企业输出技术,拓展西非共同体市场,推动企业转型升级,拓宽“走出去”渠道</span><span style=\"color: black\">。</span></span></span></div>";
|
||||
// html += "<p style='TEXT-ALIGN: center' class=\"sdfasf\"><strong>蓝海技术加以</strong></p><p style='TEXT-ALIGN: center'><em>蓝海技术加以</em></p><p style='TEXT-ALIGN: center'><span style='TEXT-DECORATION: underline'>蓝海技术加以</span></p><p style='TEXT-ALIGN: center'>蓝海技术加以</p><p></p>";
|
||||
// html = html.replaceAll("__eol__", "");
|
||||
// System.out.println(replaceMobileHtml(html));
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -106,29 +106,5 @@ public class WorkDayUtils {
|
||||
public int getHolidays(Calendar d1, Calendar d2) {
|
||||
return this.getDaysBetween(d1, d2) - this.getWorkingDay(d1, d2);
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// try {
|
||||
// String strDateStart = "2013-08-01";
|
||||
// String strDateEnd = "2014-08-31";
|
||||
//
|
||||
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
||||
// Date date_start = sdf.parse(strDateStart);
|
||||
// Date date_end = sdf.parse(strDateEnd);
|
||||
// WorkDayUtils app = new WorkDayUtils();
|
||||
// Calendar cal_start = Calendar.getInstance();
|
||||
// Calendar cal_end = Calendar.getInstance();
|
||||
// cal_start.setTime(date_start);
|
||||
// cal_end.setTime(date_end);
|
||||
// System.out.println("开始日:" + cal_start.get(Calendar.YEAR) + "-" + (cal_start.get(Calendar.MONTH) + 1)
|
||||
// + "-" + cal_start.get(Calendar.DAY_OF_MONTH) + " " + app.getChineseWeek(cal_start));
|
||||
// System.out.println("结束日:" + cal_end.get(Calendar.YEAR) + "-" + (cal_end.get(Calendar.MONTH) + 1)
|
||||
// + "-" + cal_end.get(Calendar.DAY_OF_MONTH) + " " + app.getChineseWeek(cal_end));
|
||||
// System.out.println("工作日:" + app.getWorkingDay(cal_start, cal_end));
|
||||
// System.out.println("休息日:" + app.getHolidays(cal_start, cal_end));
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,24 @@
|
||||
*/
|
||||
package com.jeesite.common.mapper;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser.Feature;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.*;
|
||||
import com.fasterxml.jackson.databind.introspect.Annotated;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.util.JSONPObject;
|
||||
import com.jeesite.common.collect.ListUtils;
|
||||
import com.jeesite.common.io.PropertiesUtils;
|
||||
import com.jeesite.common.lang.DateUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.text.SimpleDateFormat;
|
||||
@@ -12,30 +30,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser.Feature;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.introspect.Annotated;
|
||||
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
|
||||
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
|
||||
import com.fasterxml.jackson.databind.util.JSONPObject;
|
||||
import com.jeesite.common.collect.ListUtils;
|
||||
import com.jeesite.common.io.PropertiesUtils;
|
||||
import com.jeesite.common.lang.DateUtils;
|
||||
|
||||
/**
|
||||
* 简单封装Jackson,实现JSON String<->Java Object的Mapper.
|
||||
* 封装不同的输出风格, 使用不同的builder函数创建实例.
|
||||
@@ -292,30 +286,5 @@ public class JsonMapper extends ObjectMapper {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// List<Map<String, Object>> list = ListUtils.newArrayList();
|
||||
// Map<String, Object> map = MapUtils.newHashMap();
|
||||
// map.put("id", 1);
|
||||
// map.put("pId", -1);
|
||||
// map.put("name", "根节点");
|
||||
// list.add(map);
|
||||
// map = MapUtils.newHashMap();
|
||||
// map.put("id", 2);
|
||||
// map.put("pId", 1);
|
||||
// map.put("name", "你好");
|
||||
// map.put("open", true);
|
||||
// list.add(map);
|
||||
// String json = JsonMapper.toJson(list);
|
||||
// System.out.println(json);
|
||||
// List<Map<String, Object>> map2 = JsonMapper.fromJson(json, List.class);
|
||||
// System.out.println(map2);
|
||||
// Map<String, Object> map3 = JsonMapper.fromJson("{extendS1:{title:'站牌号',"
|
||||
// + "sort:1,type:'text',maxlength:0,maxlength:30},extendS2:{title:'规模分类',"
|
||||
// + "sort:2,type:'dict',dictType:'scope_category'}}", Map.class);
|
||||
// System.out.println(map3);
|
||||
// List<String> list2 = fromJson("[1,2]", List.class);
|
||||
// System.out.println(list2);
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,19 +4,18 @@
|
||||
*/
|
||||
package com.jeesite.common.mapper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.jeesite.common.io.PropertiesUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
|
||||
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.jeesite.common.io.PropertiesUtils;
|
||||
import java.io.IOException;
|
||||
import java.util.TimeZone;
|
||||
|
||||
/**
|
||||
* XML <-> Map、Object
|
||||
* 简单封装Jackson,实现XML String<->Java Object的Mapper.
|
||||
* @author ThinkGem
|
||||
* @version 2016-9-2
|
||||
*/
|
||||
@@ -24,7 +23,7 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(XmlMapper.class);
|
||||
private static final Logger logger = LoggerFactory.getLogger(XmlMapper.class);
|
||||
|
||||
/**
|
||||
* 当前类的实例持有者(静态内部类,延迟加载,懒汉式,线程安全的单例模式)
|
||||
@@ -275,38 +274,4 @@ public class XmlMapper extends com.fasterxml.jackson.dataformat.xml.XmlMapper{
|
||||
// return map;
|
||||
// }
|
||||
|
||||
// public static void main(String[] args) throws Exception {
|
||||
//
|
||||
// File file = new File(FileUtils.getProjectPath()
|
||||
// + "/../modules/core/src/main/resources/spring/spring-context-core.xml");
|
||||
// String xml = FileUtils.readFileToString(file, Charset.defaultCharset());
|
||||
//
|
||||
// System.out.println(xmlToMap(xml, true));
|
||||
// System.out.println(xmlToMapWithAttr(xml, true));
|
||||
//
|
||||
// XmlMapper m = XmlMapper.getInstance();
|
||||
// System.out.println(m.readValue(xml, List.class));
|
||||
// System.out.println(m.readValue(xml, Map.class));
|
||||
//
|
||||
// List<Map<String, Object>> list = ListUtils.newArrayList();
|
||||
// Map<String, Object> map = MapUtils.newHashMap();
|
||||
// map.put("id", 1);
|
||||
// map.put("pId", -1);
|
||||
// map.put("name", "根节点");
|
||||
// list.add(map);
|
||||
// map = MapUtils.newHashMap();
|
||||
// map.put("id", 2);
|
||||
// map.put("pId", 1);
|
||||
// map.put("name", "你好");
|
||||
// map.put("open", true);
|
||||
// list.add(map);
|
||||
//
|
||||
// String s = XmlMapper.getInstance().writeValueAsString(list);
|
||||
// System.out.println(s);
|
||||
//
|
||||
// list = XmlMapper.getInstance().readValue(s, List.class);
|
||||
// System.out.println(list);
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -435,22 +435,4 @@ public class VideoUtils {
|
||||
}
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//// VideoUtils.setFfmpegFile("d:/tools/video/ffmpeg-4.9/bin/ffmpeg.exe");
|
||||
// VideoUtils.setFfmpegFile("d:/tools/video/libav-10.6-win64/bin/avconv.exe");
|
||||
// VideoUtils.setMencoderFile("d:/tools/video/mencoder-4.9/mencoder.exe");
|
||||
// VideoUtils.setQtFaststartFile("d:/tools/video/qt-faststart/qt-faststart.exe");
|
||||
// final VideoUtils v = new VideoUtils("e:/Users/Administrator/Desktop/mp4/20150001.mp4");
|
||||
//// new Thread(new Runnable() {
|
||||
//// @Override
|
||||
//// public void run() {
|
||||
// System.out.println("img:转换" + (v.cutPic()?"成功":"失败"));
|
||||
// System.out.println("file:转换" + (v.convert()?"成功":"失败"));
|
||||
//// }
|
||||
//// }).start();
|
||||
// System.out.println("inputFile: " + v.getInputFile());
|
||||
// System.out.println("imageFile: " + v.getImgFile());
|
||||
// System.out.println("outputFile: " + v.getOutputFile());
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -188,25 +188,5 @@ public class MacUtils {
|
||||
}
|
||||
return mac == null ? "" : mac;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试用的main方法.
|
||||
*
|
||||
* @param argc 运行参数.
|
||||
*/
|
||||
public static void main(String[] argc) {
|
||||
String os = getOSName();
|
||||
System.out.println("os: " + os);
|
||||
if (os.startsWith("windows")) {
|
||||
String mac = getWindowsMACAddress();
|
||||
System.out.println("mac: " + mac);
|
||||
} else if (os.startsWith("linux")) {
|
||||
String mac = getLinuxMACAddress();
|
||||
System.out.println("mac: " + mac);
|
||||
} else {
|
||||
String mac = getUnixMACAddress();
|
||||
System.out.println("mac: " + mac);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -18,22 +18,15 @@
|
||||
*/
|
||||
package com.jeesite.common.text;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import com.jeesite.common.codec.EncodeUtils;
|
||||
|
||||
/*
|
||||
* Functions for diff, match and patch.
|
||||
* Computes the difference between two texts to create a patch.
|
||||
@@ -90,12 +83,12 @@ public class DiffMatchPatch {
|
||||
* Internal class for returning results from diff_linesToChars().
|
||||
* Other less paranoid languages just use a three-element array.
|
||||
*/
|
||||
protected static class LinesToCharsResult {
|
||||
protected String chars1;
|
||||
protected String chars2;
|
||||
protected List<String> lineArray;
|
||||
public static class LinesToCharsResult {
|
||||
public String chars1;
|
||||
public String chars2;
|
||||
public List<String> lineArray;
|
||||
|
||||
protected LinesToCharsResult(String chars1, String chars2,
|
||||
public LinesToCharsResult(String chars1, String chars2,
|
||||
List<String> lineArray) {
|
||||
this.chars1 = chars1;
|
||||
this.chars2 = chars2;
|
||||
@@ -362,7 +355,7 @@ public class DiffMatchPatch {
|
||||
* @param deadline Time at which to bail if not yet complete.
|
||||
* @return LinkedList of Diff objects.
|
||||
*/
|
||||
protected LinkedList<Diff> diff_bisect(String text1, String text2,
|
||||
public LinkedList<Diff> diff_bisect(String text1, String text2,
|
||||
long deadline) {
|
||||
// Cache the text lengths to prevent multiple calls.
|
||||
int text1_length = text1.length();
|
||||
@@ -509,7 +502,7 @@ public class DiffMatchPatch {
|
||||
* the List of unique strings. The zeroth element of the List of
|
||||
* unique strings is intentionally blank.
|
||||
*/
|
||||
protected LinesToCharsResult diff_linesToChars(String text1, String text2) {
|
||||
public LinesToCharsResult diff_linesToChars(String text1, String text2) {
|
||||
List<String> lineArray = new ArrayList<String>();
|
||||
Map<String, Integer> lineHash = new HashMap<String, Integer>();
|
||||
// e.g. linearray[4] == "Hello\n"
|
||||
@@ -566,7 +559,7 @@ public class DiffMatchPatch {
|
||||
* @param diffs LinkedList of Diff objects.
|
||||
* @param lineArray List of unique strings.
|
||||
*/
|
||||
protected void diff_charsToLines(LinkedList<Diff> diffs,
|
||||
public void diff_charsToLines(LinkedList<Diff> diffs,
|
||||
List<String> lineArray) {
|
||||
StringBuilder text;
|
||||
for (Diff diff : diffs) {
|
||||
@@ -621,7 +614,7 @@ public class DiffMatchPatch {
|
||||
* @return The number of characters common to the end of the first
|
||||
* string and the start of the second string.
|
||||
*/
|
||||
protected int diff_commonOverlap(String text1, String text2) {
|
||||
public int diff_commonOverlap(String text1, String text2) {
|
||||
// Cache the text lengths to prevent multiple calls.
|
||||
int text1_length = text1.length();
|
||||
int text2_length = text2.length();
|
||||
@@ -671,7 +664,7 @@ public class DiffMatchPatch {
|
||||
* suffix of text1, the prefix of text2, the suffix of text2 and the
|
||||
* common middle. Or null if there was no match.
|
||||
*/
|
||||
protected String[] diff_halfMatch(String text1, String text2) {
|
||||
public String[] diff_halfMatch(String text1, String text2) {
|
||||
if (Diff_Timeout <= 0) {
|
||||
// Don't risk returning a non-optimal diff if we have unlimited time.
|
||||
return null;
|
||||
@@ -1580,7 +1573,7 @@ public class DiffMatchPatch {
|
||||
* @param loc The location to search around.
|
||||
* @return Best match index or -1.
|
||||
*/
|
||||
protected int match_bitap(String text, String pattern, int loc) {
|
||||
public int match_bitap(String text, String pattern, int loc) {
|
||||
assert (Match_MaxBits == 0 || pattern.length() <= Match_MaxBits)
|
||||
: "Pattern too long for this application.";
|
||||
|
||||
@@ -1698,7 +1691,7 @@ public class DiffMatchPatch {
|
||||
* @param pattern The text to encode.
|
||||
* @return Hash of character locations.
|
||||
*/
|
||||
protected Map<Character, Integer> match_alphabet(String pattern) {
|
||||
public Map<Character, Integer> match_alphabet(String pattern) {
|
||||
Map<Character, Integer> s = new HashMap<Character, Integer>();
|
||||
char[] char_pattern = pattern.toCharArray();
|
||||
for (char c : char_pattern) {
|
||||
@@ -1722,7 +1715,7 @@ public class DiffMatchPatch {
|
||||
* @param patch The patch to grow.
|
||||
* @param text Source text.
|
||||
*/
|
||||
protected void patch_addContext(Patch patch, String text) {
|
||||
public void patch_addContext(Patch patch, String text) {
|
||||
if (text.length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1,954 +0,0 @@
|
||||
/*
|
||||
* Test harness for diff_match_patch.java
|
||||
*
|
||||
* Copyright 2006 Google Inc.
|
||||
* http://code.google.com/p/google-diff-match-patch/
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.jeesite.common.text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.jeesite.common.text.DiffMatchPatch.Diff;
|
||||
import com.jeesite.common.text.DiffMatchPatch.LinesToCharsResult;
|
||||
import com.jeesite.common.text.DiffMatchPatch.Patch;
|
||||
|
||||
public class DiffMatchPatchTest {
|
||||
|
||||
private DiffMatchPatch dmp;
|
||||
private DiffMatchPatch.Operation DELETE = DiffMatchPatch.Operation.DELETE;
|
||||
private DiffMatchPatch.Operation EQUAL = DiffMatchPatch.Operation.EQUAL;
|
||||
private DiffMatchPatch.Operation INSERT = DiffMatchPatch.Operation.INSERT;
|
||||
|
||||
public DiffMatchPatchTest() {
|
||||
// Create an instance of the diff_match_patch object.
|
||||
dmp = new DiffMatchPatch();
|
||||
}
|
||||
|
||||
|
||||
// DIFF TEST FUNCTIONS
|
||||
|
||||
public void testDiffCommonPrefix() {
|
||||
// Detect any common prefix.
|
||||
assertEquals("diff_commonPrefix: Null case.", 0, dmp.diff_commonPrefix("abc", "xyz"));
|
||||
|
||||
assertEquals("diff_commonPrefix: Non-null case.", 4, dmp.diff_commonPrefix("1234abcdef", "1234xyz"));
|
||||
|
||||
assertEquals("diff_commonPrefix: Whole case.", 4, dmp.diff_commonPrefix("1234", "1234xyz"));
|
||||
}
|
||||
|
||||
public void testDiffCommonSuffix() {
|
||||
// Detect any common suffix.
|
||||
assertEquals("diff_commonSuffix: Null case.", 0, dmp.diff_commonSuffix("abc", "xyz"));
|
||||
|
||||
assertEquals("diff_commonSuffix: Non-null case.", 4, dmp.diff_commonSuffix("abcdef1234", "xyz1234"));
|
||||
|
||||
assertEquals("diff_commonSuffix: Whole case.", 4, dmp.diff_commonSuffix("1234", "xyz1234"));
|
||||
}
|
||||
|
||||
public void testDiffCommonOverlap() {
|
||||
// Detect any suffix/prefix overlap.
|
||||
assertEquals("diff_commonOverlap: Null case.", 0, dmp.diff_commonOverlap("", "abcd"));
|
||||
|
||||
assertEquals("diff_commonOverlap: Whole case.", 3, dmp.diff_commonOverlap("abc", "abcd"));
|
||||
|
||||
assertEquals("diff_commonOverlap: No overlap.", 0, dmp.diff_commonOverlap("123456", "abcd"));
|
||||
|
||||
assertEquals("diff_commonOverlap: Overlap.", 3, dmp.diff_commonOverlap("123456xxx", "xxxabcd"));
|
||||
|
||||
// Some overly clever languages (C#) may treat ligatures as equal to their
|
||||
// component letters. E.g. U+FB01 == 'fi'
|
||||
assertEquals("diff_commonOverlap: Unicode.", 0, dmp.diff_commonOverlap("fi", "\ufb01i"));
|
||||
}
|
||||
|
||||
public void testDiffHalfmatch() {
|
||||
// Detect a halfmatch.
|
||||
dmp.Diff_Timeout = 1;
|
||||
assertNull("diff_halfMatch: No match #1.", dmp.diff_halfMatch("1234567890", "abcdef"));
|
||||
|
||||
assertNull("diff_halfMatch: No match #2.", dmp.diff_halfMatch("12345", "23"));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Single Match #1.", new String[]{"12", "90", "a", "z", "345678"}, dmp.diff_halfMatch("1234567890", "a345678z"));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Single Match #2.", new String[]{"a", "z", "12", "90", "345678"}, dmp.diff_halfMatch("a345678z", "1234567890"));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Single Match #3.", new String[]{"abc", "z", "1234", "0", "56789"}, dmp.diff_halfMatch("abc56789z", "1234567890"));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Single Match #4.", new String[]{"a", "xyz", "1", "7890", "23456"}, dmp.diff_halfMatch("a23456xyz", "1234567890"));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Multiple Matches #1.", new String[]{"12123", "123121", "a", "z", "1234123451234"}, dmp.diff_halfMatch("121231234123451234123121", "a1234123451234z"));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Multiple Matches #2.", new String[]{"", "-=-=-=-=-=", "x", "", "x-=-=-=-=-=-=-="}, dmp.diff_halfMatch("x-=-=-=-=-=-=-=-=-=-=-=-=", "xx-=-=-=-=-=-=-="));
|
||||
|
||||
assertArrayEquals("diff_halfMatch: Multiple Matches #3.", new String[]{"-=-=-=-=-=", "", "", "y", "-=-=-=-=-=-=-=y"}, dmp.diff_halfMatch("-=-=-=-=-=-=-=-=-=-=-=-=y", "-=-=-=-=-=-=-=yy"));
|
||||
|
||||
// Optimal diff would be -q+x=H-i+e=lloHe+Hu=llo-Hew+y not -qHillo+x=HelloHe-w+Hulloy
|
||||
assertArrayEquals("diff_halfMatch: Non-optimal halfmatch.", new String[]{"qHillo", "w", "x", "Hulloy", "HelloHe"}, dmp.diff_halfMatch("qHilloHelloHew", "xHelloHeHulloy"));
|
||||
|
||||
dmp.Diff_Timeout = 0;
|
||||
assertNull("diff_halfMatch: Optimal no halfmatch.", dmp.diff_halfMatch("qHilloHelloHew", "xHelloHeHulloy"));
|
||||
}
|
||||
|
||||
public void testDiffLinesToChars() {
|
||||
// Convert lines down to characters.
|
||||
ArrayList<String> tmpVector = new ArrayList<String>();
|
||||
tmpVector.add("");
|
||||
tmpVector.add("alpha\n");
|
||||
tmpVector.add("beta\n");
|
||||
assertLinesToCharsResultEquals("diff_linesToChars: Shared lines.", new LinesToCharsResult("\u0001\u0002\u0001", "\u0002\u0001\u0002", tmpVector), dmp.diff_linesToChars("alpha\nbeta\nalpha\n", "beta\nalpha\nbeta\n"));
|
||||
|
||||
tmpVector.clear();
|
||||
tmpVector.add("");
|
||||
tmpVector.add("alpha\r\n");
|
||||
tmpVector.add("beta\r\n");
|
||||
tmpVector.add("\r\n");
|
||||
assertLinesToCharsResultEquals("diff_linesToChars: Empty string and blank lines.", new LinesToCharsResult("", "\u0001\u0002\u0003\u0003", tmpVector), dmp.diff_linesToChars("", "alpha\r\nbeta\r\n\r\n\r\n"));
|
||||
|
||||
tmpVector.clear();
|
||||
tmpVector.add("");
|
||||
tmpVector.add("a");
|
||||
tmpVector.add("b");
|
||||
assertLinesToCharsResultEquals("diff_linesToChars: No linebreaks.", new LinesToCharsResult("\u0001", "\u0002", tmpVector), dmp.diff_linesToChars("a", "b"));
|
||||
|
||||
// More than 256 to reveal any 8-bit limitations.
|
||||
int n = 300;
|
||||
tmpVector.clear();
|
||||
StringBuilder lineList = new StringBuilder();
|
||||
StringBuilder charList = new StringBuilder();
|
||||
for (int x = 1; x < n + 1; x++) {
|
||||
tmpVector.add(x + "\n");
|
||||
lineList.append(x + "\n");
|
||||
charList.append(String.valueOf((char) x));
|
||||
}
|
||||
assertEquals(n, tmpVector.size());
|
||||
String lines = lineList.toString();
|
||||
String chars = charList.toString();
|
||||
assertEquals(n, chars.length());
|
||||
tmpVector.add(0, "");
|
||||
assertLinesToCharsResultEquals("diff_linesToChars: More than 256.", new LinesToCharsResult(chars, "", tmpVector), dmp.diff_linesToChars(lines, ""));
|
||||
}
|
||||
|
||||
public void testDiffCharsToLines() {
|
||||
// First check that Diff equality works.
|
||||
assertTrue("diff_charsToLines: Equality #1.", new Diff(EQUAL, "a").equals(new Diff(EQUAL, "a")));
|
||||
|
||||
assertEquals("diff_charsToLines: Equality #2.", new Diff(EQUAL, "a"), new Diff(EQUAL, "a"));
|
||||
|
||||
// Convert chars up to lines.
|
||||
LinkedList<Diff> diffs = diffList(new Diff(EQUAL, "\u0001\u0002\u0001"), new Diff(INSERT, "\u0002\u0001\u0002"));
|
||||
ArrayList<String> tmpVector = new ArrayList<String>();
|
||||
tmpVector.add("");
|
||||
tmpVector.add("alpha\n");
|
||||
tmpVector.add("beta\n");
|
||||
dmp.diff_charsToLines(diffs, tmpVector);
|
||||
assertEquals("diff_charsToLines: Shared lines.", diffList(new Diff(EQUAL, "alpha\nbeta\nalpha\n"), new Diff(INSERT, "beta\nalpha\nbeta\n")), diffs);
|
||||
|
||||
// More than 256 to reveal any 8-bit limitations.
|
||||
int n = 300;
|
||||
tmpVector.clear();
|
||||
StringBuilder lineList = new StringBuilder();
|
||||
StringBuilder charList = new StringBuilder();
|
||||
for (int x = 1; x < n + 1; x++) {
|
||||
tmpVector.add(x + "\n");
|
||||
lineList.append(x + "\n");
|
||||
charList.append(String.valueOf((char) x));
|
||||
}
|
||||
assertEquals(n, tmpVector.size());
|
||||
String lines = lineList.toString();
|
||||
String chars = charList.toString();
|
||||
assertEquals(n, chars.length());
|
||||
tmpVector.add(0, "");
|
||||
diffs = diffList(new Diff(DELETE, chars));
|
||||
dmp.diff_charsToLines(diffs, tmpVector);
|
||||
assertEquals("diff_charsToLines: More than 256.", diffList(new Diff(DELETE, lines)), diffs);
|
||||
}
|
||||
|
||||
public void testDiffCleanupMerge() {
|
||||
// Cleanup a messy diff.
|
||||
LinkedList<Diff> diffs = diffList();
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Null case.", diffList(), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "b"), new Diff(INSERT, "c"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: No change case.", diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "b"), new Diff(INSERT, "c")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(EQUAL, "b"), new Diff(EQUAL, "c"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Merge equalities.", diffList(new Diff(EQUAL, "abc")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "a"), new Diff(DELETE, "b"), new Diff(DELETE, "c"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Merge deletions.", diffList(new Diff(DELETE, "abc")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(INSERT, "a"), new Diff(INSERT, "b"), new Diff(INSERT, "c"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Merge insertions.", diffList(new Diff(INSERT, "abc")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "a"), new Diff(INSERT, "b"), new Diff(DELETE, "c"), new Diff(INSERT, "d"), new Diff(EQUAL, "e"), new Diff(EQUAL, "f"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Merge interweave.", diffList(new Diff(DELETE, "ac"), new Diff(INSERT, "bd"), new Diff(EQUAL, "ef")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "a"), new Diff(INSERT, "abc"), new Diff(DELETE, "dc"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Prefix and suffix detection.", diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "d"), new Diff(INSERT, "b"), new Diff(EQUAL, "c")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "x"), new Diff(DELETE, "a"), new Diff(INSERT, "abc"), new Diff(DELETE, "dc"), new Diff(EQUAL, "y"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Prefix and suffix detection with equalities.", diffList(new Diff(EQUAL, "xa"), new Diff(DELETE, "d"), new Diff(INSERT, "b"), new Diff(EQUAL, "cy")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(INSERT, "ba"), new Diff(EQUAL, "c"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Slide edit left.", diffList(new Diff(INSERT, "ab"), new Diff(EQUAL, "ac")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "c"), new Diff(INSERT, "ab"), new Diff(EQUAL, "a"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Slide edit right.", diffList(new Diff(EQUAL, "ca"), new Diff(INSERT, "ba")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "b"), new Diff(EQUAL, "c"), new Diff(DELETE, "ac"), new Diff(EQUAL, "x"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Slide edit left recursive.", diffList(new Diff(DELETE, "abc"), new Diff(EQUAL, "acx")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "x"), new Diff(DELETE, "ca"), new Diff(EQUAL, "c"), new Diff(DELETE, "b"), new Diff(EQUAL, "a"));
|
||||
dmp.diff_cleanupMerge(diffs);
|
||||
assertEquals("diff_cleanupMerge: Slide edit right recursive.", diffList(new Diff(EQUAL, "xca"), new Diff(DELETE, "cba")), diffs);
|
||||
}
|
||||
|
||||
public void testDiffCleanupSemanticLossless() {
|
||||
// Slide diffs to match logical boundaries.
|
||||
LinkedList<Diff> diffs = diffList();
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Null case.", diffList(), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "AAA\r\n\r\nBBB"), new Diff(INSERT, "\r\nDDD\r\n\r\nBBB"), new Diff(EQUAL, "\r\nEEE"));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Blank lines.", diffList(new Diff(EQUAL, "AAA\r\n\r\n"), new Diff(INSERT, "BBB\r\nDDD\r\n\r\n"), new Diff(EQUAL, "BBB\r\nEEE")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "AAA\r\nBBB"), new Diff(INSERT, " DDD\r\nBBB"), new Diff(EQUAL, " EEE"));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Line boundaries.", diffList(new Diff(EQUAL, "AAA\r\n"), new Diff(INSERT, "BBB DDD\r\n"), new Diff(EQUAL, "BBB EEE")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "The c"), new Diff(INSERT, "ow and the c"), new Diff(EQUAL, "at."));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Word boundaries.", diffList(new Diff(EQUAL, "The "), new Diff(INSERT, "cow and the "), new Diff(EQUAL, "cat.")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "The-c"), new Diff(INSERT, "ow-and-the-c"), new Diff(EQUAL, "at."));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Alphanumeric boundaries.", diffList(new Diff(EQUAL, "The-"), new Diff(INSERT, "cow-and-the-"), new Diff(EQUAL, "cat.")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "a"), new Diff(EQUAL, "ax"));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Hitting the start.", diffList(new Diff(DELETE, "a"), new Diff(EQUAL, "aax")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "xa"), new Diff(DELETE, "a"), new Diff(EQUAL, "a"));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Hitting the end.", diffList(new Diff(EQUAL, "xaa"), new Diff(DELETE, "a")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "The xxx. The "), new Diff(INSERT, "zzz. The "), new Diff(EQUAL, "yyy."));
|
||||
dmp.diff_cleanupSemanticLossless(diffs);
|
||||
assertEquals("diff_cleanupSemanticLossless: Sentence boundaries.", diffList(new Diff(EQUAL, "The xxx."), new Diff(INSERT, " The zzz."), new Diff(EQUAL, " The yyy.")), diffs);
|
||||
}
|
||||
|
||||
public void testDiffCleanupSemantic() {
|
||||
// Cleanup semantically trivial equalities.
|
||||
LinkedList<Diff> diffs = diffList();
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Null case.", diffList(), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "cd"), new Diff(EQUAL, "12"), new Diff(DELETE, "e"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: No elimination #1.", diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "cd"), new Diff(EQUAL, "12"), new Diff(DELETE, "e")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "abc"), new Diff(INSERT, "ABC"), new Diff(EQUAL, "1234"), new Diff(DELETE, "wxyz"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: No elimination #2.", diffList(new Diff(DELETE, "abc"), new Diff(INSERT, "ABC"), new Diff(EQUAL, "1234"), new Diff(DELETE, "wxyz")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "a"), new Diff(EQUAL, "b"), new Diff(DELETE, "c"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Simple elimination.", diffList(new Diff(DELETE, "abc"), new Diff(INSERT, "b")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "ab"), new Diff(EQUAL, "cd"), new Diff(DELETE, "e"), new Diff(EQUAL, "f"), new Diff(INSERT, "g"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Backpass elimination.", diffList(new Diff(DELETE, "abcdef"), new Diff(INSERT, "cdfg")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(INSERT, "1"), new Diff(EQUAL, "A"), new Diff(DELETE, "B"), new Diff(INSERT, "2"), new Diff(EQUAL, "_"), new Diff(INSERT, "1"), new Diff(EQUAL, "A"), new Diff(DELETE, "B"), new Diff(INSERT, "2"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Multiple elimination.", diffList(new Diff(DELETE, "AB_AB"), new Diff(INSERT, "1A2_1A2")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "The c"), new Diff(DELETE, "ow and the c"), new Diff(EQUAL, "at."));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Word boundaries.", diffList(new Diff(EQUAL, "The "), new Diff(DELETE, "cow and the "), new Diff(EQUAL, "cat.")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "abcxx"), new Diff(INSERT, "xxdef"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: No overlap elimination.", diffList(new Diff(DELETE, "abcxx"), new Diff(INSERT, "xxdef")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "abcxxx"), new Diff(INSERT, "xxxdef"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Overlap elimination.", diffList(new Diff(DELETE, "abc"), new Diff(EQUAL, "xxx"), new Diff(INSERT, "def")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "xxxabc"), new Diff(INSERT, "defxxx"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Reverse overlap elimination.", diffList(new Diff(INSERT, "def"), new Diff(EQUAL, "xxx"), new Diff(DELETE, "abc")), diffs);
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "abcd1212"), new Diff(INSERT, "1212efghi"), new Diff(EQUAL, "----"), new Diff(DELETE, "A3"), new Diff(INSERT, "3BC"));
|
||||
dmp.diff_cleanupSemantic(diffs);
|
||||
assertEquals("diff_cleanupSemantic: Two overlap eliminations.", diffList(new Diff(DELETE, "abcd"), new Diff(EQUAL, "1212"), new Diff(INSERT, "efghi"), new Diff(EQUAL, "----"), new Diff(DELETE, "A"), new Diff(EQUAL, "3"), new Diff(INSERT, "BC")), diffs);
|
||||
}
|
||||
|
||||
public void testDiffCleanupEfficiency() {
|
||||
// Cleanup operationally trivial equalities.
|
||||
dmp.Diff_EditCost = 4;
|
||||
LinkedList<Diff> diffs = diffList();
|
||||
dmp.diff_cleanupEfficiency(diffs);
|
||||
assertEquals("diff_cleanupEfficiency: Null case.", diffList(), diffs);
|
||||
System.out.println(dmp.diff_prettyHtml(diffs));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "12"), new Diff(EQUAL, "wxyz"), new Diff(DELETE, "cd"), new Diff(INSERT, "34"));
|
||||
dmp.diff_cleanupEfficiency(diffs);
|
||||
assertEquals("diff_cleanupEfficiency: No elimination.", diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "12"), new Diff(EQUAL, "wxyz"), new Diff(DELETE, "cd"), new Diff(INSERT, "34")), diffs);
|
||||
System.out.println(dmp.diff_prettyHtml(diffs));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "12"), new Diff(EQUAL, "xyz"), new Diff(DELETE, "cd"), new Diff(INSERT, "34"));
|
||||
dmp.diff_cleanupEfficiency(diffs);
|
||||
assertEquals("diff_cleanupEfficiency: Four-edit elimination.", diffList(new Diff(DELETE, "abxyzcd"), new Diff(INSERT, "12xyz34")), diffs);
|
||||
System.out.println(dmp.diff_prettyHtml(diffs));
|
||||
|
||||
diffs = diffList(new Diff(INSERT, "12"), new Diff(EQUAL, "x"), new Diff(DELETE, "cd"), new Diff(INSERT, "34"));
|
||||
dmp.diff_cleanupEfficiency(diffs);
|
||||
assertEquals("diff_cleanupEfficiency: Three-edit elimination.", diffList(new Diff(DELETE, "xcd"), new Diff(INSERT, "12x34")), diffs);
|
||||
System.out.println(dmp.diff_prettyHtml(diffs));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "12"), new Diff(EQUAL, "xy"), new Diff(INSERT, "34"), new Diff(EQUAL, "z"), new Diff(DELETE, "cd"), new Diff(INSERT, "56"));
|
||||
dmp.diff_cleanupEfficiency(diffs);
|
||||
assertEquals("diff_cleanupEfficiency: Backpass elimination.", diffList(new Diff(DELETE, "abxyzcd"), new Diff(INSERT, "12xy34z56")), diffs);
|
||||
System.out.println(dmp.diff_prettyHtml(diffs));
|
||||
|
||||
dmp.Diff_EditCost = 5;
|
||||
diffs = diffList(new Diff(DELETE, "ab"), new Diff(INSERT, "12"), new Diff(EQUAL, "wxyz"), new Diff(DELETE, "cd"), new Diff(INSERT, "34"));
|
||||
dmp.diff_cleanupEfficiency(diffs);
|
||||
assertEquals("diff_cleanupEfficiency: High cost elimination.", diffList(new Diff(DELETE, "abwxyzcd"), new Diff(INSERT, "12wxyz34")), diffs);
|
||||
dmp.Diff_EditCost = 4;
|
||||
}
|
||||
|
||||
public void testDiffPrettyHtml() {
|
||||
// Pretty print.
|
||||
LinkedList<Diff> diffs = diffList(new Diff(EQUAL, "a\n"), new Diff(DELETE, "<B>b</B>"), new Diff(INSERT, "c&d"));
|
||||
assertEquals("diff_prettyHtml:", "<span>a¶<br></span><del style=\"background:#ffe6e6;\"><B>b</B></del><ins style=\"background:#e6ffe6;\">c&d</ins>", dmp.diff_prettyHtml(diffs));
|
||||
}
|
||||
|
||||
public void testDiffText() {
|
||||
// Compute the source and destination texts.
|
||||
LinkedList<Diff> diffs = diffList(new Diff(EQUAL, "jump"), new Diff(DELETE, "s"), new Diff(INSERT, "ed"), new Diff(EQUAL, " over "), new Diff(DELETE, "the"), new Diff(INSERT, "a"), new Diff(EQUAL, " lazy"));
|
||||
assertEquals("diff_text1:", "jumps over the lazy", dmp.diff_text1(diffs));
|
||||
assertEquals("diff_text2:", "jumped over a lazy", dmp.diff_text2(diffs));
|
||||
}
|
||||
|
||||
public void testDiffDelta() {
|
||||
// Convert a diff into delta string.
|
||||
LinkedList<Diff> diffs = diffList(new Diff(EQUAL, "jump"), new Diff(DELETE, "s"), new Diff(INSERT, "ed"), new Diff(EQUAL, " over "), new Diff(DELETE, "the"), new Diff(INSERT, "a"), new Diff(EQUAL, " lazy"), new Diff(INSERT, "old dog"));
|
||||
String text1 = dmp.diff_text1(diffs);
|
||||
assertEquals("diff_text1: Base text.", "jumps over the lazy", text1);
|
||||
|
||||
String delta = dmp.diff_toDelta(diffs);
|
||||
assertEquals("diff_toDelta:", "=4\t-1\t+ed\t=6\t-3\t+a\t=5\t+old dog", delta);
|
||||
|
||||
// Convert delta string into a diff.
|
||||
assertEquals("diff_fromDelta: Normal.", diffs, dmp.diff_fromDelta(text1, delta));
|
||||
|
||||
// Generates error (19 < 20).
|
||||
try {
|
||||
dmp.diff_fromDelta(text1 + "x", delta);
|
||||
fail("diff_fromDelta: Too long.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Exception expected.
|
||||
}
|
||||
|
||||
// Generates error (19 > 18).
|
||||
try {
|
||||
dmp.diff_fromDelta(text1.substring(1), delta);
|
||||
fail("diff_fromDelta: Too short.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Exception expected.
|
||||
}
|
||||
|
||||
// Generates error (%c3%xy invalid Unicode).
|
||||
try {
|
||||
dmp.diff_fromDelta("", "+%c3%xy");
|
||||
fail("diff_fromDelta: Invalid character.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Exception expected.
|
||||
}
|
||||
|
||||
// Test deltas with special characters.
|
||||
diffs = diffList(new Diff(EQUAL, "\u0680 \000 \t %"), new Diff(DELETE, "\u0681 \001 \n ^"), new Diff(INSERT, "\u0682 \002 \\ |"));
|
||||
text1 = dmp.diff_text1(diffs);
|
||||
assertEquals("diff_text1: Unicode text.", "\u0680 \000 \t %\u0681 \001 \n ^", text1);
|
||||
|
||||
delta = dmp.diff_toDelta(diffs);
|
||||
assertEquals("diff_toDelta: Unicode.", "=7\t-7\t+%DA%82 %02 %5C %7C", delta);
|
||||
|
||||
assertEquals("diff_fromDelta: Unicode.", diffs, dmp.diff_fromDelta(text1, delta));
|
||||
|
||||
// Verify pool of unchanged characters.
|
||||
diffs = diffList(new Diff(INSERT, "A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; / ? : @ & = + $ , # "));
|
||||
String text2 = dmp.diff_text2(diffs);
|
||||
assertEquals("diff_text2: Unchanged characters.", "A-Z a-z 0-9 - _ . ! ~ * \' ( ) ; / ? : @ & = + $ , # ", text2);
|
||||
|
||||
delta = dmp.diff_toDelta(diffs);
|
||||
assertEquals("diff_toDelta: Unchanged characters.", "+A-Z a-z 0-9 - _ . ! ~ * \' ( ) ; / ? : @ & = + $ , # ", delta);
|
||||
|
||||
// Convert delta string into a diff.
|
||||
assertEquals("diff_fromDelta: Unchanged characters.", diffs, dmp.diff_fromDelta("", delta));
|
||||
}
|
||||
|
||||
public void testDiffXIndex() {
|
||||
// Translate a location in text1 to text2.
|
||||
LinkedList<Diff> diffs = diffList(new Diff(DELETE, "a"), new Diff(INSERT, "1234"), new Diff(EQUAL, "xyz"));
|
||||
assertEquals("diff_xIndex: Translation on equality.", 5, dmp.diff_xIndex(diffs, 2));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "1234"), new Diff(EQUAL, "xyz"));
|
||||
assertEquals("diff_xIndex: Translation on deletion.", 1, dmp.diff_xIndex(diffs, 3));
|
||||
}
|
||||
|
||||
public void testDiffLevenshtein() {
|
||||
LinkedList<Diff> diffs = diffList(new Diff(DELETE, "abc"), new Diff(INSERT, "1234"), new Diff(EQUAL, "xyz"));
|
||||
assertEquals("Levenshtein with trailing equality.", 4, dmp.diff_levenshtein(diffs));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "xyz"), new Diff(DELETE, "abc"), new Diff(INSERT, "1234"));
|
||||
assertEquals("Levenshtein with leading equality.", 4, dmp.diff_levenshtein(diffs));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "abc"), new Diff(EQUAL, "xyz"), new Diff(INSERT, "1234"));
|
||||
assertEquals("Levenshtein with middle equality.", 7, dmp.diff_levenshtein(diffs));
|
||||
}
|
||||
|
||||
public void testDiffBisect() {
|
||||
// Normal.
|
||||
String a = "cat";
|
||||
String b = "map";
|
||||
// Since the resulting diff hasn't been normalized, it would be ok if
|
||||
// the insertion and deletion pairs are swapped.
|
||||
// If the order changes, tweak this test as required.
|
||||
LinkedList<Diff> diffs = diffList(new Diff(DELETE, "c"), new Diff(INSERT, "m"), new Diff(EQUAL, "a"), new Diff(DELETE, "t"), new Diff(INSERT, "p"));
|
||||
assertEquals("diff_bisect: Normal.", diffs, dmp.diff_bisect(a, b, Long.MAX_VALUE));
|
||||
|
||||
// Timeout.
|
||||
diffs = diffList(new Diff(DELETE, "cat"), new Diff(INSERT, "map"));
|
||||
assertEquals("diff_bisect: Timeout.", diffs, dmp.diff_bisect(a, b, 0));
|
||||
}
|
||||
|
||||
public void testDiffMain() {
|
||||
// Perform a trivial diff.
|
||||
LinkedList<Diff> diffs = diffList();
|
||||
assertEquals("diff_main: Null case.", diffs, dmp.diff_main("", "", false));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "abc"));
|
||||
assertEquals("diff_main: Equality.", diffs, dmp.diff_main("abc", "abc", false));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "ab"), new Diff(INSERT, "123"), new Diff(EQUAL, "c"));
|
||||
assertEquals("diff_main: Simple insertion.", diffs, dmp.diff_main("abc", "ab123c", false));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "123"), new Diff(EQUAL, "bc"));
|
||||
assertEquals("diff_main: Simple deletion.", diffs, dmp.diff_main("a123bc", "abc", false));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(INSERT, "123"), new Diff(EQUAL, "b"), new Diff(INSERT, "456"), new Diff(EQUAL, "c"));
|
||||
assertEquals("diff_main: Two insertions.", diffs, dmp.diff_main("abc", "a123b456c", false));
|
||||
|
||||
diffs = diffList(new Diff(EQUAL, "a"), new Diff(DELETE, "123"), new Diff(EQUAL, "b"), new Diff(DELETE, "456"), new Diff(EQUAL, "c"));
|
||||
assertEquals("diff_main: Two deletions.", diffs, dmp.diff_main("a123b456c", "abc", false));
|
||||
|
||||
// Perform a real diff.
|
||||
// Switch off the timeout.
|
||||
dmp.Diff_Timeout = 0;
|
||||
diffs = diffList(new Diff(DELETE, "a"), new Diff(INSERT, "b"));
|
||||
assertEquals("diff_main: Simple case #1.", diffs, dmp.diff_main("a", "b", false));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "Apple"), new Diff(INSERT, "Banana"), new Diff(EQUAL, "s are a"), new Diff(INSERT, "lso"), new Diff(EQUAL, " fruit."));
|
||||
assertEquals("diff_main: Simple case #2.", diffs, dmp.diff_main("Apples are a fruit.", "Bananas are also fruit.", false));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "a"), new Diff(INSERT, "\u0680"), new Diff(EQUAL, "x"), new Diff(DELETE, "\t"), new Diff(INSERT, "\000"));
|
||||
assertEquals("diff_main: Simple case #3.", diffs, dmp.diff_main("ax\t", "\u0680x\000", false));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "1"), new Diff(EQUAL, "a"), new Diff(DELETE, "y"), new Diff(EQUAL, "b"), new Diff(DELETE, "2"), new Diff(INSERT, "xab"));
|
||||
assertEquals("diff_main: Overlap #1.", diffs, dmp.diff_main("1ayb2", "abxab", false));
|
||||
|
||||
diffs = diffList(new Diff(INSERT, "xaxcx"), new Diff(EQUAL, "abc"), new Diff(DELETE, "y"));
|
||||
assertEquals("diff_main: Overlap #2.", diffs, dmp.diff_main("abcy", "xaxcxabc", false));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "ABCD"), new Diff(EQUAL, "a"), new Diff(DELETE, "="), new Diff(INSERT, "-"), new Diff(EQUAL, "bcd"), new Diff(DELETE, "="), new Diff(INSERT, "-"), new Diff(EQUAL, "efghijklmnopqrs"), new Diff(DELETE, "EFGHIJKLMNOefg"));
|
||||
assertEquals("diff_main: Overlap #3.", diffs, dmp.diff_main("ABCDa=bcd=efghijklmnopqrsEFGHIJKLMNOefg", "a-bcd-efghijklmnopqrs", false));
|
||||
|
||||
diffs = diffList(new Diff(INSERT, " "), new Diff(EQUAL, "a"), new Diff(INSERT, "nd"), new Diff(EQUAL, " [[Pennsylvania]]"), new Diff(DELETE, " and [[New"));
|
||||
assertEquals("diff_main: Large equality.", diffs, dmp.diff_main("a [[Pennsylvania]] and [[New", " and [[Pennsylvania]]", false));
|
||||
|
||||
dmp.Diff_Timeout = 0.1f; // 100ms
|
||||
String a = "`Twas brillig, and the slithy toves\nDid gyre and gimble in the wabe:\nAll mimsy were the borogoves,\nAnd the mome raths outgrabe.\n";
|
||||
String b = "I am the very model of a modern major general,\nI've information vegetable, animal, and mineral,\nI know the kings of England, and I quote the fights historical,\nFrom Marathon to Waterloo, in order categorical.\n";
|
||||
// Increase the text lengths by 1024 times to ensure a timeout.
|
||||
for (int x = 0; x < 10; x++) {
|
||||
a = a + a;
|
||||
b = b + b;
|
||||
}
|
||||
long startTime = System.currentTimeMillis();
|
||||
dmp.diff_main(a, b);
|
||||
long endTime = System.currentTimeMillis();
|
||||
// Test that we took at least the timeout period.
|
||||
assertTrue("diff_main: Timeout min.", dmp.Diff_Timeout * 1000 <= endTime - startTime);
|
||||
// Test that we didn't take forever (be forgiving).
|
||||
// Theoretically this test could fail very occasionally if the
|
||||
// OS task swaps or locks up for a second at the wrong moment.
|
||||
assertTrue("diff_main: Timeout max.", dmp.Diff_Timeout * 1000 * 2 > endTime - startTime);
|
||||
dmp.Diff_Timeout = 0;
|
||||
|
||||
// Test the linemode speedup.
|
||||
// Must be long to pass the 100 char cutoff.
|
||||
a = "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n";
|
||||
b = "abcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\nabcdefghij\n";
|
||||
assertEquals("diff_main: Simple line-mode.", dmp.diff_main(a, b, true), dmp.diff_main(a, b, false));
|
||||
|
||||
a = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
|
||||
b = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij";
|
||||
assertEquals("diff_main: Single line-mode.", dmp.diff_main(a, b, true), dmp.diff_main(a, b, false));
|
||||
|
||||
a = "1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n1234567890\n";
|
||||
b = "abcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n1234567890\n1234567890\n1234567890\nabcdefghij\n";
|
||||
String[] texts_linemode = diff_rebuildtexts(dmp.diff_main(a, b, true));
|
||||
String[] texts_textmode = diff_rebuildtexts(dmp.diff_main(a, b, false));
|
||||
assertArrayEquals("diff_main: Overlap line-mode.", texts_textmode, texts_linemode);
|
||||
|
||||
// Test null inputs.
|
||||
try {
|
||||
dmp.diff_main(null, null);
|
||||
fail("diff_main: Null inputs.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Error expected.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MATCH TEST FUNCTIONS
|
||||
|
||||
|
||||
public void testMatchAlphabet() {
|
||||
// Initialise the bitmasks for Bitap.
|
||||
Map<Character, Integer> bitmask;
|
||||
bitmask = new HashMap<Character, Integer>();
|
||||
bitmask.put('a', 4); bitmask.put('b', 2); bitmask.put('c', 1);
|
||||
assertEquals("match_alphabet: Unique.", bitmask, dmp.match_alphabet("abc"));
|
||||
|
||||
bitmask = new HashMap<Character, Integer>();
|
||||
bitmask.put('a', 37); bitmask.put('b', 18); bitmask.put('c', 8);
|
||||
assertEquals("match_alphabet: Duplicates.", bitmask, dmp.match_alphabet("abcaba"));
|
||||
}
|
||||
|
||||
public void testMatchBitap() {
|
||||
// Bitap algorithm.
|
||||
dmp.Match_Distance = 100;
|
||||
dmp.Match_Threshold = 0.5f;
|
||||
assertEquals("match_bitap: Exact match #1.", 5, dmp.match_bitap("abcdefghijk", "fgh", 5));
|
||||
|
||||
assertEquals("match_bitap: Exact match #2.", 5, dmp.match_bitap("abcdefghijk", "fgh", 0));
|
||||
|
||||
assertEquals("match_bitap: Fuzzy match #1.", 4, dmp.match_bitap("abcdefghijk", "efxhi", 0));
|
||||
|
||||
assertEquals("match_bitap: Fuzzy match #2.", 2, dmp.match_bitap("abcdefghijk", "cdefxyhijk", 5));
|
||||
|
||||
assertEquals("match_bitap: Fuzzy match #3.", -1, dmp.match_bitap("abcdefghijk", "bxy", 1));
|
||||
|
||||
assertEquals("match_bitap: Overflow.", 2, dmp.match_bitap("123456789xx0", "3456789x0", 2));
|
||||
|
||||
assertEquals("match_bitap: Before start match.", 0, dmp.match_bitap("abcdef", "xxabc", 4));
|
||||
|
||||
assertEquals("match_bitap: Beyond end match.", 3, dmp.match_bitap("abcdef", "defyy", 4));
|
||||
|
||||
assertEquals("match_bitap: Oversized pattern.", 0, dmp.match_bitap("abcdef", "xabcdefy", 0));
|
||||
|
||||
dmp.Match_Threshold = 0.4f;
|
||||
assertEquals("match_bitap: Threshold #1.", 4, dmp.match_bitap("abcdefghijk", "efxyhi", 1));
|
||||
|
||||
dmp.Match_Threshold = 0.3f;
|
||||
assertEquals("match_bitap: Threshold #2.", -1, dmp.match_bitap("abcdefghijk", "efxyhi", 1));
|
||||
|
||||
dmp.Match_Threshold = 0.0f;
|
||||
assertEquals("match_bitap: Threshold #3.", 1, dmp.match_bitap("abcdefghijk", "bcdef", 1));
|
||||
|
||||
dmp.Match_Threshold = 0.5f;
|
||||
assertEquals("match_bitap: Multiple select #1.", 0, dmp.match_bitap("abcdexyzabcde", "abccde", 3));
|
||||
|
||||
assertEquals("match_bitap: Multiple select #2.", 8, dmp.match_bitap("abcdexyzabcde", "abccde", 5));
|
||||
|
||||
dmp.Match_Distance = 10; // Strict location.
|
||||
assertEquals("match_bitap: Distance test #1.", -1, dmp.match_bitap("abcdefghijklmnopqrstuvwxyz", "abcdefg", 24));
|
||||
|
||||
assertEquals("match_bitap: Distance test #2.", 0, dmp.match_bitap("abcdefghijklmnopqrstuvwxyz", "abcdxxefg", 1));
|
||||
|
||||
dmp.Match_Distance = 1000; // Loose location.
|
||||
assertEquals("match_bitap: Distance test #3.", 0, dmp.match_bitap("abcdefghijklmnopqrstuvwxyz", "abcdefg", 24));
|
||||
}
|
||||
|
||||
public void testMatchMain() {
|
||||
// Full match.
|
||||
assertEquals("match_main: Equality.", 0, dmp.match_main("abcdef", "abcdef", 1000));
|
||||
|
||||
assertEquals("match_main: Null text.", -1, dmp.match_main("", "abcdef", 1));
|
||||
|
||||
assertEquals("match_main: Null pattern.", 3, dmp.match_main("abcdef", "", 3));
|
||||
|
||||
assertEquals("match_main: Exact match.", 3, dmp.match_main("abcdef", "de", 3));
|
||||
|
||||
assertEquals("match_main: Beyond end match.", 3, dmp.match_main("abcdef", "defy", 4));
|
||||
|
||||
assertEquals("match_main: Oversized pattern.", 0, dmp.match_main("abcdef", "abcdefy", 0));
|
||||
|
||||
dmp.Match_Threshold = 0.7f;
|
||||
assertEquals("match_main: Complex match.", 4, dmp.match_main("I am the very model of a modern major general.", " that berry ", 5));
|
||||
dmp.Match_Threshold = 0.5f;
|
||||
|
||||
// Test null inputs.
|
||||
try {
|
||||
dmp.match_main(null, null, 0);
|
||||
fail("match_main: Null inputs.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Error expected.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// PATCH TEST FUNCTIONS
|
||||
|
||||
|
||||
public void testPatchObj() {
|
||||
// Patch Object.
|
||||
Patch p = new Patch();
|
||||
p.start1 = 20;
|
||||
p.start2 = 21;
|
||||
p.length1 = 18;
|
||||
p.length2 = 17;
|
||||
p.diffs = diffList(new Diff(EQUAL, "jump"), new Diff(DELETE, "s"), new Diff(INSERT, "ed"), new Diff(EQUAL, " over "), new Diff(DELETE, "the"), new Diff(INSERT, "a"), new Diff(EQUAL, "\nlaz"));
|
||||
String strp = "@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n %0Alaz\n";
|
||||
assertEquals("Patch: toString.", strp, p.toString());
|
||||
}
|
||||
|
||||
public void testPatchFromText() {
|
||||
assertTrue("patch_fromText: #0.", dmp.patch_fromText("").isEmpty());
|
||||
|
||||
String strp = "@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n %0Alaz\n";
|
||||
assertEquals("patch_fromText: #1.", strp, dmp.patch_fromText(strp).get(0).toString());
|
||||
|
||||
assertEquals("patch_fromText: #2.", "@@ -1 +1 @@\n-a\n+b\n", dmp.patch_fromText("@@ -1 +1 @@\n-a\n+b\n").get(0).toString());
|
||||
|
||||
assertEquals("patch_fromText: #3.", "@@ -1,3 +0,0 @@\n-abc\n", dmp.patch_fromText("@@ -1,3 +0,0 @@\n-abc\n").get(0).toString());
|
||||
|
||||
assertEquals("patch_fromText: #4.", "@@ -0,0 +1,3 @@\n+abc\n", dmp.patch_fromText("@@ -0,0 +1,3 @@\n+abc\n").get(0).toString());
|
||||
|
||||
// Generates error.
|
||||
try {
|
||||
dmp.patch_fromText("Bad\nPatch\n");
|
||||
fail("patch_fromText: #5.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Exception expected.
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatchToText() {
|
||||
String strp = "@@ -21,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n";
|
||||
List<Patch> patches;
|
||||
patches = dmp.patch_fromText(strp);
|
||||
assertEquals("patch_toText: Single.", strp, dmp.patch_toText(patches));
|
||||
|
||||
strp = "@@ -1,9 +1,9 @@\n-f\n+F\n oo+fooba\n@@ -7,9 +7,9 @@\n obar\n-,\n+.\n tes\n";
|
||||
patches = dmp.patch_fromText(strp);
|
||||
assertEquals("patch_toText: Dual.", strp, dmp.patch_toText(patches));
|
||||
}
|
||||
|
||||
public void testPatchAddContext() {
|
||||
dmp.Patch_Margin = 4;
|
||||
Patch p;
|
||||
p = dmp.patch_fromText("@@ -21,4 +21,10 @@\n-jump\n+somersault\n").get(0);
|
||||
dmp.patch_addContext(p, "The quick brown fox jumps over the lazy dog.");
|
||||
assertEquals("patch_addContext: Simple case.", "@@ -17,12 +17,18 @@\n fox \n-jump\n+somersault\n s ov\n", p.toString());
|
||||
|
||||
p = dmp.patch_fromText("@@ -21,4 +21,10 @@\n-jump\n+somersault\n").get(0);
|
||||
dmp.patch_addContext(p, "The quick brown fox jumps.");
|
||||
assertEquals("patch_addContext: Not enough trailing context.", "@@ -17,10 +17,16 @@\n fox \n-jump\n+somersault\n s.\n", p.toString());
|
||||
|
||||
p = dmp.patch_fromText("@@ -3 +3,2 @@\n-e\n+at\n").get(0);
|
||||
dmp.patch_addContext(p, "The quick brown fox jumps.");
|
||||
assertEquals("patch_addContext: Not enough leading context.", "@@ -1,7 +1,8 @@\n Th\n-e\n+at\n qui\n", p.toString());
|
||||
|
||||
p = dmp.patch_fromText("@@ -3 +3,2 @@\n-e\n+at\n").get(0);
|
||||
dmp.patch_addContext(p, "The quick brown fox jumps. The quick brown fox crashes.");
|
||||
assertEquals("patch_addContext: Ambiguity.", "@@ -1,27 +1,28 @@\n Th\n-e\n+at\n quick brown fox jumps. \n", p.toString());
|
||||
}
|
||||
|
||||
public void testPatchMake() {
|
||||
LinkedList<Patch> patches;
|
||||
patches = dmp.patch_make("", "");
|
||||
assertEquals("patch_make: Null case.", "", dmp.patch_toText(patches));
|
||||
|
||||
String text1 = "The quick brown fox jumps over the lazy dog.";
|
||||
String text2 = "That quick brown fox jumped over a lazy dog.";
|
||||
String expectedPatch = "@@ -1,8 +1,7 @@\n Th\n-at\n+e\n qui\n@@ -21,17 +21,18 @@\n jump\n-ed\n+s\n over \n-a\n+the\n laz\n";
|
||||
// The second patch must be "-21,17 +21,18", not "-22,17 +21,18" due to rolling context.
|
||||
patches = dmp.patch_make(text2, text1);
|
||||
assertEquals("patch_make: Text2+Text1 inputs.", expectedPatch, dmp.patch_toText(patches));
|
||||
|
||||
expectedPatch = "@@ -1,11 +1,12 @@\n Th\n-e\n+at\n quick b\n@@ -22,18 +22,17 @@\n jump\n-s\n+ed\n over \n-the\n+a\n laz\n";
|
||||
patches = dmp.patch_make(text1, text2);
|
||||
assertEquals("patch_make: Text1+Text2 inputs.", expectedPatch, dmp.patch_toText(patches));
|
||||
|
||||
LinkedList<Diff> diffs = dmp.diff_main(text1, text2, false);
|
||||
patches = dmp.patch_make(diffs);
|
||||
assertEquals("patch_make: Diff input.", expectedPatch, dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make(text1, diffs);
|
||||
assertEquals("patch_make: Text1+Diff inputs.", expectedPatch, dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("`1234567890-=[]\\;',./", "~!@#$%^&*()_+{}|:\"<>?");
|
||||
assertEquals("patch_toText: Character encoding.", "@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n", dmp.patch_toText(patches));
|
||||
|
||||
diffs = diffList(new Diff(DELETE, "`1234567890-=[]\\;',./"), new Diff(INSERT, "~!@#$%^&*()_+{}|:\"<>?"));
|
||||
assertEquals("patch_fromText: Character decoding.", diffs, dmp.patch_fromText("@@ -1,21 +1,21 @@\n-%601234567890-=%5B%5D%5C;',./\n+~!@#$%25%5E&*()_+%7B%7D%7C:%22%3C%3E?\n").get(0).diffs);
|
||||
|
||||
text1 = "";
|
||||
for (int x = 0; x < 100; x++) {
|
||||
text1 += "abcdef";
|
||||
}
|
||||
text2 = text1 + "123";
|
||||
expectedPatch = "@@ -573,28 +573,31 @@\n cdefabcdefabcdefabcdefabcdef\n+123\n";
|
||||
patches = dmp.patch_make(text1, text2);
|
||||
assertEquals("patch_make: Long string with repeats.", expectedPatch, dmp.patch_toText(patches));
|
||||
|
||||
// Test null inputs.
|
||||
try {
|
||||
dmp.patch_make(null);
|
||||
fail("patch_make: Null inputs.");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// Error expected.
|
||||
}
|
||||
}
|
||||
|
||||
public void testPatchSplitMax() {
|
||||
// Assumes that Match_MaxBits is 32.
|
||||
LinkedList<Patch> patches;
|
||||
patches = dmp.patch_make("abcdefghijklmnopqrstuvwxyz01234567890", "XabXcdXefXghXijXklXmnXopXqrXstXuvXwxXyzX01X23X45X67X89X0");
|
||||
dmp.patch_splitMax(patches);
|
||||
assertEquals("patch_splitMax: #1.", "@@ -1,32 +1,46 @@\n+X\n ab\n+X\n cd\n+X\n ef\n+X\n gh\n+X\n ij\n+X\n kl\n+X\n mn\n+X\n op\n+X\n qr\n+X\n st\n+X\n uv\n+X\n wx\n+X\n yz\n+X\n 012345\n@@ -25,13 +39,18 @@\n zX01\n+X\n 23\n+X\n 45\n+X\n 67\n+X\n 89\n+X\n 0\n", dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("abcdef1234567890123456789012345678901234567890123456789012345678901234567890uvwxyz", "abcdefuvwxyz");
|
||||
String oldToText = dmp.patch_toText(patches);
|
||||
dmp.patch_splitMax(patches);
|
||||
assertEquals("patch_splitMax: #2.", oldToText, dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("1234567890123456789012345678901234567890123456789012345678901234567890", "abc");
|
||||
dmp.patch_splitMax(patches);
|
||||
assertEquals("patch_splitMax: #3.", "@@ -1,32 +1,4 @@\n-1234567890123456789012345678\n 9012\n@@ -29,32 +1,4 @@\n-9012345678901234567890123456\n 7890\n@@ -57,14 +1,3 @@\n-78901234567890\n+abc\n", dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("abcdefghij , h : 0 , t : 1 abcdefghij , h : 0 , t : 1 abcdefghij , h : 0 , t : 1", "abcdefghij , h : 1 , t : 1 abcdefghij , h : 1 , t : 1 abcdefghij , h : 0 , t : 1");
|
||||
dmp.patch_splitMax(patches);
|
||||
assertEquals("patch_splitMax: #4.", "@@ -2,32 +2,32 @@\n bcdefghij , h : \n-0\n+1\n , t : 1 abcdef\n@@ -29,32 +29,32 @@\n bcdefghij , h : \n-0\n+1\n , t : 1 abcdef\n", dmp.patch_toText(patches));
|
||||
}
|
||||
|
||||
public void testPatchAddPadding() {
|
||||
LinkedList<Patch> patches;
|
||||
patches = dmp.patch_make("", "test");
|
||||
assertEquals("patch_addPadding: Both edges full.", "@@ -0,0 +1,4 @@\n+test\n", dmp.patch_toText(patches));
|
||||
dmp.patch_addPadding(patches);
|
||||
assertEquals("patch_addPadding: Both edges full.", "@@ -1,8 +1,12 @@\n %01%02%03%04\n+test\n %01%02%03%04\n", dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("XY", "XtestY");
|
||||
assertEquals("patch_addPadding: Both edges partial.", "@@ -1,2 +1,6 @@\n X\n+test\n Y\n", dmp.patch_toText(patches));
|
||||
dmp.patch_addPadding(patches);
|
||||
assertEquals("patch_addPadding: Both edges partial.", "@@ -2,8 +2,12 @@\n %02%03%04X\n+test\n Y%01%02%03\n", dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("XXXXYYYY", "XXXXtestYYYY");
|
||||
assertEquals("patch_addPadding: Both edges none.", "@@ -1,8 +1,12 @@\n XXXX\n+test\n YYYY\n", dmp.patch_toText(patches));
|
||||
dmp.patch_addPadding(patches);
|
||||
assertEquals("patch_addPadding: Both edges none.", "@@ -5,8 +5,12 @@\n XXXX\n+test\n YYYY\n", dmp.patch_toText(patches));
|
||||
}
|
||||
|
||||
public void testPatchApply() {
|
||||
dmp.Match_Distance = 1000;
|
||||
dmp.Match_Threshold = 0.5f;
|
||||
dmp.Patch_DeleteThreshold = 0.5f;
|
||||
LinkedList<Patch> patches;
|
||||
patches = dmp.patch_make("", "");
|
||||
Object[] results = dmp.patch_apply(patches, "Hello world.");
|
||||
boolean[] boolArray = (boolean[]) results[1];
|
||||
String resultStr = results[0] + "\t" + boolArray.length;
|
||||
assertEquals("patch_apply: Null case.", "Hello world.\t0", resultStr);
|
||||
|
||||
patches = dmp.patch_make("The quick brown fox jumps over the lazy dog.", "That quick brown fox jumped over a lazy dog.");
|
||||
results = dmp.patch_apply(patches, "The quick brown fox jumps over the lazy dog.");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Exact match.", "That quick brown fox jumped over a lazy dog.\ttrue\ttrue", resultStr);
|
||||
|
||||
results = dmp.patch_apply(patches, "The quick red rabbit jumps over the tired tiger.");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Partial match.", "That quick red rabbit jumped over a tired tiger.\ttrue\ttrue", resultStr);
|
||||
|
||||
results = dmp.patch_apply(patches, "I am the very model of a modern major general.");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Failed match.", "I am the very model of a modern major general.\tfalse\tfalse", resultStr);
|
||||
|
||||
patches = dmp.patch_make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy");
|
||||
results = dmp.patch_apply(patches, "x123456789012345678901234567890-----++++++++++-----123456789012345678901234567890y");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Big delete, small change.", "xabcy\ttrue\ttrue", resultStr);
|
||||
|
||||
patches = dmp.patch_make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy");
|
||||
results = dmp.patch_apply(patches, "x12345678901234567890---------------++++++++++---------------12345678901234567890y");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Big delete, big change 1.", "xabc12345678901234567890---------------++++++++++---------------12345678901234567890y\tfalse\ttrue", resultStr);
|
||||
|
||||
dmp.Patch_DeleteThreshold = 0.6f;
|
||||
patches = dmp.patch_make("x1234567890123456789012345678901234567890123456789012345678901234567890y", "xabcy");
|
||||
results = dmp.patch_apply(patches, "x12345678901234567890---------------++++++++++---------------12345678901234567890y");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Big delete, big change 2.", "xabcy\ttrue\ttrue", resultStr);
|
||||
dmp.Patch_DeleteThreshold = 0.5f;
|
||||
|
||||
// Compensate for failed patch.
|
||||
dmp.Match_Threshold = 0.0f;
|
||||
dmp.Match_Distance = 0;
|
||||
patches = dmp.patch_make("abcdefghijklmnopqrstuvwxyz--------------------1234567890", "abcXXXXXXXXXXdefghijklmnopqrstuvwxyz--------------------1234567YYYYYYYYYY890");
|
||||
results = dmp.patch_apply(patches, "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567890");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0] + "\t" + boolArray[1];
|
||||
assertEquals("patch_apply: Compensate for failed patch.", "ABCDEFGHIJKLMNOPQRSTUVWXYZ--------------------1234567YYYYYYYYYY890\tfalse\ttrue", resultStr);
|
||||
dmp.Match_Threshold = 0.5f;
|
||||
dmp.Match_Distance = 1000;
|
||||
|
||||
patches = dmp.patch_make("", "test");
|
||||
String patchStr = dmp.patch_toText(patches);
|
||||
dmp.patch_apply(patches, "");
|
||||
assertEquals("patch_apply: No side effects.", patchStr, dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("The quick brown fox jumps over the lazy dog.", "Woof");
|
||||
patchStr = dmp.patch_toText(patches);
|
||||
dmp.patch_apply(patches, "The quick brown fox jumps over the lazy dog.");
|
||||
assertEquals("patch_apply: No side effects with major delete.", patchStr, dmp.patch_toText(patches));
|
||||
|
||||
patches = dmp.patch_make("", "test");
|
||||
results = dmp.patch_apply(patches, "");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0];
|
||||
assertEquals("patch_apply: Edge exact match.", "test\ttrue", resultStr);
|
||||
|
||||
patches = dmp.patch_make("XY", "XtestY");
|
||||
results = dmp.patch_apply(patches, "XY");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0];
|
||||
assertEquals("patch_apply: Near edge exact match.", "XtestY\ttrue", resultStr);
|
||||
|
||||
patches = dmp.patch_make("y", "y123");
|
||||
results = dmp.patch_apply(patches, "x");
|
||||
boolArray = (boolean[]) results[1];
|
||||
resultStr = results[0] + "\t" + boolArray[0];
|
||||
assertEquals("patch_apply: Edge partial match.", "x123\ttrue", resultStr);
|
||||
}
|
||||
|
||||
private void assertArrayEquals(String error_msg, Object[] a, Object[] b) {
|
||||
List<Object> list_a = Arrays.asList(a);
|
||||
List<Object> list_b = Arrays.asList(b);
|
||||
assertEquals(error_msg, list_a, list_b);
|
||||
}
|
||||
|
||||
private void assertLinesToCharsResultEquals(String error_msg,
|
||||
LinesToCharsResult a, LinesToCharsResult b) {
|
||||
assertEquals(error_msg, a.chars1, b.chars1);
|
||||
assertEquals(error_msg, a.chars2, b.chars2);
|
||||
assertEquals(error_msg, a.lineArray, b.lineArray);
|
||||
}
|
||||
|
||||
// Construct the two texts which made up the diff originally.
|
||||
private static String[] diff_rebuildtexts(LinkedList<Diff> diffs) {
|
||||
String[] text = {"", ""};
|
||||
for (Diff myDiff : diffs) {
|
||||
if (myDiff.operation != DiffMatchPatch.Operation.INSERT) {
|
||||
text[0] += myDiff.text;
|
||||
}
|
||||
if (myDiff.operation != DiffMatchPatch.Operation.DELETE) {
|
||||
text[1] += myDiff.text;
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
// Private function for quickly building lists of diffs.
|
||||
private static LinkedList<Diff> diffList(Diff... diffs) {
|
||||
LinkedList<Diff> myDiffList = new LinkedList<Diff>();
|
||||
for (Diff myDiff : diffs) {
|
||||
myDiffList.add(myDiff);
|
||||
}
|
||||
return myDiffList;
|
||||
}
|
||||
|
||||
private static void assertNull(String message, Object object) {
|
||||
Assert.assertNull(message, object);
|
||||
}
|
||||
|
||||
private static void assertEquals(String message, Object expected, Object actual) {
|
||||
Assert.assertEquals(message, expected, actual);
|
||||
}
|
||||
|
||||
private static void assertEquals(String message, String expected, String actual) {
|
||||
Assert.assertEquals(message, expected, actual);
|
||||
}
|
||||
|
||||
private static void assertTrue(String message, boolean condition) {
|
||||
Assert.assertTrue(message, condition);
|
||||
}
|
||||
|
||||
private static void assertEquals(Object expected, Object actual) {
|
||||
Assert.assertEquals(expected, actual);
|
||||
}
|
||||
|
||||
private static void fail(String message) {
|
||||
Assert.fail(message);
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
//
|
||||
//// DiffMatchPatchTest d = new DiffMatchPatchTest();
|
||||
//// d.testDiffCleanupEfficiency();
|
||||
//// d.testDiffPrettyHtml();
|
||||
//
|
||||
// String text1 = "花<b>一样</b>的你,花一样的我,花一样的年华,花一样的世界,花一样的未来。";
|
||||
// String text2 = "花<b>一</b>样的你,花一样的年华,花一样的我,花一样的未来,花一样的世界。";
|
||||
//
|
||||
// DiffMatchPatch dmp = new DiffMatchPatch();
|
||||
//
|
||||
// LinkedList<Diff> diffs = dmp.diff_main(text1, text2);
|
||||
// String result = dmp.diff_prettyHtml(diffs);
|
||||
//
|
||||
// FileUtils.writeToFile("x:\\test.html", result, false);
|
||||
// System.out.println(result);
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.jeesite.common.text;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import net.sourceforge.pinyin4j.PinyinHelper;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
|
||||
@@ -9,6 +7,8 @@ import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
|
||||
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
|
||||
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 拼音工具类
|
||||
* @author ThinkGem
|
||||
@@ -153,13 +153,5 @@ public class PinyinUtils {
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// String str = "你好,123,🅻🅾🆅🅴、世界abc,~!#$_Sdf,女;hello!-";
|
||||
// String str = "你好,123,世界abc,~!#$_Sdf,女;hello!-";
|
||||
// System.out.println(getFirstSpell(str));
|
||||
// System.out.println(getFirstSpell(str, false));
|
||||
// System.out.println(getFullSpell(str));
|
||||
// System.out.println(getFullSpell(str, false));
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
@@ -1,235 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.common.utils.excel;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.xssf.eventusermodel.XSSFReader;
|
||||
import org.apache.poi.xssf.model.SharedStringsTable;
|
||||
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
/**
|
||||
* Excel超大数据读取,抽象Excel2007读取器,excel2007的底层数据结构是xml文件,采用SAX的事件驱动的方法解析
|
||||
* xml,需要继承DefaultHandler,在遇到文件内容时,事件会触发,这种做法可以大大降低 内存的耗费,特别使用于大数据量的文件。
|
||||
* @version 2014-9-2
|
||||
*/
|
||||
public abstract class ExcelReader extends DefaultHandler {
|
||||
|
||||
// 共享字符串表
|
||||
private SharedStringsTable sst;
|
||||
|
||||
// 上一次的内容
|
||||
private String lastContents;
|
||||
private boolean nextIsString;
|
||||
|
||||
private int sheetIndex = -1;
|
||||
private List<String> rowList = new ArrayList<String>();
|
||||
|
||||
// 当前行
|
||||
private int curRow = 0;
|
||||
// 当前列
|
||||
private int curCol = 0;
|
||||
// 日期标志
|
||||
private boolean dateFlag;
|
||||
// 数字标志
|
||||
private boolean numberFlag;
|
||||
|
||||
private boolean isTElement;
|
||||
|
||||
/**
|
||||
* 遍历工作簿中所有的电子表格
|
||||
* @param filename
|
||||
* @throws Exception
|
||||
*/
|
||||
public void process(String filename) throws Exception {
|
||||
OPCPackage pkg = OPCPackage.open(filename);
|
||||
XSSFReader r = new XSSFReader(pkg);
|
||||
SharedStringsTable sst = r.getSharedStringsTable();
|
||||
XMLReader parser = fetchSheetParser(sst);
|
||||
Iterator<InputStream> sheets = r.getSheetsData();
|
||||
while (sheets.hasNext()) {
|
||||
curRow = 0;
|
||||
sheetIndex++;
|
||||
InputStream sheet = sheets.next();
|
||||
InputSource sheetSource = new InputSource(sheet);
|
||||
parser.parse(sheetSource);
|
||||
sheet.close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 只遍历一个电子表格,其中sheetId为要遍历的sheet索引,从1开始,1-3
|
||||
* @param filename
|
||||
* @param sheetId
|
||||
* @throws Exception
|
||||
*/
|
||||
public void process(String filename, int sheetId) throws Exception {
|
||||
OPCPackage pkg = OPCPackage.open(filename);
|
||||
XSSFReader r = new XSSFReader(pkg);
|
||||
SharedStringsTable sst = r.getSharedStringsTable();
|
||||
XMLReader parser = fetchSheetParser(sst);
|
||||
// 根据 rId# 或 rSheet# 查找sheet
|
||||
InputStream sheet2 = r.getSheet("rId" + sheetId);
|
||||
sheetIndex++;
|
||||
InputSource sheetSource = new InputSource(sheet2);
|
||||
parser.parse(sheetSource);
|
||||
sheet2.close();
|
||||
}
|
||||
|
||||
public XMLReader fetchSheetParser(SharedStringsTable sst)
|
||||
throws SAXException {
|
||||
XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
|
||||
this.sst = sst;
|
||||
parser.setContentHandler(this);
|
||||
return parser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void startElement(String uri, String localName, String name,
|
||||
Attributes attributes) throws SAXException {
|
||||
|
||||
// System.out.println("startElement: " + localName + ", " + name + ", " + attributes);
|
||||
|
||||
// c => 单元格
|
||||
if ("c".equals(name)) {
|
||||
// 如果下一个元素是 SST 的索引,则将nextIsString标记为true
|
||||
String cellType = attributes.getValue("t");
|
||||
if ("s".equals(cellType)) {
|
||||
nextIsString = true;
|
||||
} else {
|
||||
nextIsString = false;
|
||||
}
|
||||
// 日期格式
|
||||
String cellDateType = attributes.getValue("s");
|
||||
if ("1".equals(cellDateType)) {
|
||||
dateFlag = true;
|
||||
} else {
|
||||
dateFlag = false;
|
||||
}
|
||||
String cellNumberType = attributes.getValue("s");
|
||||
if ("2".equals(cellNumberType)) {
|
||||
numberFlag = true;
|
||||
} else {
|
||||
numberFlag = false;
|
||||
}
|
||||
|
||||
}
|
||||
// 当元素为t时
|
||||
if ("t".equals(name)) {
|
||||
isTElement = true;
|
||||
} else {
|
||||
isTElement = false;
|
||||
}
|
||||
|
||||
// 置空
|
||||
lastContents = "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endElement(String uri, String localName, String name)
|
||||
throws SAXException {
|
||||
|
||||
// System.out.println("endElement: " + localName + ", " + name);
|
||||
|
||||
// 根据SST的索引值的到单元格的真正要存储的字符串
|
||||
// 这时characters()方法可能会被调用多次
|
||||
if (nextIsString) {
|
||||
try {
|
||||
int idx = Integer.parseInt(lastContents);
|
||||
lastContents = new XSSFRichTextString(sst.getEntryAt(idx))
|
||||
.toString();
|
||||
} catch (Exception e) {
|
||||
|
||||
}
|
||||
}
|
||||
// t元素也包含字符串
|
||||
if (isTElement) {
|
||||
String value = lastContents.trim();
|
||||
rowList.add(curCol, value);
|
||||
curCol++;
|
||||
isTElement = false;
|
||||
// v => 单元格的值,如果单元格是字符串则v标签的值为该字符串在SST中的索引
|
||||
// 将单元格内容加入rowlist中,在这之前先去掉字符串前后的空白符
|
||||
} else if ("v".equals(name)) {
|
||||
String value = lastContents.trim();
|
||||
value = "".equals(value) ? " " : value;
|
||||
try {
|
||||
// 日期格式处理
|
||||
if (dateFlag) {
|
||||
Date date = HSSFDateUtil.getJavaDate(Double.valueOf(value));
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
||||
value = dateFormat.format(date);
|
||||
}
|
||||
// 数字类型处理
|
||||
if (numberFlag) {
|
||||
BigDecimal bd = new BigDecimal(value);
|
||||
value = bd.setScale(3, BigDecimal.ROUND_UP).toString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 转换失败仍用读出来的值
|
||||
}
|
||||
rowList.add(curCol, value);
|
||||
curCol++;
|
||||
} else {
|
||||
// 如果标签名称为 row ,这说明已到行尾,调用 optRows() 方法
|
||||
if ("row".equals(name)) {
|
||||
getRows(sheetIndex + 1, curRow, rowList);
|
||||
rowList.clear();
|
||||
curRow++;
|
||||
curCol = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length)
|
||||
throws SAXException {
|
||||
// 得到单元格内容的值
|
||||
lastContents += new String(ch, start, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取行数据回调
|
||||
* @param sheetIndex
|
||||
* @param curRow
|
||||
* @param rowList
|
||||
*/
|
||||
public abstract void getRows(int sheetIndex, int curRow, List<String> rowList);
|
||||
|
||||
// /**
|
||||
// * 测试方法
|
||||
// */
|
||||
// public static void main(String[] args) throws Exception {
|
||||
//
|
||||
// String file = "E:/销售数据导入.xlsx";
|
||||
//
|
||||
// ExcelReader reader = new ExcelReader() {
|
||||
// @Override
|
||||
// public void getRows(int sheetIndex, int curRow, List<String> rowList) {
|
||||
//
|
||||
// System.out.println("Sheet:" + sheetIndex + ", Row:" + curRow + ", Data:" +rowList);
|
||||
//
|
||||
// }
|
||||
// };
|
||||
// reader.process(file, 1);
|
||||
//
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -1,343 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
|
||||
* No deletion without permission, or be held responsible to law.
|
||||
*/
|
||||
package com.jeesite.common.utils.excel;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.Writer;
|
||||
import java.util.Calendar;
|
||||
import java.util.Enumeration;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import org.apache.poi.hssf.util.CellReference;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
/**
|
||||
* Excel超大数据写入,抽象excel2007读入器,先构建.xlsx一张模板,改写模板中的sheet.xml,
|
||||
* 使用这种方法 写入.xlsx文件,不需要太大的内存
|
||||
* @version 2014-9-2
|
||||
*/
|
||||
public abstract class ExcelWriter {
|
||||
|
||||
private SpreadsheetWriter sw;
|
||||
|
||||
/**
|
||||
* 写入电子表格的主要流程
|
||||
*
|
||||
* @param fileName
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public void process(String fileName) throws Exception {
|
||||
|
||||
// 建立工作簿和电子表格对象
|
||||
XSSFWorkbook wb = new XSSFWorkbook();
|
||||
XSSFSheet sheet = wb.createSheet("sheet1");
|
||||
|
||||
// 持有电子表格数据的xml文件名 例如 /xl/worksheets/sheet1.xml
|
||||
String sheetRef = sheet.getPackagePart().getPartName().getName();
|
||||
|
||||
// 保存模板
|
||||
FileOutputStream os = new FileOutputStream("template.xlsx");
|
||||
wb.write(os);
|
||||
os.close();
|
||||
|
||||
// 生成xml文件
|
||||
File tmp = File.createTempFile("sheet", ".xml");
|
||||
Writer fw = new FileWriter(tmp);
|
||||
sw = new SpreadsheetWriter(fw);
|
||||
generate();
|
||||
fw.close();
|
||||
|
||||
// 使用产生的数据替换模板
|
||||
File templateFile = new File("template.xlsx");
|
||||
FileOutputStream out = new FileOutputStream(fileName);
|
||||
substitute(templateFile, tmp, sheetRef.substring(1), out);
|
||||
out.close();
|
||||
// 删除文件之前调用一下垃圾回收器,否则无法删除模板文件
|
||||
System.gc();
|
||||
// 删除临时模板文件
|
||||
if (templateFile.isFile() && templateFile.exists()) {
|
||||
templateFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 类使用者应该使用此方法进行写操作
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public abstract void generate() throws Exception;
|
||||
|
||||
public void beginSheet() throws IOException {
|
||||
sw.beginSheet();
|
||||
}
|
||||
|
||||
public void insertRow(int rowNum) throws IOException {
|
||||
sw.insertRow(rowNum);
|
||||
}
|
||||
|
||||
public void createCell(int columnIndex, String value) throws IOException {
|
||||
sw.createCell(columnIndex, value, -1);
|
||||
}
|
||||
|
||||
public void createCell(int columnIndex, double value) throws IOException {
|
||||
sw.createCell(columnIndex, value, -1);
|
||||
}
|
||||
|
||||
public void endRow() throws IOException {
|
||||
sw.endRow();
|
||||
}
|
||||
|
||||
public void endSheet() throws IOException {
|
||||
sw.endSheet();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param zipfile the template file
|
||||
* @param tmpfile the XML file with the sheet data
|
||||
* @param entry the name of the sheet entry to substitute, e.g. xl/worksheets/sheet1.xml
|
||||
* @param out the stream to write the result to
|
||||
*/
|
||||
private static void substitute(File zipfile, File tmpfile, String entry,
|
||||
OutputStream out) throws IOException {
|
||||
ZipFile zip = null;
|
||||
ZipOutputStream zos = null;
|
||||
InputStream is = null;
|
||||
try{
|
||||
zip = new ZipFile(zipfile);
|
||||
zos = new ZipOutputStream(out);
|
||||
@SuppressWarnings("unchecked")
|
||||
Enumeration<ZipEntry> en = (Enumeration<ZipEntry>) zip.entries();
|
||||
while (en.hasMoreElements()) {
|
||||
ZipEntry ze = en.nextElement();
|
||||
if (!ze.getName().equals(entry)) {
|
||||
zos.putNextEntry(new ZipEntry(ze.getName()));
|
||||
InputStream is2 = null;
|
||||
try{
|
||||
is2 = zip.getInputStream(ze);
|
||||
copyStream(is2, zos);
|
||||
}finally {
|
||||
if (is2 != null){
|
||||
is2.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
zos.putNextEntry(new ZipEntry(entry));
|
||||
is = new FileInputStream(tmpfile);
|
||||
copyStream(is, zos);
|
||||
}finally {
|
||||
if (is != null){
|
||||
is.close();
|
||||
}
|
||||
if (zos != null){
|
||||
zos.close();
|
||||
}
|
||||
if (zip != null){
|
||||
zip.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void copyStream(InputStream in, OutputStream out)
|
||||
throws IOException {
|
||||
byte[] chunk = new byte[1024];
|
||||
int count;
|
||||
while ((count = in.read(chunk)) >= 0) {
|
||||
out.write(chunk, 0, count);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在写入器中写入电子表格
|
||||
*
|
||||
*/
|
||||
public static class SpreadsheetWriter {
|
||||
private final Writer _out;
|
||||
private int _rownum;
|
||||
private static String LINE_SEPARATOR = System
|
||||
.getProperty("line.separator");
|
||||
|
||||
public SpreadsheetWriter(Writer out) {
|
||||
_out = out;
|
||||
}
|
||||
|
||||
public void beginSheet() throws IOException {
|
||||
_out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
|
||||
+ "<worksheet xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\">");
|
||||
_out.write("<sheetData>" + LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
public void endSheet() throws IOException {
|
||||
_out.write("</sheetData>");
|
||||
_out.write("</worksheet>");
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入新行
|
||||
*
|
||||
* @param rownum
|
||||
* 以0开始
|
||||
*/
|
||||
public void insertRow(int rownum) throws IOException {
|
||||
_out.write("<row r=\"" + (rownum + 1) + "\">" + LINE_SEPARATOR);
|
||||
this._rownum = rownum;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入行结束标志
|
||||
*/
|
||||
public void endRow() throws IOException {
|
||||
_out.write("</row>" + LINE_SEPARATOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入新列
|
||||
*
|
||||
* @param columnIndex
|
||||
* @param value
|
||||
* @param styleIndex
|
||||
* @throws IOException
|
||||
*/
|
||||
public void createCell(int columnIndex, String value, int styleIndex)
|
||||
throws IOException {
|
||||
String ref = new CellReference(_rownum, columnIndex)
|
||||
.formatAsString();
|
||||
_out.write("<c r=\"" + ref + "\" t=\"inlineStr\"");
|
||||
if (styleIndex != -1) {
|
||||
_out.write(" s=\"" + styleIndex + "\"");
|
||||
}
|
||||
_out.write(">");
|
||||
_out.write("<is><t>" + encoderXML(value) + "</t></is>");
|
||||
_out.write("</c>");
|
||||
}
|
||||
|
||||
public void createCell(int columnIndex, String value)
|
||||
throws IOException {
|
||||
createCell(columnIndex, value, -1);
|
||||
}
|
||||
|
||||
public void createCell(int columnIndex, double value, int styleIndex)
|
||||
throws IOException {
|
||||
String ref = new CellReference(_rownum, columnIndex)
|
||||
.formatAsString();
|
||||
_out.write("<c r=\"" + ref + "\" t=\"n\"");
|
||||
if (styleIndex != -1) {
|
||||
_out.write(" s=\"" + styleIndex + "\"");
|
||||
}
|
||||
_out.write(">");
|
||||
_out.write("<v>" + value + "</v>");
|
||||
_out.write("</c>");
|
||||
}
|
||||
|
||||
public void createCell(int columnIndex, double value)
|
||||
throws IOException {
|
||||
createCell(columnIndex, value, -1);
|
||||
}
|
||||
|
||||
public void createCell(int columnIndex, Calendar value, int styleIndex)
|
||||
throws IOException {
|
||||
createCell(columnIndex, DateUtil.getExcelDate(value, false),
|
||||
styleIndex);
|
||||
}
|
||||
}
|
||||
|
||||
// XML Encode
|
||||
private static final String[] xmlCode = new String[256];
|
||||
|
||||
static {
|
||||
// Special characters
|
||||
xmlCode['\''] = "'";
|
||||
xmlCode['\"'] = "\""; // double quote
|
||||
xmlCode['&'] = "&"; // ampersand
|
||||
xmlCode['<'] = "<"; // lower than
|
||||
xmlCode['>'] = ">"; // greater than
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* Encode the given text into xml.
|
||||
* </p>
|
||||
*
|
||||
* @param string the text to encode
|
||||
* @return the encoded string
|
||||
*/
|
||||
public static String encoderXML(String string) {
|
||||
if (string == null) {
|
||||
return "";
|
||||
}
|
||||
int n = string.length();
|
||||
char character;
|
||||
String xmlchar;
|
||||
StringBuffer buffer = new StringBuffer();
|
||||
// loop over all the characters of the String.
|
||||
for (int i = 0; i < n; i++) {
|
||||
character = string.charAt(i);
|
||||
// the xmlcode of these characters are added to a StringBuffer
|
||||
// one by one
|
||||
try {
|
||||
xmlchar = xmlCode[character];
|
||||
if (xmlchar == null) {
|
||||
buffer.append(character);
|
||||
} else {
|
||||
buffer.append(xmlCode[character]);
|
||||
}
|
||||
} catch (ArrayIndexOutOfBoundsException aioobe) {
|
||||
buffer.append(character);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
// /**
|
||||
// * 测试方法
|
||||
// */
|
||||
// public static void main(String[] args) throws Exception {
|
||||
//
|
||||
// String file = "E:/测试导出数据.xlsx";
|
||||
//
|
||||
// ExcelWriter writer = new ExcelWriter() {
|
||||
// @Override
|
||||
// public void generate() throws Exception {
|
||||
//
|
||||
// // 电子表格开始
|
||||
// this.beginSheet();
|
||||
//
|
||||
// for (int rownum = 0; rownum < 100; rownum++) {
|
||||
// // 插入新行
|
||||
// this.insertRow(rownum);
|
||||
//
|
||||
// // 建立新单元格,索引值从0开始,表示第一列
|
||||
// this.createCell(0, "第 " + rownum + " 行");
|
||||
// this.createCell(1, 34343.123456789);
|
||||
// this.createCell(2, "23.67%");
|
||||
// this.createCell(3, "12:12:23");
|
||||
// this.createCell(4, "2014-10-11 12:12:23");
|
||||
// this.createCell(5, "true");
|
||||
// this.createCell(6, "false");
|
||||
//
|
||||
// // 结束行
|
||||
// this.endRow();
|
||||
// }
|
||||
//
|
||||
// // 电子表格结束
|
||||
// this.endSheet();
|
||||
// }
|
||||
// };
|
||||
// writer.process(file);
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user