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
ConstructorsConstructorDescriptionConstruct aDOMWriterwith default settings.DOMWriter(ExtendedCSSStyleSheet<? extends ExtendedCSSRule> refSheet) Construct aDOMWriterwith a reference style sheet for formatting heuristics. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanafterStartTag(DOMElement element, SimpleWriter wri) Take decisions after serializing the start tag of an element.protected voidDeepen the indenting level.protected voidendIndentedNode(Node node, SimpleWriter wri) protected voidendIndentedNodeList(Node listParent, SimpleWriter wri) protected StringgetDisplayProperty(Element element) Get the value of thedisplayCSS property as specified in the user agent style sheet for that element.protected booleanisRawTextElement(DOMElement element) protected StringreplaceByEntities(String line) Verify if the given line contains characters for which there is the mandate to replace by entity references.serializeToString(Node root) Serializesrootinto aStringusing the configurable serialization provided by this class.intsetEntityCodepoints(DocumentType docType, int[] entities) Configure the writer to replace the given codePoints by entity references (if the document type allows it).voidsetEntityResolver(EntityResolver2 resolver) Sets the entity resolver to be used to read theDTD.voidsetIndentingUnit(int whitespaceCount) Sets a whitespace string of thewhitespaceCountlength as the level indenting unit (that is, every indenting depth shall bewhitespaceCountlarger).protected voidstartIndentedNode(Node node, SimpleWriter wri) protected voidstartIndentedNodeList(Node parent, SimpleWriter wri) protected voidupdateIndent(Node node) Update the indenting level to the parent of the given node.protected voidwriteAttribute(Attr attr, SimpleWriter wri) Serialize an element attribute.protected voidwriteAttributes(AttributeNamedNodeMap nodeMap, SimpleWriter wri) Serialize the attributes of an element.protected voidwriteCDataSection(CDATASection data, SimpleWriter wri) Serialize a CDATA section.protected voidwriteChildNodes(Node parent, SimpleWriter wri, boolean indented) Serialize a list of (child) nodes.protected voidwriteComment(Comment comment, SimpleWriter wri, boolean indented) Serialize a comment.protected voidwriteDocumentType(DocumentType docType, SimpleWriter wri) Serialize aDOCTYPEnode.protected voidwriteElement(DOMElement element, SimpleWriter wri, boolean indented) Serialize the given element and descendants, with an initial indenting context.protected voidwriteElementContentWhitespace(Text text, SimpleWriter wri) Serialize element content whitespace.protected voidwriteEntityReference(Node node, SimpleWriter wri) protected voidWrite a full indent to the writer.voidwriteNode(Node root, SimpleWriter writer) Serialize the given node and descendants.protected voidwriteNode(Node node, SimpleWriter wri, boolean indented) Serialize the given node and descendants, with an initial indenting context.protected voidwriteNonRawText(Text text, SimpleWriter wri, boolean indented) Serialize aTextnode that is not the child of a raw text element (that is, a normalTextnode).protected voidSerialize aProcessingInstructionnode.protected voidwriteRawText(Text text, String parentLocalName, SimpleWriter wri) Serialize aTextnode that is child of a raw text element.protected voidwriteText(Text text, SimpleWriter wri, boolean indented) Serialize aTextnode.protected voidwriteTextLine(String line, SimpleWriter wri) Serialize a line of a normalTextnode.static voidwriteTree(Node root, SimpleWriter writer) Create aDOMWriterobject, use it to serialize the given node and descendants.
-
Constructor Details
-
DOMWriter
public DOMWriter()Construct aDOMWriterwith default settings. -
DOMWriter
Construct aDOMWriterwith 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
EntityResolver2in 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 eitherdocTypeorentitiesarenull.
-
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 thewhitespaceCountlength as the level indenting unit (that is, every indenting depth shall bewhitespaceCountlarger).- Parameters:
whitespaceCount- the length of the indenting unit.- Throws:
IllegalArgumentException- if thewhitespaceCountis negative.
-
writeTree
Create aDOMWriterobject, 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
Serializesrootinto aStringusing the configurable serialization provided by this class.Named for similarity with W3C's
XMLSerializerinterface.- 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-trueif 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-trueif 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-trueif 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 aTextnode.- Parameters:
text- the text node.wri- the writer.indented-trueif 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 aTextnode 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 aTextnode that is not the child of a raw text element (that is, a normalTextnode).- Parameters:
text- the text node to be serialized as normal text.wri- the writer.indented-trueif 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 normalTextnode.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-trueif 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 aDOCTYPEnode.- Parameters:
docType- theDocumentTypenode.wri- the writer.- Throws:
IOException- if an I/O problem occurred while writing.
-
writeProcessingInstruction
protected void writeProcessingInstruction(ProcessingInstruction pi, SimpleWriter wri) throws IOException Serialize aProcessingInstructionnode.- Parameters:
pi- theProcessingInstructionnode.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:
trueif children of the given element should be serialized indented.- Throws:
IOException- if an I/O problem occurred while writing.
-
getDisplayProperty
Get the value of thedisplayCSS property as specified in the user agent style sheet for that element.- Parameters:
element- the element.- Returns:
- the value of the
displayproperty, ornullif 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.
-