de.dv.sbu.filehandler.ftl
Class DefaultSourceParser

java.lang.Object
  extended by de.dv.sbu.filehandler.ftl.DefaultSourceParser
All Implemented Interfaces:
ISourceParser

public class DefaultSourceParser
extends java.lang.Object
implements ISourceParser

Parses a source file's text separating by METADATA, INLUDE, IGNORED content and render COMPONENT call sections. Each command is marked by an opening and a closing tag. The following example uses "<!--#" as opening and "-->" as closing tag. It demonstrates the structure of a source file.

<!--#sbu:metadata attributes={attribute1="value1" attribute2="value2"} xml={<XML with one
root element - may span multiple lines>}-->
<!--#sbu:include-->
Some text content.
<!--#/sbu:include-->
ignored
<!--#sbu:include ftl="true"-->
Some FTL text content, e.g. 1 + 1 = ${1+1}.
The resource folder is the root for imports.
<!--#/sbu:include-->
<!--#sbu:component name="component 1" attributes={attribute1="value1"} xml={<XML with one
root element - may span multiple lines>}-->
ignored

The METADATA tag is optional but must be the first one. Attributes and XML definitions are optional. They following additional FTL root map entries will be initialized per INCLUDE or COMPONENT part handling:

COMPONENT and FTL INCLUDE parts use the resource folder as root directory for FTL imports. Components must be registered and point to an initial FREEMARKER template.

Author:
Daniel Vogtland

Nested Class Summary
(package private)  class DefaultSourceParser.ComponentResult
          Parsing result for a component tag.
(package private)  class DefaultSourceParser.DataResult
          Wraps a attributes and XML information.
(package private)  class DefaultSourceParser.IncludeResult
          Parsing result for an include opening tag.
 
Field Summary
static java.lang.String COMPONENT_ATTRIBUTES_NAME
          Attribute data for the component attributes: 'componentattributes'.
static java.lang.String COMPONENT_XML_NAME
          XML data for the meta data XML content: 'componentxml'.
static java.lang.String COMPONENTNAME_NAME
          The name key: 'componentname'.
static java.lang.String METADATA_ATTRIBUTES_NAME
          Attribute data for the meta data attributes: 'attributes'.
static java.lang.String METADATA_XML_NAME
          XML data for the meta data XML content: 'xml'.
 
Constructor Summary
DefaultSourceParser()
           
 
Method Summary
(package private)  DefaultSourceParser.ComponentResult checkComponent(java.lang.String tagContent)
          Checks the given tag content for being a component tag.
(package private)  boolean checkIncludeClose(java.lang.String tagContent)
          Checks the given tag content for being an include close tag.
(package private)  DefaultSourceParser.IncludeResult checkIncludeOpen(java.lang.String tagContent)
          Checks the given tag content for being an include open tag.
(package private)  DefaultSourceParser.DataResult checkMeta(java.lang.String tagContent)
          Checks the given tag content for being a meta tag.
(package private)  DefaultSourceParser.DataResult extractAttributesAndXml(java.lang.String tagContent, int beginIndex)
          Extracts attributes and XML data from the given tag content.
 java.util.Map<java.lang.String,java.lang.String> getComponents()
          Returns the registered render component as mapping from names to the related template paths (that are relative to the resource root).
 java.lang.String getIncludeBegin()
          Returns the text to be inserted before an INCLUDE section.
 java.lang.String getIncludeEnd()
          Returns the text to be inserted behind an INCLUDE section.
 java.lang.String getTagClose()
          Returns the close marker tag.
 java.lang.String getTagOpen()
          Returns the open marker tag.
 java.lang.String parse(java.io.Reader reader, Configuration configuration, java.util.Map<java.lang.String,java.lang.Object> rootMap)
          Reads from a source file.
 void setComponents(java.util.Map<java.lang.String,java.lang.String> components)
          Sets the available components.
 void setIncludeBegin(java.lang.String includeBegin)
          Sets the INCLUDE prefix text.
 void setIncludeEnd(java.lang.String includeEnd)
          Sets the INCLUDE suffix.
 void setTagClose(java.lang.String s)
          Sets the tag closing text.
 void setTagOpen(java.lang.String s)
          Sets the tag opening text.
