org.apache.xml.serialize

Class OutputFormat


public class OutputFormat
extends java.lang.Object

Specifies an output format to control the serializer. Based on the XSLT specification for output format, plus additional parameters. Used to select the suitable serializer and determine how the document should be formatted on output.

The two interesting constructors are:

Version:
$Revision: 1.21 $ $Date: 2004/02/24 23:34:03 $
Author:
Assaf Arkin Keith Visco
See Also:
Serializer, Method, LineSeparator

Nested Class Summary

static class
OutputFormat.Defaults
static class
OutputFormat.DTD

Constructor Summary

OutputFormat()
Constructs a new output format with the default values.
OutputFormat(Document doc)
Constructs a new output format with the proper method, document type identifiers and media type for the specified document.
OutputFormat(Document doc, String encoding, boolean indenting)
Constructs a new output format with the proper method, document type identifiers and media type for the specified document, and with the specified encoding.
OutputFormat(String method, String encoding, boolean indenting)
Constructs a new output format with the default values for the specified method and encoding.

Method Summary

String[]
getCDataElements()
Returns a list of all the elements whose text node children should be output as CDATA, or null if no such elements were specified.
String
getDoctypePublic()
Returns the specified document type public identifier, or null.
String
getDoctypeSystem()
Returns the specified document type system identifier, or null.
String
getEncoding()
Returns the specified encoding.
EncodingInfo
getEncodingInfo()
Returns an EncodingInfo instance for the encoding.
int
getIndent()
Returns the indentation specified.
boolean
getIndenting()
Returns true if indentation was specified.
char
getLastPrintable()
Returns the last printable character based on the selected encoding.
String
getLineSeparator()
Returns a specific line separator to use.
int
getLineWidth()
Return the selected line width for breaking up long lines.
String
getMediaType()
Returns the specified media type, or null.
String
getMethod()
Returns the method specified for this output format.
String[]
getNonEscapingElements()
Returns a list of all the elements whose text node children should be output unescaped (no character references), or null if no such elements were specified.
boolean
getOmitComments()
Returns true if comments should be ommited.
boolean
getOmitDocumentType()
Returns true if the DOCTYPE declaration should be ommited.
boolean
getOmitXMLDeclaration()
Returns true if the XML document declaration should be ommited.
boolean
getPreserveEmptyAttributes()
Returns the preserveEmptyAttribute flag.
boolean
getPreserveSpace()
Returns true if the default behavior for this format is to preserve spaces.
boolean
getStandalone()
Returns true if the document type is standalone.
String
getVersion()
Returns the version for this output method.
boolean
isCDataElement(String tagName)
Returns true if the text node children of the given elements should be output as CDATA.
boolean
isNonEscapingElement(String tagName)
Returns true if the text node children of the given elements should be output unescaped.
boolean
setAllowJavaNames()
Returns whether java encoding names are permitted
void
setAllowJavaNames(boolean allow)
Sets whether java encoding names are permitted
void
setCDataElements(String[] cdataElements)
Sets the list of elements for which text node children should be output as CDATA.
void
setDoctype(String publicId, String systemId)
Sets the document type public and system identifiers.
void
setEncoding(String encoding)
Sets the encoding for this output method.
void
setEncoding(EncodingInfo encInfo)
Sets the encoding for this output method with an EncodingInfo instance.
void
setIndent(int indent)
Sets the indentation.
void
setIndenting(boolean on)
Sets the indentation on and off.
void
setLineSeparator(String lineSeparator)
Sets the line separator.
void
setLineWidth(int lineWidth)
Sets the line width.
void
setMediaType(String mediaType)
Sets the media type.
void
setMethod(String method)
Sets the method for this output format.
void
setNonEscapingElements(String[] nonEscapingElements)
Sets the list of elements for which text node children should be output unescaped (no character references).
void
setOmitComments(boolean omit)
Sets comment omitting on and off.
void
setOmitDocumentType(boolean omit)
Sets DOCTYPE declaration omitting on and off.
void
setOmitXMLDeclaration(boolean omit)
Sets XML declaration omitting on and off.
void
setPreserveEmptyAttributes(boolean preserve)
Sets the preserveEmptyAttribute flag.
void
setPreserveSpace(boolean preserve)
Sets space preserving as the default behavior.
void
setStandalone(boolean standalone)
Sets document DTD standalone.
void
setVersion(String version)
Sets the version for this output method.
static String
whichDoctypePublic(Document doc)
Returns the document type public identifier specified for this document, or null.
static String
whichDoctypeSystem(Document doc)
Returns the document type system identifier specified for this document, or null.
static String
whichMediaType(String method)
Returns the suitable media format for a document output with the specified method.
static String
whichMethod(Document doc)
Determine the output method for the specified document.

