|
Re: XML schema data types like xs:Name [message #1062279 is a reply to message #1062215] |
Thu, 06 June 2013 23:34   |
Konstantin Komissarchik Messages: 1077 Registered: July 2009 |
Senior Member |
|
|
Sapphire does not validation per XML Schema, but you can certainly specify validation rules to cover what the schema requires and much more. A number of annotations such as @Required, @PossibleValues, @NumericRange, etc. result in validation. For maximum flexibility, you can implement a custom ValidationService and attach it to a property using @Service annotation. Examples in the samples project.
In Sapphire 0.7, there is also an @Validation annotation that allows custom validation rules to be specified using EL. For instance:
@Validation
(
rule = "${ Scale( Discount, 2 ) <= Scale( Subtotal, 2 ) + Scale( Delivery, 2 ) }",
message = "Discount must not exceed subtotal plus delivery charge."
)
There isn't a regex function in EL, but we can add one if that would be useful.
|
|
|
Re: XML schema data types like xs:Name [message #1062369 is a reply to message #1062279] |
Fri, 07 June 2013 12:35   |
kon f Messages: 152 Registered: March 2012 |
Senior Member |
|
|
Hey Konstantin,
thank you for the information. Since it is possible to use regex with a schema, it would be nice to have this feature. I also think that it would help save boilerplate code for checking common xml schema type like xs:ID, xs:Name and xs:NCName.
As suggested, I implemented a custom validation service that checks for schema type xs:Name.
public class NameValidationService extends ValidationService {
@Override
public Status validate() {
final Value<?> value = context(IModelElement.class).read(context(ValueProperty.class));
final String name = value.getText();
if (XMLChar.isValidName(name)) {
return Status.createOkStatus();
}
String message = String.format("%s is not a valid value for the type \"Name\".", name);
return Status.createErrorStatus(message);
}
}
While debugging some simple JAXB project, I found the com.sun.org.apache.xerces.internal.util.XMLChar type. The xs:ID type inherits from xs:NCName that can be checked with XMLChar.isValidNCName(...).
Thank you!
Kon
[Updated on: Fri, 07 June 2013 12:44] Report message to a moderator
|
|
|
|
|
|
Re: XML schema data types like xs:Name [message #1066636 is a reply to message #1065800] |
Wed, 03 July 2013 14:07   |
kon f Messages: 152 Registered: March 2012 |
Senior Member |
|
|
Hey Konstantin,
I'm trying to implement some custom custom validation service but having difficulties to access all Value<T>s, storing the the id. I'm just able to get a collection of strings.
My model has following structure:
- ElementA [1-1]
\- Id
\- ElementB [0-*]
\- Id
\- ElementC [0-*]
\- Id
Every Id is tagged with following annotations:
@Required
@DependsOn({ "/Id", "/ElementB/Id", "/ElementB/ElementC/Id" })
@Service(impl = IDValidationService.class)
ValueProperty PROP_ID = new ValueProperty(TYPE, "Id");
Value<String> getId();
void setId(String value);
I use absolute paths being able to compare the changed id against every other id that occurs in my file.
My validation method of my custom validation service:
@Override
public Status isValid(String name) {
if (!isValidContent(name)) {
return Status.createErrorStatus(getErrorMessage(name));
}
// here the actual interesting part
Value<?> value = context(IModelElement.class).read(context(ValueProperty.class));
List<DependenciesService> services = context(IModelElement.class).services(context(ModelProperty.class), DependenciesService.class);
for (DependenciesService dependenciesService : services) {
Set<ModelPath> dependencies = dependenciesService.dependencies();
for (ModelPath path : dependencies) {
SortedSet<String> read = ((IModelElement) context(IModelElement.class).root()).read(path);
System.out.println(String.format("%s %s", path, read));
// ... perform validation value against the collections content
}
}
return Status.createOkStatus();
}
Unfortunately, the method call read(ModelPath) returns a list of strings. I would need the actual values being able to exclude value from the comparison. In addition, ids with the same string content are aggregated to one due to the SortedSet implementation.
Thank you.
Kon
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02187 seconds