org.tinyjee.maven.dim.spi
Class SnippetExtractor

java.lang.Object
  extended by org.tinyjee.maven.dim.spi.SnippetExtractor

public final class SnippetExtractor
extends Object

Implements a singleton that uses snippet selectors and sources to extract portions of content and format it as a map of selected lines, indexed by their starting line numbers.

Author:
Juergen_Kellerer, 2011-10-20

Field Summary
static String LB
          Defines the system dependent line break to use.
 
Method Summary
 SortedMap<Integer,List<String>> convert(Reader contentReader)
          Low level method that converts the complete source to the snippet map format.
 SortedMap<Integer,List<String>> extractSnippets(Reader reader, Set<Integer> includedLines, Set<Integer> excludedLines)
          Low level method that extracts the snippets of the given source content.
 List<SortedSet<Integer>> findSelectedLines(Source source)
          Finds the selected lines given the specified source.
static SnippetExtractor getInstance()
          Returns a singleton instance of this class.
 boolean isSnippetSelectionRequired(Source source)
          Returns true if the given source require snippet selection.
 SortedMap<Integer,List<String>> selectSnippets(Source source)
          Selects the snippets of the given source.
 String[] splitExpression(String expression)
          Splits a given snippet expression into the individual sub-expression parts.
 String toString(List<String> lines)
          Flattens the given lines to pure text using system specific line breaks.
 StringBuilder toString(List<String> lines, StringBuilder out)
          Flattens the given lines to pure text using system specific line breaks.
 String toString(Map<Integer,List<String>> content, boolean addEmptyLineBreaks)
          Flattens the given content to pure text using system specific line breaks.
 void unIndent(SortedMap<Integer,List<String>> snippetMap, boolean blockScope)
          Removes unnecessary indents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LB

public static final String LB
Defines the system dependent line break to use.

Method Detail

getInstance

public static SnippetExtractor getInstance()
Returns a singleton instance of this class.

Returns:
a singleton instance of this class.

splitExpression

public String[] splitExpression(String expression)
Splits a given snippet expression into the individual sub-expression parts.

Parameters:
expression - the expression to split.
Returns:
the expression parts.

isSnippetSelectionRequired

public boolean isSnippetSelectionRequired(Source source)
Returns true if the given source require snippet selection.

Parameters:
source - the source to test.
Returns:
true if the given source require snippet selection.

selectSnippets

public SortedMap<Integer,List<String>> selectSnippets(Source source)
                                               throws IOException
Selects the snippets of the given source.

Parameters:
source - the source to select the snippets in.
Returns:
a map of first-line-number to lines mapping.
Throws:
IOException - in case of reading the source failed.

extractSnippets

public SortedMap<Integer,List<String>> extractSnippets(Reader reader,
                                                       Set<Integer> includedLines,
                                                       Set<Integer> excludedLines)
                                                throws IOException
Low level method that extracts the snippets of the given source content.

Parameters:
reader - the reader containing the source.
includedLines - the line numbers to include, if empty all is included (as long as exclusions exist).
excludedLines - the line numbers to exclude.
Returns:
a map of first-line-number to lines mapping.
Throws:
IOException - in case of reading the source failed.

convert

public SortedMap<Integer,List<String>> convert(Reader contentReader)
                                        throws IOException
Low level method that converts the complete source to the snippet map format.

This is a convenience method to calling extractSnippets(contentReader, emptySet(), singleton(-1)).

Parameters:
contentReader - the reader containing the source.
Returns:
a map of first-line-number to lines mapping.
Throws:
IOException - in case of reading the source failed.

toString

public String toString(Map<Integer,List<String>> content,
                       boolean addEmptyLineBreaks)
Flattens the given content to pure text using system specific line breaks.

Parameters:
content - the content to write.
addEmptyLineBreaks - If true, will add any additional line breaks to re-construct the number of lines of the original document.
Returns:
a String of containing all content.

toString

public String toString(List<String> lines)
Flattens the given lines to pure text using system specific line breaks.

Parameters:
lines - the lines to write.
Returns:
a String of containing all content.

toString

public StringBuilder toString(List<String> lines,
                              StringBuilder out)
Flattens the given lines to pure text using system specific line breaks.

Parameters:
lines - the lines to write.
out - The StringBuilder instance to write to.
Returns:
The StringBuilder instance.

unIndent

public void unIndent(SortedMap<Integer,List<String>> snippetMap,
                     boolean blockScope)
Removes unnecessary indents.

Parameters:
snippetMap - the snippets.
blockScope - true to process every snippet separately, false to detect indent to remove using a global scope.

findSelectedLines

public List<SortedSet<Integer>> findSelectedLines(Source source)
                                           throws IOException
Finds the selected lines given the specified source.

Parameters:
source - the source to process.
Returns:
a list with 2 elements: included and excluded line numbers.
Throws:
IOException - in case of reading the source failed.