Constructor Details

OutputFormat

public OutputFormat()
Constructs a new output format with the default values.

OutputFormat

public OutputFormat(Document doc)
Constructs a new output format with the proper method, document type identifiers and media type for the specified document.
Parameters:
doc - The document to output

OutputFormat

public OutputFormat(Document doc,
                    String encoding,
                    boolean indenting)
Constructs a new output format with the proper method, document type identifiers and media type for the specified document, and with the specified encoding. If indent is true, the document will be pretty printed with the default indentation level and default line wrapping.
Parameters:
doc - The document to output
encoding - The specified encoding
indenting - True for pretty printing

OutputFormat

public OutputFormat(String method,
                    String encoding,
                    boolean indenting)
Constructs a new output format with the default values for the specified method and encoding. If indent is true, the document will be pretty printed with the default indentation level and default line wrapping.
Parameters:
method - The specified output method
encoding - The specified encoding
indenting - True for pretty printing

Method Details

getCDataElements

public String[] getCDataElements()
Returns a list of all the elements whose text node children should be output as CDATA, or null if no such elements were specified.

getDoctypePublic

public String getDoctypePublic()
Returns the specified document type public identifier, or null.

getDoctypeSystem

public String getDoctypeSystem()
Returns the specified document type system identifier, or null.

getEncoding

public String getEncoding()
Returns the specified encoding. If no encoding was specified, the default is always "UTF-8".
Returns:
The encoding

getEncodingInfo

public EncodingInfo getEncodingInfo()
            throws UnsupportedEncodingException
Returns an EncodingInfo instance for the encoding.
See Also:
setEncoding

getIndent

public int getIndent()
Returns the indentation specified. If no indentation was specified, zero is returned and the document should not be indented.
Returns:
The indentation or zero

getIndenting

public boolean getIndenting()
Returns true if indentation was specified.

getLastPrintable

public char getLastPrintable()
Returns the last printable character based on the selected encoding. Control characters and non-printable characters are always printed as character references.

getLineSeparator

public String getLineSeparator()
Returns a specific line separator to use. The default is the Web line separator (\n). A string is returned to support double codes (CR + LF).
Returns:
The specified line separator

getLineWidth

public int getLineWidth()
Return the selected line width for breaking up long lines. When indenting, and only when indenting, long lines will be broken at space boundaries based on this line width. No line wrapping occurs if this value is zero.

getMediaType

public String getMediaType()
Returns:
The specified media type, or null

getMethod

public String getMethod()
Returns:
The specified output method, or null

getNonEscapingElements

public String[] getNonEscapingElements()
Returns a list of all the elements whose text node children should be output unescaped (no character references), or null if no such elements were specified.

getOmitComments

public boolean getOmitComments()
Returns true if comments should be ommited. The default is false.

getOmitDocumentType

public boolean getOmitDocumentType()
Returns true if the DOCTYPE declaration should be ommited. The default is false.

getOmitXMLDeclaration

public boolean getOmitXMLDeclaration()
Returns true if the XML document declaration should be ommited. The default is false.

getPreserveEmptyAttributes

public boolean getPreserveEmptyAttributes()
Returns the preserveEmptyAttribute flag. If flag is false, then' attributes with empty string values are output as the attribute name only (in HTML mode).
Returns:
preserve the preserve flag

getPreserveSpace

public boolean getPreserveSpace()
Returns true if the default behavior for this format is to preserve spaces. All elements that do not specify otherwise or specify the default behavior will be formatted based on this rule. All elements that specify space preserving will always preserve space.

getStandalone

public boolean getStandalone()
Returns true if the document type is standalone. The default is false.

getVersion

public String getVersion()
Returns the version for this output method. If no version was specified, will return null and the default version number will be used. If the serializerr does not support that particular version, it should default to a supported version.
Returns:
The specified method version, or null

isCDataElement

public boolean isCDataElement(String tagName)
Returns true if the text node children of the given elements should be output as CDATA.
Parameters:
tagName - The element's tag name
Returns:
True if should serialize as CDATA

isNonEscapingElement

