KickJava   Java API By Example, From Geeks To Geeks.

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


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.FormsetDocument;
21 import org.apache.beehive.netui.compiler.model.schema.validator11.FormDocument;
22
23 import java.util.Locale JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 public class LocaleSet
29 {
30     private Locale JavaDoc _locale;
31     private Map JavaDoc _entities = new HashMap JavaDoc();
32     
33     
34     public LocaleSet()
35     {
36         _locale = null; // default locale;
37
}
38     
39     public LocaleSet( Locale JavaDoc locale )
40     {
41         _locale = locale;
42     }
43     
44     public Locale JavaDoc getLocale()
45     {
46         return _locale;
47     }
48     
49     public ValidatableEntity getEntity( String JavaDoc entityName )
50     {
51         return ( ValidatableEntity ) _entities.get( entityName );
52     }
53     
54     public void addValidatableEntity( ValidatableEntity entity )
55     {
56         _entities.put( entity.getEntityName(), entity );
57     }
58     
59     public void writeToXMLBean( FormsetDocument.Formset formset )
60     {
61         if ( _locale != null )
62         {
63             formset.setLanguage( _locale.getLanguage() );
64             if ( _locale.getCountry().length() > 0 )
65             {
66                 formset.setCountry( _locale.getCountry() );
67                 if ( _locale.getVariant().length() > 0 )
68                 {
69                     formset.setVariant( _locale.getVariant() );
70                 }
71             }
72         }
73         
74         FormDocument.Form[] existingFormElements = formset.getFormArray();
75         for ( Iterator JavaDoc i = _entities.values().iterator(); i.hasNext(); )
76         {
77             ValidatableEntity entity = ( ValidatableEntity ) i.next();
78             String JavaDoc entityName = entity.getEntityName();
79             
80             //
81
// Look for an existing element, or create one if none matches this entity name.
82
//
83
FormDocument.Form formElementToUse = null;
84             
85             for ( int j = 0; j < existingFormElements.length; j++ )
86             {
87                 FormDocument.Form formElement = existingFormElements[j];
88                 
89                 if ( entityName.equals( formElement.getName() ) )
90                 {
91                     formElementToUse = formElement;
92                     break;
93                 }
94             }
95             
96             if ( formElementToUse == null )
97             {
98                 formElementToUse = formset.addNewForm();
99                 formElementToUse.setName( entityName );
100             }
101             
102             entity.writeToXMLBean( formElementToUse );
103         }
104     }
105 }
106
Popular Tags