View Javadoc

1   /*
2      Copyright 2009 jtpl.sourceforge.net
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15   */
16  package net.sf.jtpl;
17  
18  import java.io.File;
19  import java.io.FileNotFoundException;
20  import java.io.FileReader;
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.io.StringReader;
24  import java.lang.reflect.InvocationTargetException;
25  import java.lang.reflect.Method;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.Map;
29  import java.util.regex.Matcher;
30  import java.util.regex.Pattern;
31  
32  import net.sourceforge.jtpl.Jtpl;
33  
34  /**
35   * The forward compatible template engine interface,
36   * replacing {@link Jtpl}.
37   */
38  public class Template {
39  
40  	private Jtpl t;
41  	
42  	public Template(Reader template) throws IOException {
43  		t = new Jtpl(template);
44  	}
45  
46  	public Template(String templateSource) {
47  		try {
48  			t = new Jtpl(new StringReader(templateSource)); 
49  		} catch (IOException e) {
50  			throw new RuntimeException(e); // should be impossible with StringReader
51  		}
52  	}
53  	
54  	public Template(File templateFile) throws FileNotFoundException {
55  		FileReader r = new FileReader(templateFile);
56  		try {
57  			t = new Jtpl(templateFile);
58  		} catch (IOException e) {
59  			try {
60  				r.close();
61  			} catch (Exception nothingToDoAboutIt) {}
62  		}
63  	}
64  	
65  	// proxying methods from Jtpl1
66  
67  	public Template assign(String varName, String varData) {
68  		t.assign(varName, varData);
69  		return this;
70  	}
71  
72  	public Template parse(String blockName) throws IllegalArgumentException {
73  		t.parse(blockName);
74  		return this;
75  	}	
76  	
77  	public String out() {
78  		return t.out();
79  	}
80  	
81  	// bean properties support
82  	
83  	public Template parse(String blockName, Object bean) {
84  		assignAll(bean);
85  		return parse(blockName);
86  	}
87  	
88  	public String out(Object bean) {
89  		assignAll(bean);
90  		return out();
91  	}
92  	
93  	protected void assignAll(Object bean) {
94  		Map p = getBeanProperties(bean);
95  		Iterator i = p.keySet().iterator();
96  		while (i.hasNext()) {
97  			String key = (String) i.next(); 
98  			assign(key.toUpperCase(), "" + p.get(key));
99  		}
100 	}
101 	
102 	/**
103 	 * @param bean The instance that has properties named according to JavaBean standard.
104 	 * @return Map<String, Object> that should be considered immutable
105 	 */
106 	protected Map getBeanProperties(Object bean) {
107 		HashMap values = new HashMap();
108 		if (bean == null) return values;
109 		Method[] m = bean.getClass().getMethods();
110 		
111 		Pattern p = Pattern.compile("get([A-Z]\\w+)");
112 		
113 		for (int i = 0; i < m.length; i++) {
114 			if (m[i].getName().equals("getClass")) continue;
115 			if (m[i].getParameterTypes().length > 0) continue;
116 			Matcher r = p.matcher(m[i].getName());
117 			if (r.matches()) {
118 				try {
119 					values.put(r.group(1).toLowerCase(), m[i].invoke(bean, new Object[0]));
120 				} catch (IllegalArgumentException e) {
121 					throw e;
122 				} catch (IllegalAccessException e) {
123 					throw new RuntimeException(e);
124 				} catch (InvocationTargetException e) {
125 					throw new RuntimeException(e);
126 				}
127 			}
128 		}
129 		return values;
130 	}
131 	
132 }