1 package org.apache.maven.j2ee;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.io.File;
21 import java.io.FileNotFoundException;
22 import java.io.FileOutputStream;
23 import java.io.PrintStream;
24
25 /**
26 * Base class for formatters of validation events.
27 * Handles plain and xml formats.
28 *
29 * @version $Id: ValidationFormatter.java 170200 2005-05-15 06:24:19Z brett $
30 * @author dion
31 */
32 public class ValidationFormatter implements ValidationListener
33 {
34
35 /** Whether the formatter should write it's results to a file */
36 private boolean usefile;
37 /** name of the file (if usefile is true) to send output to */
38 private String file;
39 /** type of output: plain/xml */
40 private String type = ValidationFormatter.PLAIN;
41 /** Plain format output */
42 public static final String PLAIN = "plain";
43 /** xml format output */
44 public static final String XML = "xml";
45 /** output destination */
46 private PrintStream stream = null;
47
48
49 /**
50 * Creates a new instance of ValidationFormatter
51 */
52 public ValidationFormatter()
53 {
54 stream = System.out;
55 }
56
57
58
59 /**
60 * Test whether the formatter is in plain mode.
61 *
62 * @return true if {@link #type} is set to {@link PLAIN}
63 */
64 private boolean isPlain()
65 {
66 return ValidationFormatter.PLAIN.equals(getType());
67 }
68
69 /**
70 * Print an event in xml format
71 *
72 * @param event the event to print
73 * @param eventName started/ended/error etc
74 */
75 private void printEventAsXML(ValidationEvent event, String eventName)
76 {
77 getStream().println("\t<" + eventName + ">");
78 getStream().println("\t\t<source>");
79 getStream().println("\t\t\t" + event.getSource());
80 getStream().println("\t\t</source>");
81 getStream().println("\t\t<subject>");
82 getStream().println("\t\t\t" + event.getSubject());
83 getStream().println("\t\t</subject>");
84 getStream().println("\t\t<message>");
85 getStream().println("\t\t\t" + event.getMessage());
86 getStream().println("\t\t</message>");
87 getStream().println("\t</" + eventName + ">");
88 }
89
90 /**
91 * Print an event in plain format
92 *
93 * @param event the {@link ValidationEvent event} to print
94 * @param type started, ended etc
95 */
96 private void printEvent(String type, ValidationEvent event)
97 {
98 getStream().println(event.getSubject() + " " + type + ": "
99 + event.getMessage());
100 }
101
102 /**
103 * Called when validation ends
104 *
105 * @param event a {@link ValidationEvent}
106 */
107 public void validationEnded(ValidationEvent event)
108 {
109 if (isPlain())
110 {
111 printEvent("ended", event);
112 }
113 else
114 {
115 printEventAsXML(event, "ended");
116 getStream().println("</validation-report>");
117 }
118 }
119
120 /**
121 * Called when a validation error occurs. That is, the subject being
122 * validated has a serious (fatal) problem
123 *
124 * @param event a {@link ValidationEvent}
125 */
126 public void validationError(ValidationEvent event)
127 {
128 if (isPlain())
129 {
130 printEvent("error", event);
131 }
132 else
133 {
134 printEventAsXML(event, "error");
135 }
136 }
137
138 /**
139 * Called when validation starts
140 *
141 * @param event a {@link ValidationEvent}
142 */
143 public void validationStarted(ValidationEvent event)
144 {
145 if (isPlain())
146 {
147 printEvent("started", event);
148 }
149 else
150 {
151 getStream().println("<?xml version=\"1.0\" ?>");
152 getStream().println("<validation-report>");
153 printEventAsXML(event, "started");
154 }
155 }
156
157 /**
158 * Called when a validation warning occurs. That is, the subject being
159 * validated has a problem which may not be fatal
160 *
161 * @param event a {@link ValidationEvent}
162 */
163 public void validationWarning(ValidationEvent event)
164 {
165 if (isPlain())
166 {
167 printEvent("warning", event);
168 }
169 else
170 {
171 printEventAsXML(event, "warning");
172 }
173 }
174
175 /**
176 * Called when validation information events are fired.
177 *
178 * @param event a {@link ValidationEvent}
179 */
180 public void validationInformation(ValidationEvent event)
181 {
182 if (isPlain())
183 {
184 printEvent("info", event);
185 }
186 else
187 {
188 printEventAsXML(event, "info");
189 }
190 }
191
192 /**
193 * Getter for property file.
194 *
195 * @return Value of property file.
196 */
197 public String getFile()
198 {
199 return file;
200 }
201
202 /**
203 * Setter for property file.
204 *
205 * @param file New value of property file.
206 * @throws FileNotFoundException if the named file doesn't exist
207 */
208 public void setFile(String file) throws FileNotFoundException
209 {
210 this.file = file;
211 File theFile = new File(file);
212 File dir = theFile.getParentFile();
213 if (dir != null) {
214 dir.mkdirs();
215 }
216 FileOutputStream fos = new FileOutputStream(file);
217 stream = new PrintStream(fos);
218 }
219
220 /**
221 * Getter for property usefile.
222 *
223 * @return Value of property usefile.
224 */
225 public boolean isUsefile()
226 {
227 return usefile;
228 }
229
230 /**
231 * Setter for property usefile.
232 *
233 * @param usefile New value of property usefile.
234 */
235 public void setUsefile(boolean usefile)
236 {
237 this.usefile = usefile;
238 }
239
240 /**
241 * Getter for property stream.
242 *
243 * @return Value of property stream.
244 */
245 public PrintStream getStream()
246 {
247 return stream;
248 }
249
250 /**
251 * Setter for property stream.
252 *
253 * @param stream New value of property stream.
254 */
255 public void setStream(PrintStream stream)
256 {
257 this.stream = stream;
258 }
259
260 /**
261 * Getter for property type.
262 *
263 * @return Value of property type.
264 */
265 public String getType()
266 {
267 return type;
268 }
269
270 /**
271 * Setter for property type.
272 *
273 * @param type New value of property type.
274 */
275 public void setType(String type)
276 {
277 this.type = type;
278 }
279 }