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.regex.Pattern;
21  
22  import org.esigate.HttpErrorPage;
23  import org.esigate.Renderer;
24  import org.esigate.impl.DriverRequest;
25  import org.esigate.parser.Parser;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Block renderer.<br>
31   * 
32   * Extracts data between <code>&lt;!--$beginblock$myblock$--&gt;</code> and <code>&lt;!--$endblock$myblock$--&gt;</code>
33   * separators
34   * 
35   * @author Stanislav Bernatskyi
36   * @author Francois-Xavier Bonnet
37   */
38  public class BlockRenderer implements Renderer, Appendable {
39      private static final Logger LOG = LoggerFactory.getLogger(BlockRenderer.class);
40      private static final Pattern PATTERN = Pattern.compile("<!--\\$[^>]*\\$-->");
41  
42      private final Parser parser = new Parser(PATTERN, BlockElement.TYPE);
43      private final String page;
44      private final String name;
45      private boolean write;
46      private Writer out;
47  
48      public void setWrite(boolean write) {
49          this.write = write;
50      }
51  
52      public String getName() {
53          return name;
54      }
55  
56      public BlockRenderer(String name, String page) {
57          this.name = name;
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              return;
71          }
72          if (name == null) {
73              out.write(content);
74          } else {
75              parser.parse(content, this);
76          }
77      }
78  
79      @Override
80      public Appendable append(CharSequence csq) throws IOException {
81          if (write) {
82              out.append(csq);
83          }
84          return this;
85      }
86  
87      @Override
88      public Appendable append(char c) throws IOException {
89          if (write) {
90              out.append(c);
91          }
92          return this;
93      }
94  
95      @Override
96      public Appendable append(CharSequence csq, int start, int end) throws IOException {
97          if (write) {
98              out.append(csq, start, end);
99          }
100         return this;
101     }
102 
103 }