Source code: com/sample/griffin/validation/ValidationTest.java
1 /*
2 * ValidationTest.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 */
7
8 package com.sample.griffin.validation;
9
10 import java.util.Iterator;
11 import java.util.Collection;
12
13 import com.aendvari.common.model.xalan.*;
14 import com.aendvari.common.model.*;
15
16 import com.aendvari.griffin.validation.*;
17 import com.aendvari.griffin.validation.validators.simple.*;
18
19 import com.aendvari.common.util.*;
20 import com.aendvari.common.notices.*;
21
22 import com.sample.griffin.validation.*;
23
24
25 /**
26 * This class provides a quick example of how to use Griffin's validation package.
27 *
28 * @author Scott Milne
29 *
30 */
31
32 public class ValidationTest
33 {
34 public static void main(String args[])
35 {
36 // create a model tree for holding our fake data
37 ModelTree modelTree = new com.aendvari.common.model.xalan.XalanModelTreeFactory().createModelTree();
38
39 // load in "fake" data
40 try
41 {
42 modelTree.loadFromFile("model.xml");
43 }
44 catch (ModelParserException e)
45 {
46 e.printStackTrace(System.out);
47 System.exit(1);
48 }
49
50 // get nodes within <descriptor>
51 ModelNode rootNode = modelTree.getRootNode();
52 ModelNode formNode = modelTree.getNode(rootNode, "model/form");
53
54 // typically, the only reason and Exception will be thrown is if
55 // a property validation method or the configuration reading fails
56 try
57 {
58 // create a new validation, loading in the validation descriptor we want to use
59 Validation validation = new Validation(
60 new com.aendvari.common.model.xalan.XalanModelTreeFactory(),
61 SampleDatasetDescriptor.class );
62
63 // create our error handler
64 SampleErrorHandler errorHandler = new SampleErrorHandler();
65
66 // build the list of pre-defined handlers
67 ValidationHandler[] handlers = {
68 new ValidationHandler("sampleErrorHandler", errorHandler )
69 };
70
71 // execute the validation
72 validation.validate( formNode, handlers );
73
74
75 //
76 // extract and display any errors that occured
77 //
78
79 // create a notice translator. This is used to extract and parse notice text from a .properties file
80 NoticeTranslator translator = new ResourceNoticeTranslator("com.sample.griffin.ApplicationResource");
81
82 // get the messages from the error handler
83 Collection errorMessages = errorHandler.getErrorMessages().getNotices("error", true);
84
85
86 //
87 // display the "error" messages to the screen
88 //
89
90 Collection errorStrings = translator.translate(errorMessages);
91
92 Iterator messagesIterator = errorStrings.iterator();
93
94 while ( messagesIterator.hasNext() )
95 {
96 // get the parsed message string
97 String parsedMsg = (String)messagesIterator.next();
98
99 // display the message
100 System.out.println("parsedMsg="+parsedMsg);
101 System.out.println("");
102 }
103
104
105 //
106 // extract the notices into a model instead
107 //
108
109 ModelNode errorsNode = modelTree.createNode("errors");
110 com.aendvari.common.notices.NoticesUtil.noticesToModel(errorHandler.getErrorMessages(), translator, errorsNode, "error", true);
111
112 System.out.println(com.aendvari.griffin.util.XmlUtil.ViewNode(
113 ((com.aendvari.common.model.xalan.XalanModelNode)errorsNode).getXmlNode())
114 );
115
116 }
117 // typically, the only reason and Exception will be thrown is if
118 // a property validation method or the configuration reading fails
119 catch( Exception exception )
120 {
121 exception.printStackTrace();
122 }
123 }
124 }
125