Show Table of Contents
3.19. Using an MVEL Expression
- To use an MVEL expression on a rule set, divide the Regex rules and place them in two separate
.propertiesfiles. - Drop these rules into the example
rulesdirectory. - Put the MVEL expression in a
.csvfile, also in therulesdirectory.The customer-related Regex rules that go in thecustomer.propertiesfile look like this:# Customer data rules... customerId=[A-Z][0-9]{5} # Email address... email=^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$ - Insert the product-related Regex rule in the
product.propertiesfile:# Product data rules... productId=[0-9]{3} - Insert the MVEL expression for performing the order item total check into the
order-rules.csvfile.NoteThe easiest way to edit a .csv file is through using a spreadsheet application like LibreOffice Calc or Gnumeric. - Create resource bundle
.propertiesfiles for each of the rule source files.NoteThe names of these files are derived from the names of their corresponding rule files.The message bundle for the rules defined inrules/customer.propertiesis located in therules/i18n/customer.propertiesfile:customerId=ftl:Invalid customer number '${ruleResult.text}' at '${path}'. Customer number must begin with an uppercase character, followed by 5 digits. email=ftl:Invalid email address '${ruleResult.text}' at '${path}'. Email addresses match pattern '${ruleResult.pattern}'.The message bundle for the rule defined inrules/product.propertiesis located in therules/i18n/product.propertiesfile:# Product data rule messages... productId=ftl:Invalid product ID '${ruleResult.text}' at '${path}'. Product ID must match pattern '${ruleResult.pattern}'.The message bundle for the rule defined inrules/order-rules.csvis located in therules/i18n/order-rules.propertiesfile:# Order item rule messages. The "orderDetails" and "orderItem" beans are populated by Smooks bindings order_item_total=ftl:Order ${orderDetails.orderId} contains an order item for product ${orderItem.productId} with a quantity of ${orderItem.quantity} and a unit price of ${orderItem.price}. This exceeds the permitted per order item total. - Apply this validation to the rules:
<?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:rules="http://www.milyn.org/xsd/smooks/rules-1.0.xsd" xmlns:validation="http://www.milyn.org/xsd/smooks/validation-1.0.xsd" xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.2.xsd"> <params> <!-- Generate a ValidationException if we get more than 5 validation failures... --> <param name="validation.maxFails">5</param> </params> <!-- Define the ruleBases that are used by the validation rules... --> <rules:ruleBases> <!-- Field value rules using regex... --> <rules:ruleBase name="customer" src="rules/customer.properties" provider="org.milyn.rules.regex.RegexProvider"/> <rules:ruleBase name="product" src="rules/product.properties" provider="org.milyn.rules.regex.RegexProvider"/> <!-- Order business rules using MVEL expressions... --> <rules:ruleBase name="order" src="rules/order-rules.csv" provider="org.milyn.rules.mvel.MVELProvider"/> </rules:ruleBases> <!-- Capture some data into the bean context - required by the business rule validations... --> <jb:bean beanId="orderDetails" class="java.util.HashMap" createOnElement="header"> <jb:value data="header/*"/> </jb:bean> <jb:bean beanId="orderItem" class="java.util.HashMap" createOnElement="order-item"> <jb:value data="order-item/*"/> </jb:bean> <!-- Target validation rules... --> <validation:rule executeOn="header/username" name="customer.customerId" onFail="ERROR"/> <validation:rule executeOn="email" name="customer.email" onFail="WARN"/> <validation:rule executeOn="order-item/productId" name="product.productId" onFail="ERROR"/> <validation:rule executeOn="order-item" name="order.order_item_total" onFail="ERROR"/> </smooks-resource-list> - Execute from the example's
Mainclass using this code:protected static ValidationResult runSmooks(final String messageIn) throws IOException, SAXException, SmooksException { // Instantiate Smooks with the config... final Smooks smooks = new Smooks("smooks-config.xml"); try { // Create an exec context - no profiles.... final ExecutionContext executionContext = smooks.createExecutionContext(); final ValidationResult validationResult = new ValidationResult(); // Configure the execution context to generate a report... executionContext.setEventListener(new HtmlReportGenerator("target/report/report.html")); // Filter the input message... smooks.filterSource(executionContext, new StringSource(messageIn), validationResult); return validationResult; } finally { smooks.close(); } }

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.