Subversion Repositories bacoAlunos

Rev

Rev 1306 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package jomm.utils;

import pt.estgp.estgweb.utils.*;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.StringReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.text.MessageFormat;

import org.apache.log4j.Logger;

/**
 * @author Jorge Machado
 * @date 20/Mar/2008
 * @time 18:37:09
 * @see jomm.utils
 */

public class TemplateUtils
{
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(TemplateUtils.class);

    private static 1.5.0/docs/api/java/util/HashMap.html">HashMap templates = null;


    public static 1.5.0/docs/api/java/lang/String.html">String formatMessageTemplate(1.5.0/docs/api/java/lang/String.html">String template, 1.5.0/docs/api/java/lang/String.html">String[] args)
    {
        1.5.0/docs/api/java/lang/String.html">String content = TemplateUtils.getOrLoadTemplate(template);
        return 1.5.0/docs/api/java/text/MessageFormat.html">MessageFormat.format(content, args);
    }
    /**
     * Check if file content is already in Cache
     * See:
     *
     * #getNoCommentText method
     * @param  file to get from class loader
     * @return  file content
     */

    public static synchronized 1.5.0/docs/api/java/lang/String.html">String getOrLoadTemplate(1.5.0/docs/api/java/lang/String.html">String file)
    {
        if(templates == null)
            templates = new 1.5.0/docs/api/java/util/HashMap.html">HashMap();

        1.5.0/docs/api/java/lang/String.html">String temp = (1.5.0/docs/api/java/lang/String.html">String) templates.get(file);
        if(temp == null)
        {
            try{
                temp = getNoCommentText(file);
                //temp = temp.
                templates.put(file, temp);
            }
            catch(1.5.0/docs/api/java/lang/Exception.html">Exception e)
            {
                logger.fatal(e);
                return null;
            }
        }
        return temp;

    }

    /**
     * Reads a file from class loader and get all text in UTF-8 ignoring lines starting with #
     * @param file to read
     * @return a String with file content
     * @throws IOException on error opening file
     */

    public static 1.5.0/docs/api/java/lang/String.html">String getNoCommentText(1.5.0/docs/api/java/lang/String.html">String file) throws 1.5.0/docs/api/java/io/IOException.html">IOException
    {
        // Read in template
        1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader reader = new 1.5.0/docs/api/java/io/BufferedReader.html">BufferedReader(new 1.5.0/docs/api/java/io/InputStreamReader.html">InputStreamReader(TemplateUtils.class.getResourceAsStream(file),"UTF-8"));
        1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer contentBuffer = new 1.5.0/docs/api/java/lang/StringBuffer.html">StringBuffer();

        boolean more = true;

        while (more)
        {
            1.5.0/docs/api/java/lang/String.html">String line = reader.readLine();

            if (line==null)
            {
                more = false;
            }
            else if(!line.startsWith("#"))
            {
                // Add non-comment lines to the content
                contentBuffer.append(line);
                contentBuffer.append("\n");
            }
        }
        return contentBuffer.toString();
    }
}