BrightSide Workbench Full Report + Source Code
org.turro.mail.impl.MailFormat Class Reference

Public Member Functions

String inline (String html)
 
String inlineStyles (String html) throws IOException
 

Static Public Attributes

static final String MAIL_CSS = "/WEB-INF/elephant/templates-mail/css"
 

Detailed Description

Original code from Jonathan Pearlin can be found here: http://jdpgrailsdev.github.io/blog/2015/01/06/email_inline_style_jsoup_cssparser.html

Author
Lluis TurrĂ³ Cutiller lluis.nosp@m.@tur.nosp@m.ro.or.nosp@m.g

Definition at line 52 of file MailFormat.java.

Member Function Documentation

◆ inline()

String org.turro.mail.impl.MailFormat.inline ( String  html)

Definition at line 56 of file MailFormat.java.

56  {
57  if(Strings.isBlank(html)) {
58  return html;
59  }
60 // try {
61 // html = inlineStyles(html);
62 // } catch (IOException ex) {
63 // Logger.getLogger(MailFormat.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
64 // }
65  File rootCSS = new File(ElephantContext.getRealPath(MAIL_CSS));
66  if (rootCSS.exists()) {
67  for (File css : rootCSS.listFiles(new FilenameFilter() {
68  @Override
69  public boolean accept(File dir, String name) {
70  return name.endsWith(".css");
71  }
72  })) {
73  try {
74  html = inlineCSS(css, html);
75  } catch (IOException ex) {
76  Logger.getLogger(MailFormat.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
77  }
78  }
79  }
80  return HTMLEntities.escape(html);
81  }
static final String MAIL_CSS
Definition: MailFormat.java:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ inlineStyles()

String org.turro.mail.impl.MailFormat.inlineStyles ( String  html) throws IOException

Definition at line 132 of file MailFormat.java.

132  {
133  final Document doc = Jsoup.parse(html);
134  String style = "style";
135  Elements els = doc.select(style);
136  for (Element e : els) {
137  String styleRules = e.getAllElements().get(0).data().replaceAll("\n", "").trim(), delims
138  = "{}";
139  StringTokenizer st = new StringTokenizer(styleRules, delims);
140  while (st.countTokens() > 1) {
141  String selector = st.nextToken(), properties = st.nextToken();
142  if (selector.indexOf(":") > 0) {
143  selector = selector.substring(0, selector.indexOf(":"));
144  }
145  if (Strings.isBlank(selector)) {
146  continue;
147  }
148  Elements selectedElements = doc.select(selector);
149  for (Element selElem : selectedElements) {
150  String oldProperties = selElem.attr(style);
151  selElem.attr(
152  style,
153  oldProperties.length() > 0 ? concatenateProperties(oldProperties,
154  properties) : properties);
155  }
156  }
157  e.remove();
158  }
159 
160  return doc.html();
161  }

Member Data Documentation

◆ MAIL_CSS

final String org.turro.mail.impl.MailFormat.MAIL_CSS = "/WEB-INF/elephant/templates-mail/css"
static

Definition at line 54 of file MailFormat.java.


The documentation for this class was generated from the following file: