final SourceElement rootElement,
final List<SourceTransformerDefinition> transformerDefinitions,
final ControllerState controllerState)
throws SourceTransformerException, SourceException
{
SourceElement result = rootElement;
for (SourceTransformerDefinition transformerDefinition
: transformerDefinitions)
{
SourceTransformer sourceTransformer
= transformerDefinition.getSourceTransformer();
String elements = transformerDefinition.getElements();
log.debug("Applying source transformer "
+ sourceTransformer.getClass().getName()
+ (elements == null
? " to the root element"
: " to the elements " + elements));
List<SourceElement> toTransform
= SourcePath.getElementsFromRoot(rootElement, elements);
if (toTransform.isEmpty())
{
log.debug("No element found, nothing transformed");
}
for (SourceElement sourceElement : toTransform)
{
log.debug("transforming element " + sourceElement);
SourceElement transformedElement = sourceTransformer.transform(
sourceElement,
controllerState);
if (transformedElement == null)
{
throw new SourceTransformerException("Transformer "
+ sourceTransformer.getClass().getName()
+ " returned null for element "
+ sourceElement.getName());
}
SourceElement parent = sourceElement.getParent();
if (parent == null)
{
result = transformedElement;
}
else
{
List<SourceElement> children = parent.getChildren();
int index = children.indexOf(sourceElement);
children.set(index, transformedElement);
}
}
log.debug("Transformation ended");