KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > email > MimeMessageGenerator


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.applications.email;
12
13 import java.util.*;
14
15 import javax.mail.MessagingException JavaDoc;
16 import javax.mail.internet.*;
17
18
19 import org.mmbase.util.logging.Logging;
20 import org.mmbase.util.logging.Logger;
21
22 /**
23  * @javadoc
24  * @author Daniel Ockeloen
25  *
26  */

27 public class MimeMessageGenerator {
28
29     private static final Logger log = Logging.getLoggerInstance(MimeMessageGenerator.class);
30
31
32
33     /**
34      * @javadoc
35      */

36     public static MimeMultipart getMimeMultipart(String JavaDoc text) {
37         
38         Hashtable nodes = new Hashtable();
39         Vector rootnodes = new Vector();
40
41
42     Enumeration tags=MimeBodyTagger.getMimeBodyParts(text);
43     while (tags.hasMoreElements()) {
44             try {
45         MimeBodyTag tag=(MimeBodyTag)tags.nextElement();
46
47         // get all the needed fields
48
String JavaDoc type=tag.getType();
49         String JavaDoc id=tag.getId();
50         String JavaDoc related=tag.getRelated();
51         String JavaDoc alt=tag.getAlt();
52
53         // add it to the id cache
54
nodes.put(id,tag);
55
56         // is it a root node ?
57
if (alt==null && related==null) {
58                     rootnodes.addElement(tag);
59         } else if (alt!=null) {
60                     MimeBodyTag oldpart=(MimeBodyTag)nodes.get(alt);
61                     if (oldpart!=null) {
62                         oldpart.addAlt(tag);
63                     }
64         } else if (related!=null) {
65                     MimeBodyTag oldpart=(MimeBodyTag)nodes.get(related);
66                     if (oldpart!=null) {
67                         oldpart.addRelated(tag);
68                     }
69         }
70
71             } catch(Exception JavaDoc e) {
72         log.error("Mime mail error");
73             }
74     }
75
76     if (rootnodes.size()==1) {
77             MimeBodyTag t=(MimeBodyTag)rootnodes.elementAt(0);
78             MimeMultipart mmp=t.getMimeMultipart();
79             if (mmp!=null) {
80                 return(mmp);
81             }
82     } else {
83         if (rootnodes.size()>1) {
84             try {
85                 MimeMultipart root = new MimeMultipart();
86                 root.setSubType("mixed");
87                 Enumeration l = rootnodes.elements();
88                 while (l.hasMoreElements()) {
89                     MimeBodyTag t = (MimeBodyTag) l.nextElement();
90                     MimeMultipart mmp = t.getMimeMultipart();
91                     if (mmp != null) {
92                         log.info("setting parent info : " + t.getId());
93                         MimeBodyPart wrapper = new MimeBodyPart();
94                         wrapper.setContent(mmp);
95                         root.addBodyPart(wrapper);
96                     }
97                     else {
98                         log.info("adding info : " + t.getId());
99                         root.addBodyPart(t.getMimeBodyPart());
100                     }
101                 }
102                 return root;
103             } catch (MessagingException JavaDoc e) {
104                 log.error("Root generation error" + e.getMessage());
105             }
106         } else {
107                 log.error("Don't have a root node");
108         }
109     }
110     return null;
111     }
112
113 }
114
Popular Tags