1 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 ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Locale ; 35 import java.util.HashMap ; 36 import java.util.ArrayList ; 37 import java.util.Collection ; 38 import java.util.Iterator ; 39 40 public abstract class ValidationModel 41 implements JpfLanguageConstants 42 { 43 private Map _localeSets = new HashMap (); 44 private LocaleSet _defaultLocaleSet = new LocaleSet(); 45 private List _rulesToAddForAllLocales = new ArrayList (); 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 _entityName; 73 private String _fieldName; 74 private String _fieldDisplayName; 75 private String _fieldDisplayNameKey; 76 77 public RuleInfo( String entityName, String fieldName, String fieldDisplayName, String fieldDisplayNameKey ) 78 { 79 _entityName = entityName; 80 _fieldName = fieldName; 81 _fieldDisplayName = fieldDisplayName; 82 _fieldDisplayNameKey = fieldDisplayNameKey; 83 } 84 85 public String getEntityName() 86 { 87 return _entityName; 88 } 89 90 public String getFieldName() 91 { 92 return _fieldName; 93 } 94 95 public String getFieldDisplayName() 96 { 97 return _fieldDisplayName; 98 } 99 100 public String 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 validatorVersion ) 124 { 125 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 locale ) 145 { 146 LocaleSet localeSet = null; 147 148 if ( locale == null ) { 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 if ( getField( ruleInfo, _defaultLocaleSet ) == null ) 169 { 170 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 entityName = ruleInfo.getEntityName(); 183 ValidatableEntity entity = localeSet.getEntity( entityName ); 184 if ( entity == null ) { return null; } 185 186 String 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 entityName = ruleInfo.getEntityName(); 201 ValidatableEntity entity = localeSet.getEntity( entityName ); 202 if ( entity == null ) localeSet.addValidatableEntity( entity = new ValidatableEntity( entityName ) ); 203 204 String 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 if ( rule != null ) { field.addRule( rule ); } 217 } 218 219 public void writeXml( PrintStream outputStream, File mergeFile ) 220 throws XmlException, IOException , FatalCompileTimeException 221 { 222 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 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 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" ); } 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 XmlCursor curs = formValidationElement.newCursor(); 309 String headerComment = getHeaderComment( mergeFile ); 310 if ( headerComment != null ) curs.insertComment( headerComment ); 311 312 313 writeLocaleSets( formValidationElement ); 317 writeLocaleSet( _defaultLocaleSet, formValidationElement ); 318 319 XmlOptions options = new XmlOptions(); 323 options.setSavePrettyPrint(); 324 doc.save( outputStream, options ); 325 } 326 327 protected String getHeaderComment( File mergeFile ) 328 throws FatalCompileTimeException 329 { 330 return null; 331 } 332 333 private void writeLocaleSets( FormValidationDocument.FormValidation formValidationElement ) 334 { 335 List allLocales = new ArrayList ( _localeSets.keySet() ); 348 List langCountryVariant = new ArrayList (); 349 List langCountry = new ArrayList (); 350 List lang = new ArrayList (); 351 352 for ( java.util.Iterator ii = allLocales.iterator(); ii.hasNext(); ) 353 { 354 Locale locale = ( Locale ) 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 locales, FormValidationDocument.FormValidation formValidationElement ) 378 { 379 for ( java.util.Iterator ii = locales.iterator(); ii.hasNext(); ) 380 { 381 Locale locale = ( Locale ) 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 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 getOutputFileURI(); 436 } 437 | Popular Tags |