KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArgDocument;
21 import org.apache.beehive.netui.compiler.model.schema.validator11.FieldDocument;
22 import org.apache.beehive.netui.compiler.model.schema.validator11.MsgDocument;
23 import org.apache.beehive.netui.compiler.model.schema.validator11.VarDocument;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Collections JavaDoc;
30
31 public class ValidatableField
32 {
33     private String JavaDoc _propertyName;
34     private String JavaDoc _displayName;
35     private String JavaDoc _displayNameKey;
36     List JavaDoc _rules = new ArrayList JavaDoc();
37
38
39     public ValidatableField( String JavaDoc propertyName, String JavaDoc displayName, String JavaDoc displayNameKey )
40     {
41         _propertyName = propertyName;
42         _displayName = displayName;
43         _displayNameKey = displayNameKey;
44     }
45     
46     public String JavaDoc getPropertyName()
47     {
48         return _propertyName;
49     }
50
51     protected boolean hasRule( ValidatorRule rule )
52     {
53         assert rule != null;
54
55         String JavaDoc name = rule.getRuleName();
56         for ( Iterator JavaDoc ii = _rules.iterator(); ii.hasNext(); )
57         {
58             ValidatorRule existingRule = ( ValidatorRule ) ii.next();
59             if ( existingRule.getRuleName().equals( name ) )
60             {
61                 return true;
62             }
63         }
64         return false;
65     }
66
67     public void addRule( ValidatorRule rule )
68     {
69         assert rule != null;
70
71         _rules.add( rule );
72     }
73     
74     public ValidatorRule[] getRules()
75     {
76         return ( ValidatorRule[] ) _rules.toArray( new ValidatorRule[ _rules.size() ] );
77     }
78     
79     /**
80      * Merge the rule names with the list in the field element's depends attribute.
81      *
82      * @param fieldElement the XMLBean field element in the validation XML to update
83      */

84     void mergeDependsList( FieldDocument.Field fieldElement )
85     {
86         String JavaDoc depends = fieldElement.getDepends();
87         StringBuffer JavaDoc updatedDepends = new StringBuffer JavaDoc();
88
89         if ( depends != null )
90         {
91             updatedDepends.append( depends );
92         }
93         else
94         {
95             depends = "";
96         }
97
98         ArrayList JavaDoc rules = new ArrayList JavaDoc();
99         for ( Iterator JavaDoc i = _rules.iterator(); i.hasNext(); )
100         {
101             ValidatorRule rule = ( ValidatorRule ) i.next();
102             String JavaDoc name = rule.getRuleName();
103             
104             if ( depends.indexOf( name ) == -1 ) rules.add( name );
105         }
106         Collections.sort( rules );
107         
108         for ( Iterator JavaDoc i = rules.iterator(); i.hasNext(); )
109         {
110             if ( updatedDepends.length() > 0 ) updatedDepends.append( ',' );
111             updatedDepends.append( ( String JavaDoc ) i.next() );
112         }
113
114         if ( updatedDepends.length() != 0 )
115         {
116             fieldElement.setDepends( updatedDepends.toString() );
117         }
118     }
119     
120     public void writeToXMLBean( FieldDocument.Field fieldElement )
121     {
122         assert fieldElement.getProperty().equals( _propertyName );
123         
124         mergeDependsList( fieldElement );
125
126         //
127
// Add the display name as the default first argument (can be overridden by individual rules).
128
//
129
String JavaDoc displayName;
130         boolean displayNameIsResource = false;
131         
132         if ( _displayName != null )
133         {
134             displayName = ValidatorConstants.EXPRESSION_KEY_PREFIX + _displayName;
135             displayNameIsResource = true;
136         }
137         else if ( _displayNameKey != null )
138         {
139             displayName = _displayNameKey;
140             displayNameIsResource = true;
141         }
142         else
143         {
144             displayName = Character.toUpperCase( _propertyName.charAt( 0 ) ) + _propertyName.substring( 1 );
145         }
146
147         setDefaultArg0Element( displayName, displayNameIsResource, fieldElement );
148
149         //
150
// Go through the rules, and add each one. Each rule can spray into...
151
// 1) an entry in the comma-separated rules dependencies list (handled above with mergeDependsList(),
152
// 2) a set of elements,
153
// 3) a set of elements.
154
//
155
for ( Iterator JavaDoc ii = _rules.iterator(); ii.hasNext(); )
156         {
157             ValidatorRule rule = ( ValidatorRule ) ii.next();
158             //
159
// Add the message from the rule.
160
//
161
setRuleMessage( rule, fieldElement );
162
163             //
164
// Add vars from the rule.
165
//
166
VarDocument.Var[] existingVars = fieldElement.getVarArray();
167             Map JavaDoc ruleVars = rule.getVars();
168             
169             if ( ruleVars != null )
170             {
171                 for ( Iterator JavaDoc j = ruleVars.entrySet().iterator(); j.hasNext(); )
172                 {
173                     Map.Entry JavaDoc entry = ( Map.Entry JavaDoc ) j.next();
174                     String JavaDoc varName = ( String JavaDoc ) entry.getKey();
175                     
176                     //
177
// Look for an existing var entry to update, or create one if there's none with the right name.
178
//
179
VarDocument.Var varElementToUse = null;
180                     for ( int k = 0; k < existingVars.length; k++ )
181                     {
182                         VarDocument.Var existingVar = existingVars[k];
183                         
184                         if ( varName.equals( existingVar.getVarName() ) )
185                         {
186                             varElementToUse = existingVar;
187                             break;
188                         }
189                     }
190                     
191                     if ( varElementToUse == null )
192                     {
193                         varElementToUse = fieldElement.addNewVar();
194                         varElementToUse.setVarName( varName );
195                     }
196                     
197                     if ( varElementToUse.getVarValue() == null )
198                     {
199                         varElementToUse.setVarValue( ( String JavaDoc ) entry.getValue() );
200                     }
201                 }
202             }
203             
204             //
205
// Add message arguments from the rule. If the user didn't specify an args, fill it in with a variable
206
// value from the rule.
207
//
208
Iterator JavaDoc j = ruleVars != null ? ruleVars.keySet().iterator() : null;
209             setRuleArg( rule, 0, fieldElement, null );
210             setRuleArg( rule, 1, fieldElement, j != null && j.hasNext() ? ( String JavaDoc ) j.next() : null );
211             setRuleArg( rule, 2, fieldElement, j != null && j.hasNext() ? ( String JavaDoc ) j.next() : null );
212             setRuleArg( rule, 3, fieldElement, j != null && j.hasNext() ? ( String JavaDoc ) j.next() : null );
213         }
214     }
215
216     /**
217      * Find or create a default arg 0 element not associated with a
218      * specific rule and set it to the display name.
219      */

220     void setDefaultArg0Element( String JavaDoc displayName, boolean displayNameIsResource, FieldDocument.Field fieldElement )
221     {
222         ArgDocument.Arg[] argArray = fieldElement.getArgArray();
223         ArgDocument.Arg defaultArg0Element = null;
224
225         for ( int i = 0; i < argArray.length; i++ )
226         {
227             ArgDocument.Arg arg = argArray[i];
228             if ( arg.getName() == null && "0".equals( arg.getPosition() ) )
229             {
230                 defaultArg0Element = arg;
231                 break;
232             }
233         }
234
235         if ( defaultArg0Element == null && _rules.size() > 0 )
236         {
237             defaultArg0Element = fieldElement.addNewArg();
238             defaultArg0Element.setPosition( "0" );
239         }
240
241         if ( defaultArg0Element != null )
242         {
243             defaultArg0Element.setKey( displayName );
244             defaultArg0Element.setResource( Boolean.toString( displayNameIsResource ) );
245             if ( defaultArg0Element.getBundle() != null )
246             {
247                 defaultArg0Element.setBundle( null );
248             }
249         }
250     }
251
252     /**
253      * Set up the desired &lt;msg&gt; element and attributes for the given rule.
254      *
255      * @param rule the rule with the message to use
256      * @param fieldElement an XMLBean field element in the validation XML to update
257      */

258     void setRuleMessage( ValidatorRule rule, FieldDocument.Field fieldElement )
259     {
260         String JavaDoc messageKey = rule.getMessageKey();
261         String JavaDoc message = rule.getMessage();
262
263         if ( messageKey != null || message != null )
264         {
265             MsgDocument.Msg[] existingMsgElements = fieldElement.getMsgArray();
266             MsgDocument.Msg msgElementToUse = null;
267
268             for ( int j = 0; j < existingMsgElements.length; j++ )
269             {
270                 MsgDocument.Msg existingMsgElement = existingMsgElements[j];
271                 if ( rule.getRuleName().equals( existingMsgElement.getName() ) )
272                 {
273                     msgElementToUse = existingMsgElement;
274                     break;
275                 }
276             }
277
278             if ( msgElementToUse == null )
279             {
280                 msgElementToUse = fieldElement.addNewMsg();
281                 msgElementToUse.setName( rule.getRuleName() );
282             }
283
284             if ( messageKey != null )
285             {
286                 msgElementToUse.setKey( messageKey );
287                 msgElementToUse.setResource( Boolean.TRUE.toString() );
288                 String JavaDoc bundle = rule.getBundle();
289                 if ( bundle != null && bundle.length() > 0 )
290                 {
291                     msgElementToUse.setBundle( bundle );
292                 }
293             }
294             else // message != null (it's a hardcoded message)
295
{
296                 //
297
// Add our special constant as the message key, append the hardcoded message to it.
298
//
299
msgElementToUse.setKey( ValidatorConstants.EXPRESSION_KEY_PREFIX + message );
300                 msgElementToUse.setResource( Boolean.TRUE.toString() );
301             }
302         }
303     }
304
305     /**
306      * Set up the desired &lt;arg&gt; element and attributes for the given rule.
307      *
308      * @param rule the rule with the message and arg information to use
309      * @param argNum the position of the arg in the message
310      * @param fieldElement an XMLBean field element in the validation XML to update
311      * @param altMessageVar alternative message var
312      */

313     void setRuleArg( ValidatorRule rule, int argNum, FieldDocument.Field fieldElement, String JavaDoc altMessageVar )
314     {
315             Integer JavaDoc argPosition = new Integer JavaDoc( argNum );
316             String JavaDoc position = argPosition.toString();
317             ValidatorRule.MessageArg arg = rule.getArg( argPosition );
318
319             String JavaDoc ruleName = rule.getRuleName();
320             ArgDocument.Arg[] argArray = fieldElement.getArgArray();
321             ArgDocument.Arg argElementToUse = null;
322
323             for ( int i = 0; i < argArray.length; i++ )
324             {
325                 if ( ruleName.equals( argArray[i].getName() ) && position.equals( argArray[i].getPosition() ) )
326                 {
327                     argElementToUse = argArray[i];
328                     break;
329                 }
330             }
331
332             if ( arg != null || altMessageVar != null )
333             {
334                 if ( argElementToUse == null )
335                 {
336                     argElementToUse = fieldElement.addNewArg();
337                 }
338                 
339                 if ( arg != null )
340                 {
341                     String JavaDoc argMessage = arg.getMessage();
342                     String JavaDoc key = arg.isKey() ? argMessage : ValidatorConstants.EXPRESSION_KEY_PREFIX + argMessage;
343                     argElementToUse.setKey( key );
344                     argElementToUse.setResource( Boolean.TRUE.toString() );
345                     String JavaDoc bundle = rule.getBundle();
346                     if ( arg.isKey() && bundle != null && bundle.length() > 0 )
347                     {
348                         argElementToUse.setBundle( bundle );
349                     }
350                 }
351                 else
352                 {
353                     altMessageVar = "${var:" + altMessageVar + '}';
354                     argElementToUse.setKey( altMessageVar );
355                     argElementToUse.setResource( "false" );
356                 }
357
358                 argElementToUse.setPosition( position );
359                 argElementToUse.setName( ruleName );
360             }
361     }
362
363     public String JavaDoc getDisplayName()
364     {
365         return _displayName;
366     }
367
368     public void setDisplayName( String JavaDoc displayName )
369     {
370         _displayName = displayName;
371     }
372
373     public String JavaDoc getDisplayNameKey()
374     {
375         return _displayNameKey;
376     }
377
378     public void setDisplayNameKey( String JavaDoc displayNameKey )
379     {
380         _displayNameKey = displayNameKey;
381     }
382 }
383
Popular Tags