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