Class XMLOutputter
- All Implemented Interfaces:
Cloneable
This class is a fork from JDOM 1.0 modified to preserve CData sections and comments.
Outputs a JDOM document as a stream of bytes. The outputter 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
. For whitespace-normalized
output, use Format.getPrettyFormat().Format.getCompactFormat()
There are methods to print any of
the standard JDOM classes, including Document and Element, to either a Writer
or an OutputStream. 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
output(...) method. The default encoding is
UTF-8.Format.setEncoding(String)
The methods are for
convenience only; for top performance you should call one of the outputString(...) methods and pass in your own Writer or
OutputStream if possible.output(...)
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 use Format.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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classOur own null subclass of NamespaceStack. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionXMLOutputter(Format format) This will create anXMLOutputterwith the specified format characteristics.XMLOutputter(XMLOutputter that) This will create anXMLOutputterwith all the options as set in the givenXMLOutputter. -
Method Summary
Modifier and TypeMethodDescriptionclone()Returns a copy of this XMLOutputter.private XMLOutputter.NamespaceStackFactory for making new NamespaceStack objects.private booleanendsWithWhite(String str) This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes.This will take the three pre-defined entities in XML 1.0 (used specifically in XML elements) and convert their character representation to the appropriate entity reference, suitable for XML element content.Returns the current format in use by the outputter.private voidThis will print indents (only if the newlines flag was set totrue, and indent is non-null).private booleanisAllWhitespace(Object obj) private static booleanisWhitespace(char c) private WritermakeWriter(OutputStream out) Get an OutputStreamWriter, using prefered encoding (seeFormat.setEncoding(String)).private static WritermakeWriter(OutputStream out, String enc) Get an OutputStreamWriter, use specified encoding.private voidThis will print a new line only if the newlines flag was set to true.private static intnextNonText(List<?> content, int start) voidoutput(List<?> list, OutputStream out) This will handle printing out a list of nodes.voidThis will handle printing out a list of nodes.voidoutput(org.jdom2.CDATA cdata, OutputStream out) Print out anode.CDATAvoidPrint out anode.CDATAvoidoutput(org.jdom2.Comment comment, OutputStream out) Print out a.CommentvoidPrint out a.Commentvoidoutput(org.jdom2.DocType doctype, OutputStream out) Print out the.DocTypevoidPrint out the.DocTypevoidoutput(org.jdom2.Document doc, OutputStream out) This will print theDocumentto the given output stream.voidThis will print theDocumentto the given Writer.voidoutput(org.jdom2.Element element, OutputStream out) Print out an, including itsElements, and all contained (child) elements, etc.AttributevoidPrint out an, including itsElements, and all contained (child) elements, etc.Attributevoidoutput(org.jdom2.EntityRef entity, OutputStream out) Print out a.EntityRefvoidPrint out a.EntityRefvoidoutput(org.jdom2.ProcessingInstruction pi, OutputStream out) Print out a.ProcessingInstructionvoidPrint out a.ProcessingInstructionvoidoutput(org.jdom2.Text text, OutputStream out) Print out anode.TextvoidPrint out anode.TextvoidoutputElementContent(org.jdom2.Element element, OutputStream out) This will handle printing out an's content only, not including its tag, and attributes.ElementvoidoutputElementContent(org.jdom2.Element element, Writer out) This will handle printing out an's content only, not including its tag, and attributes.ElementoutputString(List<?> list) Return a string representing a list of nodes.outputString(org.jdom2.CDATA cdata) Return a string representing a CDATA node.outputString(org.jdom2.Comment comment) Return a string representing a comment.outputString(org.jdom2.DocType doctype) Return a string representing a DocType.outputString(org.jdom2.Document doc) Return a string representing a document.outputString(org.jdom2.Element element) Return a string representing an element.outputString(org.jdom2.EntityRef entity) Return a string representing an entity.outputString(org.jdom2.ProcessingInstruction pi) Return a string representing a PI.outputString(org.jdom2.Text text) Return a string representing a Text node.private voidprintAdditionalNamespaces(Writer out, org.jdom2.Element element, XMLOutputter.NamespaceStack namespaces) protected voidprintAttributes(Writer out, List<?> attributes, org.jdom2.Element parent, XMLOutputter.NamespaceStack namespaces) This will handle printing of alist.Attributeprotected voidprintCDATA(Writer out, org.jdom2.CDATA cdata) This will handle printing oftext.CDATAprotected voidprintComment(Writer out, org.jdom2.Comment comment) This will handle printing of comments.private voidprintContentRange(Writer out, List<?> content, int start, int end, int level, XMLOutputter.NamespaceStack namespaces) This will handle printing of content within a given range.protected voidprintDeclaration(Writer out, org.jdom2.Document doc, String encoding) This will handle printing of the declaration.protected voidprintDocType(Writer out, org.jdom2.DocType docType) This handle printing the DOCTYPE declaration if one exists.protected voidprintElement(Writer out, org.jdom2.Element element, int level, XMLOutputter.NamespaceStack namespaces) This will handle printing of a, itsElements, and all contained (child) elements, etc.Attributeprivate voidprintElementNamespace(Writer out, org.jdom2.Element element, XMLOutputter.NamespaceStack namespaces) protected voidprintEntityRef(Writer out, org.jdom2.EntityRef entity) This will handle printing a.EntityRefprivate voidprintNamespace(Writer out, org.jdom2.Namespace ns, XMLOutputter.NamespaceStack namespaces) This will handle printing of any neededdeclarations.Namespaceprotected voidprintProcessingInstruction(Writer out, org.jdom2.ProcessingInstruction pi) This will handle printing of processing instructions.private voidprintQualifiedName(Writer out, org.jdom2.Attribute a) private voidprintQualifiedName(Writer out, org.jdom2.Element e) private voidprintString(Writer out, String str) This will handle printing a string.protected voidThis will handle printing ofstrings.Textprivate voidprintTextRange(Writer out, List<?> content, int start, int end) This will handle printing of a sequence oforCDATAnodes.TextvoidSets the new format logic for the outputter.private intskipLeadingWhite(List<?> content, int start) private intskipTrailingWhite(List<?> content, int start) private booleanstartsWithWhite(String str) toString()Return a string listing of the settings for this XMLOutputter instance.
-
Field Details
-
userFormat
-
PRESERVE_FORMAT
-
currentFormat
-
escapeOutput
private boolean escapeOutputWhether output escaping is enabled for the being processed Element - default istrue
-
-
Constructor Details
-
XMLOutputter
public XMLOutputter() -
XMLOutputter
This will create anXMLOutputterwith the specified format characteristics. Note the format object is cloned internally before use. -
XMLOutputter
This will create anXMLOutputterwith all the options as set in the givenXMLOutputter. Note thatXMLOutputter two = (XMLOutputter)one.clone();would work equally well.- Parameters:
that- the XMLOutputter to clone
-
-
Method Details
-
setFormat
Sets the new format logic for the outputter. Note the Format object is cloned internally before use.- Parameters:
newFormat- the format to use for output
-
getFormat
Returns the current format in use by the outputter. Note the Format object returned is a clone of the one used internally. -
output
This will print theDocumentto the given output stream. The characters are printed using the encoding specified in the constructor, or a default of UTF-8.- Parameters:
doc-Documentto format.out-OutputStreamto use.- Throws:
IOException- - if there's any problem writing.
-
output
Print out the.DocType- Parameters:
doctype-DocTypeto output.out-OutputStreamto use.- Throws:
IOException
-
output
Print out an, including itsElements, and all contained (child) elements, etc.Attribute- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException
-
outputElementContent
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
-
output
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-OutputStreamto use.- Throws:
IOException
-
output
Print out anode.CDATA- Parameters:
cdata-CDATAto output.out-OutputStreamto use.- Throws:
IOException
-
output
Print out anode. Perfoms the necessary entity escaping and whitespace stripping.Text- Parameters:
text-Textto output.out-OutputStreamto use.- Throws:
IOException
-
output
Print out a.Comment- Parameters:
comment-Commentto output.out-OutputStreamto use.- Throws:
IOException
-
output
Print out a.ProcessingInstruction- Parameters:
pi-ProcessingInstructionto output.out-OutputStreamto use.- Throws:
IOException
-
output
Print out a.EntityRef- Parameters:
entity-EntityRefto output.out-OutputStreamto use.- Throws:
IOException
-
makeWriter
Get an OutputStreamWriter, using prefered encoding (seeFormat.setEncoding(String)).- Throws:
UnsupportedEncodingException
-
makeWriter
Get an OutputStreamWriter, use specified encoding.- Throws:
UnsupportedEncodingException
-
output
This will print the
Documentto 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.
-
output
Print out the.DocType- Parameters:
doctype-DocTypeto output.out-Writerto use.- Throws:
IOException
-
output
Print out an, including itsElements, and all contained (child) elements, etc.Attribute- Parameters:
element-Elementto output.out-Writerto use.- Throws:
IOException
-
outputElementContent
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
-
output
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
-
output
Print out anode.CDATA- Parameters:
cdata-CDATAto output.out-Writerto use.- Throws:
IOException
-
output
Print out anode. Perfoms the necessary entity escaping and whitespace stripping.Text- Parameters:
text-Textto output.out-Writerto use.- Throws:
IOException
-
output
Print out a.Comment- Parameters:
comment-Commentto output.out-Writerto use.- Throws:
IOException
-
output
Print out a.ProcessingInstruction- Parameters:
pi-ProcessingInstructionto output.out-Writerto use.- Throws:
IOException
-
output
Print out a.EntityRef- Parameters:
entity-EntityRefto output.out-Writerto use.- Throws:
IOException
-
outputString
Return a string representing a document. Uses an internal StringWriter. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
doc-Documentto format.
-
outputString
Return a string representing a DocType. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
doctype-DocTypeto format.
-
outputString
Return a string representing an element. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
element-Elementto format.
-
outputString
-
outputString
Return a string representing a CDATA node. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
cdata-CDATAto format.
-
outputString
Return a string representing a Text node. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
text-Textto format.
-
outputString
Return a string representing a comment. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
comment-Commentto format.
-
outputString
Return a string representing a PI. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
pi-ProcessingInstructionto format.
-
outputString
Return a string representing an entity. Warning: a String is Unicode, which may not match the outputter's specified encoding.- Parameters:
entity-EntityRefto format.
-
printDeclaration
protected void printDeclaration(Writer out, org.jdom2.Document doc, String encoding) throws IOException This will handle printing of the declaration. Assumes XML version 1.0 since we don't directly know.- Parameters:
out-Writerto use.doc-Documentwhose declaration to write.encoding- The encoding to add to the declaration- Throws:
IOException
-
printDocType
This handle printing the DOCTYPE declaration if one exists.- Parameters:
out-Writerto use.docType-Documentwhose declaration to write.- Throws:
IOException
-
printComment
This will handle printing of comments.- Parameters:
out-Writerto use.comment-Commentto write.- Throws:
IOException
-
printProcessingInstruction
protected void printProcessingInstruction(Writer out, org.jdom2.ProcessingInstruction pi) throws IOException This will handle printing of processing instructions.- Parameters:
out-Writerto use.pi-ProcessingInstructionto write.- Throws:
IOException
-
printEntityRef
This will handle printing a. Only the entity reference such asEntityRef&entity;will be printed. However, subclasses are free to override this method to print the contents of the entity instead.- Parameters:
out-Writerto use.entity-EntityRefto output.- Throws:
IOException
-
printCDATA
This will handle printing oftext.CDATA- Parameters:
out-Writerto use.cdata-CDATAto output.- Throws:
IOException
-
printText
This will handle printing ofstrings.Text- Parameters:
out-Writerto use.text-Textto write.- Throws:
IOException
-
printString
This will handle printing a string. Escapes the element entities, trims interior whitespace, etc. if necessary.- Throws:
IOException
-
printElement
protected void printElement(Writer out, org.jdom2.Element element, int level, XMLOutputter.NamespaceStack namespaces) throws IOException This will handle printing of a, itsElements, and all contained (child) elements, etc.Attribute- Parameters:
out-Writerto use.element-Elementto output.level-intlevel of indention.namespaces-Liststack of Namespaces in scope.- Throws:
IOException
-
printContentRange
private void printContentRange(Writer out, List<?> content, int start, int end, int level, XMLOutputter.NamespaceStack namespaces) throws IOException This will handle printing of content within a given range. The range to print is specified in typical Java fashion; the starting index is inclusive, while the ending index is exclusive.- Parameters:
out-Writerto use.content-Listof content to outputstart- index of first content node (inclusive.end- index of last content node (exclusive).level-intlevel of indentation.namespaces-Liststack of Namespaces in scope.- Throws:
IOException
-
printTextRange
This will handle printing of a sequence oforCDATAnodes. It is an error to have any other pass this method any other type of node.Text- Parameters:
out-Writerto use.content-Listof content to outputstart- index of first content node (inclusive).end- index of last content node (exclusive).- Throws:
IOException
-
printNamespace
private void printNamespace(Writer out, org.jdom2.Namespace ns, XMLOutputter.NamespaceStack namespaces) throws IOException This will handle printing of any neededdeclarations.Namespace- Parameters:
out-Writerto use.ns-Namespaceto print definition of- Throws:
IOException
-
printAttributes
protected void printAttributes(Writer out, List<?> attributes, org.jdom2.Element parent, XMLOutputter.NamespaceStack namespaces) throws IOException This will handle printing of alist.Attribute- Parameters:
out-Writerto useattributes-Listof Attribute objcts- Throws:
IOException
-
printElementNamespace
private void printElementNamespace(Writer out, org.jdom2.Element element, XMLOutputter.NamespaceStack namespaces) throws IOException - Throws:
IOException
-
printAdditionalNamespaces
private void printAdditionalNamespaces(Writer out, org.jdom2.Element element, XMLOutputter.NamespaceStack namespaces) throws IOException - Throws:
IOException
-
newline
This will print a new line only if the newlines flag was set to true.- Parameters:
out-Writerto use- Throws:
IOException
-
indent
This will print indents (only if the newlines flag was set totrue, and indent is non-null).- Parameters:
out-Writerto uselevel- current indent level (number of tabs)- Throws:
IOException
-
skipLeadingWhite
-
skipTrailingWhite
-
nextNonText
-
isAllWhitespace
-
startsWithWhite
-
endsWithWhite
-
isWhitespace
private static boolean isWhitespace(char c) -
escapeAttributeEntities
This will take the pre-defined entities in XML 1.0 and convert their character representation to the appropriate entity reference, suitable for XML attributes. It does not convert the single quote (') because it's not necessary as the outputter writes attributes surrounded by double-quotes.- Parameters:
str-Stringinput to escape.- Returns:
Stringwith escaped content.
-
escapeElementEntities
This will take the three pre-defined entities in XML 1.0 (used specifically in XML elements) and convert their character representation to the appropriate entity reference, suitable for XML element content.- Parameters:
str-Stringinput to escape.- Returns:
Stringwith escaped content.
-
clone
-
toString
-
createNamespaceStack
Factory for making new NamespaceStack objects. The NamespaceStack created is actually an inner class extending the package protected NamespaceStack, as a way to make NamespaceStack "friendly" toward subclassers. -
printQualifiedName
- Throws:
IOException
-
printQualifiedName
- Throws:
IOException
-