KickJava   Java API By Example, From Geeks To Geeks.

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


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.FormDocument;
21 import org.apache.beehive.netui.compiler.model.schema.validator11.FieldDocument;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 class ValidatableEntity
28 {
29     private String JavaDoc _entityName;
30     private Map JavaDoc _fields = new HashMap JavaDoc();
31     
32     
33     public ValidatableEntity( String JavaDoc entityName )
34     {
35         _entityName = entityName;
36     }
37
38     protected String JavaDoc getEntityName()
39     {
40         return _entityName;
41     }
42     
43     public void addField( ValidatableField field )
44     {
45         _fields.put( field.getPropertyName(), field );
46     }
47     
48     public ValidatableField getField( String JavaDoc fieldName )
49     {
50         return ( ValidatableField ) _fields.get( fieldName );
51     }
52     
53     public void writeToXMLBean( FormDocument.Form formElement )
54     {
55         assert formElement.getName().equals( _entityName );
56         
57         FieldDocument.Field[] existingFieldElements = formElement.getFieldArray();
58         for ( Iterator JavaDoc i = _fields.values().iterator(); i.hasNext(); )
59         {
60             ValidatableField field = ( ValidatableField ) i.next();
61             FieldDocument.Field fieldElementToUse = null;
62             String JavaDoc fieldPropertyName = field.getPropertyName();
63             
64             //
65
// Look for an existing field element to update, or create one if none matches this field's property name.
66
//
67
for ( int j = 0; j < existingFieldElements.length; j++ )
68             {
69                 FieldDocument.Field existingFieldElement = existingFieldElements[j];
70                 
71                 if ( fieldPropertyName.equals( existingFieldElement.getProperty() ) )
72                 {
73                     fieldElementToUse = existingFieldElement;
74                     break;
75                 }
76             }
77             
78             if ( fieldElementToUse == null )
79             {
80                 fieldElementToUse = formElement.addNewField();
81                 fieldElementToUse.setProperty( fieldPropertyName );
82             }
83             
84             field.writeToXMLBean( fieldElementToUse );
85         }
86     }
87 }
88
Popular Tags