KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > parser > MailUrlParser


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.parser;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Hashtable JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23 import java.util.regex.Matcher JavaDoc;
24 import java.util.regex.Pattern JavaDoc;
25
26 import org.columba.ristretto.io.CharSequenceSource;
27 import org.columba.ristretto.message.Address;
28 import org.columba.ristretto.message.BasicHeader;
29 import org.columba.ristretto.parser.HeaderParser;
30 import org.columba.ristretto.parser.ParserException;
31
32
33
34 public class MailUrlParser {
35     /** JDK 1.4+ logging framework logger, used for logging. */
36     private static final Logger JavaDoc LOG = Logger
37             .getLogger("org.columba.mail.parser");
38     
39     private static final Pattern JavaDoc mailtoPattern = Pattern.compile("mailto:([^?]*)\\??(.*)", Pattern.CASE_INSENSITIVE);
40     private static final Pattern JavaDoc headerPattern = Pattern.compile("&?([^=]+)=([^&]+)");
41     
42     public static Map JavaDoc parse(String JavaDoc in) throws ParserException {
43         Hashtable JavaDoc result = new Hashtable JavaDoc();
44         StringBuffer JavaDoc temp = new StringBuffer JavaDoc();
45         
46         Matcher JavaDoc matcher = mailtoPattern.matcher(in);
47         
48         if( matcher.matches() ) {
49             if( matcher.group(1) != null ) {
50                 temp.append("To: ");
51                 temp.append(decodeHTML(matcher.group(1)));
52                 temp.append("\r\n");
53             }
54             
55             if( matcher.group(2) != null) {
56                 matcher = headerPattern.matcher(matcher.group(2));
57                 while( matcher.find()) {
58                     String JavaDoc key = matcher.group(1).toLowerCase();
59                     
60                     if( key.equals("to") || key.equals("cc") || key.equals("bcc") || key.equals("subject")) {
61                         temp.append(matcher.group(1));
62                         temp.append(": ");
63                         temp.append(decodeHTML(matcher.group(2)));
64                         temp.append("\r\n");
65                     } else if( key.equals("body")) {
66                         result.put("body", decodeHTML(matcher.group(2)));
67                     } else {
68                         LOG.warning("Unsafe header in mailto-URL: " + matcher.group());
69                     }
70                 }
71             }
72             
73             BasicHeader header = new BasicHeader( HeaderParser.parse(new CharSequenceSource(temp)));
74             
75             // Convert to MessageOptions
76
Address[] addresses = header.getTo();
77             
78             List JavaDoc addressList = new ArrayList JavaDoc();
79             
80             for(int i=0; i<addresses.length; i++ ) {
81                 addressList.add(addresses[i].toString());
82             }
83             result.put("to", addressList.toArray(new String JavaDoc[0]));
84             
85             
86             addresses = header.getCc();
87             if( addresses.length > 0) {
88                 addressList.clear();
89                 for(int i=0; i<addresses.length; i++ ) {
90                     addressList.add(addresses[i].toString());
91                 }
92                 result.put("cc", addressList.toArray(new String JavaDoc[0]));
93             }
94             
95             addresses = header.getBcc();
96             if( addresses.length > 0) {
97                 addressList.clear();
98                 for(int i=0; i<addresses.length; i++ ) {
99                     addressList.add(addresses[i].toString());
100                 }
101                 result.put("bcc", addressList.toArray(new String JavaDoc[0]));
102             }
103             
104             if( header.getSubject() != null) {
105                 result.put("subject", header.getSubject());
106             }
107             
108         }
109         
110         return result;
111     }
112     
113     private static String JavaDoc decodeHTML(String JavaDoc in) {
114         StringBuffer JavaDoc result = new StringBuffer JavaDoc(in.length());
115         int pos = 0;
116         int nextpos = in.indexOf('%', pos);
117         
118         while( nextpos != -1 ) {
119             result.append(in.substring(pos, nextpos));
120             result.append((char) Integer.parseInt(in.substring(nextpos+1,nextpos+3),16));
121             
122             pos = nextpos + 3;
123             nextpos = in.indexOf('%', pos);
124         }
125         
126         result.append( in.substring(pos));
127         
128         return result.toString();
129     }
130     
131 }
132
133
Popular Tags