KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > w3c > tidy > TestMessageListener


1 package org.w3c.tidy;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7
8 /**
9  * @author fgiust
10  * @version $Revision: 1.2 $ ($Author: fgiust $)
11  */

12 public class TestMessageListener implements TidyMessageListener
13 {
14
15     /**
16      * filename.
17      */

18     private String JavaDoc filename;
19
20     /**
21      * Contains all the received TidyMessages.
22      */

23     private List JavaDoc received = new ArrayList JavaDoc();
24
25     /**
26      * Instantiate a new messag listener for the given test file.
27      * @param filename actual test case name
28      */

29     public TestMessageListener(String JavaDoc filename)
30     {
31         this.filename = filename;
32     }
33
34     /**
35      * @see org.w3c.tidy.TidyMessageListener#messageReceived(org.w3c.tidy.TidyMessage)
36      */

37     public void messageReceived(TidyMessage message)
38     {
39         received.add(message);
40     }
41
42     /**
43      * Write received messages as xml.
44      * @return xml containing message details.
45      */

46     public String JavaDoc messagesToXml()
47     {
48         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
49
50         buffer.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
51         buffer.append("<!-- expected messages for test ");
52         buffer.append(this.filename);
53         buffer.append("-->\n");
54
55         buffer.append("<messages>\n");
56         Iterator JavaDoc iterator = received.iterator();
57         while (iterator.hasNext())
58         {
59             TidyMessage msg = (TidyMessage) iterator.next();
60             buffer.append(" <message>\n");
61
62             buffer.append(" <code>");
63             buffer.append(msg.getErrorCode());
64             buffer.append("</code>\n");
65
66             buffer.append(" <level>");
67             buffer.append(msg.getLevel().getCode());
68             buffer.append("</level>\n");
69
70             buffer.append(" <line>");
71             buffer.append(msg.getLine());
72             buffer.append("</line>\n");
73
74             buffer.append(" <column>");
75             buffer.append(msg.getColumn());
76             buffer.append("</column>\n");
77
78             buffer.append(" <text><![CDATA[");
79             buffer.append(msg.getMessage());
80             buffer.append("]]></text>\n");
81
82             buffer.append(" </message>\n");
83         }
84
85         buffer.append("</messages>\n");
86
87         return buffer.toString();
88     }
89
90     /**
91      * Getter for <code>filename</code>.
92      * @return Returns the filename.
93      */

94     protected String JavaDoc getFilename()
95     {
96         return this.filename;
97     }
98
99     /**
100      * Returns the list of received messages.
101      * @return Returns the received messages.
102      */

103     public List JavaDoc getReceived()
104     {
105         return this.received;
106     }
107
108 }
109
Popular Tags