吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2611|回复: 7
收起左侧

[Java 原创] java判断html表格是不是标准表格

[复制链接]
1664593601 发表于 2019-2-26 12:27
本帖最后由 1664593601 于 2019-5-14 23:27 编辑


标准表格,就是每个tr的td长度一致:有clospan的,rowspan的,合并的个数计算。

[Java] 纯文本查看 复制代码
package test.isEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReadHtml {
        public static void main(String[] args) {
                System.out.println("start!");
                traverseFolder2("C:/Users/cylyc/Desktop/test/newhtm");
                System.out.println("end!");
        }

        // 获得需要的文件类型
        public static void traverseFolder2(String path) {
                File file = new File(path);
                if (file.exists()) {
                        File[] files = file.listFiles();
                        if (null == files || files.length == 0) {
                                return;
                        } else {
                                for (File file2 : files) {
                                        if (file2.isDirectory()) {
                                                traverseFolder2(file2.getAbsolutePath());
                                        } else {
                                                String absolutePath = file2.getAbsolutePath();
                                                if (absolutePath.endsWith(".html")
                                                                || absolutePath.endsWith(".htm")) {
                                                        //System.out.println("当前检测文件名为:"+absolutePath);
                                                        String txt2String = txt2String(absolutePath);
                                                        
                                                        if (!isContainsTable(txt2String)) {
                                                                System.out.println("该文件名为:"+absolutePath);
                                                        }
                                                }
                                        }
                                }
                        }
                } else {
                        System.out.println("文件路径不存在!");
                }
        }

        // 读取文件
        private static String txt2String(String fileName) {
                BufferedReader br = null;
                StringBuffer sb = null;

                try {
                        br = new BufferedReader(new InputStreamReader(new FileInputStream(
                                        fileName), "UTF8"));
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        System.out.println("文件编码错误:" + fileName);
                } catch (FileNotFoundException e) {
                        e.printStackTrace();
                        System.out.println("文件找不到:" + fileName);
                }
                sb = new StringBuffer();
                String line = null;
                try {
                        while ((line = br.readLine()) != null) {
                                sb.append(line);
                        }
                } catch (IOException e) {
                        e.printStackTrace();
                        System.out.println("IO异常" + fileName);
                }
                return new String(sb);

        }

        public static boolean isContainsTable(String content) {
                boolean returnFlag = true;
                Document doc = Jsoup.parse(content);
                Elements tables = doc.getElementsByTag("table");
                if (tables != null && tables.size() > 0) {
                        //表格编号
                        int tableNum = 0;
                        for (Element table : tables) {
                                //确定数组的大小
                                //获得i的最大值
                                int trSize = table.getElementsByTag("tr").size();
                                //获得j的最大值
                                int tdSize = table.getElementsByTag("td").size();
                                boolean[][] colBytes = new boolean[trSize][tdSize];
                                
                                tableNum++;
                                int maxColLength = 0;
                                int i = 0;
                                Elements trs = table.getElementsByTag("tr");
                                for (Element tr : trs) {
                                        Elements tds = tr.getElementsByTag("td");
                                        for (Element td : tds) {
                                                int j = 0;
                                                String col = td.attr("colspan");
                                                col = StringUtils.isBlank(col) ? "1" : col;
                                                String row = td.attr("rowspan");
                                                row = StringUtils.isBlank(row) ? "1" : row;

                                                for (int tempJ = 0; tempJ < Integer.valueOf(col); tempJ++, j++) {
                                                        while (true) {
                                                                if (colBytes[i][j]) {
                                                                        j++;
                                                                }else {
                                                                        for (int tempI = 0; tempI < Integer.valueOf(row); tempI++) {
                                                                                colBytes[i + tempI][j] = true;
                                                                        }
                                                                        break;
                                                                }

                                                        }

                                                }
                                                // 记录行的最大值
                                                maxColLength = j > maxColLength ? j : maxColLength;
                                        }
                                        i++;
                                        
                                }
                                for (int q = 0; q < i; q++) {
                                        if (!colBytes[q][maxColLength-1]) {
                                                System.out.println("第"+tableNum+"个表格不是标准表格");
                                                returnFlag = false;
                                                break;
                                        }
                                }

                        }
                }
                return returnFlag;
        }
}

这里删之前是标准表格

这里删之前是标准表格

这里被删了,td不相等,就不是标准表格了

这里被删了,td不相等,就不是标准表格了

免费评分

参与人数 10吾爱币 +9 热心值 +10 收起 理由
woshiyizhidailv + 1 热心回复!
zgcwkj + 2 + 1 鼓励转贴优秀软件安全工具和文档!
tyj9713 + 1 + 1 来啦老弟
吾爱2018-7-18 + 1 + 1 我很赞同!
苏紫方璇 + 3 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
一叶成林 + 1 + 1 我很赞同!
血染孤傲 + 1 我很赞同!
撩人道士 + 1 我很赞同!
linux9420 + 1 我很赞同!
haoxiaowu + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

linux9420 发表于 2019-2-26 12:34 来自手机
滴滴滴,来看看。
血染孤傲 发表于 2019-2-26 12:39
一叶成林 发表于 2019-2-26 12:39
神羽飞扬 发表于 2019-2-26 13:01 来自手机
正在努力正在努力
SGC沉默 发表于 2019-2-26 13:06
楼主我想知道你的项目是什么奇葩需求要求验证html表格完整度
 楼主| 1664593601 发表于 2019-2-26 13:11
SGC沉默 发表于 2019-2-26 13:06
楼主我想知道你的项目是什么奇葩需求要求验证html表格完整度

这批html文档是从Word转过来的,公式还是字符串呢,
刘刘刘勇军12138 发表于 2019-2-26 13:28
帮你了  你自己看着办吧
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-30 09:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表