Mercurial Hosting > nabble
view src/nabble/model/MessageUtils.java @ 62:4674ed7d56df default tip
remove n2
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 30 Sep 2023 20:25:29 -0600 |
parents | 7ecd1a4ef557 |
children |
line wrap: on
line source
package nabble.model; import java.net.URL; import java.net.MalformedURLException; import java.util.Iterator; import java.util.regex.Matcher; import fschmidt.html.Html; import fschmidt.html.HtmlTag; import fschmidt.html.HtmlTextContainer; import fschmidt.util.mail.MailAddress; public final class MessageUtils { private MessageUtils() {} // never public static String getTextWithoutQuotes(String msg) { Html list = newHtml(msg); removeQuotes(list); return htmlToText(list); } static Html newHtml(String msg) { Html html = new Html(); html.containerTags().add("raw"); html.parse(msg); return html; } private static void removeQuotes(Html list) { int quoteDepth = 0; for( Iterator i=list.iterator(); i.hasNext(); ) { Object next = i.next(); boolean remove = quoteDepth > 0; if (next instanceof HtmlTag) { String tagName = ((HtmlTag)next).getName().toLowerCase(); if( tagName.equals("quote") ) { quoteDepth++; remove = true; } else if( tagName.equals("/quote") && quoteDepth > 0 ) { quoteDepth--; } } if( remove ) i.remove(); } } static String htmlToText(Html html) { StringBuilder buf = new StringBuilder(); for( Iterator i=html.iterator(); i.hasNext(); ) { Object next = i.next(); if( next instanceof String ) { buf.append(next); } else if (next instanceof HtmlTag) { boolean isSeparator = Message.htmlSeparators.containsKey(((HtmlTag)next).getName().toLowerCase()); if (isSeparator && buf.length()>0 && !Character.isWhitespace(buf.charAt(buf.length()-1))) buf.append(' '); } } return buf.toString(); } public static String htmlToSearchText(Html html) { StringBuilder buf = new StringBuilder(); for( Iterator i=html.iterator(); i.hasNext(); ) { Object next = i.next(); if( next instanceof String ) { buf.append(next); } else if (next instanceof HtmlTag) { HtmlTag tag = (HtmlTag)next; String name = tag.getName().toLowerCase(); if( name.equals("iframe") ) { buf.append(" html_iframe"); } else if( name.equals("img") ) { String src = HtmlTag.unquote(tag.getAttributeValue("src")); if( src != null ) buf.append(" html_img_").append( src.substring(src.lastIndexOf('.')+1) ); } else if( name.equals("a") ) { String href = HtmlTag.unquote(tag.getAttributeValue("href")); if( href != null ) { buf.append(" html_a"); try { String domain = new URL(href).getHost(); buf.append(" link_").append(domain); } catch(MalformedURLException e) {} // ignore } } buf.append(' '); } } return buf.toString(); } public static String hideAllEmails(String txt) { if (txt==null) return null; Matcher m = MailAddress.EMAIL_PATTERN.matcher(txt); StringBuffer buf = new StringBuffer(); while (m.find()) { m.appendReplacement(buf, "$1@..."); } m.appendTail(buf); return buf.toString(); } }