KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > compiler > model > validation > ValidatableFieldOld


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.compiler.model.validation;
19
20 import org.apache.beehive.netui.compiler.model.schema.validator11.Arg0Document;
21 import org.apache.beehive.netui.compiler.model.schema.validator11.FieldDocument;
22 import org.apache.beehive.netui.compiler.model.schema.validator11.MsgDocument;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25
26 public class ValidatableFieldOld extends ValidatableField
27 {
28     public ValidatableFieldOld( String JavaDoc propertyName, String JavaDoc displayName, String JavaDoc displayNameKey )
29     {
30         super( propertyName, displayName, displayNameKey );
31     }
32
33     //
34
// Support for validator version 1.0 arg0,...,arg3 elements
35
//
36

37     void setDefaultArg0Element( String JavaDoc displayName, boolean displayNameIsResource, FieldDocument.Field fieldElement )
38     {
39         Arg0Document.Arg0[] arg0Array = fieldElement.getArg0Array();
40         Arg0Document.Arg0 defaultArg0Element = null;
41
42         for ( int i = 0; i < arg0Array.length; i++ )
43         {
44             Arg0Document.Arg0 arg0 = arg0Array[i];
45             if ( arg0.getName() == null )
46             {
47                 defaultArg0Element = arg0;
48                 break;
49             }
50         }
51
52         if ( defaultArg0Element == null && _rules.size() > 0 )
53         {
54             defaultArg0Element = fieldElement.addNewArg0();
55         }
56
57         if ( defaultArg0Element != null )
58         {
59             defaultArg0Element.setKey( displayName );
60             defaultArg0Element.setResource( Boolean.toString( displayNameIsResource ) );
61         }
62     }
63
64     void setRuleMessage( ValidatorRule rule, FieldDocument.Field fieldElement )
65     {
66         String JavaDoc messageKey = rule.getMessageKey();
67         String JavaDoc message = rule.getMessage();
68
69         if ( messageKey != null || message != null )
70         {
71             MsgDocument.Msg[] existingMsgElements = fieldElement.getMsgArray();
72             MsgDocument.Msg msgElementToUse = null;
73
74             for ( int j = 0; j < existingMsgElements.length; j++ )
75             {
76                 MsgDocument.Msg existingMsgElement = existingMsgElements[j];
77                 if ( rule.getRuleName().equals( existingMsgElement.getName() ) )
78                 {
79                     msgElementToUse = existingMsgElement;
80                     break;
81                 }
82             }
83
84             if ( msgElementToUse == null )
85             {
86                 msgElementToUse = fieldElement.addNewMsg();
87                 msgElementToUse.setName( rule.getRuleName() );
88             }
89
90             if ( messageKey != null )
91             {
92                 msgElementToUse.setKey( messageKey );
93                 msgElementToUse.setResource( Boolean.TRUE.toString() );
94             }
95             else // message != null (it's a hardcoded message)
96
{
97                 //
98
// Add our special constant as the message key, append the hardcoded message to it.
99
//
100
msgElementToUse.setKey( ValidatorConstants.EXPRESSION_KEY_PREFIX + message );
101                 msgElementToUse.setResource( Boolean.TRUE.toString() );
102             }
103         }
104     }
105
106     //
107
// Support for validator version 1.0 and the deprecated ,..., elements,
108
// rather than the general <arg position="N"> element of validator version 1.1.
109
//
110

111     void setRuleArg( ValidatorRule rule, int argNum, FieldDocument.Field fieldElement, String JavaDoc altMessageVar )
112     {
113         try
114         {
115             Class JavaDoc fieldElementClass = fieldElement.getClass();
116             ValidatorRule.MessageArg arg = rule.getArg( new Integer JavaDoc( argNum ) );
117
118             String JavaDoc ruleName = rule.getRuleName();
119             Object JavaDoc argElementToUse = null;
120             Object JavaDoc[] existingArgElements =
121                     ( Object JavaDoc[] ) fieldElementClass.getMethod( "getArg" + argNum + "Array", null ).invoke( fieldElement, null );
122
123             for ( int i = 0; i < existingArgElements.length; i++ )
124             {
125                 Object JavaDoc existingElement = existingArgElements[i];
126                 if ( ruleName.equals( existingElement.getClass().getMethod( "getName", null ).invoke( existingElement, null ) ) )
127                 {
128                     argElementToUse = existingElement;
129                     break;
130                 }
131             }
132
133             if ( arg != null || altMessageVar != null )
134             {
135                 if ( argElementToUse == null )
136                 {
137                     argElementToUse = fieldElementClass.getMethod( "addNewArg" + argNum, null ).invoke( fieldElement, null );
138                 }
139
140                 Class JavaDoc argElementToUseClass = argElementToUse.getClass();
141
142                 if ( arg != null )
143                 {
144                     String JavaDoc argMessage = arg.getMessage();
145                     String JavaDoc key = arg.isKey() ? argMessage : ValidatorConstants.EXPRESSION_KEY_PREFIX + argMessage;
146                     argElementToUseClass.getMethod( "setKey", new Class JavaDoc[]{ String JavaDoc.class } ).invoke( argElementToUse, new Object JavaDoc[]{ key } );
147                     String JavaDoc isResource = Boolean.TRUE.toString();
148                     argElementToUseClass.getMethod( "setResource", new Class JavaDoc[]{ String JavaDoc.class } ).invoke( argElementToUse, new Object JavaDoc[]{ isResource } );
149                     argElementToUseClass.getMethod( "setName", new Class JavaDoc[]{ String JavaDoc.class } ).invoke( argElementToUse, new Object JavaDoc[]{ ruleName } );
150                 }
151                 else
152                 {
153                     altMessageVar = "${var:" + altMessageVar + '}';
154                     argElementToUseClass.getMethod( "setKey", new Class JavaDoc[]{ String JavaDoc.class } ).invoke( argElementToUse, new Object JavaDoc[]{ altMessageVar } );
155                     argElementToUseClass.getMethod( "setResource", new Class JavaDoc[]{ String JavaDoc.class } ).invoke( argElementToUse, new Object JavaDoc[]{ "false" } );
156                     argElementToUseClass.getMethod( "setName", new Class JavaDoc[]{ String JavaDoc.class } ).invoke( argElementToUse, new Object JavaDoc[]{ ruleName } );
157                 }
158             }
159         }
160         catch ( NoSuchMethodException JavaDoc e )
161         {
162             assert false : e; // this shouldn't ever happen -- we know what the compiled types look like.
163
}
164         catch ( IllegalAccessException JavaDoc e )
165         {
166             assert false : e; // this shouldn't ever happen -- we know what the compiled types look like.
167
}
168         catch ( InvocationTargetException JavaDoc e )
169         {
170             assert false : e; // this shouldn't ever happen -- we know what the compiled types look like.
171
}
172     }
173 }
174
Popular Tags