Package com.yahoo.text
Class XMLWriter
java.lang.Object
java.io.Writer
com.yahoo.text.GenericWriter
com.yahoo.text.ForwardWriter
com.yahoo.text.XMLWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
A stream wrapper which contains utility methods for writing xml.
All methods return this for convenience.
The methods of this writer can be used in conjunction with writing tags in raw form directly to the writer if some care is taken to close start tags and insert line breaks explicitly. If all content is written using these methods, start tags are closed and newlines inserted automatically as appropriate.
- Author:
- bratseth, baldersheim
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates an XML wrapper of a writer having maxIndentLevel=10 and maxLineSeparatorLevel=1Creates an XML wrapper of a writer having maxIndentLevel=10 and maxLineSeparatorLevel=1Creates an XML wrapper of a writer having maxLineSeparatorLevel=1Creates an XML wrapper of a writer having maxLineSeparatorLevel=1Creates an XML wrapper of a writerCreates an XML wrapper of a writer -
Method Summary
Modifier and TypeMethodDescriptionattribute
(Utf8String name, boolean value) Writes an attribute by its utf8 valueattribute
(Utf8String name, double value) Writes an attribute by its utf8 valueattribute
(Utf8String name, long value) Writes an attribute by its utf8 valueattribute
(Utf8String name, AbstractUtf8Array value) Writes an attribute by its utf8 valueattribute
(Utf8String name, Number value) Writes an attribute by its utf8 valueattribute
(Utf8String name, String value) Writes an attribute by XML.xmlEscape(value.toString(),false)Closes the start tag.closeTag()
XML escapes and writes the content.toString().escapedAsciiContent
(String content, boolean multiline) Writes the given US-ASCII only string as-is.escapedContent
(String content, boolean multiline) Writes the given string as-is.forceAttribute
(Utf8String name, Object value) Writes an attribute by XML.xmlEscape(value.toString(),false)forceAttribute
(String name, Object value) static XMLWriter
Returns the input writer as-is if it is an XMLWriter instance.static XMLWriter
Returns the input writer as-is if it is an XMLWriter instance.boolean
isIn
(Utf8String containingTag) Returns true if the immediate parent (i.e the last element in openTags) is the tag with the given nameboolean
openTag
(Utf8String tag) openTags()
Returns a read only view of the currently open tags we are within, sorted by highest to lowest in the hierarchy Only used for testing.void
Writes the first line of an XML file
-
Constructor Details
-
XMLWriter
Creates an XML wrapper of a writer having maxIndentLevel=10 and maxLineSeparatorLevel=1- Parameters:
writer
- the writer to which this writers (accessible from this by getWrapped)
-
XMLWriter
Creates an XML wrapper of a writer having maxIndentLevel=10 and maxLineSeparatorLevel=1- Parameters:
writer
- the writer to which this writers (accessible from this by getWrapped)markupIsAscii
- set to false to make this encode markup (tags, attributes). By default encoding is skipped if the underlying stream uses utf encoding for performance (yes, this matters)
-
XMLWriter
Creates an XML wrapper of a writer having maxLineSeparatorLevel=1- Parameters:
writer
- the writer to which this writers (accessible from this by getWrapped)maxIndentLevel
- the max number of tag levels for which we'll continue to indent, or -1 to never indent. The top level tag is level 0, etc.
-
XMLWriter
Creates an XML wrapper of a writer having maxLineSeparatorLevel=1- Parameters:
writer
- the writer to which this writers (accessible from this by getWrapped)maxIndentLevel
- the max number of tag levels for which we'll continue to indent, or -1 to never indent. The top level tag is level 0, etc.markupIsAscii
- set to false to make this encode markup (tags, attributes). By default encoding is skipped if the underlying stream uses utf encoding for performance (yes, this matters)
-
XMLWriter
Creates an XML wrapper of a writer- Parameters:
writer
- the writer to which this writers (accessible from this by getWrapped)maxIndentLevel
- the max number of tag levels for which we'll continue to indent, or -1 to never indent. The top level tag is level 0, etc.maxLineSeparatorLevel
- the max number of tag levels for which we'll add a blank line separator, or -1 to never add line separators. The top level tag is level 0, etc.
-
XMLWriter
public XMLWriter(Writer writer, int maxIndentLevel, int maxLineSeparatorLevel, boolean markupIsAscii) Creates an XML wrapper of a writer- Parameters:
writer
- the writer to which this writers (accessible from this by getWrapped)maxIndentLevel
- the max number of tag levels for which we'll continue to indent, or -1 to never indent. The top level tag is level 0, etc.maxLineSeparatorLevel
- the max number of tag levels for which we'll add a blank line separator, or -1 to never add line separators. The top level tag is level 0, etc.markupIsAscii
- set to false to make this encode markup (tags, attributes). By default encoding is skipped if the underlying stream uses utf encoding for performance (yes, this matters)
-
-
Method Details
-
from
Returns the input writer as-is if it is an XMLWriter instance. Returns new XMLWriter(writer) otherwise -
from
Returns the input writer as-is if it is an XMLWriter instance. Returns new XMLWriter(writer) otherwise -
getWrapped
-
xmlHeader
Writes the first line of an XML file -
openTag
-
openTag
-
closeTag
-
closeStartTag
Closes the start tag. Usually, it is not necessary to call this, as the other methods in this will do the right thing as needed. However, this can be called explicitly to allow content or subtags to be written by a regular write call which bypasses the logic in this. If a start tag is not currently open this has no effect. -
forceAttribute
Writes an attribute by XML.xmlEscape(value.toString(),false)- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute. The empty string if the attribute is null or empty
-
forceAttribute
-
attribute
Writes an attribute by its utf8 value- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute. This method does nothing if the value is null or empty
-
attribute
Writes an attribute by its utf8 value- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute. This method does nothing if the value is null.
-
attribute
Writes an attribute by its utf8 value- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute.
-
attribute
Writes an attribute by its utf8 value- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute.
-
attribute
Writes an attribute by its utf8 value- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute. This method does nothing if the value is null or empty
-
attribute
Writes an attribute by XML.xmlEscape(value.toString(),false)- Parameters:
name
- the name of the attribute. An exception is thrown if this is nullvalue
- the value of the attribute. This method does nothing if the value is null or empty
-
attribute
-
content
XML escapes and writes the content.toString(). If the content is null this does nothing but closing the start tag.- Parameters:
content
- the content - output by XML.xmlEscape(content.toString())multiline
- whether the content should be treated as multiline, such that the following end tag should appear on a new line
-
escapedContent
Writes the given string as-is. The content string must be XML escaped before calling this. If the content is null this does nothing but closing the start tag.- Parameters:
content
- the content - output by XML.xmlEscape(content.toString())multiline
- whether the content should be treated as multiline, such that the following end tag should appear on a new line
-
escapedAsciiContent
Writes the given US-ASCII only string as-is. If the content is not US-ASCII only this may cause incorrectly encoded content to be written. This is faster than using escapedContent as transcoding is skipped.The content string must be XML escaped before calling this. If the content is null this does nothing but closing the start tag.
- Parameters:
content
- the content - output by XML.xmlEscape(content.toString())multiline
- whether the content should be treated as multiline, such that the following end tag should appear on a new line
-
openTags
Returns a read only view of the currently open tags we are within, sorted by highest to lowest in the hierarchy Only used for testing. -
isIn
Returns true if the immediate parent (i.e the last element in openTags) is the tag with the given name -
isIn
-