All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.nuiton.eugene.Transformer Maven / Gradle / Ivy

/*
 * *##% 
 * EUGene :: EUGene
 * Copyright (C) 2004 - 2009 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
 * .
 * ##%*
 */

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
 * @version $Revision: 799 $
 *
 * Mise a jour: $Date: 2010-01-17 21:55:45 +0100 (dim., 17 janv. 2010) $
 * par : $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() {
        super();
    }

    /**
     * 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; this.previousTransformer = initPreviousTransformer(); this.outputTemplate = initOutputTemplate(); this.outputTemplate.setProperties(getOutputProperties()); this.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 this.outputTemplate; } public O getOutputModel() { return this.outputModel; } /** * Old method from previous EUGene 1.0.1 version. Used before creation of ModelReader. * * @param file * @param destDir * @deprecated no need to manage list of files in input, prefer use input model. */ @Override @Deprecated public void generate(File[] file, File destDir) { this.outputTemplate = initOutputTemplate(); this.outputTemplate.setProperties(getOutputProperties()); getOutputTemplate().generate(file, destDir); } /** * 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(org.nuiton.eugene.models.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(); }