Class XmlWriterSupport
java.lang.Object
org.pentaho.reporting.libraries.xmlns.writer.XmlWriterSupport
A support class for writing XML files.
- Author:
- Thomas Morgner
-
Field Summary
Modifier and TypeFieldDescriptionstatic final boolean
A constant for close.static final int
A constant for controlling the indent function.static final int
A constant for controlling the indent function.static final boolean
A constant for open.static final int
A constant for controlling the indent function. -
Constructor Summary
ConstructorDescriptionDefault Constructor.XmlWriterSupport
(TagDescription safeTags, String indentString) Creates a new support instance.XmlWriterSupport
(TagDescription safeTags, String indentString, String lineseparator) Create a new XmlWriterSupport instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addImpliedNamespace
(String uri, String prefix) Adds an implied namespace to the document.protected DeclaredNamespaces
Computes the current collection of defined namespaces.void
copyNamespaces
(XmlWriterSupport writerSupport) Copies all currently declared namespaces of the given XmlWriterSupport instance as new implied namespaces into this instance.int
Returns the indent level that should be added to the automaticly computed indentation.int
Returns the current indention level.Returns the line separator.Returns all namespaces as properties-collection.Returns the list of safe tags.void
Indent the line.void
indentForClose
(Writer writer) Indent the line.boolean
Checks, whether the XML writer should always add a namespace prefix to the attributes.boolean
Checks, whether attributes of the same namespace as the current element should be written without a prefix.boolean
Checks, whether the HTML compatibility mode is enabled.boolean
Checks, whether the currently generated line of text is empty.boolean
isNamespaceDefined
(String uri) Checks, whether the given URI is defined as valid namespace.boolean
isNamespacePrefixDefined
(String prefix) Checks, whether the given namespace prefix is defined.boolean
Checks, whether the written XML file should end with an empty line.static String
Normalises a string, replacing certain characters with their escape sequences so that the XML text is not corrupted.normalizeLocal
(String s, boolean transformNewLine) Normalizes the given string using a shared buffer.void
setAdditionalIndent
(int additionalIndent) Defines the indent level that should be added to the automaticly computed indentation.void
setAlwaysAddNamespace
(boolean alwaysAddNamespace) Defines, whether the XML writer should always add a namespace prefix to the attributes.void
setAssumeDefaultNamespace
(boolean assumeDefaultNamespace) Defines, whether attributes of the same namespace as the current element should be written without a prefix.void
setEncoding
(String encoding) void
setHtmlCompatiblityMode
(boolean htmlCompatiblityMode) Enables or disables the HTML Compatibility mode.void
setLineEmpty
(boolean lineEmpty) A marker flag to track, wether the current line is empty.void
setWriteFinalLinebreak
(boolean writeFinalLinebreak) Defines, whether the written XML file should end with an empty line.void
Writes a closing XML tag.void
writeComment
(Writer writer, String comment) Writes a comment into the generated xml file.void
writeNewLine
(Writer writer) Writes a linebreak to the writer.void
Writes an opening XML tag that has no attributes.void
writeTag
(Writer w, String namespace, String name, String attributeName, String attributeValue, boolean close) Writes an opening XML tag with an attribute/value pair.void
writeTag
(Writer w, String namespaceUri, String name, AttributeList attributes, boolean close) Writes an opening XML tag along with a list of attribute/value pairs.void
writeTextNormalized
(Writer writer, String s, boolean transformNewLine) Normalizes the given string and writes the result directly to the stream.void
writeXmlDeclaration
(Writer writer, String encoding) Writes the XML declaration that usually appears at the top of every XML file.
-
Field Details
-
OPEN_TAG_INCREASE
public static final int OPEN_TAG_INCREASEA constant for controlling the indent function.- See Also:
-
CLOSE_TAG_DECREASE
public static final int CLOSE_TAG_DECREASEA constant for controlling the indent function.- See Also:
-
INDENT_ONLY
public static final int INDENT_ONLYA constant for controlling the indent function.- See Also:
-
CLOSE
public static final boolean CLOSEA constant for close.- See Also:
-
OPEN
public static final boolean OPENA constant for open.- See Also:
-
-
Constructor Details
-
XmlWriterSupport
public XmlWriterSupport()Default Constructor. The created XMLWriterSupport will not have no safe tags and starts with an indention level of 0. -
XmlWriterSupport
Creates a new support instance.- Parameters:
safeTags
- the tags that are safe for line breaks.indentString
- the indent string.
-
XmlWriterSupport
Create a new XmlWriterSupport instance.- Parameters:
safeTags
- the tags that are safe for line breaks.indentString
- the indent string.lineseparator
- the lineseparator that should be used for writing XML files.
-
-
Method Details
-
isHtmlCompatiblityMode
public boolean isHtmlCompatiblityMode()Checks, whether the HTML compatibility mode is enabled. In HTML compatibility mode, closed empty tags will have a space between the tagname and the close-indicator.- Returns:
- true, if the HTML compatiblity mode is enabled, false otherwise.
-
setHtmlCompatiblityMode
public void setHtmlCompatiblityMode(boolean htmlCompatiblityMode) Enables or disables the HTML Compatibility mode. In HTML compatibility mode, closed empty tags will have a space between the tagname and the close-indicator.- Parameters:
htmlCompatiblityMode
- true, if the HTML compatiblity mode is enabled, false otherwise.
-
isAlwaysAddNamespace
public boolean isAlwaysAddNamespace()Checks, whether the XML writer should always add a namespace prefix to the attributes. The XML specification leaves it up to the application on how to handle unqualified attributes. If this mode is enabled, all attributes will always be fully qualified - which removed the ambugity but may not be compatible with simple, non namespace aware parsers.- Returns:
- true, if all attributes should be qualified, false otherwise.
-
setAlwaysAddNamespace
public void setAlwaysAddNamespace(boolean alwaysAddNamespace) Defines, whether the XML writer should always add a namespace prefix to the attributes. The XML specification leaves it up to the application on how to handle unqualified attributes. If this mode is enabled, all attributes will always be fully qualified - which removed the ambuigity but may not be compatible with simple, non namespace aware parsers.- Parameters:
alwaysAddNamespace
- set to true, if all attributes should be qualified, false otherwise.
-
getAdditionalIndent
public int getAdditionalIndent()Returns the indent level that should be added to the automaticly computed indentation.- Returns:
- the indent level.
-
setAdditionalIndent
public void setAdditionalIndent(int additionalIndent) Defines the indent level that should be added to the automaticly computed indentation.- Parameters:
additionalIndent
- the indent level.
-
getLineSeparator
Returns the line separator.- Returns:
- the line separator.
-
writeXmlDeclaration
Writes the XML declaration that usually appears at the top of every XML file.- Parameters:
encoding
- the encoding that should be declared (this has to match the encoding of the writer, or funny things may happen when parsing the xml file later).- Throws:
IOException
- if there is a problem writing to the character stream.
-
setEncoding
-
writeTag
Writes an opening XML tag that has no attributes.- Parameters:
w
- the writer.namespaceUri
- the namespace URI for the element.name
- the tag name.- Throws:
IOException
- if there is an I/O problem.
-
writeCloseTag
Writes a closing XML tag.- Parameters:
w
- the writer.- Throws:
IOException
- if there is an I/O problem.
-
writeNewLine
Writes a linebreak to the writer.- Parameters:
writer
- the writer.- Throws:
IOException
- if there is a problem writing to the character stream.
-
isLineEmpty
public boolean isLineEmpty()Checks, whether the currently generated line of text is empty.- Returns:
- true, if the line is empty, false otherwise.
-
setLineEmpty
public void setLineEmpty(boolean lineEmpty) A marker flag to track, wether the current line is empty. This influences the indention.- Parameters:
lineEmpty
- defines, whether the current line should be treated as empty line.
-
writeTag
public void writeTag(Writer w, String namespace, String name, String attributeName, String attributeValue, boolean close) throws IOException Writes an opening XML tag with an attribute/value pair.- Parameters:
w
- the writer.namespace
- the namespace URI for the elementname
- the tag name.attributeName
- the attribute name.attributeValue
- the attribute value.close
- controls whether the tag is closed.- Throws:
IOException
- if there is an I/O problem.
-
addImpliedNamespace
Adds an implied namespace to the document. Such a namespace is not explicitly declared, it is assumed that the xml-parser knows the prefix by some other means. Using implied namespaces for standalone documents is almost always a bad idea.- Parameters:
uri
- the uri of the namespace.prefix
- the defined prefix.
-
copyNamespaces
Copies all currently declared namespaces of the given XmlWriterSupport instance as new implied namespaces into this instance.- Parameters:
writerSupport
- the Xml-writer from where to copy the declared namespaces.
-
isNamespaceDefined
Checks, whether the given URI is defined as valid namespace.- Parameters:
uri
- the uri of the namespace.- Returns:
- true, if there's a namespace defined, false otherwise.
-
isNamespacePrefixDefined
Checks, whether the given namespace prefix is defined.- Parameters:
prefix
- the namespace prefix.- Returns:
- true, if the prefix is defined, false otherwise.
-
getNamespaces
Returns all namespaces as properties-collection. This reflects the currently defined namespaces, therefore calls to writeOpenTag(..) might cause this method to return different collections.- Returns:
- the defined namespaces.
-
computeNamespaces
Computes the current collection of defined namespaces.- Returns:
- the namespaces declared at this writing position.
-
writeTag
public void writeTag(Writer w, String namespaceUri, String name, AttributeList attributes, boolean close) throws IOException Writes an opening XML tag along with a list of attribute/value pairs.- Parameters:
w
- the writer.namespaceUri
- the namespace uri for the element (can be null).name
- the tag name.attributes
- the attributes.close
- controls whether the tag is closed.- Throws:
IOException
- if there is an I/O problem.
-
normalizeLocal
Normalizes the given string using a shared buffer.- Parameters:
s
- the string that should be XML-Encoded.transformNewLine
- a flag controling whether to transform newlines into character-entities.- Returns:
- the transformed string.
- Throws:
IOException
-
writeTextNormalized
public void writeTextNormalized(Writer writer, String s, boolean transformNewLine) throws IOException Normalizes the given string and writes the result directly to the stream.- Parameters:
writer
- the writer that should receive the normalized content.s
- the string that should be XML-Encoded.transformNewLine
- a flag controling whether to transform newlines into character-entities.- Throws:
IOException
- if writing to the stream failed.
-
normalize
Normalises a string, replacing certain characters with their escape sequences so that the XML text is not corrupted.- Parameters:
s
- the string.transformNewLine
- true, if a newline in the string should be converted into a character entity.- Returns:
- the normalised string.
-
indent
Indent the line. Called for proper indenting in various places.- Parameters:
writer
- the writer which should receive the indentention.- Throws:
IOException
- if writing the stream failed.
-
indentForClose
Indent the line. Called for proper indenting in various places.- Parameters:
writer
- the writer which should receive the indentention.- Throws:
IOException
- if writing the stream failed.
-
getTagDescription
Returns the list of safe tags.- Returns:
- The list.
-
writeComment
Writes a comment into the generated xml file.- Parameters:
writer
- the writer.comment
- the comment text- Throws:
IOException
- if there is a problem writing to the character stream.
-
isAssumeDefaultNamespace
public boolean isAssumeDefaultNamespace()Checks, whether attributes of the same namespace as the current element should be written without a prefix. Attributes without a prefix are considered to be not in any namespace at all. How to treat such attributes is implementation dependent. (Appendix A; Section 6.2 of the XmlNamespaces recommendation)- Returns:
- true, if attributes in the element's namespace should be written without a prefix, false to write all attributes with a prefix.
-
setAssumeDefaultNamespace
public void setAssumeDefaultNamespace(boolean assumeDefaultNamespace) Defines, whether attributes of the same namespace as the current element should be written without a prefix. Attributes without a prefix are considered to be not in any namespace at all. How to treat such attributes is implementation dependent. (Appendix A; Section 6.2 of the XmlNamespaces recommendation)- Parameters:
assumeDefaultNamespace
- true, if attributes in the element's namespace should be written without a prefix, false to write all attributes with a prefix.
-
getCurrentIndentLevel
public int getCurrentIndentLevel()Returns the current indention level.- Returns:
- the indention level.
-
setWriteFinalLinebreak
public void setWriteFinalLinebreak(boolean writeFinalLinebreak) Defines, whether the written XML file should end with an empty line.- Parameters:
writeFinalLinebreak
- true, if an linebreak should be added at the end of the file, false otherwise.
-
isWriteFinalLinebreak
public boolean isWriteFinalLinebreak()Checks, whether the written XML file should end with an empty line.- Returns:
- true, if an linebreak should be added at the end of the file, false otherwise.
-