org.agiso.tempel.Tempel.java Source code

Java tutorial

Introduction

Here is the source code for org.agiso.tempel.Tempel.java

Source

/* org.agiso.tempel.Tempel (14-09-2012)
 * 
 * Tempel.java
 * 
 * Copyright 2012 agiso.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.agiso.tempel;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.TreeMap;

import org.agiso.core.i18n.annotation.I18n;
import org.agiso.core.i18n.util.I18nUtils.I18nId;
import org.agiso.core.logging.I18nLogger;
import org.agiso.core.logging.util.LogUtils;
import org.agiso.tempel.api.internal.IExpressionEvaluator;
import org.agiso.tempel.api.internal.IParamReader;
import org.agiso.tempel.api.internal.ITemplateExecutor;
import org.agiso.tempel.api.internal.ITemplateProvider;
import org.apache.commons.lang.LocaleUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * 
 * @author Karol Kopacz
 * @since 1.0
 */
public class Tempel implements ITempel {
    private static final I18nLogger<Logs> coreLogger = LogUtils.getLogger(LOGGER_CORE);

    private static enum Logs implements I18nId {
        @I18n(def = "Property {0}: {1} <-- {2}")
        LOG_01,
    }

    private final Map<String, Object> systemProperties;

    //   --------------------------------------------------------------------------
    private ITemplateProvider templateProvider;
    private ITemplateExecutor templateExecutor;

    @Autowired
    private IExpressionEvaluator expressionEvaluator;

    //   --------------------------------------------------------------------------
    public Tempel() {
        // Odczytujemy waciwoci systemowe i zapamitujemy je w mapie systemProperties:
        Properties properties = System.getProperties();
        Map<String, Object> map = new HashMap<String, Object>(properties.size());
        for (String key : properties.stringPropertyNames()) {
            map.put(key.replace('.', '_'), properties.getProperty(key));
        }
        systemProperties = Collections.unmodifiableMap(map);
    }

    //   --------------------------------------------------------------------------
    @Autowired
    public void setTemplateProvider(ITemplateProvider templateProvider) {
        this.templateProvider = templateProvider;
    }

    @Autowired
    public void setTemplateExecutor(ITemplateExecutor templateExecutor) {
        this.templateExecutor = templateExecutor;
    }

    //   --------------------------------------------------------------------------
    @Override
    public void setParamReader(IParamReader paramReader) {
        templateExecutor.setParamReader(paramReader);
    }

    /**
     * 
     */
    @Override
    public void startTemplate(String name, Map<String, String> params, String workDir) throws Exception {
        Map<String, Object> properties = new TreeMap<String, Object>();

        // Wypeniamy map properties dodajc do niej parametry poszczeglnych
        // poziomw tak, e parametry wyszego poziomu przykrywaj parametry
        // niszego poziomu (przedefiniowywanie parametrw):

        // Parametry systemowe wywoania maszyny wirtualnej Java (1):
        properties.putAll(systemProperties);
        properties.put("SYSTEM", systemProperties);

        // Parametry ustawie globalnych (2), uytkownika (3) i lokalnych (4):
        templateProvider.initialize(properties);

        // Parametry uruchomieniowe (5):
        properties.putAll(params);

        // W oparciu o parametry uytkownika budujemy parametry uruchomienia:
        properties.putAll(addRuntimeProperties(properties));

        // Po zbudowaniu mapy properties przegldamy j i rozwijamy parametry:
        for (String key : properties.keySet()) {
            Object value = properties.get(key);
            if (value instanceof String) {
                String oldValue = (String) value;
                String newValue = expressionEvaluator.evaluate(oldValue, properties);
                if (!oldValue.equals(newValue)) {
                    coreLogger.debug(Logs.LOG_01, key, oldValue, newValue);
                    properties.put(key, newValue);
                }
            }
        }

        properties = Collections.unmodifiableMap(properties);

        // Po rozwiniciu parametrw inicjalizujemy provider'a szablonw:
        templateProvider.configure(properties);

        // Uruchamianie procesu generacji w oparciu o wskazany szablon:
        templateExecutor.executeTemplate(name, properties, workDir);
    }

