View Javadoc

1   package org.paneris.jammyjoes.mail;
2   
3   import java.io.IOException;
4   import java.io.OutputStream;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import javax.mail.MessagingException;
9   
10  import com.quiotix.html.parser.HtmlDocument;
11  import com.quiotix.html.parser.HtmlDumper;
12  import com.quiotix.html.parser.HtmlDocument.Attribute;
13  import com.quiotix.html.parser.HtmlDocument.Tag;
14  
15  public class ImageTranslator extends HtmlDumper {
16    
17    private int count;
18    private NewsletterInterface newsletter;
19  
20    public ImageTranslator(OutputStream out, NewsletterInterface newsletter) {
21      super(out);
22      this.newsletter = newsletter;
23    }
24  
25    public void visit(Tag t)  {
26      if (t.tagName.equals("img")){
27        out.write("<img ");
28        Iterator attributes = t.attributeList.attributes.iterator();
29        while (attributes.hasNext()) {
30          Attribute element = (Attribute) attributes.next();
31          if (element.name.equals("src")) {
32            out.write("src=\"cid:image" + count + "\" ");
33          } else { 
34            out.write(element.toString() + " ");
35          }
36        }
37        out.write(">");
38          try {
39            newsletter.addImage(findName(t.attributeList.attributes), count);
40          } catch (MessagingException e) {
41            throw new RuntimeException(e);
42          } catch (IOException e) {
43            throw new RuntimeException(e);
44          }
45          count++;
46      } else {
47        super.visit(t);
48      }
49    }
50  
51    public String findName(List vector) {
52      Iterator iter = vector.iterator();
53      while (iter.hasNext()) {
54        HtmlDocument.Attribute element = (HtmlDocument.Attribute) iter.next();
55        if (element.name.equals("src")) {
56          return element.value.replaceAll("\"","");
57        }
58      }
59      return null;
60    }
61  
62  }