01: /*
02: *
03: * Copyright 2005 Joe Walker
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package uk.ltd.getahead.dwrdemo.asmg;
19:
20: import java.util.StringTokenizer;
21:
22: import org.directwebremoting.Security;
23:
24: /**
25: * Generate an anti-spam mailto link from an email address.
26: * The output link looks something like this (where $1 is the username part of
27: * the address and $2 is the hostname part:
28: * <pre>
29: * Contact us using:
30: * <script type="text/javascript">
31: * var a = $1 + "@" + $2;
32: * document.write("<a href='mail" + "to:" + a + "'>" + a + "</a>");
33: * </script>
34: * <noscript>[$1 at $2]</noscript>
35: * </pre>
36: * @author Joe Walker [joe at getahead dot ltd dot uk]
37: */
38: public class Generator {
39: /**
40: * Generate an anti-spam mailto link from an email address
41: * @param name The person to contact
42: * @param email The address to generate a link from
43: * @return The HTML snippet
44: */
45: public String generateAntiSpamMailto(String name, String email) {
46: StringTokenizer st = new StringTokenizer(email, "@");
47: if (Security.containsXssRiskyCharacters(email)
48: || st.countTokens() != 2) {
49: throw new IllegalArgumentException(
50: "Invalid email address: " + email);
51: }
52:
53: String before = st.nextToken();
54: String after = st.nextToken();
55:
56: StringBuffer buffer = new StringBuffer();
57:
58: buffer.append("Contact ");
59: buffer.append(Security.replaceXmlCharacters(name));
60: buffer.append(" using: <span id=\"asmgLink\"></span>\n");
61: buffer.append("<script type='text/javascript'>\n");
62:
63: buffer.append("var before = '");
64: buffer.append(before);
65: buffer.append("';\n");
66:
67: buffer.append("var after = '");
68: buffer.append(after);
69: buffer.append("';\n");
70:
71: buffer
72: .append("var link = \"<a href='mail\" + \"to:\" + before + '@' + after + \"'>\" + before + '@' + after + \"</a>\";\n");
73:
74: buffer
75: .append("document.getElementById(\"asmgLink\").innerHTML = link;\n");
76: buffer.append("</script>\n");
77: buffer.append("<noscript>[");
78: buffer.append(before);
79: buffer.append(" at ");
80: buffer.append(after);
81: buffer.append("]</noscript>\n");
82:
83: return buffer.toString();
84: }
85: }
|