org.milyn.javabean.Value Maven / Gradle / Ivy
The newest version!
/*
Milyn - Copyright (C) 2006 - 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (version 2.1) as published by the Free Software
Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details:
http://www.gnu.org/licenses/lgpl.txt
*/
package org.milyn.javabean;
import org.milyn.assertion.AssertArgument;
import org.milyn.cdr.SmooksResourceConfiguration;
import org.milyn.delivery.VisitorConfigMap;
import org.milyn.javabean.ext.SelectorPropertyResolver;
/**
* Programmatic Value Configurator.
*
* This class can be used to programmatically configure a Smooks instance for creating value
* objects using the Smooks DataDecoders.
*
* This class uses a Fluent API (all methods return the Bean instance), making it easy to
* string configurations together.
*
* Example
* Taking the "classic" Order message as an example and getting the order number and
* name as Value Objects in the form of an Integer and String.
* The Message
*
* <order xmlns="http://x">
* <header>
* <y:date xmlns:y="http://y">Wed Nov 15 13:45:28 EST 2006</y:date>
* <customer number="123123">Joe</customer>
* <privatePerson></privatePerson>
* </header>
* <order-items>
* <!-- .... --!>
* </order-items>
* </order>
*
*
* The Binding Configuration and Execution Code
* The configuration code (Note: Smooks instance defined and instantiated globally):
*
* Smooks smooks = new Smooks();
*
* Value customerNumberValue = new Value( "customerNumber", "customer/@number")
* .setDecoder("Integer");
* Value customerNameValue = new Value( "customerName", "customer")
* .setDefault("Unknown");
*
* smooks.addVisitors(customerNumberValue);
* smooks.addVisitors(customerNameValue);
*
*
* And the execution code:
*
* JavaResult result = new JavaResult();
*
* smooks.filterSource(new StreamSource(orderMessageStream), result);
* Integer customerNumber = (Integer) result.getBean("customerNumber");
* String customerName = (String) result.getBean("customerName");
*
*
* @author [email protected]
* @see Bean
*/
public class Value extends BindingAppender {
private String dataSelector;
private String targetNamespace;
private String defaultValue;
private DataDecoder decoder;
/**
* Create a Value binding configuration.
*
* @param beanId The bean id under which the value will be stored.
* @param data The data selector for the data value to be bound.
*/
public Value(String beanId, String data) {
super(beanId);
AssertArgument.isNotNullAndNotEmpty(beanId, "beanId");
AssertArgument.isNotNullAndNotEmpty(data, "dataSelector");
this.dataSelector = data;
}
/**
* Create a Value binding configuration.
*
* @param beanId The bean id under which the value will be stored.
* @param data The data selector for the data value to be bound.
* @param type Data type.
*/
public Value(String beanId, String data, Class> type) {
this(beanId, data);
AssertArgument.isNotNull(type, "type");
this.decoder = DataDecoder.Factory.create(type);
}
/**
* The namespace for the data selector for the data value to be bound.
*
* @param targetNamespace The namespace
* @return this
Value configuration instance.
*/
public Value setTargetNamespace(String targetNamespace) {
this.targetNamespace = targetNamespace;
return this;
}
/**
* The default value for if the data is null or empty
*
* @param targetNamespace The default value
* @return this
Value configuration instance.
*/
public Value setDefaultValue(String defaultValue) {
this.defaultValue = defaultValue;
return this;
}
/**
* Set the binding value data type.
* @param type The data type.
*
* @return this
Value configuration instance.
*/
public Value setType(Class> type) {
this.decoder = DataDecoder.Factory.create(type);
return this;
}
/**
* The {@link org.milyn.javabean.DataDecoder} to be used for decoding
* the data value.
*
* @param targetNamespace The {@link org.milyn.javabean.DataDecoder}
* @return this
Value configuration instance.
*/
public Value setDecoder(DataDecoder dataDecoder) {
this.decoder = dataDecoder;
return this;
}
/**
* Used by Smooks to retrieve the visitor configuration of this Value Configuration
*/
public void addVisitors(VisitorConfigMap visitorMap) {
ValueBinder binder = new ValueBinder(getBeanId());
SmooksResourceConfiguration populatorConfig = new SmooksResourceConfiguration(dataSelector);
SelectorPropertyResolver.resolveSelectorTokens(populatorConfig);
binder.setDecoder(decoder);
binder.setDefaultValue(defaultValue);
binder.setValueAttributeName(populatorConfig.getStringParameter(BeanInstancePopulator.VALUE_ATTRIBUTE_NAME));
visitorMap.addVisitor(binder, populatorConfig.getSelector(), targetNamespace, true);
}
}