(package private)  int skipWhitespacePosition(java.lang.String tagContent, int beginIndex)
          Returns the next non-whitespace index (maybe the text's length).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

METADATA_ATTRIBUTES_NAME

public static final java.lang.String METADATA_ATTRIBUTES_NAME
Attribute data for the meta data attributes: 'attributes'. If defined via meta data an instance of java.util.Map.

See Also:
Constant Field Values

METADATA_XML_NAME

public static final java.lang.String METADATA_XML_NAME
XML data for the meta data XML content: 'xml'. If defined via meta data an instance of NodeModel.

See Also:
Constant Field Values

COMPONENT_ATTRIBUTES_NAME

public static final java.lang.String COMPONENT_ATTRIBUTES_NAME
Attribute data for the component attributes: 'componentattributes'.

See Also:
Constant Field Values

COMPONENT_XML_NAME

public static final java.lang.String COMPONENT_XML_NAME
XML data for the meta data XML content: 'componentxml'. If defined via component part parameters an instance of NodeModel.

See Also:
Constant Field Values

COMPONENTNAME_NAME

public static final java.lang.String COMPONENTNAME_NAME
The name key: 'componentname'. This is the current component's name in a COMOPONENT part.

See Also:
Constant Field Values
Constructor Detail

DefaultSourceParser

public DefaultSourceParser()
Method Detail

getTagOpen

public java.lang.String getTagOpen()
Returns the open marker tag. The default value is '

setTagOpen

public void setTagOpen(java.lang.String s)
                throws java.lang.NullPointerException
Sets the tag opening text.

Parameters:
s - The text to use.
Throws:
java.lang.NullPointerException - The argument is null.

getTagClose

public java.lang.String getTagClose()
Returns the close marker tag. The default value is '-->'.

Returns:
The close marker tag.

setTagClose

public void setTagClose(java.lang.String s)
                 throws java.lang.NullPointerException
Sets the tag closing text.

Parameters:
s - The text to use.
Throws:
java.lang.NullPointerException - The argument is null.

getIncludeBegin

public java.lang.String getIncludeBegin()
Returns the text to be inserted before an INCLUDE section. Default is the empty text.

Returns:
The include prefix text.

setIncludeBegin

public void setIncludeBegin(java.lang.String includeBegin)
                     throws java.lang.NullPointerException
Sets the INCLUDE prefix text.

Parameters:
includeBegin - The text to set.
Throws:
java.lang.NullPointerException - The text is null.

getIncludeEnd

public java.lang.String getIncludeEnd()
Returns the text to be inserted behind an INCLUDE section. Default is the empty text.

Returns:
The include suffix text.

setIncludeEnd

public void setIncludeEnd(java.lang.String includeEnd)
                   throws java.lang.NullPointerException
Sets the INCLUDE suffix.

Parameters:
includeEnd - The text to set.
Throws:
java.lang.NullPointerException - The text is null.

getComponents

public java.util.Map<java.lang.String,java.lang.String> getComponents()
Returns the registered render component as mapping from names to the related template paths (that are relative to the resource root).

Returns:
The registered components.

setComponents

public void setComponents(java.util.Map<java.lang.String,java.lang.String> components)
                   throws java.lang.NullPointerException
Sets the available components.

Parameters:
components - The components to set.
Throws:
java.lang.NullPointerException - The argument is null.

parse

public java.lang.String parse(java.io.Reader reader,
                              Configuration configuration,
                              java.util.Map<java.lang.String,java.lang.Object> rootMap)
                       throws java.lang.Exception
Reads from a source file.

Specified by:
parse in interface ISourceParser
Parameters:
reader - The reader consuming the related source file's text content.
configuration - The Freemarker configuration to use.
rootMap - The FTL root map. Might be modified.
Returns:
The resulting content.
Throws:
java.lang.Exception - Any exception.
See Also:
de.dv.sbu.filehandler.ftl.ISourceParser#parse(java.io.Reader, freemarker.template.Configuration, java.util.Map)

skipWhitespacePosition

int skipWhitespacePosition(java.lang.String tagContent,
                           int beginIndex)
Returns the next non-whitespace index (maybe the text's length).

Parameters:
tagContent - The text to parse.
beginIndex - The beginning index.
Returns:
The first position being no whitespace.

extractAttributesAndXml

DefaultSourceParser.DataResult extractAttributesAndXml(java.lang.String tagContent,
                                                       int beginIndex)
Extracts attributes and XML data from the given tag content. Empty content is valid.

Parameters:
tagContent - The tag content to check.
beginIndex - The index offset.
Returns:
The positive parsing result or null on mismatch.

checkIncludeClose

boolean checkIncludeClose(java.lang.String tagContent)
Checks the given tag content for being an include close tag.

Parameters:
tagContent - The tag content to check.
Returns:
true if the tag content matches the INCLUDE close pattern.

checkIncludeOpen

DefaultSourceParser.IncludeResult checkIncludeOpen(java.lang.String tagContent)
Checks the given tag content for being an include open tag.

Parameters:
tagContent - The tag content to check.
Returns:
The positive parsing result or null on mismatch.

checkComponent

DefaultSourceParser.ComponentResult checkComponent(java.lang.String tagContent)
Checks the given tag content for being a component tag.

Parameters:
tagContent - The tag content to check.
Returns:
The positive parsing result or null on mismatch.

checkMeta

DefaultSourceParser.DataResult checkMeta(java.lang.String tagContent)
Checks the given tag content for being a meta tag.

Parameters:
tagContent - The tag content to check.
Returns:
The positive parsing result or null on mismatch.