public boolean isNonEscapingElement(String tagName)
Returns true if the text node children of the given elements should be output unescaped.
Parameters:
tagName - The element's tag name
Returns:
True if should serialize unescaped

setAllowJavaNames

public boolean setAllowJavaNames()
Returns whether java encoding names are permitted

setAllowJavaNames

public void setAllowJavaNames(boolean allow)
Sets whether java encoding names are permitted

setCDataElements

public void setCDataElements(String[] cdataElements)
Sets the list of elements for which text node children should be output as CDATA.
Parameters:
cdataElements - List of CDATA element tag names

setDoctype

public void setDoctype(String publicId,
                       String systemId)
Sets the document type public and system identifiers. Required only if the DOM Document or SAX events do not specify the document type, and one must be present in the serialized document. Any document type specified by the DOM Document or SAX events will override these values.
Parameters:
publicId - The public identifier, or null
systemId - The system identifier, or null

setEncoding

public void setEncoding(String encoding)
Sets the encoding for this output method. If no encoding was specified, the default is always "UTF-8". Make sure the encoding is compatible with the one used by the java.io.Writer.
Parameters:
encoding - The encoding, or null

setEncoding

public void setEncoding(EncodingInfo encInfo)
Sets the encoding for this output method with an EncodingInfo instance.

setIndent

public void setIndent(int indent)
Parameters:
indent - The indentation, or zero

setIndenting

public void setIndenting(boolean on)
Parameters:
on - True if indentation should be on

setLineSeparator

public void setLineSeparator(String lineSeparator)
Sets the line separator. The default is the Web line separator (\n). The machine's line separator can be obtained from the system property line.separator, but is only useful if the document is edited on machines of the same type. For general documents, use the Web line separator.
Parameters:
lineSeparator - The specified line separator

setLineWidth

public void setLineWidth(int lineWidth)
Parameters:
lineWidth - The line width to use, zero for default

setMediaType

public void setMediaType(String mediaType)
Sets the media type.
Parameters:
mediaType - The specified media type

setMethod

public void setMethod(String method)
Sets the method for this output format.
Parameters:
method - The output method, or null

setNonEscapingElements

public void setNonEscapingElements(String[] nonEscapingElements)
Sets the list of elements for which text node children should be output unescaped (no character references).
Parameters:
nonEscapingElements - List of unescaped element tag names

setOmitComments

public void setOmitComments(boolean omit)
Sets comment omitting on and off.
Parameters:
omit - True if comments should be ommited

setOmitDocumentType

public void setOmitDocumentType(boolean omit)
Sets DOCTYPE declaration omitting on and off.
Parameters:
omit - True if DOCTYPE declaration should be ommited

setOmitXMLDeclaration

public void setOmitXMLDeclaration(boolean omit)
Sets XML declaration omitting on and off.
Parameters:
omit - True if XML declaration should be ommited

setPreserveEmptyAttributes

public void setPreserveEmptyAttributes(boolean preserve)
Sets the preserveEmptyAttribute flag. If flag is false, then' attributes with empty string values are output as the attribute name only (in HTML mode).
Parameters:
preserve - the preserve flag

setPreserveSpace

public void setPreserveSpace(boolean preserve)
Sets space preserving as the default behavior. The default is space stripping and all elements that do not specify otherwise or use the default value will not preserve spaces.
Parameters:
preserve - True if spaces should be preserved

setStandalone

public void setStandalone(boolean standalone)
Sets document DTD standalone. The public and system identifiers must be null for the document to be serialized as standalone.
Parameters:
standalone - True if document DTD is standalone

setVersion

public void setVersion(String version)
Sets the version for this output method. For XML the value would be "1.0", for HTML it would be "4.0".
Parameters:
version - The output method version, or null

whichDoctypePublic

public static String whichDoctypePublic(Document doc)
Returns the document type public identifier specified for this document, or null.

whichDoctypeSystem

public static String whichDoctypeSystem(Document doc)
Returns the document type system identifier specified for this document, or null.

whichMediaType

public static String whichMediaType(String method)
Returns the suitable media format for a document output with the specified method.

whichMethod

public static String whichMethod(Document doc)
Determine the output method for the specified document. If the document is an instance of org.w3c.dom.html.HTMLDocument then the method is said to be html. If the root element is 'html' and all text nodes preceding the root element are all whitespace, then the method is said to be html. Otherwise the method is xml.
Parameters:
doc - The document to check
Returns:
The suitable method

Copyright B) 1999-2005 Apache XML Project. All Rights Reserved.