Author: tchemit Date: 2009-01-11 16:38:29 +0000 (Sun, 11 Jan 2009) New Revision: 1140 Modified: jaxx/trunk/jaxx-compiler-validator/changelog.txt jaxx/trunk/jaxx-compiler-validator/src/main/java/jaxx/tags/validator/BeanValidatorHandler.java jaxx/trunk/jaxx-runtime-validator/changelog.txt jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/BeanValidator.java jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/Util.java jaxx/trunk/jaxx-runtime-validator/src/test/java/jaxx/junit/ValidatorTest.java Log: beanClass is now mandatory on BeanValidator (can be guest from bean attribute if present). add parent attribute on BeanValidator to chain validators Modified: jaxx/trunk/jaxx-compiler-validator/changelog.txt =================================================================== --- jaxx/trunk/jaxx-compiler-validator/changelog.txt 2009-01-11 16:36:16 UTC (rev 1139) +++ jaxx/trunk/jaxx-compiler-validator/changelog.txt 2009-01-11 16:38:29 UTC (rev 1140) @@ -1,4 +1,6 @@ -1.0 ???? 200812?? +1.0 chemit 20090111 + * 20090111 [chemit] - beanClass is now mandatory on BeanValidator (can be guest from bean attribute if present). + - add parent attribute on BeanValidator to chain validators 0.8 ??? 200812?? * 20081228 [chemit] - generify ClassDescriptor Modified: jaxx/trunk/jaxx-compiler-validator/src/main/java/jaxx/tags/validator/BeanValidatorHandler.java =================================================================== --- jaxx/trunk/jaxx-compiler-validator/src/main/java/jaxx/tags/validator/BeanValidatorHandler.java 2009-01-11 16:36:16 UTC (rev 1139) +++ jaxx/trunk/jaxx-compiler-validator/src/main/java/jaxx/tags/validator/BeanValidatorHandler.java 2009-01-11 16:38:29 UTC (rev 1140) @@ -56,6 +56,8 @@ public static final String SCOPE_ATTRIBUTE = "scope"; + public static final String PARENT_VALIDATOR_ATTRIBUTE = "parentValidator"; + /** to use log facility, just put in your code: log.info(\"...\"); */ static Log log = LogFactory.getLog(BeanValidatorHandler.class); @@ -122,6 +124,10 @@ error = info.addScope(this, compiler); } + if (!error) { + error = info.addParentValidator(tag, this, compiler); + } + if (error) { log.warn("error were detected in second compile pass of CompiledObject [" + info + "]"); } @@ -166,6 +172,7 @@ protected String errorTableModel; protected String errorTable; protected Scope scope; + protected String parentValidator; public CompiledBeanValidator(String id, ClassDescriptor objectClass, JAXXCompiler compiler) { super(id, objectClass, compiler); @@ -269,10 +276,14 @@ return; } + if (PARENT_VALIDATOR_ATTRIBUTE.equals(property)) { + if (value != null && !value.trim().isEmpty()) { + parentValidator = value.trim(); + } + return; + } + throw new CompilerException("property " + property + " is not allowed on object " + this); - - //todo should not allowed to find other attributes - //super.addProperty(property, value); } public String getBean() { @@ -307,13 +318,18 @@ return scope; } + public String getParentValidator() { + return parentValidator; + } + public JAXXBeanInfo getBeanDescriptor(JAXXCompiler compiler) { if (beanDescriptor == null && foundBean()) { String beanClassName = null; try { + //TC-20090111 beanClass is mandatory // get the real bean class name (from bean or beanClass) - if (beanClass != null) { + /*if (beanClass != null) { beanClassName = beanClass; } else { beanClassName = compiler.getSymbolTable().getClassTagIds().get(bean); @@ -321,8 +337,8 @@ compiler.reportError("could not find class of the bean '" + bean + "'"); return null; } - } - ClassDescriptor beanClassDescriptor = ClassDescriptorLoader.getClassDescriptor(beanClassName); + }*/ + ClassDescriptor beanClassDescriptor = ClassDescriptorLoader.getClassDescriptor(beanClass); beanDescriptor = DefaultObjectHandler.getJAXXBeanInfo(beanClassDescriptor); } catch (ClassNotFoundException e) { compiler.reportError("could not load class " + beanClassName); @@ -340,7 +356,7 @@ } public boolean foundBean() { - return !(bean == null && beanClass == null); + return !(beanClass == null || beanClass.isEmpty()); } protected boolean addUiClass(BeanValidatorHandler handler, JAXXCompiler compiler) { @@ -431,6 +447,28 @@ return false; } + protected boolean addParentValidator(Element tag, BeanValidatorHandler handler, JAXXCompiler compiler) { + if (parentValidator != null) { + String initializer; + if (parentValidator.startsWith("{") && parentValidator.endsWith("}")) { + + // todo : should be able to bind + initializer = parentValidator.substring(1, parentValidator.length() - 1); + + } else { + // the attribute referes an existing widget + if (!compiler.checkReference(tag, parentValidator, true, PARENT_VALIDATOR_ATTRIBUTE)) { + // parentValidator is not defined + return true; + } + initializer = parentValidator; + } + String code = handler.getSetPropertyCode(getJavaCode(), PARENT_VALIDATOR_ATTRIBUTE, initializer, compiler); + appendAdditionCode(code); + } + return false; + } + protected boolean addErrorList(Element tag, JAXXCompiler compiler) { if (errorList == null) { @@ -475,11 +513,27 @@ protected boolean addBean(Element tag, BeanValidatorHandler handler, JAXXCompiler compiler) { - if (!foundBean()) { - compiler.reportError("tag '" + tag + "' requires a 'bean' or a 'beanClass' attribute"); + if (beanClass == null || beanClass.isEmpty()) { + // try to guest beanClass from bean attribute + if (bean != null && !bean.isEmpty()) { + beanClass = compiler.getSymbolTable().getClassTagIds().get(bean); + if (beanClass == null) { + compiler.reportError("could not find class of the bean '" + bean + "', and no beanClass was setted"); + return true; + } + } + } + if (beanClass == null) { + compiler.reportError("tag '" + tag + "' requires a 'beanClass' attribute, and could not guest it from 'bean' attribute (no bean attribute setted...)"); return true; } + JAXXBeanInfo beanInfo = getBeanDescriptor(compiler); + if (beanInfo == null) { + compiler.reportError(tag, "could not find descriptor of class " + beanClass); + return true; + } + String beanInitializer = null; if (bean != null) { @@ -514,12 +568,10 @@ } // add generic type to validator - JAXXBeanInfo beanInfo = getBeanDescriptor(compiler); - if (beanInfo == null) { - return true; - } - setGenericTypes(new String[]{beanInfo.getJAXXBeanDescriptor().getClassDescriptor().getName()}); + String beanClassName = beanInfo.getJAXXBeanDescriptor().getClassDescriptor().getName(); + setConstructorParams(beanClassName + ".class"); + setGenericTypes(new String[]{beanClassName}); if (getAutoField()) { registerAutoFieldBean(tag, compiler, beanInfo); Modified: jaxx/trunk/jaxx-runtime-validator/changelog.txt =================================================================== --- jaxx/trunk/jaxx-runtime-validator/changelog.txt 2009-01-11 16:36:16 UTC (rev 1139) +++ jaxx/trunk/jaxx-runtime-validator/changelog.txt 2009-01-11 16:38:29 UTC (rev 1140) @@ -1,4 +1,5 @@ -1.0 ???? 200812?? +1.0 chemit 20090111 +* 20090111 [chemit] - add parent attribute on BeanValidator to chain validators 0.8 ??? 200812?? * 20081228 [chemit] - generify ClassDescriptor Modified: jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/BeanValidator.java =================================================================== --- jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/BeanValidator.java 2009-01-11 16:36:16 UTC (rev 1139) +++ jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/BeanValidator.java 2009-01-11 16:38:29 UTC (rev 1140) @@ -175,6 +175,9 @@ /** state of the validator */ protected boolean valid; + /** la classe du bean à surveiller */ + protected final Class<T> beanClass; + /** le bean a surveiller */ protected T bean = null; @@ -202,9 +205,14 @@ /** the validation named context */ protected String contextName; + /** the scope of the validator */ protected Scope scope = Scope.ERROR; - public BeanValidator() { + /** to chain to a prent validator */ + protected BeanValidator<?> parentValidator; + + public BeanValidator(Class<T> beanClass) { + this.beanClass = beanClass; pcs = new PropertyChangeSupport(this); validationSupport = new ValidationAwareSupport(); validationContext = new DelegatingValidatorContext(validationSupport); @@ -251,6 +259,18 @@ return scope; } + public Class<T> getBeanClass() { + return beanClass; + } + + public BeanValidator<?> getParentValidator() { + return parentValidator; + } + + public String getContextName() { + return contextName; + } + /** * Retourne vrai si l'objet bean a ete modifie depuis le dernier * {@link #setBean} @@ -334,20 +354,26 @@ public void setChanged(boolean changed) { boolean oldChanged = this.changed; this.changed = changed; - pcs.firePropertyChange(CHANGED_PROERTY, oldChanged, changed); + // force the property to be propagated + pcs.firePropertyChange(CHANGED_PROERTY, null, changed); + //pcs.firePropertyChange(CHANGED_PROERTY, oldChanged, changed); } public void setValid(boolean valid) { boolean oldValid = this.valid; this.valid = valid; - if (oldValid != valid) { - pcs.firePropertyChange(VALID_PROERTY, oldValid, valid); - } + // force the property to be propagated + pcs.firePropertyChange(VALID_PROERTY, null, valid); + //if (oldValid != valid) { + // pcs.firePropertyChange(VALID_PROERTY, oldValid, valid); + //} } public void setBean(T bean) { T oldBean = this.bean; - + if (log.isDebugEnabled()) { + log.debug(this + " : " + bean); + } // clean conversions of previous bean conversionErrors.clear(); if (oldBean != null) { @@ -391,6 +417,10 @@ this.scope = scope; } + public void setParentValidator(BeanValidator<?> parentValidator) { + this.parentValidator = parentValidator; + } + /** @return <code>true</code> if errors are detected, <code>false</code> otherwise */ public boolean hasErrors() { //todo should also detecte actionErrors ? @@ -515,6 +545,10 @@ c.getParent().repaint(); } } + if (parentValidator != null) { + // chain validation + parentValidator.l.propertyChange(null); + } } catch (ValidationException eee) { log.warn("Error during validation", eee); } @@ -522,7 +556,7 @@ @Override public String toString() { - return super.toString() + "<contextName:" + contextName + ">"; + return super.toString() + "<beanClass:" + beanClass + ", contextName:" + contextName + ">"; } /** Modified: jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/Util.java =================================================================== --- jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/Util.java 2009-01-11 16:36:16 UTC (rev 1139) +++ jaxx/trunk/jaxx-runtime-validator/src/main/java/jaxx/runtime/validator/Util.java 2009-01-11 16:38:29 UTC (rev 1140) @@ -73,7 +73,7 @@ */ @SuppressWarnings({"unchecked"}) public static void setValidatorBean(JAXXObject ui, Object bean, String... excludeIds) { - if (!JAXXValidator.class.isAssignableFrom(ui.getClass()) ) { + if (!JAXXValidator.class.isAssignableFrom(ui.getClass())) { return; } JAXXValidator jaxxValidator = (JAXXValidator) ui; @@ -86,7 +86,10 @@ } for (String validatorId : validatorIds) { BeanValidator beanValidator = jaxxValidator.getValidator(validatorId); - beanValidator.setBean(bean); + if (bean == null || beanValidator.getBeanClass().isAssignableFrom(bean.getClass())) { + // touch validator, only if fits the bean type (or bean is null) + beanValidator.setBean(bean); + } } } @@ -101,7 +104,7 @@ */ @SuppressWarnings({"unchecked"}) public static void setValidatorChanged(JAXXObject ui, boolean newValue, String... excludeIds) { - if (!JAXXValidator.class.isAssignableFrom(ui.getClass()) ) { + if (!JAXXValidator.class.isAssignableFrom(ui.getClass())) { return; } JAXXValidator jaxxValidator = (JAXXValidator) ui; Modified: jaxx/trunk/jaxx-runtime-validator/src/test/java/jaxx/junit/ValidatorTest.java =================================================================== --- jaxx/trunk/jaxx-runtime-validator/src/test/java/jaxx/junit/ValidatorTest.java 2009-01-11 16:36:16 UTC (rev 1139) +++ jaxx/trunk/jaxx-runtime-validator/src/test/java/jaxx/junit/ValidatorTest.java 2009-01-11 16:38:29 UTC (rev 1140) @@ -29,7 +29,7 @@ b = new File("").getAbsolutePath(); } basedir = new File(b); - validator = new BeanValidator<ValidatorBean>(); + validator = new BeanValidator<ValidatorBean>(ValidatorBean.class); validator.setErrorListModel(errors = new BeanValidatorErrorListModel()); JLabel label = new JLabel(); validator.setFieldRepresentation("existingFile", label);