获取HTML项目中使用的类和ID列表

时间:2015-11-26 16:34:16

标签: html css netbeans

是否可以获取HTML项目中使用的所有类/ ID的列表?

我正在处理的项目有18个HTML页面(JSP公平),我想知道每个页面上使用了哪些类/ ID。

例如:<div class="div-class" id="div-id">包含类"div-class"和标识"div-id"。我不想手动搜索每个页面并记下我找到的所有类/ ID。

有没有可以为我做这个的工具?

我使用NetBeans 8.0.2作为IDE,因此如果可以使用NetBeans执行此操作,那就太棒了!

1 个答案:

答案 0 :(得分:0)

据我所知,Netbeans不支持这种功能。我正在提供一个java类文件,您可以运行该文件以获取此信息。

  1. 在C目录中创建一个名为html的文件夹。将所有html / jsp文件放在html目录中。 enter image description here

  2. 运行此java类文件

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.nio.file.DirectoryStream;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    public class HtmlIdsAndClasses {
    
        public static void main(String[] args) {
    
            String path = "C:\\html";
    
            scanFiles(path);
        }
    
        private static void scanFiles(String pathLocation) {
    
            try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(pathLocation))) {
                for (Path path : directoryStream) {
    
                    HtmlPageInfo pageInfo = new HtmlPageInfo();
                    List<String> classList = new ArrayList<String>();
                    List<String> idList = new ArrayList<String>();
                    pageInfo.setPageName(path.getFileName().toString());
                    pageInfo.setClassList(classList);
                    pageInfo.setIdList(idList);
                    getAllIds(path, pageInfo);
                    getAllClasses(path, pageInfo);
                    System.out.println(pageInfo);
                }
            } catch (IOException ex) {}
        }
    
        private static void getAllClasses(Path file, HtmlPageInfo pageInfo) {
    
            Pattern classPattern = Pattern.compile("class=\"([-A-Za-z_ ]*)\"");
            Charset charset = Charset.forName("UTF8");
            BufferedReader reader = null;
            try {
    
                reader = Files.newBufferedReader(file, charset);
                String line = null;
                while ((line = reader.readLine()) != null) {
    
                    Matcher classMatcher = classPattern.matcher(line);
                    while(classMatcher.find()) {
                        String classGroup = classMatcher.group(1);
                        for(String cls : classGroup.split(" ")) {
                            pageInfo.getClassList().add(cls);
                        }
                    }
                }
            } 
            catch (IOException x) {
                System.err.format("IOException: %s%n", x);
            }
            finally {
    
                if(reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        private static void getAllIds(Path file, HtmlPageInfo pageInfo) {
    
            Pattern idPattern = Pattern.compile("id=\"([-A-Za-z_]*)\"");
            Charset charset = Charset.forName("UTF8");
            BufferedReader reader = null;
            try {
    
                reader = Files.newBufferedReader(file, charset);
                String line = null;
                while ((line = reader.readLine()) != null) {
    
                    Matcher idMatcher = idPattern.matcher(line);
                    while(idMatcher.find()) {
                        String id = idMatcher.group(1);
                        pageInfo.getIdList().add(id);
                    }
                }
            } 
            catch (IOException x) {
                System.err.format("IOException: %s%n", x);
            }
            finally {
    
                if(reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    class HtmlPageInfo {
    
        private String pageName;
        private List<String> idList;
        private List<String> classList;
        public String getPageName() {
            return pageName;
        }
        public void setPageName(String pageName) {
            this.pageName = pageName;
        }
        public List<String> getIdList() {
            return idList;
        }
        public void setIdList(List<String> idList) {
            this.idList = idList;
        }
        public List<String> getClassList() {
            return classList;
        }
        public void setClassList(List<String> classList) {
            this.classList = classList;
        }
    
        @Override
        public String toString() {
    
            StringBuilder sb = new StringBuilder();
            sb.append("[Page - ");
            sb.append(pageName);
            sb.append("]");
            sb.append("\n\t[IDs - ");
            sb.append("(" + idList.size() + ")]\n\t\t");
            for(String id : idList) {
                sb.append(id + ",");
            }
            sb.append("\n\t[Classes ");
            sb.append("(" + classList.size() + ")]\n\t\t");
            for(String cls : classList) {
                sb.append(cls + ",");
            }
            sb.append("]\n\n");
    
            return sb.toString();
        }
    }
    
  3. 您将在控制台中获得输出,如下图所示。 enter image description here