Thursday, September 23, 2010

java property loading and template substitution

public class PropertyLoader {
    private Properties properties;
    public PropertyLoader() {
        properties = loadProperties("input.properties");
        substitutePropertiesWithinProperties( properties );
    }
   
    public static void main(String[] args) throws IOException {
        PropertyLoader loader = new PropertyLoader();
        String htmlFile = loader.loadHtmlFileAsString("template.html");       
        System.out.println("The substituted HTML is : " + loader.substituteTemplateValues( htmlFile) );       
    }
   
    private String substituteTemplateValues(String htmlFile ) {
        Pattern pattern = Pattern.compile( "\\$\\{\\w+}" );
        Matcher matcher = pattern.matcher( htmlFile );
        while( matcher.find( ) ){//Look for a place holder in prop value.
            String placeHolder = htmlFile.substring( matcher.start(), matcher.end());
            String placeHolderKey = placeHolder.substring( 2, placeHolder.length() - 1 );
            String placeHolderValue = (String)properties.get( placeHolderKey );
            if (placeHolderValue == null){
                throw new IllegalArgumentException("Property " + placeHolder + " cannot be substituted.");               
            }
            htmlFile =  htmlFile.replaceFirst( "\\$\\{\\w+}" , placeHolderValue);
            matcher = pattern.matcher( htmlFile );    //Update the matcher for the next iteration
        }       
           
        return htmlFile;
    }

    private String loadHtmlFileAsString(String filePath) throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        ClassLoader loader = Thread.currentThread ().getContextClassLoader ();       
        BufferedReader reader = new BufferedReader( new InputStreamReader(loader.getResourceAsStream(filePath) ) );
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            fileData.append(buf, 0, numRead);
        }
        reader.close();
        return fileData.toString();
    }   
   
    private void substitutePropertiesWithinProperties( Properties properties) {
        Pattern pattern = Pattern.compile( "\\$\\{\\w+}" );
        for(Object key : properties.keySet()){
            String val = (String) properties.get(key);
            Matcher matcher = pattern.matcher(val);   
            while( matcher.find( ) ){//Look for a place holder in prop value.
                String placeHolder = val.substring( matcher.start(), matcher.end());
                String placeHolderKey = placeHolder.substring( 2, placeHolder.length() - 1 );
                String placeHolderValue = (String) properties.get( placeHolderKey );
                if (placeHolderValue == null){
                    throw new IllegalArgumentException("Property strToReplace cannot be substituted.");               
                }
                val =  val.replaceFirst( "\\$\\{\\w+}" , placeHolderValue);
                properties.put(key, val);    //Update the properties set
                matcher = pattern.matcher(val);    //Update the matcher for the next iteration
            }
        }
    }

    public Properties loadProperties (String name){
        ClassLoader loader = Thread.currentThread ().getContextClassLoader ();
        Properties result = null;       
        InputStream in = null;
        try{                               
            in = loader.getResourceAsStream (name);            // Returns null on lookup failures:
            if (in != null){
                result = new Properties ();
                result.load (in); // Can throw IOException
            }
        }
        catch (Exception e){
            result = null;
        }
        finally{
            if (in != null) try { in.close (); } catch (Throwable ignore) {}
        }
       
        return result;
    }
}

No comments:

Post a Comment