    //   --------------------------------------------------------------------------
    //   Obsuga parametrw szablonw.
    //   RP_ - Parametry linii polece wykonania programu. W oparciu o ich wartoci
    //         wyznaczane s parametry TP_ wykorzystywane w szablonach.
    //   UP_ - Parametry konfiguracyjne (uytkownika). Okrelaj sposb wyznaczania
    //         parametrw TP_ wykorzystywanych w szablonach.
    //   TP_ - Parametry szablonw. Przeznaczone do bezporedniego uzycia w treci
    //         szablonw.
    //   --------------------------------------------------------------------------
    public static final String RP_DATE = "date";
    public static final String RP_DATE_FORMAT = "date_format";

    public static final String UP_DATE_LOCALE = "date_locale";
    public static final String UP_DATE_FORMAT_SHORT = "date_format_short";
    public static final String UP_DATE_FORMAT_MEDIUM = "date_format_medium";
    public static final String UP_DATE_FORMAT_LONG = "date_format_long";
    public static final String UP_DATE_FORMAT_FULL = "date_format_full";
    public static final String UP_TIME_ZONE = "time_zone";
    public static final String UP_TIME_FORMAT_SHORT = "time_format_short";
    public static final String UP_TIME_FORMAT_MEDIUM = "time_format_medium";
    public static final String UP_TIME_FORMAT_LONG = "time_format_long";
    public static final String UP_TIME_FORMAT_FULL = "time_format_full";

    public static final String TP_YEAR = "year";
    public static final String TP_MONTH = "month";
    public static final String TP_DAY = "day";
    public static final String TP_DATE_SHORT = "date_short";
    public static final String TP_DATE_MEDIUM = "date_medium";
    public static final String TP_DATE_LONG = "date_long";
    public static final String TP_DATE_FULL = "date_full";
    public static final String TP_TIME_SHORT = "time_short";
    public static final String TP_TIME_MEDIUM = "time_medium";
    public static final String TP_TIME_LONG = "time_long";
    public static final String TP_TIME_FULL = "time_full";

    /**
     * 
     * 
     * @param properties
     * @throws Exception 
     */
    private Map<String, Object> addRuntimeProperties(Map<String, Object> properties) throws Exception {
        Map<String, Object> props = new HashMap<String, Object>();

        // Okrelanie lokalizacji daty/czasu uywanej do wypenienia paramtrw szablonw
        // zawierajcych dat/czas w formatach DateFormat.SHORT, .MEDIUM, .LONG i .FULL:
        Locale date_locale;
        if (properties.containsKey(UP_DATE_LOCALE)) {
            date_locale = LocaleUtils.toLocale((String) properties.get(UP_DATE_LOCALE));
            Locale.setDefault(date_locale);
        } else {
            date_locale = Locale.getDefault();
        }

        TimeZone time_zone;
        if (properties.containsKey(UP_TIME_ZONE)) {
            time_zone = TimeZone.getTimeZone((String) properties.get(UP_DATE_LOCALE));
            TimeZone.setDefault(time_zone);
        } else {
            time_zone = TimeZone.getDefault();
        }

        // Wyznaczanie daty, na podstawie ktrej zostan wypenione parametry szablonw
        // przechowujce dat/czas w formatach DateFormat.SHORT, .MEDIUM, .LONG i .FULL.
        // Odbywa si w oparciu o wartoci parametrw 'date_format' i 'date'. Parametr
        // 'date_format' definiuje format uywany do parsowania acucha reprezentujcego
        // dat okrelon parametrem 'date'. Parametr 'date_format' moe nie by okrelony.
        // W takiej sytuacji uyty jest format DateFormat.LONG aktywnej lokalizacji (tj.
        // systemowej, ktra moe by przedefiniowana przez parametr 'date_locale'), ktry
        // moe by przedefiniowany przez parametry 'date_format_long' i 'time_format_long':
        Calendar calendar = Calendar.getInstance(date_locale);
        if (properties.containsKey(RP_DATE)) {
            String date_string = (String) properties.get(RP_DATE);
            if (properties.containsKey(RP_DATE_FORMAT)) {
                String date_format = (String) properties.get(RP_DATE_FORMAT);
                DateFormat formatter = new SimpleDateFormat(date_format);
                formatter.setTimeZone(time_zone);
                calendar.setTime(formatter.parse(date_string));
            } else if (properties.containsKey(UP_DATE_FORMAT_LONG) && properties.containsKey(UP_TIME_FORMAT_LONG)) {
                // TODO: Zaoenie, e format data-czas jest zoony z acucha daty i czasu rozdzelonych spacj:
                // 'UP_DATE_FORMAT_LONG UP_TIME_FORMAT_LONG'
                DateFormat formatter = new SimpleDateFormat((String) properties.get(UP_DATE_FORMAT_LONG) + " "
                        + (String) properties.get(UP_TIME_FORMAT_LONG), date_locale);
                formatter.setTimeZone(time_zone);
                calendar.setTime(formatter.parse(date_string));
            } else {
                DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG,
                        date_locale);
                formatter.setTimeZone(time_zone);
                calendar.setTime(formatter.parse(date_string));
            }
        }

