View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  
16  package org.esigate.tags;
17  
18  import java.io.IOException;
19  import java.io.Writer;
20  import java.util.Map;
21  import java.util.regex.Pattern;
22  
23  import org.esigate.HttpErrorPage;
24  import org.esigate.Renderer;
25  import org.esigate.impl.DriverRequest;
26  import org.esigate.parser.Parser;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Template renderer.
32   * <p>
33   * Retrieves a template from the provider application and renders it to the writer replacing the parameters with the
34   * given map. If <code>name</code> param is <code>null</code>, the whole page will be used as the template.<br>
35   * eg: The template <code>mytemplate</code> can be delimited in the provider page by comments
36   * <code>&lt;!--$begintemplate$mytemplate$--&gt;</code> and <code>&lt;!--$endtemplate$mytemplate$--&gt;</code>.<br>
37   * Inside the template, the parameters can be defined by comments.<br>
38   * eg: parameter named <code>myparam</code> should be delimited by comments
39   * <code>&lt;!--$beginparam$myparam$--&gt;</code> and <code>&lt;!--$endparam$myparam$--&gt;</code>
40   * 
41   * @author Stanislav Bernatskyi
42   * @author Francois-Xavier Bonnet
43   */
44  public class TemplateRenderer implements Renderer, Appendable {
45      private static final Logger LOG = LoggerFactory.getLogger(TemplateRenderer.class);
46      private static final Pattern PATTERN = Pattern.compile("<!--\\$[^>]*\\$-->");
47  
48      private final Parser parser = new Parser(PATTERN, TemplateElement.TYPE, ParamElement.TYPE);
49      private final String page;
50      private final String name;
51      private final Map<String, String> params;
52      private boolean write;
53      private Writer out;
54  
55      public TemplateRenderer(String name, Map<String, String> params, String page) {
56          this.name = name;
57          this.params = params;
58          this.page = page;
59          if (name == null) {
60              write = true;
61          }
62      }
63  
64      /** {@inheritDoc} */
65      @Override
66      public void render(DriverRequest httpRequest, String content, Writer pOut) throws IOException, HttpErrorPage {
67          LOG.debug("Rendering block " + name + " in page " + page);
68          this.out = pOut;
69          if (content == null) {
70              if (params != null) {
71                  for (String value : params.values()) {
72                      out.write(value);
73                  }
74              }
75          } else {
76              parser.parse(content, this);
77          }
78      }
79  
80      public String getName() {
81          return name;
82      }
83  
84      public void setWrite(boolean write) {
85          this.write = write;
86      }
87  
88      public String getParam(String pName) {
89          return params.get(pName);
90      }
91  
92      @Override
93      public Appendable append(CharSequence csq) throws IOException {
94          if (write) {
95              out.append(csq);
96          }
97          return this;
98      }
99  
100     @Override
101     public Appendable append(char c) throws IOException {
102         if (write) {
103             out.append(c);
104         }
105         return this;
106     }
107 
108     @Override
109     public Appendable append(CharSequence csq, int start, int end) throws IOException {
110         if (write) {
111             out.append(csq, start, end);
112         }
113         return this;
114     }
115 
116 }