KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > asmg > Generator


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.getahead.dwrdemo.asmg;
17
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.directwebremoting.Security;
21
22 /**
23  * Generate an anti-spam mailto link from an email address.
24  * The output link looks something like this (where $1 is the username part of
25  * the address and $2 is the hostname part:
26  * <pre>
27  * Contact us using:
28  * &lt;script type="text/javascript"&gt;
29  * var a = $1 + "@" + $2;
30  * document.write("&lt;a HREF='mail" + "to:" + a + "'&gt;" + a + "&lt;/a&gt;");
31  * &lt;/script&gt;
32  * &lt;noscript&gt;[$1 at $2]&lt;/noscript&gt;
33  * </pre>
34  * @author Joe Walker [joe at getahead dot ltd dot uk]
35  */

36 public class Generator
37 {
38     /**
39      * Generate an anti-spam mailto link from an email address
40      * @param name The person to contact
41      * @param email The address to generate a link from
42      * @return The HTML snippet
43      */

44     public String JavaDoc generateAntiSpamMailto(String JavaDoc name, String JavaDoc email)
45     {
46         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(email, "@");
47         if (Security.containsXssRiskyCharacters(email) || st.countTokens() != 2)
48         {
49             throw new IllegalArgumentException JavaDoc("Invalid email address: " + email);
50         }
51
52         String JavaDoc before = st.nextToken();
53         String JavaDoc after = st.nextToken();
54
55         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
56
57         buffer.append("Contact ");
58         buffer.append(Security.replaceXmlCharacters(name));
59         buffer.append(" using: <span id=\"asmgLink\"></span>\n");
60         buffer.append("<script type='text/javascript'>\n");
61
62         buffer.append("var before = '");
63         buffer.append(before);
64         buffer.append("';\n");
65
66         buffer.append("var after = '");
67         buffer.append(after);
68         buffer.append("';\n");
69
70         buffer.append("var link = \"<a HREF='mail\" + \"to:\" + before + '@' + after + \"'>\" + before + '@' + after + \"</a>\";\n");
71
72         buffer.append("document.getElementById(\"asmgLink\").innerHTML = link;\n");
73         buffer.append("</script>\n");
74         buffer.append("<noscript>[");
75         buffer.append(before);
76         buffer.append(" at ");
77         buffer.append(after);
78         buffer.append("]</noscript>\n");
79
80         return buffer.toString();
81     }
82 }
83
Popular Tags