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.aggregator;
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
27 /**
28 * Parses a page to find tags to be replaced by contents from other providers.
29 *
30 * Sample syntax used for includes :
31 * <ul>
32 * <li>
33 * <!--$includeblock$provider$page$blockname$--><!--$endincludeblock$ --></li>
34 * <li><!--$includetemplate$provider$page$templatename$--><!-- $endincludetemplate$--></li>
35 * <li><!--$beginput$name$--><!--$endput$--></li>
36 * </ul>
37 *
38 * Sample syntax used inside included contents for template and block definition:
39 * <ul>
40 * <li><!--$beginblock$name$--></li>
41 * <li><!--$begintemplate$name$--></li>
42 * <li><!--$beginparam$name$--></li>
43 * </ul>
44 *
45 * @author Stanislav Bernatskyi
46 * @author Francois-Xavier Bonnet
47 */
48 public class AggregateRenderer implements Renderer, Appendable {
49 /** Generic pattern for all the tags we want to look for. */
50 private static final Pattern PATTERN = Pattern.compile("<!--\\$[^>]*\\$-->");
51
52 private final Parser parser = new Parser(PATTERN, IncludeBlockElement.TYPE, IncludeTemplateElement.TYPE,
53 PutElement.TYPE);
54 private Writer out;
55
56 /** {@inheritDoc} */
57 @Override
58 public void render(DriverRequest httpRequest, String content, Writer outWriter) throws IOException, HttpErrorPage {
59 this.out = outWriter;
60 if (content == null) {
61 return;
62 }
63 parser.setHttpRequest(httpRequest);
64 parser.parse(content, this);
65 }
66
67 @Override
68 public Appendable append(CharSequence csq) throws IOException {
69 out.append(csq);
70 return this;
71 }
72
73 @Override
74 public Appendable append(char c) throws IOException {
75 out.append(c);
76 return this;
77 }
78
79 @Override
80 public Appendable append(CharSequence csq, int start, int end) throws IOException {
81 out.append(csq, start, end);
82 return this;
83 }
84
85 }