org.milyn.javabean
Class BeanPopulator

java.lang.Object
  extended by org.milyn.javabean.BeanPopulator
All Implemented Interfaces:
ConfigurationExpander, ContentHandler

public class BeanPopulator
extends Object
implements ConfigurationExpander

Javabean Populator.

Sample Configuration

Populate an Order bean with Header and OrderItem beans (OrderItems added to a list).

Input XML

 <order>
     <header>
         <date>Wed Nov 15 13:45:28 EST 2006</date>
         <customer number="123123">Joe</customer>
     </header>
     <order-items>
         <order-item>
             <product>111</product>
             <quantity>2</quantity>
             <price>8.90</price>
         </order-item>
         <order-item>
             <product>222</product>
             <quantity>7</quantity>
             <price>5.20</price>
         </order-item>
     </order-items>
 </order>
 

Target Java Beans

(Not including getters and setters):
 public class Order {
     private Header header;
     private List<OrderItem> orderItems;
 }
 public class Header {
     private Date date;
     private Long customerNumber;
     private String customerName;
 }
 public class OrderItem {
     private long productId;
     private Integer quantity;
     private double price;
 }
 

Smooks Configuration

The following configuration, when applied to the input XML, will create (and populate) the object graph defined by the Target Java Beans using the data in the input XML.
 <?xml version="1.0"?>
 <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd">

     <-- Create the Order bean instance when we encounter the "order" element
            and call it "order"... -->
     <resource-config selector="order">
         <resource>org.milyn.javabean.BeanPopulator</resource>
         <param name="beanId">order</param>
         <param name="beanClass">org.milyn.javabean.Order</param>
         <param name="bindings">
             <binding property="header" selector="${header}" /> <-- Wire the header bean to the header property. See header configuration below... -->
             <binding property="orderItems" selector="${orderItems}" /> <-- Wire the orderItems ArrayList to the orderItems property. See orderItems configuration below... -->             
         </param>
     </resource-config>

     <-- Create a List for the OrderItem instances when we encounter the "order" element.
            Call it "orderItems" and set it on the "order" bean... -->
     <resource-config selector="order">
         <resource>org.milyn.javabean.BeanPopulator</resource>
         <param name="beanId">orderItems</param>
         <param name="beanClass">java.util.ArrayList</param>
         <param name="bindings">
             <binding selector="${orderItem}" /> <-- Wire the orderItem to this ArrayList. See order-item configuration below... -->
         </param>
     </resource-config>

     <-- Create the Header bean instance when we encounter the "header" element.
            Call it "header" -->
     <resource-config selector="header">
         <resource>org.milyn.javabean.BeanPopulator</resource>
         <param name="beanId">header</param>
         <param name="beanClass">org.milyn.javabean.Header</param>
         <param name="bindings">
             <-- Header bindings... -->
             <binding property="date" type="OrderDateLong" selector="header/date" /> <-- See OrderDateLong decoder definition below... -->
             <binding property="customerNumber" type="Long" selector="header/customer/@number" />
             <binding property="customerName" selector="header/customer" /> <-- Type defaults to String -->
         </param>
     </resource-config>

     <-- Create OrderItem instances when we encounter the "order-item" element.
            Set them on the "orderItems" bean (List)... -->
     <resource-config selector="order-item">
         <resource>org.milyn.javabean.BeanPopulator</resource>
         <param name="beanClass">org.milyn.javabean.OrderItem</param>
         <param name="bindings">
             <-- OrderItem bindings... -->
             <binding property="productId" type="Long" selector="order-item/product" />
             <binding property="quantity" type="Integer" selector="order-item/quantity" />
             <binding property="price" type="Double" selector="order-item/price" />
         </param>
     </resource-config>

     <-- Explicitly defining a decoder for the date field on the order header so as to define the format... -->
     <resource-config selector="decoder:OrderDateLong">
         <resource>org.milyn.javabean.decoders.DateDecoder</resource>
         <param name="format">EEE MMM dd HH:mm:ss z yyyy</param>
     </resource-config>

 </smooks-resource-list>
 

Smooks Configuration

To trigger this visitor during the Assembly Phase, simply set the "VisitPhase" param to "ASSEMBLY".

Author:
tfennelly, maurice.zeijen@smies.com

Constructor Summary
BeanPopulator()
           
 
Method Summary
 List<SmooksResourceConfiguration> expandConfigurations()
           
 void initialize()
          Set the resource configuration on the bean populator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanPopulator

public BeanPopulator()
Method Detail

initialize

@Initialize
public void initialize()
                throws SmooksConfigurationException
Set the resource configuration on the bean populator.

Throws:
SmooksConfigurationException - Incorrectly configured resource.

expandConfigurations

public List<SmooksResourceConfiguration> expandConfigurations()
                                                       throws SmooksConfigurationException
Specified by:
expandConfigurations in interface ConfigurationExpander
Throws:
SmooksConfigurationException


Copyright © 2008. All Rights Reserved.