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; }
private void replaceRange(String newStr, int start, int end) { HTMLDocument doc = (HTMLDocument) getDocument(); try { doc.remove(start, (end - start)); insert(newStr, start); } catch (BadLocationException ble) { ble.printStackTrace(); } setDocument(doc); }
// appends a message to the log tab and system out. public void Log(String msg) { try { kit.insertHTML(doc, doc.getLength(), msg, 0, 0, null); int over_length = doc.getLength() - msg.length() - 5000; doc.remove(0, over_length); } catch (BadLocationException e) { // TODO Auto-generated catch block // e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block // e.printStackTrace(); } }
/** * Sets the HTML content to be displayed. * * @param content an HTML document */ private void setDocumentContent(String content) { HTMLDocument doc = new HTMLDocument(); try { doc.remove(0, doc.getLength()); } catch (BadLocationException e) { e.printStackTrace(); } doc.putProperty("IgnoreCharsetDirective", Boolean.TRUE); try { htmlPane.read(new ByteArrayInputStream(content.getBytes()), doc); } catch (IOException e) { e.printStackTrace(); } htmlPane.setDocument(doc); htmlPane.setCaretPosition(0); taTextResponse.setText(content); taTextResponse.setCaretPosition(0); taTextResponse.requestFocus(); }