Source for de.webdings.tools.MailtoLinkEncoder

   1: /* MailtoLinkEncoder.java - Copyright (c) 2005 by Stefan Thesing
   2:  <p>This file is part of Webdings Tools.</p>
   3:  <p>Webdings Tools is free software; you can redistribute it and/or modify
   4:  it under the terms of the GNU General Public License as published by
   5:  the Free Software Foundation; either version 2 of the License, or
   6:  (at your option) any later version.</p>
   7: <p>Webdings Tools is distributed in the hope that it will be useful,
   8: but WITHOUT ANY WARRANTY; without even the implied warranty of
   9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10: GNU General Public License for more details.</p>
  11: <p>You should have received a copy of the GNU General Public License
  12: along with Webdings Tools; if not, write to the<br>
  13: Free Software Foundation, Inc.,<br>
  14: 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA<br>
  15: */
  16: package de.webdings.tools;
  17: import java.util.Random;
  18: /**
  19:  * MailToLinkEncoder is used encode the characters of an email address into browser-readable
  20:  * mixed characters from decimal and hexadecimal html encodings. This is useful for 
  21:  * mailto-links in html in order to make it harder for harvester programs that scan websites
  22:  * for mailto-links and add them to spam-lists. 
  23:  * 
  24:  * @author Stefan Thesing<br>
  25:  * Website: <a href="http://www.webdings.de">http://www.webdings.de</a>
  26:  * @version 0.1 31.05.2005
  27:  */
  28: public class MailtoLinkEncoder {
  29:     /**
  30:      * @param eMailAdress
  31:      * @return the encoded String
  32:      * @throws Exception if the specified string contains characters that are not allowed in 
  33:      * email addresses.
  34:      */
  35:     public static String encode(String eMailAdress) throws Exception {
  36:         StringBuffer encoded = new StringBuffer();
  37:         char[] array = eMailAdress.toCharArray();
  38:         Random r = new Random();
  39:         int encoding;
  40:         String dec = new String();
  41:         String hex = new String();
  42:         for(int i=0;i<array.length;++i) {
  43:           encoding = r.nextInt(3);
  44:           if(array[i]== '-')
  45:           {
  46:               dec="&#45;";
  47:               hex="&#x2D;";
  48:           }
  49:           else if(array[i]== '.')
  50:           {
  51:               dec="&#46;";
  52:               hex="&#x2E;";
  53:           } else if(array[i]== '0')
  54:           {
  55:               dec="&#48;";
  56:               hex="&#x30;";
  57:           } else if(array[i]== '1')
  58:           {
  59:               dec="&#49;";
  60:               hex="&#x31;";
  61:           } else if(array[i]== '2')
  62:           {
  63:               dec="&#50;";
  64:               hex="&#x32;";
  65:           } else if(array[i]== '3')
  66:           {
  67:               dec="&#51;";
  68:               hex="&#x33;";
  69:           } else if(array[i]== '4')
  70:           {
  71:               dec="&#52;";
  72:               hex="&#x34;";
  73:           } else if(array[i]== '5')
  74:           {
  75:               dec="&#53;";
  76:               hex="&#x35;";
  77:           } else if(array[i]== '6')
  78:           {
  79:               dec="&#54;";
  80:               hex="&#x36;";
  81:           } else if(array[i]== '7')
  82:           {
  83:               dec="&#55;";
  84:               hex="&#x37;";
  85:           } else if(array[i]== '8')
  86:           {
  87:               dec="&#56;";
  88:               hex="&#x38;";
  89:           } else if(array[i]== '9')
  90:           {
  91:               dec="&#57;";
  92:               hex="&#x39;";
  93:           } else if(array[i]== '@') 
  94:           {
  95:               dec="&#64;";
  96:               hex="&#x40;";
  97:           } else if(array[i]== 'A')
  98:           {
  99:               dec="&#65;";
 100:               hex="&#x41;";
 101:           } else if(array[i]== 'B')
 102:           {
 103:               dec="&#66;";
 104:               hex="&#x42;";
 105:           } else if(array[i]== 'C')
 106:           {
 107:               dec="&#67;";
 108:               hex="&#x43;";
 109:           } else if(array[i]== 'D')
 110:           {
 111:               dec="&#68;";
 112:               hex="&#x44;";
 113:           } else if(array[i]== 'E')
 114:           {
 115:               dec="&#69;";
 116:               hex="&#x45;";
 117:           } else if(array[i]== 'F')
 118:           {
 119:               dec="&#70;";
 120:               hex="&#x46;";
 121:           } else if(array[i]== 'G')
 122:           {
 123:               dec="&#71;";
 124:               hex="&#x47;";
 125:           } else if(array[i]== 'H')
 126:           {
 127:               dec="&#72;";
 128:               hex="&#x48;";
 129:           } else if(array[i]== 'C')
 130:           {
 131:               dec="&#67;";
 132:               hex="&#x43;";
 133:           } else if(array[i]== 'I')
 134:           {
 135:               dec="&#73;";
 136:               hex="&#x49;";
 137:           } else if(array[i]== 'J')
 138:           {
 139:               dec="&#74;";
 140:               hex="&#x4A;";
 141:           } else if(array[i]== 'K')
 142:           {
 143:               dec="&#75;";
 144:               hex="&#x4B;";
 145:           } else if(array[i]== 'L')
 146:           {
 147:               dec="&#76;";
 148:               hex="&#x4C;";
 149:           } else if(array[i]== 'M')
 150:           {
 151:               dec="&#77;";
 152:               hex="&#x4D;";
 153:           } else if(array[i]== 'N')
 154:           {
 155:               dec="&#78;";
 156:               hex="&#x4E;";
 157:           } else if(array[i]== 'O')
 158:           {
 159:               dec="&#79;";
 160:               hex="&#x4F;";
 161:           } else if(array[i]== 'P')
 162:           {
 163:               dec="&#80;";
 164:               hex="&#x50;";
 165:           } else if(array[i]== 'Q')
 166:           {
 167:               dec="&#81;";
 168:               hex="&#x51;";
 169:           } else if(array[i]== 'R')
 170:           {
 171:               dec="&#82;";
 172:               hex="&#x52;";
 173:           } else if(array[i]== 'S')
 174:           {
 175:               dec="&#83;";
 176:               hex="&#x53;";
 177:           } else if(array[i]== 'T')
 178:           {
 179:               dec="&#84;";
 180:               hex="&#x54;";
 181:           } else if(array[i]== 'U')
 182:           {
 183:               dec="&#85;";
 184:               hex="&#x55;";
 185:           } else if(array[i]== 'V')
 186:           {
 187:               dec="&#86;";
 188:               hex="&#x56;";
 189:           } else if(array[i]== 'W')
 190:           {
 191:               dec="&#87;";
 192:               hex="&#x57;";
 193:           } else if(array[i]== 'X')
 194:           {
 195:               dec="&#88;";
 196:               hex="&#x58;";
 197:           } else if(array[i]== 'Y')
 198:           {
 199:               dec="&#89;";
 200:               hex="&#x59;";
 201:           } else if(array[i]== 'Z')
 202:           {
 203:               dec="&#90;";
 204:               hex="&#x5A;";
 205:           } else if(array[i]== '_')
 206:           {
 207:               dec="&#95;";
 208:               hex="&#x5F;";
 209:           } else if(array[i]== 'a')
 210:           {
 211:               dec="&#97;";
 212:               hex="&#x61;";
 213:           } else if(array[i]== 'b')
 214:           {
 215:               dec="&#98;";
 216:               hex="&#x62;";
 217:           } else if(array[i]== 'c')
 218:           {
 219:               dec="&#99;";
 220:               hex="&#x63;";
 221:           } else if(array[i]== 'd')
 222:           {
 223:               dec="&#100;";
 224:               hex="&#x64;";
 225:           } else if(array[i]== 'e')
 226:           {
 227:               dec="&#101;";
 228:               hex="&#x65;";
 229:           } else if(array[i]== 'f')
 230:           {
 231:               dec="&#102;";
 232:               hex="&#x66;";
 233:           } else if(array[i]== 'g')
 234:           {
 235:               dec="&#103;";
 236:               hex="&#x67;";
 237:           } else if(array[i]== 'h')
 238:           {
 239:               dec="&#104;";
 240:               hex="&#x68;";
 241:           } else if(array[i]== 'i')
 242:           {
 243:               dec="&#105;";
 244:               hex="&#x69;";
 245:           } else if(array[i]== 'j')
 246:           {
 247:               dec="&#106;";
 248:               hex="&#x6A;";
 249:           } else if(array[i]== 'k')
 250:           {
 251:               dec="&#107;";
 252:               hex="&#x6B;";
 253:           } else if(array[i]== 'l')
 254:           {
 255:               dec="&#108;";
 256:               hex="&#x6C;";
 257:           } else if(array[i]== 'm')
 258:           {
 259:               dec="&#109;";
 260:               hex="&#x6D;";
 261:           } else if(array[i]== 'n')
 262:           {
 263:               dec="&#110;";
 264:               hex="&#x6E;";
 265:           } else if(array[i]== 'o')
 266:           {
 267:               dec="&#111;";
 268:               hex="&#x6F;";
 269:           } else if(array[i]== 'p')
 270:           {
 271:               dec="&#112;";
 272:               hex="&#x70;";
 273:           } else if(array[i]== 'q')
 274:           {
 275:               dec="&#113;";
 276:               hex="&#x71;";
 277:           } else if(array[i]== 'r')
 278:           {
 279:               dec="&#114;";
 280:               hex="&#x72;";
 281:           } else if(array[i]== 's')
 282:           {
 283:               dec="&#115;";
 284:               hex="&#x73;";
 285:           } else if(array[i]== 't')
 286:           {
 287:               dec="&#116;";
 288:               hex="&#x74;";
 289:           } else if(array[i]== 'u')
 290:           {
 291:               dec="&#117;";
 292:               hex="&#x75;";
 293:           } else if(array[i]== 'v')
 294:           {
 295:               dec="&#118;";
 296:               hex="&#x76;";
 297:           } else if(array[i]== 'w')
 298:           {
 299:               dec="&#119;";
 300:               hex="&#x77;";
 301:           } else if(array[i]== 'x')
 302:           {
 303:               dec="&#120;";
 304:               hex="&#x78;";
 305:           } else if(array[i]== 'y')
 306:           {
 307:               dec="&#121;";
 308:               hex="&#x79;";
 309:           } else if(array[i]== 'z')
 310:           {
 311:               dec="&#122;";
 312:               hex="&#x7A;";
 313:           } else {
 314:               throw new Exception("This is no valid E-Mail address!");
 315:           }
 316:           
 317:           if(encoding==0) {
 318:               encoded.append(dec);
 319:           } else if(encoding==1) {
 320:               encoded.append(hex);
 321:           } else {
 322:               encoded.append(array[i]);
 323:           }
 324:         }
 325:         return new String(encoded); 
 326:     }
 327: }

© 2005 by Stefan Thesing;
Verbatim copying and redistribution of this entire page are permitted provided this notice is preserved.