Class Trans

java.lang.Object
  |
  +--Trans
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable

public class Trans
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

Trans is a container of indented text objects used alone or with JavaCC tool from WebGain for easy and rapid design and development of high-to-high language translators, scripting and template engines, macro expanders, format transformers...

Profit for JavaCC teams

General use

Specific use with JavaCC

Trans compared to Jjtree (and partially to JTB)

See Also:
Serialized Form

Field Summary
protected static Trans concatenate
          Internal flag object for concatenation of items, never to be printed itself neither used by user.
static java.lang.String cr
          System newline character(s) that are used when Trans is transformed to String.
static int defaultOffset
          Default number of spaces before each nested level for all new containers.
protected  java.util.ArrayList list
          Physical container.
protected  int offset
          Number of spaces written before each line of this container.
 
Constructor Summary
Trans()
          Creates container with default offset.
Trans(int offs)
          Creates container with specified offset.
Trans(java.lang.Object obj)
          Created container with default offset and specified item, if not null.
 
Method Summary
 Trans add(boolean par)
           
 Trans add(byte par)
           
 Trans add(double par)
           
 Trans add(float par)
           
 Trans add(char par)
           
 Trans add(int par)
           
 Trans add(long par)
           
 Trans add(java.lang.Object next)
          Adds object to the end of container, if not null.
 Trans add(short par)
           
 Trans conc(boolean par)
           
 Trans conc(byte par)
           
 Trans conc(double par)
           
 Trans conc(float par)
           
 Trans conc(char par)
           
 Trans conc(int par)
           
 Trans conc(long par)
           
 Trans conc(java.lang.Object next)
          Object is concatenated, if not null.
 Trans conc(short par)
           
 boolean equals(java.lang.Object obj)
          Compares whether parameter is also Trans and their toString() representation is same.
 int hashCode()
          Returns a constant (0).
 Trans newLine()
          Adds system newline character(s).
 Trans preadd(boolean par)
           
 Trans preadd(byte par)
           
 Trans preadd(double par)
           
 Trans preadd(float par)
           
 Trans preadd(char par)
           
 Trans preadd(int par)
           
 Trans preadd(long par)
           
 Trans preadd(java.lang.Object next)
          Adds object at the first place of container, if not null.
 Trans preadd(short par)
           
 Trans preconc(boolean par)
           
 Trans preconc(byte par)
           
 Trans preconc(double par)
           
 Trans preconc(float par)
           
 Trans preconc(char par)
           
 Trans preconc(int par)
           
 Trans preconc(long par)
           
 Trans preconc(java.lang.Object next)
          Object is preconcatenated, if not null.
 Trans preconc(short par)
           
 java.lang.String toString()
          Transforms to String, no first level offset made in this function, so direct items aren't indented.
 java.lang.StringBuffer write(java.lang.StringBuffer buffer)
          Writes content to end of passed buffer, and appends system newline character(s) to it.
protected  java.lang.StringBuffer write(java.lang.StringBuffer buffer, int offs, boolean conc)
          Transforms container's content - all its items to String in recursion, managing indentation level and concatenation.
 void writeFile(java.lang.String name)
          Writes content to file, and appends system newline character(s) to it.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

cr

public static final java.lang.String cr
System newline character(s) that are used when Trans is transformed to String. It can changed by user. Its value is used during calls to toString(), write(..), not when containers are composed.

concatenate

protected static final Trans concatenate
Internal flag object for concatenation of items, never to be printed itself neither used by user. Next element will be concatenated to previous one, its first line will be concatenated to last line of previous text. Otherwise, when an object is added to container, its text starts on new line. Nothing wrong happens if it's added several times in sequence in subclassed code - then whole sequence of references is treated as one, that 'concatenates' last line of previous and first line of next object. It's resolved to its actual static representation at deserialization.

defaultOffset

public static int defaultOffset
Default number of spaces before each nested level for all new containers. Its value can be changed by user (initially 3).
See Also:
Trans(int).

offset

protected int offset
Number of spaces written before each line of this container.

list

protected java.util.ArrayList list
Physical container. No need for synchronized Vector. User can synchronize on Trans object itself.
Constructor Detail

Trans

public Trans()
Creates container with default offset.
See Also:
defaultOffset

Trans

public Trans(int offs)
Creates container with specified offset.
Parameters:
offs - sets number of spaces before each line of this container

Trans

