Thursday, December 10, 2015

Easy binding template using regExp in java

this is what easy way to just data binding with regExpression

my template

java

/**
 * Copyright 2009 by  Corp.,
 * All rights reserved.
 * 
 */
package hanwha.neo.modules.mobileapp.util;
import hanwha.neo.commons.IOUtils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * @author : ddavid
 * @version $Id: HtmlCovertToString.java,v 1.1.2.1 2015/12/11 15:01:53 Exp $
 */
public class HtmlCovertToString {
    
    
    public static String getConvertedString(String path, HashMap<StringString> map){
        return getChangedStr(getFileString(path), map);
    }
    public static String getFileString(String path) {
        FileInputStream fi = null;
        String str = "";
        try {
            fi = new FileInputStream(path);
            str = IOUtils.getString(fi);            
            System.out.println(str);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            org.apache.commons.io.IOUtils.closeQuietly(fi);
        }
        return str;
    }
    public static String getChangedStr(String str, HashMap<StringString> map) {
        StringBuffer output = new StringBuffer();
        Pattern pattern = Pattern.compile("\\{\\{[a-zA-Z0-0]+\\}\\}",
                Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(str);
        // using Matcher find(), group(), start() and end() methods
        int currentIndex = 0;
        while (matcher.find()) {
            String match = matcher.group();
            match = match.substring(2, match.length() - 2); // remove {{ }}
            output.append(str.substring(currentIndex, matcher.start()));
            if (map.containsKey(match)) {
                output.append(map.get(match));
            } else {
                output.append(matcher.group());
            }
            currentIndex = matcher.end();
            /*
             * System.out.println("Found the text " + matcher.group() +
             * " starting at " + matcher.start() + " index and ending at index "
             * + matcher.end());
             */
        }
        output.append(str.substring(currentIndex, str.length()));
        return output.toString();
    }
}
cs

and html file is

1
2
3
4
5
6
7
8
<html>
    <head></head>
<body>
    <p>    this is very good template  name {{name}} and age {{age}}
    </p>
</body>
</html>
cs

where to call is

1
2
3
4
5
6
HashMap<StringString> map = new HashMap<StringString>();
        
map.put("name""david");
map.put("age""15");
            
body = HtmlCovertToString.getConvertedString("testhtml", map);
cs

this is output
1
2
3
4
5
6
7
<html>
    <head></head>
<body>
    <p>    this is very good template  name david and age 15    
    </p>
</body>
</html>
cs

No comments:

Post a Comment