1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.datadictionary.validator;
17
18 import java.io.BufferedWriter;
19 import java.io.FileWriter;
20 import java.io.IOException;
21 import java.io.PrintStream;
22
23 import org.apache.commons.logging.Log;
24 import org.kuali.rice.krad.datadictionary.DefaultListableBeanFactory;
25 import org.kuali.rice.krad.uif.component.Component;
26 import org.springframework.core.io.ResourceLoader;
27
28
29
30
31
32
33
34 public class ValidationController {
35 protected static final String endl = System.getProperty("line.separator");
36
37 protected boolean displayWarnings;
38 protected boolean displayErrors;
39 protected boolean displayXmlPages;
40 protected boolean displayErrorMessages;
41 protected boolean displayWarningMessages;
42
43
44
45
46
47
48
49
50
51
52 public ValidationController(boolean displayErrors, boolean displayWarnings, boolean displayErrorMessages,
53 boolean displayWarningMessages, boolean displayXmlPages) {
54
55 this.displayErrors = displayErrors;
56 this.displayWarnings = displayWarnings;
57 this.displayErrorMessages = displayErrorMessages;
58 this.displayWarningMessages = displayWarningMessages;
59 this.displayXmlPages = displayXmlPages;
60 }
61
62
63
64
65 public ValidationController() {
66
67 displayErrors = true;
68 displayWarnings = true;
69 displayErrorMessages = true;
70 displayWarningMessages = true;
71 displayXmlPages = true;
72 }
73
74
75
76
77
78
79
80
81
82
83 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
84 boolean failOnWarning) {
85
86 Validator validator = new Validator();
87
88 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
89
90 return passed;
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
104 String outputFile, boolean failOnWarning) {
105 Validator validator = new Validator();
106
107
108 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
109
110 writeToFile(outputFile, validator, passed);
111
112 return passed;
113 }
114
115
116
117
118
119
120
121
122
123
124
125 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans,
126 PrintStream stream, boolean failOnWarning) {
127 Validator validator = new Validator();
128
129
130 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
131
132 writeToStream(stream, validator, passed);
133
134 return passed;
135 }
136
137
138
139
140
141
142
143
144
145
146
147 public boolean validate(String[] xmlFiles, ResourceLoader loader, DefaultListableBeanFactory beans, Log log,
148 boolean failOnWarning) {
149 Validator validator = new Validator();
150
151
152 boolean passed = validator.validate(xmlFiles, loader, beans, failOnWarning);
153
154 writeToLog(log, validator, passed);
155
156 return passed;
157 }
158
159
160
161
162
163
164
165
166 public boolean validate(String[] xmlFiles, boolean failOnWarning) {
167
168 Validator validator = new Validator();
169
170 boolean passed = validator.validate(xmlFiles, failOnWarning);
171
172 return passed;
173 }
174
175
176
177
178
179
180
181
182
183 public boolean validate(String[] xmlFiles, String outputFile, boolean failOnWarning) {
184 Validator validator = new Validator();
185
186
187 boolean passed = validator.validate(xmlFiles, failOnWarning);
188
189 writeToFile(outputFile, validator, passed);
190
191 return passed;
192 }
193
194
195
196
197
198
199
200
201
202 public boolean validate(String[] xmlFiles, PrintStream stream, boolean failOnWarning) {
203 Validator validator = new Validator();
204
205
206 boolean passed = validator.validate(xmlFiles, failOnWarning);
207
208 writeToStream(stream, validator, passed);
209
210 return passed;
211 }
212
213
214
215
216
217
218
219
220
221 public boolean validate(String[] xmlFiles, Log log, boolean failOnWarning) {
222 Validator validator = new Validator();
223
224
225 boolean passed = validator.validate(xmlFiles, failOnWarning);
226
227 writeToLog(log, validator, passed);
228
229 return passed;
230 }
231
232
233
234
235
236
237
238
239
240 public boolean validate(Component object, Log log, boolean failOnWarning) {
241 Validator validator = new Validator();
242
243
244 boolean passed = validator.validate(object, failOnWarning);
245
246 writeToLog(log, validator, passed);
247
248 return passed;
249 }
250
251
252
253
254
255
256
257
258 protected void writeToFile(String path, Validator validator, boolean passed) {
259 try {
260 BufferedWriter fout = new BufferedWriter(new FileWriter(path));
261
262 fout.write("Validation Results" + endl);
263 fout.write("Passed: " + passed + endl);
264 if (displayErrors) {
265 fout.write("Number of Errors: " + validator.getNumberOfErrors() + endl);
266 }
267 if (displayWarnings) {
268 fout.write("Number of Warnings: " + validator.getNumberOfWarnings() + endl);
269 }
270
271 if (displayErrorMessages) {
272 for (int i = 0; i < validator.getErrorReportSize(); i++) {
273 if (displayWarningMessages) {
274 fout.write(endl);
275 fout.write(validator.getErrorReport(i).errorMessage());
276 } else if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
277 fout.write(endl);
278 fout.write(validator.getErrorReport(i).errorMessage());
279 }
280
281 if (displayXmlPages) {
282 fout.write(validator.getErrorReport(i).errorPageList());
283 }
284 }
285 }
286
287 fout.close();
288 } catch (IOException e) {
289
290 }
291 }
292
293
294
295
296
297
298
299
300 protected void writeToStream(PrintStream stream, Validator validator, boolean passed) {
301 stream.println("Validation Results");
302 stream.println("Passed: " + passed);
303 if (displayErrors) {
304 stream.println("Number of Errors: " + validator.getNumberOfErrors());
305 }
306 if (displayWarnings) {
307 stream.println("Number of Warnings: " + validator.getNumberOfWarnings());
308 }
309
310 if (displayErrorMessages) {
311 for (int i = 0; i < validator.getErrorReportSize(); i++) {
312 stream.println();
313 if (displayWarningMessages) {
314 stream.println(validator.getErrorReport(i).errorMessage());
315 } else if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
316 stream.println(validator.getErrorReport(i).errorMessage());
317 }
318
319 if (displayXmlPages) {
320 stream.println(validator.getErrorReport(i).errorPageList());
321 }
322 }
323 }
324 }
325
326
327
328
329
330
331
332
333 protected void writeToLog(Log log, Validator validator, boolean passed) {
334 log.info("Passed: " + passed);
335 if (displayErrors) {
336 log.info("Number of Errors: " + validator.getNumberOfErrors());
337 }
338 if (displayWarnings) {
339 log.info("Number of Warnings: " + validator.getNumberOfWarnings());
340 }
341
342 if (displayErrorMessages) {
343 for (int i = 0; i < validator.getErrorReportSize(); i++) {
344 if (validator.getErrorReport(i).getErrorStatus() == ErrorReport.ERROR) {
345 if (displayXmlPages) {
346 log.error(validator.getErrorReport(i).errorMessage() + validator.getErrorReport(i)
347 .errorPageList());
348 } else {
349 log.error(validator.getErrorReport(i).errorMessage());
350 }
351
352 } else {
353 if (displayWarningMessages) {
354 if (displayXmlPages) {
355 log.warn(validator.getErrorReport(i).errorMessage() + validator.getErrorReport(i)
356 .errorPageList());
357 } else {
358 log.warn(validator.getErrorReport(i).errorMessage());
359 }
360 }
361 }
362
363 }
364 }
365 }
366
367
368
369
370
371
372 public void setDisplayWarnings(boolean display) {
373 displayWarnings = display;
374 }
375
376
377
378
379
380
381 public void setDisplayErrors(boolean display) {
382 displayErrors = display;
383 }
384
385
386
387
388
389
390 public void setDisplayXmlPages(boolean display) {
391 displayXmlPages = display;
392 }
393
394
395
396
397
398
399 public void setDisplayErrorMessages(boolean display) {
400 displayErrorMessages = display;
401 }
402
403
404
405
406
407
408 public void setDisplayWarningMessages(boolean display) {
409 displayWarningMessages = display;
410 }
411
412
413
414
415
416
417 public boolean isDisplayWarnings() {
418 return displayWarnings;
419 }
420
421
422
423
424
425
426 public boolean isDisplayErrors() {
427 return displayErrors;
428 }
429
430
431
432
433
434
435 public boolean isDisplayXmlPages() {
436 return displayXmlPages;
437 }
438
439
440
441
442
443
444 public boolean isDisplayErrorMessages() {
445 return displayErrorMessages;
446 }
447
448
449
450
451
452
453 public boolean isDisplayWarningMessages() {
454 return displayWarningMessages;
455 }
456 }