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 | 0 | super(out); |
22 | 0 | this.newsletter = newsletter; |
23 | 0 | } |
24 | |
|
25 | |
public void visit(Tag t) { |
26 | 0 | if (t.tagName.equals("img")){ |
27 | 0 | out.write("<img "); |
28 | 0 | Iterator attributes = t.attributeList.attributes.iterator(); |
29 | 0 | while (attributes.hasNext()) { |
30 | 0 | Attribute element = (Attribute) attributes.next(); |
31 | 0 | if (element.name.equals("src")) { |
32 | 0 | out.write("src=\"cid:image" + count + "\" "); |
33 | |
} else { |
34 | 0 | out.write(element.toString() + " "); |
35 | |
} |
36 | 0 | } |
37 | 0 | out.write(">"); |
38 | |
try { |
39 | 0 | newsletter.addImage(findName(t.attributeList.attributes), count); |
40 | 0 | } catch (MessagingException e) { |
41 | 0 | throw new RuntimeException(e); |
42 | 0 | } catch (IOException e) { |
43 | 0 | throw new RuntimeException(e); |
44 | 0 | } |
45 | 0 | count++; |
46 | 0 | } else { |
47 | 0 | super.visit(t); |
48 | |
} |
49 | 0 | } |
50 | |
|
51 | |
public String findName(List vector) { |
52 | 0 | Iterator iter = vector.iterator(); |
53 | 0 | while (iter.hasNext()) { |
54 | 0 | HtmlDocument.Attribute element = (HtmlDocument.Attribute) iter.next(); |
55 | 0 | if (element.name.equals("src")) { |
56 | 0 | return element.value.replaceAll("\"",""); |
57 | |
} |
58 | 0 | } |
59 | 0 | return null; |
60 | |
} |
61 | |
|
62 | |
} |