public Trans(java.lang.Object obj)
Created container with default offset and specified item, if not null.
Parameters:
obj - first item
Method Detail

add

public Trans add(java.lang.Object next)
Adds object to the end of container, if not null. Its content is indented and starts on new line. Next item starts on new line, unless it's concatenated.
Parameters:
obj - item to be added

preadd

public Trans preadd(java.lang.Object next)
Adds object at the first place of container, if not null. Its content is indented and starts on new line. Former first object starts on new line, so their contents are not concatenated together. Name "preadd" is more clear than "ins" or "insert", because it seemed like 'insert it here' in streamed call. Try:

new Trans("Third line").preadd("Second line").preadd("First line")

Parameters:
obj - item to be added.

conc

public Trans conc(java.lang.Object next)
Object is concatenated, if not null. Whole content is indented except its first line, that is appended to last line of previous item - concatenated.
Parameters:
obj - item to be added at end of container

preconc

public Trans preconc(java.lang.Object next)
Object is preconcatenated, if not null. Whole content is indented and it starts on new line. Last line is concatenated in front of first line of former first object.
Parameters:
obj - item to be added at first place of container

newLine

public Trans newLine()
Adds system newline character(s). In fact, it adds empty string, and write(..) engine puts there cr newline character(s) when it is called.

add

public Trans add(boolean par)

preadd

public Trans preadd(boolean par)

conc

public Trans conc(boolean par)

preconc

public Trans preconc(boolean par)

add

public Trans add(byte par)

preadd

public Trans preadd(byte par)

conc

public Trans conc(byte par)

preconc

public Trans preconc(byte par)

add

public Trans add(char par)

preadd

public Trans preadd(char par)

conc

public Trans conc(char par)

preconc

public Trans preconc(char par)

add

public Trans add(short par)

preadd

public Trans preadd(short par)

conc

public Trans conc(short par)

preconc

public Trans preconc(short par)

add

public Trans add(int par)

preadd

public Trans preadd(int par)

conc

public Trans conc(int par)

preconc

public Trans preconc(int par)

add

public Trans add(long par)

preadd

public Trans preadd(long par)

conc

public Trans conc(long par)

preconc

public Trans preconc(long par)

add

public Trans add(float par)

preadd

public Trans preadd(float par)

conc

public Trans conc(float par)

preconc

public Trans preconc(float par)

add

public Trans add(double par)

preadd

public Trans preadd(double par)

conc

public Trans conc(double par)

preconc

public Trans preconc(double par)

toString

public java.lang.String toString()
Transforms to String, no first level offset made in this function, so direct items aren't indented. It can't be cached, see equals(Object).
Overrides:
toString in class java.lang.Object
See Also:
equals(Object)

write

protected java.lang.StringBuffer write(java.lang.StringBuffer buffer,
                                       int offs,
                                       boolean conc)
Transforms container's content - all its items to String in recursion, managing indentation level and concatenation. Transforms alone characters "\n", "\r" and couples of "\n\r", "\r\n"to one presence of system newline character(s). Couples are transformed together only if they are printed from the same Object with its toString().
Parameters:
buffer - Buffer of upper container (caller), ending by newline character.
Returns:
Passedbuffer with added content, newline character at end of each line, but not of last one (so concatenation can be made to it by the successor).

write

public java.lang.StringBuffer write(java.lang.StringBuffer buffer)
Writes content to end of passed buffer, and appends system newline character(s) to it.
Parameters:
buffer - Buffer to add content.
Returns:
Passed buffer with added content.
See Also:
write(StringBuffer,int,boolean)

writeFile

public void writeFile(java.lang.String name)
               throws java.io.IOException
Writes content to file, and appends system newline character(s) to it.
Parameters:
file - File to write content.

equals

public boolean equals(java.lang.Object obj)
Compares whether parameter is also Trans and their toString() representation is same. This is usefull because default equals() checks equality of references. Implementation uses result of toString() and it can't be cached, because Trans subnodes can be accessed and changed later by 'reservation'. There is no way to cache equality or object status in principle. Unchanged hashCode() means only that object probably didn't change. Anyway, it's not used often. See source for faster restricted version that compares subnodes as Objects.

User can redefine equals(Object) in any way, as standard Trans mechanism that generates its string representation (write(...)) doesn't use it.

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Returns a constant (0). Otherwise it had to return hashCode() of string representation generated by toString().
Overrides:
hashCode in class java.lang.Object
See Also:
equals(Object)