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

org.nuiton.eugene.models.state.StateModelReader 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.models.state;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import org.apache.commons.digester.Digester;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuiton.eugene.ModelReader;
import org.nuiton.eugene.models.state.StateModel;
import org.nuiton.eugene.models.state.xml.DigesterStateModelRuleSet;
import org.nuiton.eugene.models.state.xml.StateModelImpl;
import org.nuiton.util.FileUtil;
import org.nuiton.util.RecursiveProperties;
import org.xml.sax.SAXException;

/**
 * To read state model files into a memory state model.
 *
 * Created: 26 oct. 2009
 *
 * @author fdesbois
 * @version $Revision: 777 $
 *
 * Mise a jour: $Date: 2009-12-20 17:27:20 +0100 (dim., 20 déc. 2009) $
 * par : $Author: tchemit $
 *
 * @plexus.component role="org.nuiton.eugene.ModelReader" role-hint="statemodel"
 */
public class StateModelReader extends ModelReader {

    private static final Log log = LogFactory.getLog(StateModelReader.class);

    @Override
    public StateModel read(File[] files) {
        Digester digester = new Digester();
        digester.addRuleSet(new DigesterStateModelRuleSet());

        StateModelImpl stateModel = new StateModelImpl();

        // process each file
        for (File file : files) {

            // fin a deplacer
            try {
                digester.push(stateModel);
                digester.parse(file);

                // try to load property file
                loadPropertyFile(file, stateModel);
            } catch (IOException e) {
                log.warn("Can't read model file", e);
            } catch (SAXException e) {
                log.warn("Can't read model file", e);
            }
        }
        return stateModel;
    }

    /**
     * Try to load property file, associated to current statemodel file
     *
     * @param stateModelFile
     * @param stateModel
     */
    protected void loadPropertyFile(File stateModelFile,
            StateModelImpl stateModel) {
        // recherche et charge le fichier propriete associe au modele
        File dir = stateModelFile.getParentFile();
        String ext = FileUtil.extension(stateModelFile);
        String name = FileUtil.basename(stateModelFile, "." + ext);
        File propFile = new File(dir, name + ".properties");
        RecursiveProperties prop = new RecursiveProperties();

        if (!propFile.exists()) {
            if (log.isInfoEnabled()) {
                log.info("No property file associated to model : " + propFile);
            }
        } else {
            if (log.isInfoEnabled()) {
                log.info("Reading model property file " + propFile);
            }
            try {
                prop.load(new FileInputStream(propFile));
            } catch (IOException e) {
                log.warn("Cannot read property file " + propFile, e);
            }

            // on ajoute les proprietes du fichier associe au model
            for (Enumeration e = prop.keys(); e.hasMoreElements();) {
                String key = (String) e.nextElement();
                String value = prop.getProperty(key);

                if (!key.startsWith("model.tagvalue.")) {
                    log.warn("only tagvalue is allowed on model in properties");
                } else {
                    String tag = key.substring("model.tagvalue.".length());
                    stateModel.addTagValue(tag, value);
                }
            }
        }
    }
}