tags:

views:

217

answers:

2

Hello all ,

Is there a way to avoid or set up a schema to display better user friendly messages?

I am parsing the string and using reg ex to interpret them, but there might be a better way.

Ex.

"cvc-complex-type.2.4.b: The content of element 'node' is not complete. One of '{\"\":offer,\"\":links}' is expected."

Instead I want:

"The element 'node' is not complete. The child elements 'offer' and 'links' are expected."

Again, I've solved the problem by creating an extra layer that validates it. But when I have to use a XML tool with a schema validation, the crypt messages are the ones displayed.

Thanks

A: 

I asked a similar question a while ago.

My conclusion was there is no provided way of mapping the errors, and that it's something you need to do yourself.

Hope someone out there can do better!

Brabster
Thank you, I appreciate
pribeiro
A: 

Not that I know of. You will probably have to create some custom code to adapt your error messages. One way might be to define a set of regular expressions that can pull out the relevant pieces of the validator's error messages and then plug them back into your own error messages. Something like this comes to mind (not optimized, doesn't handle general case, etc. but I think you'll get the idea):

String uglyMessage = "cvc-complex-type.2.4.b: The content of element 'node' is not complete. One of '{\"\":offer,\"\":links}' is expected.";

String findRegex = "cvc-complex-type\\.2\\.4\\.b: The content of element '(\\w+)' is not complete\\. One of '\\{\"\":(\\w+),\"\":(\\w+)}' is expected\\.";

String replaceRegex = "The element '$1' is not complete. The child elements '$2' and '$3' are expected.";

String userFriendlyMessage = Pattern.compile(findRegex).matcher(uglyMessage).replaceAll(replaceRegex);

System.out.println(userFriendlyMessage);
// OUTPUT:
//   The element 'node' is not complete. The child elements 'offer' and 'links' are expected.

I suspect those validator error messages are vendor-specific so if you don't have control over the XML validator in your deployed app, this may not work for you.

SingleShot