comparison src/nabble/model/MessageUtils.java @ 0:7ecd1a4ef557

add content
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 21 Mar 2019 19:15:52 -0600
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 package nabble.model;
2
3 import java.net.URL;
4 import java.net.MalformedURLException;
5 import java.util.Iterator;
6 import java.util.regex.Matcher;
7 import fschmidt.html.Html;
8 import fschmidt.html.HtmlTag;
9 import fschmidt.html.HtmlTextContainer;
10 import fschmidt.util.mail.MailAddress;
11
12
13 public final class MessageUtils {
14 private MessageUtils() {} // never
15
16 public static String getTextWithoutQuotes(String msg) {
17 Html list = newHtml(msg);
18 removeQuotes(list);
19 return htmlToText(list);
20 }
21
22 static Html newHtml(String msg) {
23 Html html = new Html();
24 html.containerTags().add("raw");
25 html.parse(msg);
26 return html;
27 }
28
29 private static void removeQuotes(Html list) {
30 int quoteDepth = 0;
31 for( Iterator i=list.iterator(); i.hasNext(); ) {
32 Object next = i.next();
33 boolean remove = quoteDepth > 0;
34 if (next instanceof HtmlTag) {
35 String tagName = ((HtmlTag)next).getName().toLowerCase();
36 if( tagName.equals("quote") ) {
37 quoteDepth++;
38 remove = true;
39 } else if( tagName.equals("/quote") && quoteDepth > 0 ) {
40 quoteDepth--;
41 }
42 }
43 if( remove )
44 i.remove();
45 }
46 }
47
48 static String htmlToText(Html html) {
49 StringBuilder buf = new StringBuilder();
50 for( Iterator i=html.iterator(); i.hasNext(); ) {
51 Object next = i.next();
52 if( next instanceof String ) {
53 buf.append(next);
54 } else if (next instanceof HtmlTag) {
55 boolean isSeparator = Message.htmlSeparators.containsKey(((HtmlTag)next).getName().toLowerCase());
56 if (isSeparator && buf.length()>0 && !Character.isWhitespace(buf.charAt(buf.length()-1)))
57 buf.append(' ');
58 }
59 }
60 return buf.toString();
61 }
62
63 public static String htmlToSearchText(Html html) {
64 StringBuilder buf = new StringBuilder();
65 for( Iterator i=html.iterator(); i.hasNext(); ) {
66 Object next = i.next();
67 if( next instanceof String ) {
68 buf.append(next);
69 } else if (next instanceof HtmlTag) {
70 HtmlTag tag = (HtmlTag)next;
71 String name = tag.getName().toLowerCase();
72 if( name.equals("iframe") ) {
73 buf.append(" html_iframe");
74 } else if( name.equals("img") ) {
75 String src = HtmlTag.unquote(tag.getAttributeValue("src"));
76 if( src != null )
77 buf.append(" html_img_").append( src.substring(src.lastIndexOf('.')+1) );
78 } else if( name.equals("a") ) {
79 String href = HtmlTag.unquote(tag.getAttributeValue("href"));
80 if( href != null ) {
81 buf.append(" html_a");
82 try {
83 String domain = new URL(href).getHost();
84 buf.append(" link_").append(domain);
85 } catch(MalformedURLException e) {} // ignore
86 }
87 }
88 buf.append(' ');
89 }
90 }
91 return buf.toString();
92 }
93
94 public static String hideAllEmails(String txt) {
95 if (txt==null) return null;
96 Matcher m = MailAddress.EMAIL_PATTERN.matcher(txt);
97 StringBuffer buf = new StringBuffer();
98 while (m.find()) {
99 m.appendReplacement(buf, "$1@...");
100 }
101 m.appendTail(buf);
102 return buf.toString();
103 }
104
105 }