        // Jeli nie okrelono, wypenianie parametrw przechowujcych poszczeglne
        // skadniki daty, tj. rok, miesic i dzie:
        if (!properties.containsKey(TP_YEAR)) {
            props.put(TP_YEAR, calendar.get(Calendar.YEAR));
        }
        if (!properties.containsKey(TP_MONTH)) {
            props.put(TP_MONTH, calendar.get(Calendar.MONTH));
        }
        if (!properties.containsKey(TP_DAY)) {
            props.put(TP_DAY, calendar.get(Calendar.DAY_OF_MONTH));
        }

        // Jeli nie okrelono, wypenianie parametrw przechowujcych dat i czas w
        // formatach SHORT, MEDIUM, LONG i FULL (na podstawie wyznaczonej lokalizacji):
        Date date = calendar.getTime();
        if (!properties.containsKey(TP_DATE_SHORT)) {
            DateFormat formatter;
            if (properties.containsKey(UP_DATE_FORMAT_SHORT)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_DATE_FORMAT_SHORT), date_locale);
            } else {
                formatter = DateFormat.getDateInstance(DateFormat.SHORT, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_DATE_SHORT, formatter.format(date));
        }
        if (!properties.containsKey(TP_DATE_MEDIUM)) {
            DateFormat formatter;
            if (properties.containsKey(UP_DATE_FORMAT_MEDIUM)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_DATE_FORMAT_MEDIUM), date_locale);
            } else {
                formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_DATE_MEDIUM, formatter.format(date));
        }
        if (!properties.containsKey(TP_DATE_LONG)) {
            DateFormat formatter;
            if (properties.containsKey(UP_DATE_FORMAT_LONG)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_DATE_FORMAT_LONG), date_locale);
            } else {
                formatter = DateFormat.getDateInstance(DateFormat.LONG, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_DATE_LONG, formatter.format(date));
        }
        if (!properties.containsKey(TP_DATE_FULL)) {
            DateFormat formatter;
            if (properties.containsKey(UP_DATE_FORMAT_FULL)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_DATE_FORMAT_FULL), date_locale);
            } else {
                formatter = DateFormat.getDateInstance(DateFormat.FULL, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_DATE_FULL, formatter.format(date));
        }

        if (!properties.containsKey(TP_TIME_SHORT)) {
            DateFormat formatter;
            if (properties.containsKey(UP_TIME_FORMAT_SHORT)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_TIME_FORMAT_SHORT), date_locale);
            } else {
                formatter = DateFormat.getTimeInstance(DateFormat.SHORT, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_TIME_SHORT, formatter.format(date));
        }
        if (!properties.containsKey(TP_TIME_MEDIUM)) {
            DateFormat formatter;
            if (properties.containsKey(UP_TIME_FORMAT_MEDIUM)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_TIME_FORMAT_MEDIUM), date_locale);
            } else {
                formatter = DateFormat.getTimeInstance(DateFormat.MEDIUM, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_TIME_MEDIUM, formatter.format(date));
        }
        if (!properties.containsKey(TP_TIME_LONG)) {
            DateFormat formatter;
            if (properties.containsKey(UP_TIME_FORMAT_LONG)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_TIME_FORMAT_LONG), date_locale);
            } else {
                formatter = DateFormat.getTimeInstance(DateFormat.LONG, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_TIME_LONG, formatter.format(date));
        }
        if (!properties.containsKey(TP_TIME_FULL)) {
            DateFormat formatter;
            if (properties.containsKey(UP_TIME_FORMAT_FULL)) {
                formatter = new SimpleDateFormat((String) properties.get(UP_TIME_FORMAT_FULL), date_locale);
            } else {
                formatter = DateFormat.getTimeInstance(DateFormat.FULL, date_locale);
            }
            formatter.setTimeZone(time_zone);
            props.put(TP_TIME_FULL, formatter.format(date));
        }

        return props;
    }
}