View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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  
17  package org.gwe.app.client.web.view;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.gwe.utils.IOUtils;
23  
24  /**
25   * @author Marco Ruiz
26   * @since Dec 12, 2008
27   */
28  public class MainVelocityTemplate {
29  
30  	private static final String CONTENT_BOX = "${meta_contentBox}";
31  	
32      public static String readTemplateFile(String templateName) throws IOException {
33  	    templateName = MainVelocityTemplate.class.getPackage().getName().replace('.','/') + "/" + templateName;
34  		InputStream is = MainVelocityTemplate.class.getClassLoader().getResourceAsStream(templateName);
35  		return new String(IOUtils.readStream(is, null).toByteArray());
36      }
37      
38  	private String mainPage = "";
39      private String contentMacros = "";
40      
41      private String layoutWithMacros = "";
42  	
43      public MainVelocityTemplate() throws IOException {
44      	this("main.html", "macros.vm");
45      }
46      
47      public MainVelocityTemplate(String mainTemplate, String contentMacros) throws IOException {
48      	this.mainPage      = readTemplateFile(mainTemplate);
49      	this.contentMacros = readTemplateFile(contentMacros);
50      	this.layoutWithMacros = this.contentMacros + "\n" + mainPage;
51      }
52      
53  	public String createPageTemplate(String templateFileName) throws IOException {
54  	    String template = readTemplateFile(templateFileName);
55  //	    String parsedContent = VelocityUtils.evaluate(contentMacros + "\n" + template);
56  		return layoutWithMacros.replace(CONTENT_BOX, template);
57      }
58  	
59  	public String getMainPage() {
60      	return mainPage;
61      }
62  
63  	public static void main(String[] args) throws IOException {
64  		String page = new MainVelocityTemplate().createPageTemplate("grid.html");
65  		System.out.println(page);
66  	}
67  }