Highlight backgroung colors of some words in HTML Document in java

≡放荡痞女 提交于 2019-12-13 02:56:37

问题


I am reading HTML document as text and extracting matched strings(with start and end index) from documents based on string array.Now i have to change the background color of those extracted strings in the original HTML document.But i should not change the format of HTML file.

so i am using "javax.swing.text.DefaultHighlighter.DefaultHighlightPainter", which will not change the format of document.After highlighting, i am saving file locally, then view in browser to see the colors.I dont want to use any editors for this, as my intention is to view in browser.But thecolor changes are not reflecting here, i did not unserstand where was the problem.Here is my code sample.

public class TestEditer {

public static void main(String[] args) throws IOException, BadLocationException {

     JEditorPane editor = new JEditorPane();
      StringWriter writer = new StringWriter();
      String output;

     HTMLEditorKit htmlKit = new HTMLEditorKit();
     List<Pattern> patternList = new ArrayList<Pattern>();
     output = new Scanner(new File("C:/test.in")).useDelimiter("\\Z").next();//26787.in contains HTML text

         editor.setText(output);

     Document document = editor.getDocument();
    String doc=  document.getText(0, document.getLength());
    System.out.println(document);
     String[] names={"Shannon","Sperling","Kim","Tammy","Nancy","Lana"};
     for (int i=0; i<names.length; i++)
        {
            String st = names[i];
         Pattern p= Pattern.compile(st);
         patternList.add(p);
        }
        System.out.println(patternList);

      for (int i=0; i<patternList.size(); i++)
      {
          Matcher matcher = patternList.get(i).matcher(editor.getText());
          int mStartPos=0;
          int mEndPos=0;
          while (matcher.find()) 
          {
                String matched = matcher.group();
                System.out.println(matched);
                mStartPos = matcher.start();
                mEndPos = matcher.end();
                System.out.println(mStartPos+" "+mEndPos);
                DefaultHighlightPainter highlightPainter =new DefaultHighlightPainter(Color.RED);
                editor.getHighlighter().addHighlight(mStartPos+1, mEndPos,
                        highlightPainter);
          }
      }
      //htmlKit.write(writer, editor.getDocument(),0,editor.getDocument().getLength());
      //String s=editor.getText();

      File file = new File("C:/data/file.in");
      editor.getEditorKit().write(writer,document,0,document.getLength());
     String s1 = writer.toString();
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(s1);
        bw.close();
}

来源:https://stackoverflow.com/questions/20044777/highlight-backgroung-colors-of-some-words-in-html-document-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!