org.apache.tapestry
Class AbstractMarkupWriter

java.lang.Object
  extended by org.apache.tapestry.AbstractMarkupWriter
All Implemented Interfaces:
IMarkupWriter
Direct Known Subclasses:
HTMLWriter, WMLWriter

public abstract class AbstractMarkupWriter
extends Object
implements IMarkupWriter

Abstract base class implementing the IMarkupWriter interface. This class is used to create a Generic Tag Markup Language (GTML) output. It is more sophisticated than PrintWriter in that it maintains a concept hierarchy of open GTML tags. It also supplies a number of other of the features that are useful when creating GTML. Elements are started with the begin(String) or beginEmpty(String) methods. Once they are started, attributes for the elements may be set with the various attribute() methods. The element is closed off (i.e., the closing '>' character is written) when any other method is invoked (exception: methods which do not produce output, such as flush()). The end() methods end an element, writing an GTML close tag to the output.

TBD:

This class is derived from the original class com.primix.servlet.HTMLWriter, part of the ServletUtils framework available from The Giant Java Tree.

Since:
0.2.9
Version:
$Id: AbstractMarkupWriter.java 243791 2004-02-19 17:38:13Z hlship $
Author:
Howard Ship, David Solis

Constructor Summary
protected AbstractMarkupWriter(boolean[] safe, String[] entities, String contentType)
          Special constructor used for nested response writers.
protected AbstractMarkupWriter(boolean[] safe, String[] entities, String contentType, OutputStream stream)
          General constructor used by subclasses.
protected AbstractMarkupWriter(boolean[] safe, String[] entities, String contentType, PrintWriter writer)
          Creates new markup writer around the underlying PrintWriter.
protected AbstractMarkupWriter(boolean[] safe, String[] entities, String contentType, String encoding, OutputStream stream)
          General constructor used by subclasses.
 
Method Summary
 void attribute(String name, boolean value)
          Writes a boolean attribute into the currently open tag.
 void attribute(String name, int value)
          Writes an integer attribute into the currently open tag.
 void attribute(String name, String value)
          Writes an attribute into the most recently opened tag.
 void attributeRaw(String name, String value)
          Similar to attribute(String, String) but no escaping of invalid elements is done for the value.
 void begin(String name)
          Closes any existing tag then starts a new element.
 void beginEmpty(String name)
          Starts an element that will not later be matched with an end() call.
 boolean checkError()
          Invokes checkError() on the PrintWriter used to format output.
 void close()
          Closes this IMarkupWriter.
 void closeTag()
          Closes the most recently opened element by writing the '>' that ends it.
 void comment(String value)
          Writes an GTML comment.
 void end()
          Ends the element most recently started by begin(String).
 void end(String name)
          Ends the most recently started element with the given name.
 void flush()
          Forwards flush() to this AbstractMarkupWriter's PrintWriter.
 String getContentType()
          Returns the type of content generated by this response writer, as a MIME type.
abstract  IMarkupWriter getNestedWriter()
          Returns a nested writer, one that accumulates its changes in a buffer.
protected  String pop()
          Removes the top element from the active element stack and returns it.
 void print(char value)
          Prints a single character.
 void print(char[] data, int offset, int length)
          The primary print() method, used by most other methods.
 void print(int value)
          Prints an integer.
 void print(String value)
          Invokes print(char[], int, int) to print the string.
 void println()
          Closes the open tag (if any), then prints a line seperator to the output stream.
 void printRaw(char[] buffer, int offset, int length)
          Prints and portion of an output buffer to the stream.
 void printRaw(String value)
          Prints output to the stream.
protected  void push(String name)
          Adds an element to the active element stack.
protected  void setOutputStream(OutputStream stream, String encoding)
           
protected  void setWriter(PrintWriter writer)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractMarkupWriter

protected AbstractMarkupWriter(boolean[] safe,
                               String[] entities,
                               String contentType,
                               String encoding,
                               OutputStream stream)
General constructor used by subclasses.

Parameters:
safe - an array of flags indicating which characters can be passed directly through with out filtering. Characters marked unsafe, or outside the range defined by safe, are converted to entities.
entities - a set of prefered entities, unsafe characters with a defined entity use the entity, other characters are converted to numeric entities.
contentType - the MIME type of the content produced by the writer.
encoding - the encoding of content produced by the writer.
stream - stream to which content will be written.

AbstractMarkupWriter

protected AbstractMarkupWriter(boolean[] safe,
                               String[] entities,
                               String contentType,
                               OutputStream stream)
General constructor used by subclasses. This constructor is left for backward compatibility. It is preferred that it is not used since it does not specify an encoding for conversion.

Parameters:
safe - an array of flags indicating which characters can be passed directly through with out filtering. Characters marked unsafe, or outside the range defined by safe, are converted to entities.
entities - a set of prefered entities, unsafe characters with a defined entity use the entity, other characters are converted to numeric entities.
contentType - the type of content produced by the writer.
stream - stream to which content will be written.

AbstractMarkupWriter

protected AbstractMarkupWriter(boolean[] safe,
                               String[] entities,
                               String contentType,
                               PrintWriter writer)
Creates new markup writer around the underlying PrintWriter.

This is primarily used by TagSupportService, which is inlcuding content, and therefore this method will not close the writer when the markup writer is closed.

Since:
3.0

AbstractMarkupWriter

protected AbstractMarkupWriter(boolean[] safe,
                               String[] entities,
                               String contentType)
Special constructor used for nested response writers. The subclass is responsible for creating the writer.

Method Detail

getContentType

public String getContentType()
Description copied from interface: IMarkupWriter
Returns the type of content generated by this response writer, as a MIME type.

