org.tinyjee.maven.dim.sources
Class IdSnippetSelector

java.lang.Object
  extended by org.tinyjee.maven.dim.sources.AbstractSnippetSelector
      extended by org.tinyjee.maven.dim.sources.AbstractStreamingSnippetSelector
          extended by org.tinyjee.maven.dim.sources.IdSnippetSelector
All Implemented Interfaces:
SnippetSelector
Direct Known Subclasses:
IdDefinitionSelector

public class IdSnippetSelector
extends AbstractStreamingSnippetSelector

Implements the default ID based selector already known from the classic snippet macro that is part of Doxia.

In difference to the snippet macro, IDs must be prefixed with "#" and are matched fully. Sub-id matching is not allowed unless hierarchically built IDs use one of the defined delimiting chars ID_DELIMITER_CHARS) (e.g. "ParentId.ChildId").

Snippets are processed on a line by line basis and multiple snippets with the same name may appear in one file. In order to define a section using a snippet id, surround a block with:
// START SNIPPET: snippet-id
... code to include ...
// END SNIPPET: snippet-id
Expression samples:

This selector is always case-insensitive.

Author:
Juergen_Kellerer, 2011-10-14

Nested Class Summary
 
Nested classes/interfaces inherited from class org.tinyjee.maven.dim.sources.AbstractStreamingSnippetSelector
AbstractStreamingSnippetSelector.AbstractStreamIterator<E>
 
Field Summary
static String ID_DELIMITER_CHARS
          Defines the chars that may surround an ID.
 
Fields inherited from class org.tinyjee.maven.dim.sources.AbstractSnippetSelector
expressionPrefix
 
Fields inherited from interface org.tinyjee.maven.dim.spi.SnippetSelector
CASE_SENSITIVE, EXPAND_SNIPPETS, SELECTORS
 
Constructor Summary
  IdSnippetSelector()
          Constructs a new SnippetId selector.
protected IdSnippetSelector(String expressionPrefix)
           
 
Method Summary
 boolean canSelectSnippetsWith(String expression, URL contentUrl, Map<String,Object> macroParameters)
          Returns true if the expression is supported by this snippet selector.
protected  boolean isDemarcator(String snippetId, String what, String line)
          Determines if the given line is a demarcator.
protected  boolean isEnd(String snippetId, String line)
          Determines if the given line is an end demarcator.
protected  boolean isStart(String snippetId, String line)
          Determines if the given line is a start demarcator.
 Iterator<Integer> selectSnippets(String expression, URL contentUrl, LineNumberReader content, Map<String,Object> macroParameters)
          Selects snippets in the given content and returns an iterator over selected line numbers.
 
Methods inherited from class org.tinyjee.maven.dim.sources.AbstractSnippetSelector
assertExpressionIsValid, getExpressionPrefixes, stripPrefix, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ID_DELIMITER_CHARS

public static final String ID_DELIMITER_CHARS
Defines the chars that may surround an ID.

See Also:
Constant Field Values
Constructor Detail

IdSnippetSelector

public IdSnippetSelector()
Constructs a new SnippetId selector.


IdSnippetSelector

protected IdSnippetSelector(String expressionPrefix)
Method Detail

canSelectSnippetsWith

public boolean canSelectSnippetsWith(String expression,
                                     URL contentUrl,
                                     Map<String,Object> macroParameters)
Description copied from interface: SnippetSelector
Returns true if the expression is supported by this snippet selector.

Specified by:
canSelectSnippetsWith in interface SnippetSelector
Overrides:
canSelectSnippetsWith in class AbstractSnippetSelector
Parameters:
expression - a single snippet expression.
contentUrl - the resolved URL of the content to include (might be 'null' if the source does not provide a Url).
macroParameters - the parameters used with the macro call.
Returns:
true if the snippet selector supports the given expression.

selectSnippets

public Iterator<Integer> selectSnippets(String expression,
                                        URL contentUrl,
                                        LineNumberReader content,
                                        Map<String,Object> macroParameters)
                                 throws IOException
Description copied from interface: SnippetSelector
Selects snippets in the given content and returns an iterator over selected line numbers.

Note: The caller reads the returned iterator fully before closing the content stream. This way snippet selectors can truly implement streaming selection (in difference to the provided example).

Parameters:
expression - a single snippet expression.
contentUrl - the resolved URL of the content to include (might be 'null' if the source does not provide a Url).
content - a reader providing the content. Implementations must read content from this reader as the Url may point to an outdated static source that does not reflect the content to include.
macroParameters - the parameters used with the macro call.
Returns:
an iterator containing all selected line numbers.
Throws:
IOException - In case of reading the URL failed.

isStart

protected boolean isStart(String snippetId,
                          String line)
Determines if the given line is a start demarcator.

Parameters:
snippetId - the id of the snippet.
line - the line.
Returns:
True, if the line is a start demarcator.

isDemarcator

protected boolean isDemarcator(String snippetId,
                               String what,
                               String line)
Determines if the given line is a demarcator.

Parameters:
snippetId - the id of the snippet.
what - Identifier for the demarcator.
line - the line.
Returns:
True, if the line is a start demarcator.

isEnd

protected boolean isEnd(String snippetId,
                        String line)
Determines if the given line is an end demarcator.

Parameters:
snippetId - the id of the snippet.
line - the line.
Returns:
True, if the line is an end demarcator.