org.nuiton.eugene.Transformer Maven / Gradle / Ivy
/*
* #%L
* EUGene :: EUGene
*
* $Id: Transformer.java 906 2010-05-16 12:19:55Z tchemit $
* $HeadURL: http://svn.nuiton.org/svn/eugene/tags/eugene-2.0.2/eugene/src/main/java/org/nuiton/eugene/Transformer.java $
* %%
* Copyright (C) 2004 - 2010 CodeLutin
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.eugene;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import org.nuiton.eugene.models.Model;
/**
* Transformer
*
* Created: 28 oct. 2009
*
* @param input model to transform
* @param output model transformed
* @author fdesbois
*/
public abstract class Transformer
extends Template {
/**
* Output generator, to generate files from Output model.
*
* Could be an other Transformer.
*/
protected Template outputTemplate;
/**
* Previous transformer to modify input model
*/
protected Transformer previousTransformer;
/**
* Output model.
*/
protected O outputModel;
/**
* Properties for output generator.
*/
protected Properties outputProperties;
public Transformer() {
}
/**
* Initialization of the Transformer :
*
* - initOutputModel : default, do nothing
* - initOutputGenerator : must be override to instanciate output Generator
* - initOutputModel : must be override to instanciate output Model
*
*
* @param model input model
* @since 2.0.0
*/
protected void init(I model) {
this.model = model;
previousTransformer = initPreviousTransformer();
outputTemplate = initOutputTemplate();
// build output template configuration
// merge input configuration + output properties
Properties properties = getConfiguration().getProperties();
properties.putAll(getOutputProperties());
TemplateConfiguration outconfig =
new DefaultTemplateConfiguration(properties);
// push back to outputPropertie the all configuration
//FIXME tchemit 20100516 Should rethink this...
outputProperties = properties;
outputTemplate.setConfiguration(outconfig);
outputModel = initOutputModel();
}
protected Transformer initPreviousTransformer() {
return null;
}
/**
* Initialization of the Output generator. Must be override to instanciate
* the Generator that will make the generation of the Output model.
*
* @return the output generator
* @since 2.0.0
*/
protected abstract Template initOutputTemplate();
/**
* Initialization of the Output model. Must be override to instanciate
* and initialize the output model.
*
* For ObjectModel you can use, ObjectModelBuilder to build easily an
* empty ObjectModel.
*
* @return the output model
* @since 2.0.0
*/
protected abstract O initOutputModel();
@Override
public void setProperties(Properties outputProperties) {
this.outputProperties = outputProperties;
}
/**
* Output properties for output generator. Initially set from build configuration.
*
* @return output properties
* @since 2.0.0
*/
protected Properties getOutputProperties() {
if (outputProperties == null) {
outputProperties = new Properties();
}
return outputProperties;
}
public Template getOutputTemplate() {
return outputTemplate;
}
public O getOutputModel() {
return outputModel;
}
/**
* This method apply the current transformation. You can use an other
* transformer for previous transformation by overriding
* {@link #initPreviousTransformer} method. In this case, the current
* transformation will be apply on the output model of the previous one.
* In the simple other case, the current transformation transform the
* input model into an output model. You must override
* {@link #initOutputModel} and {@link #initOutputTemplate } methods
* to initialize output model and output template to apply (generally a
* generator).
*
* @param model input model to transform and generate
* @param destDir destination directory to put generated files
* @throws IOException
* @since 2.0.0
*/
@Override
public void applyTemplate(I model, File destDir) throws IOException {
// previous transformation
if (previousTransformer != null) {
// transformation only, no application of next template
previousTransformer.transform(model);
// current transformation
transform(previousTransformer.getOutputModel());
} else {
// current transformation
transform(model);
}
// after template application
getOutputTemplate().applyTemplate(getOutputModel(), destDir);
}
/**
* Transformation method from an input Model. This method also initialize
* previous transformer, output model and output generator with
* ${@link #init(Model) } method.
*
* @param model input model
* @since 2.0.0
*/
public void transform(I model) {
init(model);
transform();
}
/**
* Method to override for the transformation. Initialization
* (PreviousTransformer, OutputModel, OutputGenerator) is made before
* transformation call. This method only transform an input model into an
* output model.
*
* No generation is done neither outputGenerator call.
*
* @since 2.0.0
*/
protected abstract void transform();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy