users@jaxb.java.net

Re: XJC and the visitor pattern

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Mon, 26 Oct 2009 23:47:14 +0100

Hi,

>> What is your task, by the way?
>
> I am currently looking for an elegant solution to validate schema
> derived classes in a way that any validation errors can be collected,
> processed and presented to a user. I cannot use schema validation for
> this since those exception messages are quite meaningless from a users
> perspective and the ValidationEvent interface does not provide enough
> information to transform it to something more meaningful. So I was
> thinking of something which simply passes every object in a given graph
> to some callback interface. Ideally having something else generate an
> abstract base implementation containing the validation logic from the
> schema (facets, uses, etc.) so that the schema constraints could be reused.


I have solved such task for JAXB1, check the
https://jaxbvalidation.dev.java.net/ project. I have to say the task
is very very complex and I'm hesitating for years now to start the
implementation for JAXB2.

In any case, now I understand better what your requirements are. I
think it's better to start from the other end - to consider how these
callback interfaces should look like. In the first approach I would
expect something like:

public class MyObjectValidator implements Validator<MyObject>
{
  public void validate(ValidatorFactory factory, ObjectLocator
locator, ValidationEventHandler handler, MyObject value)
  { // validate value and send validation events to the handler}
}

Validator implementations may be registered with the validator factory
- or read by this factory, for instance, from annotations. When
validator is validating a complex object, it may request the factory
to create validators for its fields.

In this case you don't need the visitor pattern at all, you need to
generate validators which would allow subclassing and re-registration
in the validator factory.

I'd also check JSR-303 and Hibernate Validator. This may be a way to go.

Bye.
/lexi