KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > formview > test > FormViewTestWithSeveralValidator


1 package net.sourceforge.formview.test;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5
6 import org.xml.sax.SAXException JavaDoc;
7
8 import net.sourceforge.formview.FormView;
9 import net.sourceforge.formview.FormViewException;
10 import net.sourceforge.formview.FormViewResources;
11 import net.sourceforge.formview.displayer.DisplayersConfigResources;
12 import net.sourceforge.formview.permission.IPermissionsAdapter;
13 import net.sourceforge.formview.util.ApplicationFormViewUtil;
14 import net.sourceforge.formview.validator.ExtendedValidatorResources;
15
16 public class FormViewTestWithSeveralValidator {
17
18     /**
19      * @param args
20      */

21     public static void main(String JavaDoc[] args)
22         throws FormViewException, IOException JavaDoc, SAXException JavaDoc {
23         InputStream JavaDoc inFomView = null;
24
25         
26         // Load Form view
27
FormViewResources resources = null;
28         try {
29             
30             // Create a new instance of a ValidatorResource, then get a stream
31
// handle on the XML file with the actions in it, and initialize the
32
// resources from it. This would normally be done by a servlet
33
// run during JSP initialization or some other application-startup
34
// routine.
35

36             inFomView = FormViewTest.class.getResourceAsStream("form-view-test.xml");
37             resources = new FormViewResources(inFomView);
38             
39         } finally {
40 // Make sure we close the input stream.
41
if (inFomView != null) {
42                 inFomView.close();
43             }
44
45         }
46
47         InputStream JavaDoc inDisplayerConfig = null;
48         // Merge with DisplayersConfigResources
49
DisplayersConfigResources displayersResources = null;
50         try {
51             
52             inDisplayerConfig = DisplayersConfigResources.class.getResourceAsStream("displayers-config.xml");
53             displayersResources = new DisplayersConfigResources(inDisplayerConfig);
54             
55         } finally {
56 // Make sure we close the input stream.
57
if (inDisplayerConfig != null) {
58                 inDisplayerConfig.close();
59             }
60         }
61         resources.mergeDisplayersWithDisplayerConfig(displayersResources);
62         
63         // Merge with SEVERAL validator
64
InputStream JavaDoc[] inSeveralValidator = new InputStream JavaDoc[2];
65         ExtendedValidatorResources validatorResources = null;
66         
67         try {
68         
69             // Create a new instance of a ValidatorResource, then get a stream
70
// handle on the XML file with the actions in it, and initialize the
71
// resources from it. This would normally be done by a servlet
72
// run during JSP initialization or some other application-startup
73
// routine.
74
inSeveralValidator[0] = FormViewTest.class.getResourceAsStream("validation1.xml");
75             inSeveralValidator[1] = FormViewTest.class.getResourceAsStream("validation2.xml");
76             validatorResources = new ExtendedValidatorResources(inSeveralValidator);
77             
78         } finally {
79             // Make sure we close the input stream.
80
if (inSeveralValidator != null) {
81                 for (int streamIndex = 0;streamIndex < inSeveralValidator.length;streamIndex++) {
82                     InputStream JavaDoc inValidator = (InputStream JavaDoc) inSeveralValidator[streamIndex];
83                     inValidator.close();
84                 }
85             }
86         }
87         
88         resources.mergeFormSetWithValidator(validatorResources);
89         
90         ///////// PUT Name of context of WEB Application
91
resources.addContextValue("contextPath", "myapplication");
92         
93         // Test with validation1.xml => nom is required
94
String JavaDoc formName = "/user/identite_collaborateur_ONE";
95         String JavaDoc state = "READ";
96          
97         String JavaDoc[] roles = null;
98         ApplicationFormViewUtil.init(resources);
99         
100         FormView form = ApplicationFormViewUtil.getFormView(formName, state);
101         //defaultBehaviour = FormViewUtil.getDefaultBehaviour(defaultState);
102

103         // WEB => FormViewUtil.saveFormView(formName, state, request);
104

105         IPermissionsAdapter permissionsAdapter = new TestRolesPermissionAdapter();
106         
107         
108         
109         // Test role with buttonSave
110
String JavaDoc htmlContent = getHtmlContent();
111         String JavaDoc htmlContentProcess = ApplicationFormViewUtil.processHtmlContent(form, state, permissionsAdapter, htmlContent);
112         System.out.println(htmlContentProcess);
113         
114         // Test with validation2.xml => nom is NOT required
115
formName = "/user/identite_collaborateur_TWO";
116         form = ApplicationFormViewUtil.getFormView(formName, state);
117         htmlContent = getHtmlContent();
118         htmlContentProcess = ApplicationFormViewUtil.processHtmlContent(form, state, permissionsAdapter, htmlContent);
119         System.out.println(htmlContentProcess);
120         
121         
122
123     }
124     
125     private static String JavaDoc getHtmlContent2() {
126         StringBuffer JavaDoc htmlContent = new StringBuffer JavaDoc("");
127         htmlContent.append("<html>\n");
128         htmlContent.append("<input name=\"buttonSave\" type=\"button\" >\n");
129         htmlContent.append("</html>\n");
130         return htmlContent.toString();
131     }
132     
133     private static String JavaDoc getHtmlContent() {
134         StringBuffer JavaDoc htmlContent = new StringBuffer JavaDoc("");
135         htmlContent.append("<html>\n");
136             htmlContent.append("<input name=\"buttonSave\" type=\"button\" >\n");
137             htmlContent.append("<input name=\"nom\" type=\"text\" >\n");
138             htmlContent.append("<input name=\"prenom\" type=\"text\" maxlength=\"200\" >\n");
139             htmlContent.append("<input name=\"dateNaissance\" type=\"text\" maxlength=\"8\" >\n");
140             htmlContent.append("<input name=\"lieuNaissance\" type=\"text\" >\n");
141             htmlContent.append("<input name=\"INPUT_NOT_IN_FORM\" type=\"text\" >\n");
142             htmlContent.append("<select name=\"selectList\" >\n");
143             htmlContent.append("<option value=\"ITEM0\" >Item0</option>");
144             htmlContent.append("<option value=\"ITEM1\" selected=\"selected\">Item1</option>");
145             htmlContent.append("</select>\n");
146             
147         htmlContent.append("</html>\n");
148         return htmlContent.toString();
149     }
150
151 }
152
Popular Tags