KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > html > HTMLFromMIME


1 package SnowMailClient.view.html;
2
3 import SnowMailClient.model.multipart.*;
4 import snow.utils.storage.*;
5 import SnowMailClient.*;
6 import SnowMailClient.html.*;
7
8 import java.util.*;
9 import java.io.*;
10
11 /** this class make it possible to view HTML files embedded in a mime tree,
12     with images referenced with "cid:" links
13
14    1) extract the html content
15    2) remove the scripts, comments, meta, ...
16    3) resolve the links
17       the images are embedded as attachment, and references with content-id's
18    4) pack them out in a temp dir and replace links with absolute links.
19 */

20 public class HTMLFromMIME
21 {
22   MimeTreeModel mimeTree;
23   String JavaDoc htmlContent;
24
25   public HTMLFromMIME(MimeTreeModel mimeTree) throws Exception JavaDoc
26   {
27     this.mimeTree = mimeTree;
28
29     MimePart part = mimeTree.getFirstHTMLTextPart();
30     if(part==null) throw new Exception JavaDoc("No HTML part !");
31     htmlContent = part.getBodyAsText();
32
33     String JavaDoc cont = htmlContent;
34 /* HTMLCleaner hc = new HTMLCleaner(cont, false);
35     htmlContent = hc.getCleanHTMLText();*/

36
37     resolveLinks();
38
39   } // Constructor
40

41
42   public String JavaDoc getHTMLCodeWithLocalizedLinks()
43   {
44     return htmlContent;
45   }
46
47
48   /** 3) the links for images are in the following format
49       <img ... SRC="cid:348240823-848289">
50
51       A related mime parts has the content-id <cid:348240823-848289>
52   */

53   private void resolveLinks() throws Exception JavaDoc
54   {
55      Vector<CIDLink> cids = new Vector<CIDLink>();
56      String JavaDoc contUP = this.htmlContent.toUpperCase();
57      int pos = -5;
58      while(true)
59      {
60         pos = contUP.indexOf("\"CID:", pos+5);
61         if(pos==-1) break; // END
62

63         int posEnd = contUP.indexOf("\"", pos+4);
64         if(posEnd==-1)
65         {
66           throw new Exception JavaDoc("Bad content-id syntax, no closing \" found.");
67         }
68
69         String JavaDoc cid = contUP.substring(pos+5, posEnd);
70         MimePart mp = mimeTree.getPartWithID("<"+cid+">");
71         if(mp!=null)
72         {
73           CIDLink cl = new CIDLink(cid, pos, mp);
74           cids.add(cl);
75           //System.out.println("Part found, CID= "+cid);
76
}
77         else
78         {
79           // not fatal, simply not replaced...
80
System.out.println("Part not found, CID= "+cid);
81         }
82      }
83
84      // now, replace, from the end, all cids with absolute paths
85
for(int i=cids.size()-1; i>=0; i--)
86      {
87         CIDLink cl = cids.elementAt(i);
88         int start = cl.getPosInSource()+1;
89         int end = start+cl.CID.length()+4;
90         htmlContent =
91             htmlContent.substring(0,start)
92           + "file:"+cl.getAbsolutePath()
93           + htmlContent.substring(end);
94      }
95
96      //System.out.println(htmlContent);
97

98   }
99
100
101   // Utils
102
//
103

104   //private
105

106 } // HTMLFromMIME
Popular Tags