Class XMLOutputter2
- java.lang.Object
-
- org.jdom.output.XMLOutputter2
-
- All Implemented Interfaces:
Cloneable
public final class XMLOutputter2 extends Object implements Cloneable
Outputs a JDOM document as a stream of bytes.The XMLOutputter can manage many styles of document formatting, from untouched to 'pretty' printed. The default is to output the document content exactly as created, but this can be changed by setting a new Format object:
- For pretty-print output, use
.Format.getPrettyFormat() - For whitespace-normalised output, use
.Format.getCompactFormat() - For unmodified-format output, use
.Format.getRawFormat()
There are
methods to print any of the standard JDOM classes to either a Writer or an OutputStream.output(...)Warning: When outputting to a Writer, make sure the writer's encoding matches the encoding setting in the Format object. This ensures the encoding in which the content is written (controlled by the Writer configuration) matches the encoding placed in the document's XML declaration (controlled by the XMLOutputter). Because a Writer cannot be queried for its encoding, the information must be passed to the Format manually in its constructor or via the
method. The default encoding is UTF-8.Format.setEncoding(java.lang.String)The methods
are for convenience only; for top performance you should call one of theoutputString(...)methods and pass in your own Writer or OutputStream if possible.output(...)All of the
output*(...)methods will flush the destination Writer or OutputStream before returning, and none of them willclose()the destination.XML declarations are always printed on their own line followed by a line separator (this doesn't change the semantics of the document). To omit printing of the declaration use
. To omit printing of the encoding in the declaration useFormat.setOmitDeclaration(boolean). Unfortunately there is currently no way to know the original encoding of the document.Format.setOmitEncoding(boolean)Empty elements are by default printed as <empty/>, but this can be configured with
to cause them to be expanded to <empty></empty>.Format.setExpandEmptyElements(boolean)If changing the
Formatsettings are insufficient for your output needs you can customise this XMLOutputter further by setting a differentXMLOutputProcessorwith thesetXMLOutputProcessor(XMLOutputProcessor)method or an appropriate constructor. A fully-enabled Abstract classAbstractXMLOutputProcessoris available to be further extended to your needs if all you want to do is tweak some details.
-
-
Constructor Summary
Constructors Constructor Description XMLOutputter2()XMLOutputter2(Format format)This will create anXMLOutputterwith the specified format characteristics.XMLOutputter2(Format format, XMLOutputProcessor processor)This will create anXMLOutputterwith the specified format characteristics.XMLOutputter2(XMLOutputProcessor processor)This will create anXMLOutputterwith the specified XMLOutputProcessor.XMLOutputter2(XMLOutputter2 that)This will create anXMLOutputterwith the same customisations set in the givenXMLOutputterinstance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description XMLOutputter2clone()Returns a cloned copy of this XMLOutputter.StringescapeAttributeEntities(String str)Escape any characters in the input string in such a way that the returned value is valid as output in an XML Attribute value.StringescapeElementEntities(String str)Escape any characters in the input string in such a way that the returned value is valid as output in an XML Element text.FormatgetFormat()Returns the current format in use by the XMLOutputter.XMLOutputProcessorgetXMLOutputProcessor()Returns the current XMLOutputProcessor instance in use by the XMLOutputter.voidoutput(List<? extends Content> list, OutputStream out)This will handle printing out a list of nodes.voidoutput(List<? extends Content> list, Writer out)This will handle printing out a list of nodes.voidoutput(CDATA cdata, OutputStream out)Print out anode.CDATAvoidoutput(CDATA cdata, Writer out)Print out anode.CDATAvoidoutput(Comment comment, OutputStream out)Print out a.Commentvoidoutput(Comment comment, Writer out)Print out a.Commentvoidoutput(DocType doctype, OutputStream out)This will print theto the given OutputStream.DocTypevoidoutput(DocType doctype, Writer out)Print out the.DocTypevoidoutput(Document doc, OutputStream out)This will print theto the given OutputStream.Documentvoidoutput(Document doc, Writer out)This will print theDocumentto the given Writer.voidoutput(Element element, OutputStream out)voidoutput(Element element, Writer out)voidoutput(EntityRef entity, OutputStream out)Print out a.EntityRefvoidoutput(EntityRef entity, Writer out)Print out an.EntityRefvoidoutput(ProcessingInstruction pi, OutputStream out)Print out a.ProcessingInstructionvoidoutput(ProcessingInstruction pi, Writer out)Print out a.ProcessingInstructionvoidoutput(Text text, OutputStream out)Print out anode.Textvoidoutput(Text text, Writer out)Print out anode.TextvoidoutputElementContent(Element element, OutputStream out)This will handle printing out an's content only, not including its tag, and attributes.ElementvoidoutputElementContent(Element element, Writer out)This will handle printing out an's content only, not including its tag, and attributes.ElementStringoutputElementContentString(Element element)This will handle printing out an's content only, not including its tag, and attributes.ElementStringoutputString(List<? extends Content> list)Return a string representing a List ofContentnodes.StringoutputString(CDATA cdata)Return a string representing aCDATAnode.StringoutputString(Comment comment)Return a string representing aComment.StringoutputString(DocType doctype)Return a string representing aDocType.StringoutputString(Document doc)Return a string representing aDocument.StringoutputString(Element element)Return a string representing anElement.StringoutputString(EntityRef entity)Return a string representing anEntityRef.StringoutputString(ProcessingInstruction pi)Return a string representing aProcessingInstruction.StringoutputString(Text text)Return a string representing aTextnode.voidsetFormat(Format newFormat)Sets the new format logic for the XMLOutputter.voidsetXMLOutputProcessor(XMLOutputProcessor processor)Sets a new XMLOutputProcessor instance for this XMLOutputter.StringtoString()Return a string listing of the settings for this XMLOutputter instance.
-
-
-
Constructor Detail
-
XMLOutputter2
public XMLOutputter2(Format format, XMLOutputProcessor processor)
This will create anXMLOutputterwith the specified format characteristics.Note: the format object is cloned internally before use. If you want to modify the Format after constructing the XMLOutputter you can modify the Format instance
getFormat()returns.- Parameters:
format- The Format instance to use. This instance will be cloned() and as a consequence, changes made to the specified format instance will not be reflected in this XMLOutputter. A null input format indicates that XMLOutputter should use the defaultFormat.getRawFormat()processor- The XMLOutputProcessor to delegate output to. If null the XMLOutputter will use the default XMLOutputProcessor.
-
XMLOutputter2
public XMLOutputter2()
-
XMLOutputter2
public XMLOutputter2(XMLOutputter2 that)
This will create anXMLOutputterwith the same customisations set in the givenXMLOutputterinstance. Note thatXMLOutputter two = one.clone();would work equally well.- Parameters:
that- the XMLOutputter to clone
-
XMLOutputter2
public XMLOutputter2(Format format)
This will create anXMLOutputterwith the specified format characteristics.Note: the format object is cloned internally before use.
- Parameters:
format- The Format instance to use. This instance will be cloned() and as a consequence, changes made to the specified format instance will not be reflected in this XMLOutputter. A null input format indicates that XMLOutputter should use the defaultFormat.getRawFormat()
-
XMLOutputter2
public XMLOutputter2(XMLOutputProcessor processor)
This will create anXMLOutputterwith the specified XMLOutputProcessor.- Parameters:
processor- The XMLOutputProcessor to delegate output to. If null the XMLOutputter will use the default XMLOutputProcessor.
-
-
Method Detail
-
setFormat
public void setFormat(Format newFormat)
Sets the new format logic for the XMLOutputter. Note the Format object is cloned internally before use.- Parameters:
newFormat- the format to use for subsequent output- See Also:
getFormat()
-
getFormat
public Format getFormat()
Returns the current format in use by the XMLOutputter. Note the Format object returned is not a clone of the one used internally, thus, an XMLOutputter instance is able to have it's Format changed by changing the settings on the Format instance returned by this method.- Returns:
- the current Format instance used by this XMLOutputter.
-
getXMLOutputProcessor
public XMLOutputProcessor getXMLOutputProcessor()
Returns the current XMLOutputProcessor instance in use by the XMLOutputter.- Returns:
- the current XMLOutputProcessor instance.
-
setXMLOutputProcessor
public void setXMLOutputProcessor(XMLOutputProcessor processor)
Sets a new XMLOutputProcessor instance for this XMLOutputter. Note the processor object is expected to be thread-safe.- Parameters:
processor- the new XMLOutputProcesor to use for output
-
output
public final void output(Document doc, OutputStream out) throws IOException
This will print theto the given OutputStream. The characters are printed using the encoding specified in the constructor, or a default of UTF-8.Document- Parameters:
doc-Documentto format.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(DocType doctype, OutputStream out) throws IOException
This will print theto the given OutputStream.DocType- Parameters:
doctype-DocTypeto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(Element element, OutputStream out) throws IOException
- Parameters:
element-Elementto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
outputElementContent
public final void outputElementContent(Element element, OutputStream out) throws IOException
This will handle printing out an's content only, not including its tag, and attributes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".Element- Parameters:
element-Elementto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(List<? extends Content> list, OutputStream out) throws IOException
This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".The list is assumed to contain legal JDOM nodes. If other content is coerced on to the list it will cause ClassCastExceptions, and null Lists or null list members will cause NullPointerException.
- Parameters:
list-Listof nodes.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.ClassCastException- if non-Contentis forced in to the listNullPointerException- if the List is null or contains null members.
-
output
public final void output(CDATA cdata, OutputStream out) throws IOException
Print out anode.CDATA- Parameters:
cdata-CDATAto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(Text text, OutputStream out) throws IOException
Print out anode. Perfoms the necessary entity escaping and whitespace stripping.Text- Parameters:
text-Textto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(Comment comment, OutputStream out) throws IOException
Print out a.Comment- Parameters:
comment-Commentto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(ProcessingInstruction pi, OutputStream out) throws IOException
Print out a.ProcessingInstruction- Parameters:
pi-ProcessingInstructionto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public void output(EntityRef entity, OutputStream out) throws IOException
Print out a.EntityRef- Parameters:
entity-EntityRefto output.out-OutputStreamto use.- Throws:
IOException- if there's any problem writing.NullPointerException- if the specified content is null.
-
outputString
public final String outputString(Document doc)
Return a string representing aDocument. Uses an internal StringWriter.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
doc-Documentto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(DocType doctype)
Return a string representing aDocType.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
doctype-DocTypeto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(Element element)
Return a string representing anElement.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
element-Elementto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(List<? extends Content> list)
Return a string representing a List ofContentnodes.
The list is assumed to contain legal JDOM nodes. If other content is coerced on to the list it will cause ClassCastExceptions, and null List members will cause NullPointerException.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
list-Listto format.- Returns:
- the input content formatted as an XML String.
- Throws:
ClassCastException- if non-Contentis forced in to the listNullPointerException- if the List is null or contains null members.
-
outputString
public final String outputString(CDATA cdata)
Return a string representing aCDATAnode.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
cdata-CDATAto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(Text text)
Return a string representing aTextnode.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
text-Textto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(Comment comment)
Return a string representing aComment.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
comment-Commentto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(ProcessingInstruction pi)
Return a string representing aProcessingInstruction.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
pi-ProcessingInstructionto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputString
public final String outputString(EntityRef entity)
Return a string representing anEntityRef.Warning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
entity-EntityRefto format.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
outputElementContentString
public final String outputElementContentString(Element element)
This will handle printing out an's content only, not including its tag, and attributes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".ElementWarning: a String is Unicode, which may not match the outputter's specified encoding.
- Parameters:
element-Elementto output.- Returns:
- the input content formatted as an XML String.
- Throws:
NullPointerException- if the specified content is null.
-
output
public final void output(Document doc, Writer out) throws IOException
This will print theDocumentto the given Writer.Warning: using your own Writer may cause the outputter's preferred character encoding to be ignored. If you use encodings other than UTF-8, we recommend using the method that takes an OutputStream instead.
- Parameters:
doc-Documentto format.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(DocType doctype, Writer out) throws IOException
Print out the.DocType- Parameters:
doctype-DocTypeto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(Element element, Writer out) throws IOException
- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
outputElementContent
public final void outputElementContent(Element element, Writer out) throws IOException
This will handle printing out an's content only, not including its tag, and attributes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".Element- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(List<? extends Content> list, Writer out) throws IOException
This will handle printing out a list of nodes. This can be useful for printing the content of an element that contains HTML, like "<description>JDOM is <b>fun>!</description>".- Parameters:
list-Listof nodes.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(CDATA cdata, Writer out) throws IOException
Print out anode.CDATA- Parameters:
cdata-CDATAto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(Text text, Writer out) throws IOException
Print out anode. Perfoms the necessary entity escaping and whitespace stripping.Text- Parameters:
text-Textto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(Comment comment, Writer out) throws IOException
Print out a.Comment- Parameters:
comment-Commentto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(ProcessingInstruction pi, Writer out) throws IOException
Print out a.ProcessingInstruction- Parameters:
pi-ProcessingInstructionto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
output
public final void output(EntityRef entity, Writer out) throws IOException
Print out an.EntityRef- Parameters:
entity-EntityRefto output.out-Writerto use.- Throws:
IOException- - if there's any problem writing.NullPointerException- if the specified content is null.
-
escapeAttributeEntities
public String escapeAttributeEntities(String str)
Escape any characters in the input string in such a way that the returned value is valid as output in an XML Attribute value.- Parameters:
str- the input String to escape- Returns:
- the escaped version of the input String
-
escapeElementEntities
public String escapeElementEntities(String str)
Escape any characters in the input string in such a way that the returned value is valid as output in an XML Element text.- Parameters:
str- the input String to escape- Returns:
- the escaped version of the input String
-
clone
public XMLOutputter2 clone()
Returns a cloned copy of this XMLOutputter.
-
-