org.jproggy.snippetory.engine
Class Region

java.lang.Object
  extended by org.jproggy.snippetory.engine.chars.CharSequences
      extended by org.jproggy.snippetory.engine.Region
All Implemented Interfaces:
CharSequence, Cloneable, SelfAppender, EncodedData, Template

public class Region
extends CharSequences
implements Template, Cloneable


Constructor Summary
Region(Location placeHolder, List<Object> parts, Map<String,Region> children)
           
 
Method Summary
 Region append(String key, Object value)
          Appends a String representation of the value to all variables with given name.
<T extends Appendable>
T
appendTo(T result)
          Whenever one char sequence is appended to another, it analyzes the target type to figure out an efficient way.
 Region clear()
          removes all data already bound to this instance.
 Template get(String... path)
          Get further elements out of this repository.
 String getEncoding()
          The encoding is represented by it's name.
 Template getParent()
           
 Set<String> names()
          The names of all locations, to be accessed by the set operation.
 Set<String> regionNames()
          Delivers the names of the regions.
 void render()
          Appends the textual representation of this Template to the location where it was created.
 void render(PrintStream out)
          A convenience method to write to a PrintStream
 void render(String target)
          Appends the textual representation of this Template to a sibling of the location where it was created.
 void render(Template target, String key)
          Appends the textual representation of this Template to an arbitrary location.
 void render(Writer out)
          A convenience method to write to a Writer
 Region set(String key, Object value)
          Sets all variables with given name to a String representation of the value.
 void setParent(Template parent)
           
 CharSequence toCharSequence()
          Convert to a char sequence.
 String toString()
           
 
Methods inherited from class org.jproggy.snippetory.engine.chars.CharSequences
charAt, length, subSequence
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Region

public Region(Location placeHolder,
              List<Object> parts,
              Map<String,Region> children)
Method Detail

get

public Template get(String... path)
Description copied from interface: Template
Get further elements out of this repository.

Specified by:
get in interface Template
Parameters:
path - the path within the repository. This might consist of several elements.
Returns:
a clean instance of the child template identified by the name or null if there is no child template with this name. It's undefined if this is a new copy or if only a single instance exists. Though subsequent calls an get on the same instance with the same name might clear the instances returned by previous call or not.

set

public Region set(String key,
                  Object value)
Description copied from interface: Template
Sets all variables with given name to a String representation of the value. Exact value might differ according to different meta data associated with each of these variables. Eventually set or appended data is overwritten. All matching formats and encodings are used. However, there is some special handling for the interface (@link EncodedData). In this case the provided encoding in determined to calculate the correct transcoding.

Specified by:
set in interface Template
Returns:
the Template itself

append

public Region append(String key,
                     Object value)
Description copied from interface: Template
Appends a String representation of the value to all variables with given name. The exact value might differ according to different meta data associated with each of these variables. Eventually set or appended data is kept and new data is appended behind the last character. All matching formats and encodings are used. However, there is some special handling for the interface (@link EncodedData). In this case the provided encoding in determined to calculate the correct transcoding.

Specified by:
append in interface Template
Returns:
the Template itself

clear

public Region clear()
Description copied from interface: Template
removes all data already bound to this instance.

Specified by:
clear in interface Template
Returns:
the Template itself

toCharSequence

public CharSequence toCharSequence()
Description copied from interface: EncodedData
Convert to a char sequence. This methods is added, because a toString method might be more expensive in many cases. This method allows to return a StringBuilder instead of a String. This might avoid copying the data from the StringBuilder into a String.

Specified by:
toCharSequence in interface EncodedData

appendTo

public <T extends Appendable> T appendTo(T result)
Description copied from interface: SelfAppender
Whenever one char sequence is appended to another, it analyzes the target type to figure out an efficient way. If the type of the target is unknown single characters are read, which is pretty slow. This method works as a workaround for that design flaw.

Specified by:
appendTo in interface SelfAppender

toString

public String toString()
Specified by:
toString in interface CharSequence
Overrides:
toString in class Object

getEncoding

public String getEncoding()
Description copied from interface: EncodedData
The encoding is represented by it's name. The name can be resolved by Encoding.REGISTRY.get(String).

Specified by:
getEncoding in interface EncodedData
Returns:

render

public void render()
Description copied from interface: Template
Appends the textual representation of this Template to the location where it was created. (I.e. got from) This works pretty fine for the really simple cases.

Specified by:
render in interface Template

render

public void render(String target)
Description copied from interface: Template
Appends the textual representation of this Template to a sibling of the location where it was created. (I.e. got from) This will be used when handling several variants of presentations within a list. To avoid sorting of elements by variant it has to be ensured that all variants are rendered to the same target.

A template:
  <ul>
   {v:target}
   <t:debit>  <li class="debit"> {v:action} : {v:value} </li> </t:debit>
   <t:credit> <li class="credit">{v:action} : {v:value} </li> </t:credit>
  </ul>
  
And the logic:
   for (Booking b: bookings) {
     if (b.isCredit()) {
       snip.get("credit").set("action", b.getAction()).set("value", b.getValue()).render("target");
     } else {
       snip.get("debit").set("action", b.getAction()).set("value", b.getValue()).render("target");
     }
   }
  
using render() instead of render("target0") in the "debit" line would show all debits first and all credits afterwards. But in the example it's ensured that all data is written to the same location, though, the order stays as defined.

Specified by:
render in interface Template
Parameters:
target - names a child of the same parent. If using the name of this Snippetory it's the same as render() without parameter.

render

public void render(Template target,
                   String key)
Description copied from interface: Template
Appends the textual representation of this Template to an arbitrary location. This allows even to mix data from different files. Quite similar to
 target.append(name, thisOne);
 
however an interceptor gets more information what's going on.

Specified by:
render in interface Template
Parameters:
target - a Template to append to
key - the name of the place where the data should go to.

render

public void render(Writer out)
            throws IOException
Description copied from interface: Template
A convenience method to write to a Writer

Specified by:
render in interface Template
Throws:
IOException

render

public void render(PrintStream out)
            throws IOException
Description copied from interface: Template
A convenience method to write to a PrintStream

Specified by:
render in interface Template
Throws:
IOException

names

public Set<String> names()
Description copied from interface: Template
The names of all locations, to be accessed by the set operation. Can be used to ensure to access all existing names. This method belongs to the reflective API and as such is only for special use.
Use with care as it's a more expensive operation.

Specified by:
names in interface Template

regionNames

public Set<String> regionNames()
Description copied from interface: Template
Delivers the names of the regions. Regions can be accessed by the get operation. This is a subset of the names delivered by names().

Specified by:
regionNames in interface Template

getParent

public Template getParent()

setParent

public void setParent(Template parent)