[jbpm-commits] JBoss JBPM SVN: r4503 - jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl.

do-not-reply at jboss.org do-not-reply at jboss.org
Wed Apr 8 16:20:16 EDT 2009


Author: bradsdavis
Date: 2009-04-08 16:20:16 -0400 (Wed, 08 Apr 2009)
New Revision: 4503

Modified:
   jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java
Log:
Improved the regex and replace methods.

Modified: jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java
===================================================================
--- jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java	2009-04-08 18:52:42 UTC (rev 4502)
+++ jbpm4/branches/email/modules/pvm/src/main/java/org/jbpm/pvm/internal/email/producer/impl/HtmlScriptMailProducer.java	2009-04-08 20:20:16 UTC (rev 4503)
@@ -3,6 +3,7 @@
 import java.util.Collection;
 import java.util.Date;
 import java.util.HashSet;
+import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -25,7 +26,7 @@
  */
 public class HtmlScriptMailProducer extends ScriptMailProducer {
 
-	private static final Pattern imgSrcPattern = Pattern.compile("<img [.]*src=['\"]\\S+['\"]");
+	private static final Pattern imgSrcPattern = Pattern.compile("<img[^>]*(src=['\"](\\S+)['\"])[^>]*",Pattern.CASE_INSENSITIVE);
 	private static final Log log = LogFactory.getLog(HtmlScriptMailProducer.class);
 	
 	protected String html;
@@ -51,27 +52,39 @@
 		email.setTextMsg(this.body);
 		email.setSubject(this.subject);
 		
-		//Run through the HTML, download images, and embed images within HTML message.
+		
+		Set<String> cidSet = new HashSet<String>();
 		Matcher imgMatcher = imgSrcPattern.matcher(html);
-		StringBuffer htmlBuffer = new StringBuffer();
 		while(imgMatcher.find())
 		{
-			String replace = embedImage(email,imgMatcher.group());
-			if(log.isDebugEnabled())
+			String replace = embedImage(email,imgMatcher.group(2));
+			if(replace==null)
 			{
-				log.debug("Group: "+imgMatcher.group());
-				log.debug("Replacement: "+replace);
+				log.warn("The image source is not provided.  Skipping: "+imgMatcher.group());
+				continue;
 			}
-			imgMatcher.appendReplacement(htmlBuffer, replace);
+			if(cidSet.add(replace))
+			{
+				this.html=this.html.replace(imgMatcher.group(2), replace);
+			}
+			else
+			{
+				if(log.isDebugEnabled())
+				{
+					log.debug("Skipped replace as the image has been handled before: "+imgMatcher.group(2));
+				}
+				
+			}
+				
 		}
-		imgMatcher.appendTail(htmlBuffer);
+		
 		if(log.isDebugEnabled())
 		{
-			log.debug("Embedded HTML: "+htmlBuffer.toString());
+			log.debug("Embedded HTML: "+this.html);
 		}
 		try {
 			//Set the embedded HTML to the Email Message.
-			email.setHtmlMsg(htmlBuffer.toString());
+			email.setHtmlMsg(this.html);
 		}
 		catch(Exception e)
 		{
@@ -89,16 +102,18 @@
 	}
 	
 
-	protected String embedImage(HtmlEmail email, String imageTag)
+
+	public String embedImage(HtmlEmail email, String imageSrc)
 	{
-		String special = null;
-		special = imageTag.contains("'") ? "'" : "\"";
-		
-		int opening = imageTag.indexOf(special,1);
-		int closing = imageTag.lastIndexOf(special);
-
-		String prefix = imageTag.substring(0,opening);
-		String src = imageTag.substring(opening+1,closing);
+		if(imageSrc==null||imageSrc.isEmpty())
+		{
+			return null;
+		}
+		if(imageSrc.startsWith("cid:"))
+		{
+			return imageSrc;
+		}
+		String src = imageSrc;
 		String hashName = Integer.toString(src.hashCode());
 		String cid = null; 
 		try{
@@ -106,9 +121,10 @@
 		}
 		catch(Exception e)
 		{
-			return imageTag;
+			log.error("Error fetching image.",e);
+			return imageSrc;
 		}
-		return prefix+special+"cid:"+cid+special;
+		return "cid:"+cid;
 		
 	}
 




More information about the jbpm-commits mailing list