net.officefloor.web.template.parse.WebTemplateParser Maven / Gradle / Ivy
/* Generated By:JavaCC: Do not edit this line. WebTemplateParser.java */
package net.officefloor.web.template.parse;
import java.util.List;
import java.util.LinkedList;
import java.io.IOException;
import java.io.Reader;
/**
* Web template parser.
*
* @author Daniel Sagenschneider
* @author javacc (many thanks to this wonderful tool)
*/
public class WebTemplateParser implements WebTemplateParserConstants {
/**
* Default name for the first {@link ParsedTemplateSection}.
*/
public static final String DEFAULT_FIRST_SECTION_NAME = "template";
/**
* Listing of {@link ParsedTemplateSection} instances.
*/
private List < ParsedTemplateSection > sections = new LinkedList < ParsedTemplateSection > ();
/**
* Current {@link ParsedTemplateSection} name.
*/
private String currentSectionName = null;
/**
* {@link NodeParsedTemplateSectionContent} for the {@link ParsedTemplateSection}.
*/
private NodeParsedTemplateSectionContent currentSectionContents = new NodeParsedTemplateSectionContent("SECTION_BEAN", null);
/**
* {@link NodeParsedTemplateSectionContent} for the current bean.
*/
private NodeParsedTemplateSectionContent currentBeanContents = this.currentSectionContents;
/**
* Current static HTML content.
*/
private StringBuilder currentStaticContent = new StringBuilder();
/**
* Current raw section content.
*/
private StringBuilder currentRawSectionContent = new StringBuilder();
/**
* Parses the input.
*
* @param content Template content.
*/
public static ParsedTemplate parse(Reader templateContent) throws IOException
{
// Create the parser
WebTemplateParser parser = new WebTemplateParser(templateContent);
try
{
// Parse the template
parser.parseTemplate();
// Complete the last section
parser.completeCurrentSection();
}
catch (ParseException ex)
{
// Failed to parse template
throw new IOException(ex);
}
// Return the template
return new ParsedTemplate(parser.sections.toArray(new ParsedTemplateSection [parser.sections.size()]));
}
/**
* Adds a {@link ParsedTemplateSection}.
*/
private void addSection(String sectionName)
{
// Complete the current section
this.completeCurrentSection();
// Parse out the comment, white spacing, brackets
sectionName = sectionName.substring("".length()));
sectionName = sectionName.trim();
sectionName = sectionName.substring("{".length(), (sectionName.length() - "}".length()));
// Specify the new section name
this.currentSectionName = sectionName;
}
/**
* Adds a {@link BeanParsedTemplateSectionContent} that is within a comment.
*/
public void addCommentBean(String beanName)
{
// Add the raw content
this.currentRawSectionContent.append(beanName);
// Parse out comment and white spacing
beanName = beanName.substring("".length()));
beanName = beanName.trim();
// Add the bean
this.addBean(beanName, true);
}
/**
* Adds a {@link BeanParsedTemplateSectionContent}.
*/
private void addBean(String beanName, boolean isWithinComment)
{
// Add the raw content (if not within comment)
if (!isWithinComment)
{
this.currentRawSectionContent.append(beanName);
}
// Complete static content
this.completeStaticContent();
// Parse out bean bracket, white spacing
beanName = beanName.substring("${".length());
beanName = beanName.trim();
// Create the node content for the bean
NodeParsedTemplateSectionContent beanContent = new NodeParsedTemplateSectionContent(beanName, this.currentBeanContents);
// Add content and set as current bean
this.currentBeanContents.addContent(beanContent);
this.currentBeanContents = beanContent;
}
/**
* Completes the current {@link BeanParsedTemplateSectionContent}.
*/
private void completeBean(Token token) throws ParseException
{
// Add the raw content
this.currentRawSectionContent.append(token);
// Complete static content
this.completeStaticContent();
// Move to parent bean
this.currentBeanContents = this.currentBeanContents.getParent();
// Ensure have parent
if (this.currentBeanContents == null)
{
throw new ParseException("No open Bean to close at line " + token.beginLine + " column " + token.beginColumn);
}
}
/**
* Adds a {@link PropertyParsedTemplateSectionContent}.
*/
private void addProperty(String propertyName)
{
// Add the raw content
this.currentRawSectionContent.append(propertyName);
// Complete static content
this.completeStaticContent();
// Parse out property brackets
propertyName = propertyName.substring("${".length(), (propertyName.length() - "}".length()));
// Add the property
this.currentBeanContents.addContent(new NodeParsedTemplateSectionContent(new PropertyParsedTemplateSectionContent(propertyName)));
}
/**
* Adds a {@link LinkParsedTemplateSectionContent}.
*/
private void addLink(String linkName)
{
// Add the raw content
this.currentRawSectionContent.append(linkName);
// Complete static content
this.completeStaticContent();
// Parse out link brackets
linkName = linkName.substring("#{".length(), (linkName.length() - "}".length()));
// Add the link
this.currentBeanContents.addContent(new NodeParsedTemplateSectionContent(new LinkParsedTemplateSectionContent(linkName)));
}
/**
* Adds the static content for a {@link StaticParsedTemplateSectionContent}.
*/
private void addStaticContent(String staticContent)
{
// Add the raw content
this.currentRawSectionContent.append(staticContent);
// Add the static content
this.currentStaticContent.append(staticContent);
}
/**
* Completes current {@link StaticParsedTemplateSectionContent}.
*/
private void completeStaticContent()
{
// Must have static content
String content = this.currentStaticContent.toString();
if (content.length() == 0)
{
return; // no static content
}
// Append the static content
this.currentBeanContents.addContent(new NodeParsedTemplateSectionContent(new StaticParsedTemplateSectionContent(content)));
// Clear for next static content
this.currentStaticContent = new StringBuilder();
}
/**
* Completes current {@link ParsedTemplateSection}.
*/
private void completeCurrentSection()
{
// Ensure content complete
this.completeStaticContent();
// Must have content for section
if (!this.currentSectionContents.isContent())
{
return; // no contents, so do not include section
}
// Obtain the current section name
String sectionName = (this.currentSectionName == null ? DEFAULT_FIRST_SECTION_NAME : this.currentSectionName);
// Obtain the contents and reset for next section
BeanParsedTemplateSectionContent sectionBean = (BeanParsedTemplateSectionContent) this.currentSectionContents.getParsedTemplateSectionContent();
ParsedTemplateSectionContent [] sectionContents = sectionBean.getContent();
this.currentSectionContents = new NodeParsedTemplateSectionContent("SECTION_BEAN", null);
this.currentBeanContents = this.currentSectionContents;
// Create and add the section
ParsedTemplateSection section = new ParsedTemplateSection(sectionName, this.currentRawSectionContent.toString(), sectionContents);
this.sections.add(section);
// Reset for next section raw content
this.currentRawSectionContent = new StringBuilder();
}
/**
* {@link ParsedTemplateSectionContent} node within the parsing of the {@link ParsedTemplateSection}.
*/
private static class NodeParsedTemplateSectionContent implements ParsedTemplateSectionContent
{
/**
* Property name to obtain the bean.
*/
private final String beanName;
/**
* {@link ParsedTemplateSectionContent} instances for the bean.
*/
private final List < NodeParsedTemplateSectionContent > contents;
/**
* Parent {@link NodeParsedTemplateSectionContent}.
*/
private final NodeParsedTemplateSectionContent parent;
/**
* Delegate {@link ParsedTemplateSectionContent}.
*/
private final ParsedTemplateSectionContent delegate;
/**
* Initiate to be {@link BeanParsedTemplateSectionContent}.
*/
public NodeParsedTemplateSectionContent(String beanName, NodeParsedTemplateSectionContent parent)
{
this.beanName = beanName;
this.contents = new LinkedList < NodeParsedTemplateSectionContent > ();
this.parent = parent;
this.delegate = null;
}
/**
* Initiate with delegate {@link ParsedTemplateSectionContent}.
*/
public NodeParsedTemplateSectionContent(ParsedTemplateSectionContent delegate)
{
this.beanName = null;
this.contents = null;
this.parent = null;
this.delegate = delegate;
}
/**
* Adds a {@link NodeParsedTemplateSectionContent}.
*/
public void addContent(NodeParsedTemplateSectionContent content)
{
this.contents.add(content);
}
/**
* Indicates if contains a {@link NodeParsedTemplateSectionContent}.
*/
public boolean isContent()
{
return (this.contents.size() > 0);
}
/**
* Obtains the parent {@link NodeParsedTemplateSectionContent}.
*/
public NodeParsedTemplateSectionContent getParent()
{
return this.parent;
}
/**
* Obtains the {@link ParsedTemplateSectionContent}.
*/
public ParsedTemplateSectionContent getParsedTemplateSectionContent()
{
// Determine if delegate content
if (this.delegate != null)
{
return this.delegate;
}
// As here it is a bean so generate the bean
ParsedTemplateSectionContent [] beanContents = new ParsedTemplateSectionContent [this.contents.size()];
for (int i = 0; i < beanContents.length; i++)
{
beanContents [i] = this.contents.get(i).getParsedTemplateSectionContent();
}
// Create and return the Bean content
BeanParsedTemplateSectionContent beanContent = new BeanParsedTemplateSectionContent(this.beanName, beanContents);
return beanContent;
}
}
/**
* Parses the template.
*/
final public void parseTemplate() throws ParseException {
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case LINK:
case PROPERTY:
case SECTION:
case BEAN_COMMENT_OPEN:
case BEAN_COMMENT_CLOSE:
case BEAN_OPEN:
case BEAN_CLOSE:
case STATIC:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
if (jj_2_1(2147483647)) {
parseSection();
} else if (jj_2_2(2147483647)) {
parseBeanCommentOpen();
} else if (jj_2_3(2147483647)) {
parseBeanCommentClose();
} else if (jj_2_4(2147483647)) {
parseBeanOpen();
} else if (jj_2_5(2147483647)) {
parseBeanClose();
} else if (jj_2_6(2147483647)) {
parseProperty();
} else if (jj_2_7(2147483647)) {
parseLink();
} else if (jj_2_8(2147483647)) {
parseStaticContent();
} else {
jj_consume_token(-1);
throw new ParseException();
}
}
jj_consume_token(0);
}
/**
* Parses the section.
*/
final public void parseSection() throws ParseException {
Token t;
t = jj_consume_token(SECTION);
this.addSection(t.image);
}
/**
* Parses the bean open.
*/
final public void parseBeanOpen() throws ParseException {
Token t;
t = jj_consume_token(BEAN_OPEN);
this.addBean(t.image, false);
}
/**
* Parses the bean open.
*/
final public void parseBeanCommentOpen() throws ParseException {
Token t;
t = jj_consume_token(BEAN_COMMENT_OPEN);
this.addCommentBean(t.image);
}
/**
* Parses the bean close.
*/
final public void parseBeanClose() throws ParseException {
Token t;
t = jj_consume_token(BEAN_CLOSE);
this.completeBean(t);
}
/**
* Parses the bean close.
*/
final public void parseBeanCommentClose() throws ParseException {
Token t;
t = jj_consume_token(BEAN_COMMENT_CLOSE);
this.completeBean(t);
}
/**
* Parses the property.
*/
final public void parseProperty() throws ParseException {
Token t;
t = jj_consume_token(PROPERTY);
this.addProperty(t.image);
}
/**
* Parses the link.
*/
final public void parseLink() throws ParseException {
Token t;
t = jj_consume_token(LINK);
this.addLink(t.image);
}
/**
* Parses the static content.
*/
final public void parseStaticContent() throws ParseException {
Token t;
t = jj_consume_token(STATIC);
this.addStaticContent(t.image);
}
private boolean jj_2_1(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_1(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(0, xla); }
}
private boolean jj_2_2(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_2(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(1, xla); }
}
private boolean jj_2_3(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_3(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(2, xla); }
}
private boolean jj_2_4(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_4(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(3, xla); }
}
private boolean jj_2_5(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_5(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(4, xla); }
}
private boolean jj_2_6(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_6(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(5, xla); }
}
private boolean jj_2_7(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_7(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(6, xla); }
}
private boolean jj_2_8(int xla) {
jj_la = xla; jj_lastpos = jj_scanpos = token;
try { return !jj_3_8(); }
catch(LookaheadSuccess ls) { return true; }
finally { jj_save(7, xla); }
}
private boolean jj_3_8() {
if (jj_scan_token(STATIC)) return true;
return false;
}
private boolean jj_3_2() {
if (jj_scan_token(BEAN_COMMENT_OPEN)) return true;
return false;
}
private boolean jj_3_7() {
if (jj_scan_token(LINK)) return true;
return false;
}
private boolean jj_3_1() {
if (jj_scan_token(SECTION)) return true;
return false;
}
private boolean jj_3_6() {
if (jj_scan_token(PROPERTY)) return true;
return false;
}
private boolean jj_3_5() {
if (jj_scan_token(BEAN_CLOSE)) return true;
return false;
}
private boolean jj_3_4() {
if (jj_scan_token(BEAN_OPEN)) return true;
return false;
}
private boolean jj_3_3() {
if (jj_scan_token(BEAN_COMMENT_CLOSE)) return true;
return false;
}
/** Generated Token Manager. */
public WebTemplateParserTokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
public Token token;
/** Next token. */
public Token jj_nt;
private int jj_ntk;
private Token jj_scanpos, jj_lastpos;
private int jj_la;
private int jj_gen;
final private int[] jj_la1 = new int[1];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x1fe,};
}
final private JJCalls[] jj_2_rtns = new JJCalls[8];
private boolean jj_rescan = false;
private int jj_gc = 0;
/** Constructor with InputStream. */
public WebTemplateParser(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public WebTemplateParser(java.io.InputStream stream, String encoding) {
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new WebTemplateParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 1; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 1; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
/** Constructor. */
public WebTemplateParser(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new WebTemplateParserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 1; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
/** Reinitialise. */
public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 1; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
/** Constructor with generated Token Manager. */
public WebTemplateParser(WebTemplateParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 1; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
/** Reinitialise. */
public void ReInit(WebTemplateParserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 1; i++) jj_la1[i] = -1;
for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
}
private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
if (++jj_gc > 100) {
jj_gc = 0;
for (int i = 0; i < jj_2_rtns.length; i++) {
JJCalls c = jj_2_rtns[i];
while (c != null) {
if (c.gen < jj_gen) c.first = null;
c = c.next;
}
}
}
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
static private final class LookaheadSuccess extends java.lang.Error { }
final private LookaheadSuccess jj_ls = new LookaheadSuccess();
private boolean jj_scan_token(int kind) {
if (jj_scanpos == jj_lastpos) {
jj_la--;
if (jj_scanpos.next == null) {
jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
} else {
jj_lastpos = jj_scanpos = jj_scanpos.next;
}
} else {
jj_scanpos = jj_scanpos.next;
}
if (jj_rescan) {
int i = 0; Token tok = token;
while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
if (tok != null) jj_add_error_token(kind, i);
}
if (jj_scanpos.kind != kind) return true;
if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
return false;
}
/** Get the next Token. */
final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}
/** Get the specific Token. */
final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}
private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}
private java.util.List jj_expentries = new java.util.ArrayList();
private int[] jj_expentry;
private int jj_kind = -1;
private int[] jj_lasttokens = new int[100];
private int jj_endpos;
private void jj_add_error_token(int kind, int pos) {
if (pos >= 100) return;
if (pos == jj_endpos + 1) {
jj_lasttokens[jj_endpos++] = kind;
} else if (jj_endpos != 0) {
jj_expentry = new int[jj_endpos];
for (int i = 0; i < jj_endpos; i++) {
jj_expentry[i] = jj_lasttokens[i];
}
jj_entries_loop: for (java.util.Iterator> it = jj_expentries.iterator(); it.hasNext();) {
int[] oldentry = (int[])(it.next());
if (oldentry.length == jj_expentry.length) {
for (int i = 0; i < jj_expentry.length; i++) {
if (oldentry[i] != jj_expentry[i]) {
continue jj_entries_loop;
}
}
jj_expentries.add(jj_expentry);
break jj_entries_loop;
}
}
if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
}
}
/** Generate ParseException. */
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[13];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 1; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1< jj_gen) {
jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
switch (i) {
case 0: jj_3_1(); break;
case 1: jj_3_2(); break;
case 2: jj_3_3(); break;
case 3: jj_3_4(); break;
case 4: jj_3_5(); break;
case 5: jj_3_6(); break;
case 6: jj_3_7(); break;
case 7: jj_3_8(); break;
}
}
p = p.next;
} while (p != null);
} catch(LookaheadSuccess ls) { }
}
jj_rescan = false;
}
private void jj_save(int index, int xla) {
JJCalls p = jj_2_rtns[index];
while (p.gen > jj_gen) {
if (p.next == null) { p = p.next = new JJCalls(); break; }
p = p.next;
}
p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
}
static final class JJCalls {
int gen;
Token first;
int arg;
JJCalls next;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy