private final void find(Pattern pattern) { final WriteOnce<Integer> once = new WriteOnce<Integer>(); final Highlighter highlighter = textPane.getHighlighter(); highlighter.removeAllHighlights(); int matches = 0; final HTMLDocument doc = (HTMLDocument) textPane.getDocument(); for (final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.CONTENT); it.isValid(); it.next()) try { final String fragment = doc.getText(it.getStartOffset(), it.getEndOffset() - it.getStartOffset()); final Matcher matcher = pattern.matcher(fragment); while (matcher.find()) { highlighter.addHighlight( it.getStartOffset() + matcher.start(), it.getStartOffset() + matcher.end(), HIGHLIGHTER); once.set(it.getStartOffset()); ++matches; } } catch (final BadLocationException ex) { } if (!once.isEmpty()) textPane.setCaretPosition(once.get()); this.matches.setText(String.format("%d matches", matches)); }
public JSONArray convertHTMLToFlag(HTMLDocument htmlDoc) { boolean isExistFace = false; ElementIterator it = new ElementIterator(htmlDoc); Element element; while ((element = it.next()) != null) { if (element.getName().equals(HTML.Tag.IMG.toString())) { isExistFace = true; try { String name = element.getAttributes().getAttribute(HTML.Attribute.NAME).toString(); // String src = element.getAttributes().getAttribute(HTML.Attribute.SRC).toString(); int offset = element.getStartOffset(); htmlDoc.replace(offset, element.getEndOffset() - offset, "~face:" + name + "~", null); } catch (BadLocationException ex) { Logger.getLogger(QQImageUtil.class.getName()).log(Level.SEVERE, null, ex); } } } String text = null; try { text = htmlDoc.getText(0, htmlDoc.getLength()); htmlDoc.remove(0, htmlDoc.getLength()); } catch (BadLocationException ex) { Logger.getLogger(QQImageUtil.class.getName()).log(Level.SEVERE, null, ex); } if (isExistFace) { text = text.replaceFirst("\\n", ""); } // Log.println(text); JSONArray msg = new JSONArray(); String[] arr = text.split("~"); for (int i = 0; i < arr.length; i++) { String temp = arr[i]; // Log.println(temp); if (temp.startsWith("face:")) { String[] tempArray = temp.split(":"); JSONArray face = new JSONArray(); face.add(tempArray[0]); String regex = ",([0-9]*):" + tempArray[1] + ","; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(faceFlag); String result = null; if (m.find()) { result = m.group(1); } else { result = tempArray[1]; } int faceNumber = Integer.parseInt(result); face.add(faceNumber); msg.add(face); } else { msg.add(temp); } } // Log.println(msg); return msg; }
/** * Extracts text from an HTML document and stores it in the document. * * @param filesInputStream An input stream pointing to the HTML document to be read. * @throws BadLocationException * @throws IOException */ private static char[] loadHTML(InputStream filesInputStream) throws IOException, BadLocationException { EditorKit kit = new HTMLEditorKit(); HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument(); doc.putProperty("IgnoreCharsetDirective", true); kit.read(filesInputStream, doc, 0); char[] origText = doc.getText(0, doc.getLength()).toCharArray(); return origText; }