Specified by:
getContentType in interface IMarkupWriter

getNestedWriter

public abstract IMarkupWriter getNestedWriter()
Description copied from interface: IMarkupWriter
Returns a nested writer, one that accumulates its changes in a buffer. When the nested writer is closed, it writes its buffer into its containing IMarkupWriter.

Specified by:
getNestedWriter in interface IMarkupWriter

setWriter

protected void setWriter(PrintWriter writer)

setOutputStream

protected void setOutputStream(OutputStream stream,
                               String encoding)

attribute

public void attribute(String name,
                      int value)
Writes an integer attribute into the currently open tag.

TBD: Validate that name is legal.

Specified by:
attribute in interface IMarkupWriter
Throws:
IllegalStateException - if there is no open tag.

attribute

public void attribute(String name,
                      boolean value)
Writes a boolean attribute into the currently open tag.

TBD: Validate that name is legal.

Specified by:
attribute in interface IMarkupWriter
Throws:
IllegalStateException - if there is no open tag.
Since:
3.0

attribute

public void attribute(String name,
                      String value)
Writes an attribute into the most recently opened tag. This must be called after begin(String) and before any other kind of writing (which closes the tag).

The value may be null. A null value will be rendered as an empty string.

Troublesome characters in the value are converted to thier GTML entities, much like a print() method, with the following exceptions:

Specified by:
attribute in interface IMarkupWriter
Parameters:
name - The name of the attribute to write (no validation is done on the name).
value - The value to write. If null, the attribute name is written as the value. Otherwise, the value is written,
Throws:
IllegalStateException - if there is no open tag.

attributeRaw

public void attributeRaw(String name,
                         String value)
Similar to attribute(String, String) but no escaping of invalid elements is done for the value.

Specified by:
attributeRaw in interface IMarkupWriter
Throws:
IllegalStateException - if there is no open tag.
Since:
3.0

begin

public void begin(String name)
Closes any existing tag then starts a new element. The new element is pushed onto the active element stack.

Specified by:
begin in interface IMarkupWriter

beginEmpty

public void beginEmpty(String name)
Starts an element that will not later be matched with an end() call. This is useful for elements such as <hr;> or <br> that do not need closing tags.

Specified by:
beginEmpty in interface IMarkupWriter

checkError

public boolean checkError()
Invokes checkError() on the PrintWriter used to format output.

Specified by:
checkError in interface IMarkupWriter

close

public void close()
Closes this IMarkupWriter. Any active elements are closed. The PrintWriter is then sent PrintWriter.close().

Specified by:
close in interface IMarkupWriter

closeTag

public void closeTag()
Closes the most recently opened element by writing the '>' that ends it. May write a slash before the '>' if the tag was opened by beginEmpty(String).

Once this is invoked, the attribute() methods may not be used until a new element is opened with begin(String) or or beginEmpty(String).

Specified by:
closeTag in interface IMarkupWriter

comment

public void comment(String value)
Writes an GTML comment. Any open tag is first closed. The method takes care of providing the <!-- and -->, including a blank line after the close of the comment.

Most characters are valid inside an GTML comment, so no check of the contents is made (much like printRaw(String).

Specified by:
comment in interface IMarkupWriter

end

public void end()
Ends the element most recently started by begin(String). The name of the tag is popped off of the active element stack and used to form an GTML close tag.

TBD: Error checking for the open element stack empty.

Specified by:
end in interface IMarkupWriter

end

public void end(String name)
Ends the most recently started element with the given name. This will also end any other intermediate elements. This is very useful for easily ending a table or even an entire page.

TBD: Error check if the name matches nothing on the open tag stack.

Specified by:
end in interface IMarkupWriter

flush

public void flush()
Forwards flush() to this AbstractMarkupWriter's PrintWriter.

Specified by:
flush in interface IMarkupWriter

pop

protected final String pop()
Removes the top element from the active element stack and returns it.


print

public void print(char[] data,
                  int offset,
                  int length)
The primary print() method, used by most other methods.

Prints the character array, first closing any open tag. Problematic characters ('<', '>' and '&') are converted to their GTML entities.

All 'unsafe' characters are properly converted to either a named or numeric GTML entity. This can be somewhat expensive, so use printRaw(char[], int, int) if the data to print is guarenteed to be safe.

Does nothing if data is null.

Closes any open tag.

Specified by:
print in interface IMarkupWriter

print

public void print(char value)
Prints a single character. If the character is not a 'safe' character, such as '<', then it's GTML entity (named or numeric) is printed instead.

Closes any open tag.

Specified by:
print in interface IMarkupWriter

print

public void print(int value)
Prints an integer.

Closes any open tag.

Specified by:
print in interface IMarkupWriter

print

public void print(String value)
Invokes print(char[], int, int) to print the string. Use printRaw(String) if the character data is known to be safe.

Does nothing if value is null.

Closes any open tag.

Specified by:
print in interface IMarkupWriter
See Also:
print(char[], int, int)

println

public void println()
Closes the open tag (if any), then prints a line seperator to the output stream.

Specified by:
println in interface IMarkupWriter

printRaw

public void printRaw(char[] buffer,
                     int offset,
                     int length)
Prints and portion of an output buffer to the stream. No escaping of invalid GTML elements is done, which makes this more effecient than print(). Does nothing if buffer is null.

Closes any open tag.

Specified by:
printRaw in interface IMarkupWriter

printRaw

public void printRaw(String value)
Prints output to the stream. No escaping of invalid GTML elements is done, which makes this more effecient than print(). Does nothing if value is null.

Closes any open tag.

Specified by:
printRaw in interface IMarkupWriter

push

protected final void push(String name)
Adds an element to the active element stack.