To pretty-print a document (or any node), uses heuristics that take into
account the default values of the display
CSS property for the
elements, according to a reference style sheet (by default, the user agent's
style sheet). And when serializing a Text
node, also allows to
replace a specified subset of codepoints with the proper entity references.
This class could be subclassed to customize the printing.
-
Constructor Summary
ConstructorDescriptionConstruct aDOMWriter
with default settings.DOMWriter
(ExtendedCSSStyleSheet<? extends ExtendedCSSRule> refSheet) Construct aDOMWriter
with a reference style sheet for formatting heuristics. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
afterStartTag
(DOMElement element, SimpleWriter wri) Take decisions after serializing the start tag of an element.protected void
Deepen the indenting level.protected void
endIndentedNode
(Node node, SimpleWriter wri) protected void
endIndentedNodeList
(Node listParent, SimpleWriter wri) protected String
getDisplayProperty
(Element element) Get the value of thedisplay
CSS property as specified in the user agent style sheet for that element.protected boolean
isRawTextElement
(DOMElement element) protected String
replaceByEntities
(String line) Verify if the given line contains characters for which there is the mandate to replace by entity references.serializeToString
(Node root) Serializesroot
into aString
using the configurable serialization provided by this class.int
setEntityCodepoints
(DocumentType docType, int[] entities) Configure the writer to replace the given codePoints by entity references (if the document type allows it).void
setEntityResolver
(EntityResolver2 resolver) Sets the entity resolver to be used to read theDTD
.void
setIndentingUnit
(int whitespaceCount) Sets a whitespace string of thewhitespaceCount
length as the level indenting unit (that is, every indenting depth shall bewhitespaceCount
larger).protected void
startIndentedNode
(Node node, SimpleWriter wri) protected void
startIndentedNodeList
(Node parent, SimpleWriter wri) protected void
updateIndent
(Node node) Update the indenting level to the parent of the given node.protected void
writeAttribute
(Attr attr, SimpleWriter wri) Serialize an element attribute.protected void
writeAttributes
(AttributeNamedNodeMap nodeMap, SimpleWriter wri) Serialize the attributes of an element.protected void
writeCDataSection
(CDATASection data, SimpleWriter wri) Serialize a CDATA section.protected void
writeChildNodes
(Node parent, SimpleWriter wri, boolean indented) Serialize a list of (child) nodes.protected void
writeComment
(Comment comment, SimpleWriter wri, boolean indented) Serialize a comment.protected void
writeDocumentType
(DocumentType docType, SimpleWriter wri) Serialize aDOCTYPE
node.protected void
writeElement
(DOMElement element, SimpleWriter wri, boolean indented) Serialize the given element and descendants, with an initial indenting context.protected void
writeElementContentWhitespace
(Text text, SimpleWriter wri) Serialize element content whitespace.protected void
writeEntityReference
(Node node, SimpleWriter wri) protected void
Write a full indent to the writer.void
writeNode
(Node root, SimpleWriter writer) Serialize the given node and descendants.protected void
writeNode
(Node node, SimpleWriter wri, boolean indented) Serialize the given node and descendants, with an initial indenting context.protected void
writeNonRawText
(Text text, SimpleWriter wri, boolean indented) Serialize aText
node that is not the child of a raw text element (that is, a normalText
node).protected void
Serialize aProcessingInstruction
node.protected void
writeRawText
(Text text, String parentLocalName, SimpleWriter wri) Serialize aText
node that is child of a raw text element.protected void
writeText
(Text text, SimpleWriter wri, boolean indented) Serialize aText
node.protected void
writeTextLine
(String line, SimpleWriter wri) Serialize a line of a normalText
node.static void
writeTree
(Node root, SimpleWriter writer) Create aDOMWriter
object, use it to serialize the given node and descendants.
-
Constructor Details
-
DOMWriter
public DOMWriter()Construct aDOMWriter
with default settings. -
DOMWriter
Construct aDOMWriter
with a reference style sheet for formatting heuristics.Use this constructor when you know that the user agent's sheet is not adequate for the nodes that you are going to print (or that you'll be using always the same reference sheet, and you want to spare the retrieval of the UA sheet for each
writeNode(Node, SimpleWriter)
call).- Parameters:
refSheet
- the reference sheet.
-
-
Method Details
-
setEntityCodepoints
public int setEntityCodepoints(DocumentType docType, int[] entities) throws SAXException, IOException Configure the writer to replace the given codePoints by entity references (if the document type allows it).If you want to use a specific
EntityResolver2
in the process, be sure to callsetEntityResolver(EntityResolver2)
first.- Parameters:
docType
- the document type.entities
- the codePoints that should be replaced by entity references.- Returns:
- the number of requested entities that could be found.
- Throws:
IOException
- if an I/O problem occurred reading the DTD.SAXException
- if a DTD-related error happened.NullPointerException
- if eitherdocType
orentities
arenull
.
-
setEntityResolver
Sets the entity resolver to be used to read theDTD
.If no entity resolver is set, this library's default resolver will be used.
- Parameters:
resolver
- the entity resolver.
-
setIndentingUnit
public void setIndentingUnit(int whitespaceCount) Sets a whitespace string of thewhitespaceCount
length as the level indenting unit (that is, every indenting depth shall bewhitespaceCount
larger).- Parameters:
whitespaceCount
- the length of the indenting unit.- Throws:
IllegalArgumentException
- if thewhitespaceCount
is negative.
-
writeTree
Create aDOMWriter
object, use it to serialize the given node and descendants.Example:
Document document = ... BufferSimpleWriter writer = new BufferSimpleWriter(); DOMWriter.writeTree(document, writer); System.out.println(writer.toString());
- Parameters:
root
- the root node.writer
- the writer.- Throws:
DOMException
- NAMESPACE_ERR if there is a DOM namespace inconsistency preventing a satisfactory serialization.IOException
- if an I/O problem occurred while writing.
-
serializeToString
Serializesroot
into aString
using the configurable serialization provided by this class.Named for similarity with W3C's
XMLSerializer
interface.- Parameters:
root
- the root node.- Returns:
- the serialization.
- Throws:
DOMException
- NAMESPACE_ERR if there is a DOM namespace inconsistency preventing a satisfactory serialization.
-
writeNode
Serialize the given node and descendants.Example:
Document document = ... BufferSimpleWriter writer = new BufferSimpleWriter(); DOMWriter domwriter = new DOMWriter(document); domwriter.writeNode(document, writer); System.out.println(writer.toString());
- Parameters:
root
- the node.writer
- the writer.- Throws:
DOMException
- NAMESPACE_ERR if there is a DOM namespace inconsistency preventing a satisfactory serialization.IOException
- if an I/O problem occurred while writing.
-
writeNode
protected void writeNode(Node node, SimpleWriter wri, boolean indented) throws DOMException, IOException Serialize the given node and descendants, with an initial indenting context.- Parameters:
node
- the node.wri
- the writer.indented
-true
if the current behaviour (specified by parent or by direct method call) is to put each child node on its own line and indent it.- Throws:
DOMException
- NAMESPACE_ERR if there is a DOM namespace inconsistency preventing a satisfactory serialization.IOException
- if an I/O problem occurred while writing.
-
writeElement
protected void writeElement(DOMElement element, SimpleWriter wri, boolean indented) throws DOMException, IOException Serialize the given element and descendants, with an initial indenting context.- Parameters:
element
- the element.wri
- the writer.indented
-true
if the current behaviour (specified by parent or by direct method call) is to put each child node on its own line and indent it.- Throws:
DOMException
- NAMESPACE_ERR if there is a DOM namespace inconsistency preventing a satisfactory serialization.IOException
- if an I/O problem occurred while writing.
-
writeAttributes
Serialize the attributes of an element.Only explicitly specified attributes are serialized.
- Parameters:
nodeMap
- the attribute map.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeAttribute
Serialize an element attribute.- Parameters:
attr
- the attribute.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeChildNodes
protected void writeChildNodes(Node parent, SimpleWriter wri, boolean indented) throws DOMException, IOException Serialize a list of (child) nodes.- Parameters:
parent
- the parent node.wri
- the writer.indented
-true
if the current behaviour (specified by parent or by direct method call) is to put each child node on its own line and indent it.- Throws:
DOMException
- if there is a DOM inconsistency preventing a satisfactory serialization.IOException
- if an I/O problem occurred while writing.
-
writeText
Serialize aText
node.- Parameters:
text
- the text node.wri
- the writer.indented
-true
if the current behaviour (specified by parent or by direct method call) is to put each child node on its own line and indent it.- Throws:
IOException
- if an I/O problem occurred while writing.
-
isRawTextElement
-
writeRawText
Serialize aText
node that is child of a raw text element.See Raw text elements and also
escapable raw text elements
.- Parameters:
text
- the text node to be serialized as raw text.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeNonRawText
Serialize aText
node that is not the child of a raw text element (that is, a normalText
node).- Parameters:
text
- the text node to be serialized as normal text.wri
- the writer.indented
-true
if the current behaviour (specified by parent or by direct method call) is to put each child node on its own line and indent it.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeTextLine
Serialize a line of a normalText
node.If there is the mandate to replace characters by entity references, call
replaceByEntities(String)
before writing.- Parameters:
line
- the line to write.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
replaceByEntities
Verify if the given line contains characters for which there is the mandate to replace by entity references.- Parameters:
line
- the line where the characters should be replaced.- Returns:
- the line, with the relevant characters replaced by entity references.
- Throws:
SAXException
- if there was a non-I/O problem handling the DTD.IOException
- if an I/O problem occurred while reading a DTD or serializing.
-
writeElementContentWhitespace
Serialize element content whitespace.- Parameters:
text
- the text node.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeCDataSection
Serialize a CDATA section.- Parameters:
data
- the CDATA section.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeComment
Serialize a comment.- Parameters:
comment
- the comment node.wri
- the writer.indented
-true
if the current behaviour (specified by parent or by direct method call) is to put each child node on its own line and indent it.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeDocumentType
Serialize aDOCTYPE
node.- Parameters:
docType
- theDocumentType
node.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeProcessingInstruction
protected void writeProcessingInstruction(ProcessingInstruction pi, SimpleWriter wri) throws IOException Serialize aProcessingInstruction
node.- Parameters:
pi
- theProcessingInstruction
node.wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-
writeEntityReference
- Throws:
IOException
-
startIndentedNodeList
- Throws:
IOException
-
startIndentedNode
- Throws:
IOException
-
endIndentedNode
- Throws:
IOException
-
endIndentedNodeList
- Throws:
IOException
-
afterStartTag
Take decisions after serializing the start tag of an element.- Parameters:
element
- the element whose start tag was serialized.wri
- the writer.- Returns:
true
if children of the given element should be serialized indented.- Throws:
IOException
- if an I/O problem occurred while writing.
-
getDisplayProperty
Get the value of thedisplay
CSS property as specified in the user agent style sheet for that element.- Parameters:
element
- the element.- Returns:
- the value of the
display
property, ornull
if no rule with that type selector defining that property was found.
-
deepenIndent
protected void deepenIndent()Deepen the indenting level. -
updateIndent
Update the indenting level to the parent of the given node.- Parameters:
node
- the current parent node.
-
writeFullIndent
Write a full indent to the writer.- Parameters:
wri
- the writer.- Throws:
IOException
- if an I/O problem occurred while writing.
-