KickJava   Java API By Example, From Geeks To Geeks.

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


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.FormValidationDocument;
22 import org.apache.beehive.netui.compiler.JpfLanguageConstants;
23 import org.apache.beehive.netui.compiler.FatalCompileTimeException;
24 import org.apache.xmlbeans.XmlException;
25 import org.apache.xmlbeans.XmlDocumentProperties;
26 import org.apache.xmlbeans.XmlCursor;
27 import org.apache.xmlbeans.XmlOptions;
28
29 import java.io.PrintStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Locale JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 public abstract class ValidationModel
41         implements JpfLanguageConstants
42 {
43     private Map JavaDoc _localeSets = new HashMap JavaDoc();
44     private LocaleSet _defaultLocaleSet = new LocaleSet();
45     private List JavaDoc _rulesToAddForAllLocales = new ArrayList JavaDoc(); // list of RuleAdd
46
private boolean _empty = true;
47     private ValidatorVersion _validatorVersion = ValidatorVersion.oneOne;
48
49     public static class ValidatorVersion
50     {
51         private static final int INT_ONE_ZERO = 0;
52         private static final int INT_ONE_ONE = 1;
53         
54         private int _val;
55         
56         private ValidatorVersion( int val )
57         {
58             _val = val;
59         }
60         
61         public boolean equals( ValidatorVersion vv )
62         {
63             return _val == vv._val;
64         }
65         
66         public static final ValidatorVersion oneZero = new ValidatorVersion( INT_ONE_ZERO );
67         public static final ValidatorVersion oneOne = new ValidatorVersion( INT_ONE_ONE );
68     }
69
70     public static class RuleInfo
71     {
72         private String JavaDoc _entityName;
73         private String JavaDoc _fieldName;
74         private String JavaDoc _fieldDisplayName;
75         private String JavaDoc _fieldDisplayNameKey;
76
77         public RuleInfo( String JavaDoc entityName, String JavaDoc fieldName, String JavaDoc fieldDisplayName, String JavaDoc fieldDisplayNameKey )
78         {
79             _entityName = entityName;
80             _fieldName = fieldName;
81             _fieldDisplayName = fieldDisplayName;
82             _fieldDisplayNameKey = fieldDisplayNameKey;
83         }
84
85         public String JavaDoc getEntityName()
86         {
87             return _entityName;
88         }
89
90         public String JavaDoc getFieldName()
91         {
92             return _fieldName;
93         }
94
95         public String JavaDoc getFieldDisplayName()
96         {
97             return _fieldDisplayName;
98         }
99
100         public String JavaDoc getFieldDisplayNameKey()
101         {
102             return _fieldDisplayNameKey;
103         }
104     }
105
106     private static class RuleAdd
107     {
108         public RuleAdd( RuleInfo ruleInfo, ValidatorRule rule )
109         {
110             this.ruleInfo = ruleInfo;
111             this.rule = rule;
112         }
113
114         public RuleInfo ruleInfo;
115         public ValidatorRule rule;
116     }
117
118     public ValidatorVersion getValidatorVersion()
119     {
120         return _validatorVersion;
121     }
122
123     public void setValidatorVersion( String JavaDoc validatorVersion )
124     {
125         //
126
// default to the least common denominator (validator v1.0) unless
127
// explicitly set to v1.1.
128
//
129
if ( validatorVersion != null && validatorVersion.equals( VALIDATOR_VERSION_ONE_ONE_STR ) )
130         {
131             _validatorVersion = ValidatorVersion.oneOne;
132         }
133         else
134         {
135             _validatorVersion = ValidatorVersion.oneZero;
136         }
137     }
138
139     public void addFieldRuleForAllLocales( RuleInfo ruleInfo, ValidatorRule rule )
140     {
141         _rulesToAddForAllLocales.add( new RuleAdd( ruleInfo, rule ) );
142     }
143     
144     public void addFieldRule( RuleInfo ruleInfo, ValidatorRule rule, Locale JavaDoc locale )
145     {
146         LocaleSet localeSet = null;
147         
148         if ( locale == null ) // default locale
149
{
150             localeSet = _defaultLocaleSet;
151         }
152         else
153         {
154             localeSet = ( LocaleSet ) _localeSets.get( locale );
155             
156             if ( localeSet == null )
157             {
158                 localeSet = new LocaleSet( locale );
159                 _localeSets.put( locale, localeSet );
160             }
161
162             //
163
// The Commons Validator uses specific locale rules for a field only if there
164
// is a rule for the same field in the default formset. Therefor, we need to
165
// keep a place holder for each locale specific field we find in the default
166
// formset entity so that the Commons Validator will behave as desired.
167
//
168
if ( getField( ruleInfo, _defaultLocaleSet ) == null )
169             {
170                 //
171
// create a simple placeholder for the field, without any rules
172
//
173
addFieldRule( ruleInfo, ( ValidatorRule ) null, _defaultLocaleSet );
174             }
175         }
176         
177         addFieldRule( ruleInfo, rule, localeSet );
178     }
179
180     private ValidatableField getField( RuleInfo ruleInfo, LocaleSet localeSet )
181     {
182         String JavaDoc entityName = ruleInfo.getEntityName();
183         ValidatableEntity entity = localeSet.getEntity( entityName );
184         if ( entity == null ) { return null; }
185
186         String JavaDoc fieldName = ruleInfo.getFieldName();
187         return entity.getField( fieldName );
188     }
189
190     private boolean hasFieldRule( RuleInfo ruleInfo, ValidatorRule rule, LocaleSet localeSet )
191     {
192         ValidatableField field = getField( ruleInfo, localeSet );
193         if ( field == null ) { return false; }
194
195         return field.hasRule( rule );
196     }
197
198     private void addFieldRule( RuleInfo ruleInfo, ValidatorRule rule, LocaleSet localeSet )
199     {
200         String JavaDoc entityName = ruleInfo.getEntityName();
201         ValidatableEntity entity = localeSet.getEntity( entityName );
202         if ( entity == null ) localeSet.addValidatableEntity( entity = new ValidatableEntity( entityName ) );
203         
204         String JavaDoc fieldName = ruleInfo.getFieldName();
205         ValidatableField field = entity.getField( fieldName );
206         if ( field == null )
207         {
208             field = ValidatableFieldFactory.getInstance( fieldName, ruleInfo.getFieldDisplayName(),
209                                                          ruleInfo.getFieldDisplayNameKey(), getValidatorVersion() );
210             entity.addField( field );
211         }
212
213         //
214
// A field element without rules is OK, but we don't want to add a null rule.
215
//
216
if ( rule != null ) { field.addRule( rule ); }
217     }
218     
219     public void writeXml( PrintStream JavaDoc outputStream, File JavaDoc mergeFile )
220         throws XmlException, IOException JavaDoc, FatalCompileTimeException
221     {
222         //
223
// First, if we haven't written the all-locale rules to each locale, do so now.
224
// However, before we add a rule, check that it does not already exist. We don't
225
// want to overload a rule explicitly defined for a specific locale with
226
// an all-locale rule of the same name.
227
//
228
if ( _rulesToAddForAllLocales != null )
229         {
230             for ( int i = 0; i < _rulesToAddForAllLocales.size(); i++ )
231             {
232                 RuleAdd ruleAdd = ( RuleAdd ) _rulesToAddForAllLocales.get( i );
233                 
234                 for ( Iterator JavaDoc j = _localeSets.values().iterator(); j.hasNext(); )
235                 {
236                     LocaleSet localeSet = ( LocaleSet ) j.next();
237                     if ( !hasFieldRule( ruleAdd.ruleInfo, ruleAdd.rule, localeSet ) ) {
238                         addFieldRule( ruleAdd.ruleInfo, ruleAdd.rule, localeSet );
239                     }
240                 }
241
242                 if ( !hasFieldRule( ruleAdd.ruleInfo, ruleAdd.rule, _defaultLocaleSet ) ) {
243                     addFieldRule( ruleAdd.ruleInfo, ruleAdd.rule, _defaultLocaleSet );
244                 }
245             }
246             
247             _rulesToAddForAllLocales = null;
248         }
249         
250         
251         //
252
// Create and initialize the document, or parse the given one (with which we'll merge).
253
//
254
FormValidationDocument doc;
255         
256         if ( mergeFile != null && mergeFile.canRead() )
257         {
258             doc = FormValidationDocument.Factory.parse( mergeFile );
259         }
260         else
261         {
262             doc = FormValidationDocument.Factory.newInstance();
263         }
264         
265         XmlDocumentProperties dp = doc.documentProperties();
266         
267         if ( dp.getDoctypeName() == null )
268         {
269             dp.setDoctypeName( "form-validation" ); // NOI18N
270
}
271
272         if ( dp.getDoctypePublicId() == null )
273         {
274             if ( _validatorVersion.equals( ValidatorVersion.oneZero ) )
275             {
276                 dp.setDoctypePublicId( "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" );
277             }
278             else
279             {
280                 dp.setDoctypePublicId( "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1//EN" );
281             }
282         }
283
284         if ( dp.getDoctypeSystemId() == null )
285         {
286             if ( _validatorVersion.equals( ValidatorVersion.oneZero ) )
287             {
288                 dp.setDoctypeSystemId( "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd" );
289             }
290             else
291             {
292                 dp.setDoctypeSystemId( "http://jakarta.apache.org/commons/dtds/validator_1_1.dtd" );
293             }
294         }
295         
296         
297         FormValidationDocument.FormValidation formValidationElement = doc.getFormValidation();
298         
299         if ( formValidationElement == null )
300         {
301             formValidationElement = doc.addNewFormValidation();
302         }
303         
304         
305         //
306
// Write the "generated by" comment.
307
//
308
XmlCursor curs = formValidationElement.newCursor();
309         String JavaDoc headerComment = getHeaderComment( mergeFile );
310         if ( headerComment != null ) curs.insertComment( headerComment );
311                 
312         
313         //
314
// Now write out all the LocaleSets, which contain the forms/fields/rules.
315
//
316
writeLocaleSets( formValidationElement );
317         writeLocaleSet( _defaultLocaleSet, formValidationElement );
318         
319         //
320
// Write the file.
321
//
322
XmlOptions options = new XmlOptions();
323         options.setSavePrettyPrint();
324         doc.save( outputStream, options );
325     }
326     
327     protected String JavaDoc getHeaderComment( File JavaDoc mergeFile )
328             throws FatalCompileTimeException
329     {
330         return null;
331     }
332     
333     private void writeLocaleSets( FormValidationDocument.FormValidation formValidationElement )
334     {
335         //
336
// Commons Validator behavior is to build a key from the locale of a FormSet
337
// or uses the default Locale (Locale.getDefault() - the system locale) to
338
// track different elements. This implies that the
339
// without language or country attributes could be mapped to "en_US"
340
// if that's the default locale.
341
// See org.apache.commons.validator.ValidatorResources.buildKey()
342
//
343
// Therefor, to ensure the validator uses rules for of a specific
344
// locale before the FormSet with no language or country attributes (even
345
// if it is the locale of the system), write the most specific locales first.
346
//
347
List JavaDoc allLocales = new ArrayList JavaDoc( _localeSets.keySet() );
348         List JavaDoc langCountryVariant = new ArrayList JavaDoc();
349         List JavaDoc langCountry = new ArrayList JavaDoc();
350         List JavaDoc lang = new ArrayList JavaDoc();
351
352         for ( java.util.Iterator JavaDoc ii = allLocales.iterator(); ii.hasNext(); )
353         {
354             Locale JavaDoc locale = ( Locale JavaDoc ) ii.next();
355             if ( locale.getCountry().length() > 0 )
356             {
357                 if ( locale.getVariant().length() > 0 )
358                 {
359                     langCountryVariant.add( locale );
360                 }
361                 else
362                 {
363                     langCountry.add( locale );
364                 }
365             }
366             else
367             {
368                 lang.add( locale );
369             }
370         }
371
372         writeLocaleSets( langCountryVariant, formValidationElement );
373         writeLocaleSets( langCountry, formValidationElement );
374         writeLocaleSets( lang, formValidationElement );
375     }
376
377     private void writeLocaleSets( Collection JavaDoc locales, FormValidationDocument.FormValidation formValidationElement )
378     {
379         for ( java.util.Iterator JavaDoc ii = locales.iterator(); ii.hasNext(); )
380         {
381             Locale JavaDoc locale = ( Locale JavaDoc ) ii.next();
382             LocaleSet localeSet = ( LocaleSet ) _localeSets.get( locale );
383             writeLocaleSet( localeSet, formValidationElement );
384         }
385     }
386
387     private void writeLocaleSet( LocaleSet localeSet, FormValidationDocument.FormValidation formValidationElement )
388     {
389         FormsetDocument.Formset[] existingFormSetElements = formValidationElement.getFormsetArray();
390         FormsetDocument.Formset formSetElementToUse = null;
391         Locale JavaDoc locale = localeSet.getLocale();
392         
393         for ( int i = 0; i < existingFormSetElements.length; i++ )
394         {
395             FormsetDocument.Formset existingFormSetElement = existingFormSetElements[i];
396             
397             if ( locale == null && existingFormSetElement.getLanguage() == null )
398             {
399                 formSetElementToUse = existingFormSetElement;
400                 break;
401             }
402             else if ( locale != null && locale.getLanguage().equals( existingFormSetElement.getLanguage() ) )
403             {
404                 if ( ( locale.getCountry().length() == 0 && existingFormSetElement.getCountry() == null )
405                      || locale.getCountry().equals( existingFormSetElement.getCountry() ) )
406                 {
407                     if ( ( locale.getVariant().length() == 0 && existingFormSetElement.getVariant() == null )
408                          || locale.getVariant().equals( existingFormSetElement.getVariant() ) )
409                     {
410                         formSetElementToUse = existingFormSetElement;
411                         break;
412                     }
413                 }
414             }
415         }
416         
417         if ( formSetElementToUse == null )
418         {
419             formSetElementToUse = formValidationElement.addNewFormset();
420         }
421         
422         localeSet.writeToXMLBean( formSetElementToUse );
423     }
424
425     public boolean isEmpty()
426     {
427         return _empty;
428     }
429
430     protected void setEmpty( boolean empty )
431     {
432         _empty = empty;
433     }
434     
435     public abstract String JavaDoc getOutputFileURI();
436 }
437
Popular Tags