JEditorPaneHTMLEditorKitParserSearch搜索算法

wen java案例 3

本文目录导读:

JEditorPaneHTMLEditorKitParserSearch搜索算法

  1. JEditorPane 基本概念
  2. HTMLEditorKit 与解析器
  3. 文本搜索算法实现
  4. HTML 标签解析搜索
  5. 搜索高亮功能
  6. 性能优化算法
  7. 完整搜索示例

我来详细解释一下 JEditorPane、HTMLEditorKit 和 HTML 解析搜索的相关算法。

JEditorPane 基本概念

JEditorPane 是 Swing 中的文本组件,可以显示和编辑多种格式的内容,包括 HTML 和 RTF。

JEditorPane editor = new JEditorPane();
editor.setContentType("text/html");
editor.setText(htmlContent);

HTMLEditorKit 与解析器

HTMLEditorKit 架构

HTMLEditorKit kit = new HTMLEditorKit();
editor.setEditorKit(kit);

获取文档和解析器

HTMLDocument doc = (HTMLDocument) editor.getDocument();
HTMLEditorKit.Parser parser = kit.getParser();

文本搜索算法实现

1 基础文本搜索

public class HTMLTextSearch {
    // 简单文本搜索
    public List<Integer> searchPlainText(JEditorPane editor, String keyword) {
        List<Integer> positions = new ArrayList<>();
        try {
            HTMLDocument doc = (HTMLDocument) editor.getDocument();
            String content = doc.getText(0, doc.getLength());
            int index = 0;
            while ((index = content.indexOf(keyword, index)) != -1) {
                positions.add(index);
                index += keyword.length();
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        return positions;
    }
}

2 基于正则表达式的搜索

public class RegexHTMLSearch {
    public List<Integer> searchWithRegex(JEditorPane editor, String regex) {
        List<Integer> positions = new ArrayList<>();
        try {
            HTMLDocument doc = (HTMLDocument) editor.getDocument();
            String content = doc.getText(0, doc.getLength());
            Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
            Matcher matcher = pattern.matcher(content);
            while (matcher.find()) {
                positions.add(matcher.start());
            }
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
        return positions;
    }
}

3 高级搜索算法 - 多线程搜索

public class ParallelHTMLSearch {
    public List<Integer> parallelSearch(JEditorPane editor, String keyword, int numThreads) {
        List<Integer> positions = Collections.synchronizedList(new ArrayList<>());
        try {
            HTMLDocument doc = (HTMLDocument) editor.getDocument();
            String content = doc.getText(0, doc.getLength());
            ExecutorService executor = Executors.newFixedThreadPool(numThreads);
            int segmentSize = content.length() / numThreads;
            for (int i = 0; i < numThreads; i++) {
                int start = i * segmentSize;
                int end = (i == numThreads - 1) ? content.length() : (i + 1) * segmentSize;
                executor.submit(() -> {
                    int localIndex = start;
                    while ((localIndex = content.indexOf(keyword, localIndex)) != -1 && localIndex < end) {
                        positions.add(localIndex);
                        localIndex += keyword.length();
                    }
                });
            }
            executor.shutdown();
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Collections.sort(positions);
        return positions;
    }
}

HTML 标签解析搜索

1 基于标签的搜索

public class TagBasedSearch {
    // 搜索特定标签内的文本
    public List<String> searchInTags(JEditorPane editor, String tag, String keyword) {
        List<String> results = new ArrayList<>();
        HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
            boolean inTargetTag = false;
            StringBuilder text = new StringBuilder();
            @Override
            public void handleText(char[] data, int pos) {
                if (inTargetTag) {
                    String content = new String(data);
                    if (content.toLowerCase().contains(keyword.toLowerCase())) {
                        results.add(content.trim());
                    }
                }
            }
            @Override
            public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
                if (t.toString().equalsIgnoreCase(tag)) {
                    inTargetTag = true;
                }
            }
            @Override
            public void handleEndTag(HTML.Tag t, int pos) {
                if (t.toString().equalsIgnoreCase(tag)) {
                    inTargetTag = false;
                }
            }
        };
        try {
            HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit();
            Reader reader = new StringReader(editor.getText());
            kit.getParser().parse(reader, callback, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return results;
    }
}

2 属性搜索

public class AttributeSearch {
    // 搜索特定属性的值
    public List<String> searchAttributes(JEditorPane editor, String attribute) {
        List<String> attributeValues = new ArrayList<>();
        HTMLEditorKit.ParserCallback callback = new HTMLEditorKit.ParserCallback() {
            @Override
            public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
                Object value = a.getAttribute(attribute);
                if (value != null) {
                    attributeValues.add(value.toString());
                }
            }
        };
        try {
            HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit();
            Reader reader = new StringReader(editor.getText());
            kit.getParser().parse(reader, callback, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return attributeValues;
    }
}

搜索高亮功能

1 高亮搜索结果

public class SearchHighlighter {
    public void highlightSearchResults(JEditorPane editor, String keyword, Color highlightColor) {
        try {
            HTMLDocument doc = (HTMLDocument) editor.getDocument();
            String content = doc.getText(0, doc.getLength());
            Highlighter highlighter = editor.getHighlighter();
            highlighter.removeAllHighlights();
            int index = 0;
            while ((index = content.indexOf(keyword, index)) != -1) {
                highlighter.addHighlight(index, index + keyword.length(), 
                    new DefaultHighlighter.DefaultHighlightPainter(highlightColor));
                index += keyword.length();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

2 自定义高亮样式

public class CustomHighlightPainter extends DefaultHighlighter.DefaultHighlightPainter {
    public CustomHighlightPainter(Color color) {
        super(color);
    }
    @Override
    public Shape paintLayer(Graphics g, int offs0, int offs1, 
                          Shape bounds, JTextComponent c, View view) {
        Rectangle rect = bounds.getBounds();
        g.setColor(getColor());
        g.fillRoundRect(rect.x, rect.y, rect.width, rect.height, 5, 5);
        return rect;
    }
}

性能优化算法

1 缓存搜索

public class CachedSearch {
    private Map<String, List<Integer>> searchCache = new HashMap<>();
    private String lastContent = "";
    public List<Integer> searchWithCache(JEditorPane editor, String keyword) {
        try {
            String currentContent = editor.getText();
            // 如果内容没有变化,使用缓存
            if (currentContent.equals(lastContent)) {
                return searchCache.getOrDefault(keyword, new ArrayList<>());
            }
            // 内容变化,重新搜索并缓存
            lastContent = currentContent;
            searchCache.clear();
            List<Integer> positions = searchPlainText(editor, keyword);
            searchCache.put(keyword, positions);
            return positions;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
    private List<Integer> searchPlainText(JEditorPane editor, String keyword) {
        // 实现基本的文本搜索
        return new ArrayList<>();
    }
}

2 增量搜索

public class IncrementalSearch {
    private int lastSearchIndex = 0;
    private String lastKeyword = "";
    public int incrementalSearch(JEditorPane editor, String keyword, boolean forward) {
        try {
            HTMLDocument doc = (HTMLDocument) editor.getDocument();
            String content = doc.getText(0, doc.getLength());
            int result = -1;
            if (forward) {
                result = content.indexOf(keyword, lastSearchIndex);
            } else {
                result = content.lastIndexOf(keyword, lastSearchIndex);
            }
            if (result != -1) {
                lastSearchIndex = result + (forward ? keyword.length() : 0);
            }
            return result;
        } catch (BadLocationException e) {
            e.printStackTrace();
            return -1;
        }
    }
}

完整搜索示例

public class HTMLSearchDemo {
    public static void main(String[] args) {
        JEditorPane editor = new JEditorPane();
        editor.setContentType("text/html");
        editor.setText("<html><body>" +
                      "<h1>标题</h1>" +
                      "<p>这是第一段文本内容</p>" +
                      "<a href='link1'>链接1</a>" +
                      "<p>这是第二段文本内容</p>" +
                      "</body></html>");
        HTMLSearchEngine searchEngine = new HTMLSearchEngine(editor);
        // 基础搜索
        List<Integer> positions = searchEngine.basicSearch("文本");
        // 高亮结果
        searchEngine.highlightResults(positions, Color.YELLOW);
        // 标签内搜索
        List<String> paragraphContent = 
            searchEngine.searchInTag("p", "内容");
        // 属性搜索
        List<String> links = 
            searchEngine.searchAttribute("href");
    }
}

这些算法和方法提供了完整的 HTML 内容搜索解决方案,可以根据具体需求选择合适的实现方式。

抱歉,评论功能暂时关闭!