Class BeanPopulator

  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

         <date>Wed Nov 15 13:45:28 EST 2006</date>
         <customer number="123123">Joe</customer>

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="">

     <-- Create the Order bean instance when we encounter the "order" element
            and call it "order"... -->
     <resource-config selector="order">
         <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... -->             

     <-- 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">
         <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... -->

     <-- Create the Header bean instance when we encounter the "header" element.
            Call it "header" -->
     <resource-config selector="header">
         <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 -->

     <-- Create OrderItem instances when we encounter the "order-item" element.
            Set them on the "orderItems" bean (List)... -->
     <resource-config selector="order-item">
         <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" />

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


Smooks Configuration

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


Constructor Summary
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


public BeanPopulator()
Method Detail


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

SmooksConfigurationException - Incorrectly configured resource.


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

Copyright © 2008. All Rights Reserved.