Index: src/main/java/org/onehippo/forge/contentblocks/validator/ContentBlocksValidator.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- src/main/java/org/onehippo/forge/contentblocks/validator/ContentBlocksValidator.java (revision 270) +++ src/main/java/org/onehippo/forge/contentblocks/validator/ContentBlocksValidator.java (revision ) @@ -9,8 +9,11 @@ import org.hippoecm.frontend.model.ocm.StoreException; import org.hippoecm.frontend.plugin.IPluginContext; import org.hippoecm.frontend.plugin.config.IPluginConfig; +import org.hippoecm.frontend.types.IFieldDescriptor; import org.hippoecm.frontend.types.ITypeDescriptor; import org.hippoecm.frontend.validation.IFieldValidator; +import org.hippoecm.frontend.validation.ModelPath; +import org.hippoecm.frontend.validation.ModelPathElement; import org.hippoecm.frontend.validation.ValidationException; import org.hippoecm.frontend.validation.Violation; import org.slf4j.Logger; @@ -44,22 +47,27 @@ public Set validate(IFieldValidator iFieldValidator, JcrNodeModel jcrNodeModel, IModel iModel) throws ValidationException { try { - Node node = (Node) iModel.getObject(); - final String primaryNodeType = node.getPrimaryNodeType().getName(); + // get descriptor based on actual node type + final Node contentBlockNode = (Node) iModel.getObject(); + final String primaryNodeType = contentBlockNode.getPrimaryNodeType().getName(); + final JcrTypeLocator jcrTypeLocator = new JcrTypeLocator(); + final ITypeDescriptor contentBlockDescriptor = jcrTypeLocator.locate(primaryNodeType); + log.debug("Content block descriptor {} found by type {}", contentBlockDescriptor, primaryNodeType); - JcrTypeLocator jcrTypeLocator = new JcrTypeLocator(); - ITypeDescriptor descriptor = jcrTypeLocator.locate(primaryNodeType); - log.debug("Descriptor {} found by type {}", descriptor, primaryNodeType); - + if (contentBlockDescriptor != null) { - ValidatorService validatorService = getPluginContext().getService("field.validator.service", ValidatorService.class); + ValidatorService validatorService = getPluginContext().getService("field.validator.service", ValidatorService.class); - - if (validatorService == null) { + if (validatorService == null) { - log.warn("ValidatorService is not found by 'field.validator.service': cannot validate content block node {}", node.getPath()); + log.error("ValidatorService is not found by 'field.validator.service': cannot validate content block node {}", contentBlockNode.getPath()); - } - else { + } + else { - final JcrTypeValidator validator = new JcrTypeValidator(descriptor, validatorService); - return validator.validate(new JcrNodeModel(node)); + // delegate to the actual content block's validator + final JcrTypeValidator validator = new JcrTypeValidator(contentBlockDescriptor, validatorService); + final Set violations = validator.validate(new JcrNodeModel(contentBlockNode)); + + final IFieldDescriptor fieldDescriptor = iFieldValidator.getFieldDescriptor(); + return prependFieldPathToViolations(violations, contentBlockNode, fieldDescriptor); - } + } + } } catch (RepositoryException e) { log.warn("RepositoryException occurred accessing node", e); } catch (StoreException e) { @@ -68,5 +76,32 @@ // no validation done return new HashSet(); + } + + protected Set prependFieldPathToViolations(Set violations, Node contentBlockNode, IFieldDescriptor fieldDescriptor) + throws ValidationException { + + // correct node index on behalf on multiples (which content blocks are) + int index; + try { + index = contentBlockNode.getIndex() - 1; + } catch (RepositoryException e) { + throw new ValidationException("Could not get index for node", e); + } + + // replace the violations by violations that have also the path element of the content block compound in them + final Set newViolations = new HashSet(violations.size()); + for (Violation violation : violations) { + Set childPaths = violation.getDependentPaths(); + Set newPaths = new HashSet(); + for (ModelPath childPath : childPaths) { + ModelPathElement[] elements = new ModelPathElement[childPath.getElements().length + 1]; + System.arraycopy(childPath.getElements(), 0, elements, 1, childPath.getElements().length); + elements[0] = new ModelPathElement(fieldDescriptor, fieldDescriptor.getPath(), index); + newPaths.add(new ModelPath(elements)); + } + newViolations.add(new Violation(violation.getResourceBundleClass(), violation.getMessageKey(), violation.getParameters(), newPaths)); + } + return newViolations; } }