I'm working with a Struts2 program that constructs an email message using an email template file and replaces certain placeholder strings in the template with user input retrieved through a form field. The JavaMail project is being used to form the email.
Here's what that looks like:
InputStream in = getClass().getResourceAsStream("emailTemplate.html");
Scanner scanner = new Scanner( in );
String content = "";
while( scanner.hasNextLine() ) {
content += parseLine( scanner.nextLine() );
}
// ...
email.setHtmlContent( content );
The parseLine
method looks like this:
String emailReqNum = //internal code to return number from form;
String emailCompDate = //internal code to return date from form;
String emailComment = //internal code to return comment from form;
String emailStatus = //internal code to return status from form;
String emailServer = //internal code to return server from form;
line = line.replaceAll( "\\[REQUEST NUMBER\\]", emailReqNum );
line = line.replaceAll( "\\[ESTIMATED COMPLETION DATE\\]", emailCompDate );
line = line.replaceAll( "\\[COMMENT\\]", emailComment );
line = line.replaceAll( "\\[STATUS\\]", emailStatus );
line = line.replaceAll( "\\[SERVER\\]", emailServer );
And this is what part of the email template looks like:
<table>
<tr><td><b>Request Number:</b></td><td>[REQUEST NUMBER]</td></tr>
<tr><td><b>Est. Completion Date:</b></td><td>[ESTIMATED COMPLETION DATE]</td></tr>
<tr><td><b>Comment:</b></td><td>[COMMENT]</td></tr>
<tr><td><b>Status:</b></td><td>[STATUS]</td></tr>
</table>
The problem is that whenever a user enters a special RegEx character (\ ^ $ . | ? * + ( ) [ ] { }
) in the comment field, it breaks the replaceAll
methods:
Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
at java.util.regex.Matcher.appendReplacement(Matcher.java:725)
at java.util.regex.Matcher.replaceAll(Matcher.java:825)
at java.lang.String.replaceAll(String.java:1572)
I've tried replacing each of the offending special characters with an escaped version, but that didn't seem to work, and looks ugly as sin:
String filtered = emailComment;
filtered.replaceAll("\\","\\\\");
filtered.replaceAll("\\^","\\\\^");
filtered.replaceAll("\\$","\\\\$");
filtered.replaceAll("\\.","\\\\.");
filtered.replaceAll("\\|","\\\\|");
filtered.replaceAll("\\?","\\\\?");
filtered.replaceAll("\\*","\\\\*");
filtered.replaceAll("\\+","\\\\+");
filtered.replaceAll("\\(","\\\\(");
filtered.replaceAll("\\)","\\\\)");
filtered.replaceAll("\\[","\\\\[");
filtered.replaceAll("\\]","\\\\]");
filtered.replaceAll("\\{","\\\\{");
filtered.replaceAll("\\}","\\\\}");
I fooled around a bit with loops, too.
What I'm ultimately trying to do is go through the user-entered comments, find any special characters, and possibly change them so that the replaceAll method calls in parseLine (above) work successfully to replace the comment in the email's contents. Any ideas?
Thanks!
EDIT:
I guess it makes sense that HTML can't be effectively parsed with RegEx. Maybe I can make the email content plaintext, but I'm not certain that's what my company wants.