View Javadoc
1   package org.esigate.aggregator;
2   
3   import org.esigate.Parameters;
4   import org.esigate.parser.Element;
5   import org.esigate.parser.ElementType;
6   import org.esigate.parser.ParserContext;
7   
8   class PutElement implements Element {
9       public static final ElementType TYPE = new ElementType() {
10  
11          @Override
12          public boolean isStartTag(String tag) {
13              return tag.startsWith("<!--$beginput$");
14          }
15  
16          @Override
17          public boolean isEndTag(String tag) {
18              return tag.startsWith("<!--$endput$");
19          }
20  
21          @Override
22          public Element newInstance() {
23              return new PutElement();
24          }
25  
26          @Override
27          public boolean isSelfClosing(String tag) {
28              return false;
29          }
30  
31      };
32  
33      private IncludeTemplateElement includeTemplateElement;
34      private StringBuilder body = new StringBuilder(Parameters.DEFAULT_BUFFER_SIZE);
35      private String name;
36  
37      @Override
38      public boolean onError(Exception e, ParserContext ctx) {
39          return false;
40      }
41  
42      @Override
43      public void onTagEnd(String tag, ParserContext ctx) {
44          includeTemplateElement.addParam(name, body.toString());
45      }
46  
47      @Override
48      public boolean onTagStart(String tag, ParserContext ctx) {
49          String[] parameters = tag.split("\\$");
50          if (parameters.length != 4) {
51              throw new AggregationSyntaxException("Invalid syntax: " + tag);
52          }
53          name = parameters[2];
54          includeTemplateElement = ctx.findAncestor(IncludeTemplateElement.class);
55          if (includeTemplateElement == null) {
56              throw new AggregationSyntaxException(tag + " should be nested in an includetemplate tag");
57          }
58          return true;
59      }
60  
61      @Override
62      public void characters(CharSequence csq, int start, int end) {
63          body.append(csq, start, end);
64      }
65  
66  }