public class DocumentIndenter extends DocumentWriter
Will not add whitespace (indentation), unless this whitespace is ignoreable according to the document type of the document to be saved.
DocumentWriter
,
SaveDocument
Modifier and Type | Class and Description |
---|---|
protected static class |
DocumentIndenter.Layout |
protected class |
DocumentIndenter.State |
Modifier and Type | Field and Description |
---|---|
protected boolean |
addOpenLines |
protected boolean |
guessContentType |
protected boolean[] |
guessed |
protected int |
indent |
static int |
MAX_INDENT
Largest value for the indentation.
|
protected int |
maxLineLength |
static int |
MIN_LINE_LENGTH
Smallest value for the maximum line length.
|
protected DocumentIndenter.State[] |
stateStack |
protected int |
stateStackSize |
protected Structure |
structure |
cdataSections, documentType, duplicateIDFilter, favorInteroperability, filterDuplicateIDs, out, predeclarePrefixPrefs, preserveInclusions, qNameHelper, textHelper
Constructor and Description |
---|
DocumentIndenter(Writer out)
Constructs a writer writing XML to specified output.
|
Modifier and Type | Method and Description |
---|---|
protected int |
computeLineEnd(int lineFirst) |
protected DocumentIndenter.State |
createInitialState() |
boolean |
getAddOpenLines()
Returns
true if an open line is added between the child
elements of a parent element (if the content model of the parent only
allows child elements). |
protected static String |
getElementAttribute(Element element,
ElementType elementType,
Name attributeName,
AttributeDefaultValue defaultValue) |
int |
getIndent()
Returns the number of space characters used to indent a child element
relatively to its parent element.
|
int |
getMaxLineLength()
Returns the maximum line length for elements containing text
interspersed with child elements.
|
protected boolean |
hasEmptyContentType(Element element) |
protected void |
popElement() |
protected void |
popInclusion() |
protected void |
popState() |
protected void |
pushElement(Element element) |
protected void |
pushInclusion(Inclusion inclusion,
Node firstIncludedNode,
Node lastIncludedNode,
Node[] inclusionSpec) |
protected void |
pushState(DocumentIndenter.State state) |
void |
setAddOpenLines(boolean add)
If specified value is
true , an open line is added between
the child elements of a parent element (if the content model of the
parent only allows child elements). |
void |
setDocumentType(DocumentType documentType)
Specifies the document type of the document to be saved.
|
void |
setIndent(int indent)
Specifies the number of space characters used to indent a child element
relatively to its parent element.
|
void |
setMaxLineLength(int length)
Specifies the maximum line length for elements containing text
interspersed with child elements.
|
protected DocumentIndenter.State |
topState() |
void |
write(Document document)
Save specified document.
|
void |
write(Element element)
Save specified element as a document.
|
protected void |
writeAttribute(String tag,
String value) |
protected void |
writeCDATA(Text text,
Element element) |
protected void |
writeComment(Comment comment) |
protected void |
writeElement(Element element) |
protected void |
writeEmptyEndTag(String tag,
Element element) |
protected void |
writeEndTag(String tag,
Element element) |
protected void |
writeInclusion(Inclusion inclusion,
Node firstIncludedNode,
Node lastIncludedNode) |
protected void |
writeProcessingInstruction(ProcessingInstruction pi) |
protected void |
writeStartTag(String tag,
Element element) |
protected void |
writeText(Text text) |
addXMLBase, beginWrite, beginWrite, checkComment, checkProcessingInstruction, endWrite, endWrite, flush, getCdataSectionElements, getCharEntities, getCharsSavedAsEntityRefs, getDocumentType, getEncoding, getFavorInteroperability, getFilterDuplicateIDs, getPredeclarePrefixPreferences, getPrefixPreferences, getPreserveInclusions, getWriter, isEmptyElement, predeclarePrefixPrefs, setCdataSectionElements, setCharEntities, setCharsSavedAsEntityRefs, setEncoding, setFavorInteroperability, setFilterDuplicateIDs, setPredeclarePrefixPreferences, setPrefixPreferences, setPreserveInclusions, shorten, writeAttributes, writeDocumentTypeDeclaration, writeElementProperties, writeEntityRef, writeFrame
public static final int MAX_INDENT
setIndent(int)
,
Constant Field Valuespublic static final int MIN_LINE_LENGTH
setMaxLineLength(int)
,
Constant Field Valuesprotected boolean guessContentType
protected int indent
protected int maxLineLength
protected boolean addOpenLines
protected DocumentIndenter.State[] stateStack
protected int stateStackSize
protected Structure structure
protected boolean[] guessed
public DocumentIndenter(Writer out)
public void setDocumentType(DocumentType documentType)
DocumentWriter
null
.setDocumentType
in class DocumentWriter
public void setIndent(int indent)
Default: 2.
A negative value is adjusted to 0. A value greater than MAX_INDENT
is adjusted to MAX_INDENT
.
public int getIndent()
public void setMaxLineLength(int length)
Default: 78.
This is just a hint: lines written by this formatter may be much longer that specified value.
A value negative or null may be used to specify an unbound maximum
line length. A positive value smaller than MIN_LINE_LENGTH
is
adjusted to MIN_LINE_LENGTH
.
public int getMaxLineLength()
public void setAddOpenLines(boolean add)
true
, an open line is added between
the child elements of a parent element (if the content model of the
parent only allows child elements).
Default: true
.
public boolean getAddOpenLines()
true
if an open line is added between the child
elements of a parent element (if the content model of the parent only
allows child elements). Returns false
otherwise.public void write(Document document) throws IOException
DocumentWriter
The saved file does not automatically start with a XML declaration
<?xml version="...." ?>. You need to use
SaveDocument.writeXMLDeclaration(java.lang.String, java.lang.String, java.lang.String, java.io.Writer)
for that.
write
in class DocumentWriter
document
- Document to be saved.IOException
- if, for any reason, the document cannot be
saved.public void write(Element element) throws IOException
DocumentWriter
The saved file does not automatically start with a XML declaration
<?xml version="...." ?>. You need to use
SaveDocument.writeXMLDeclaration(java.lang.String, java.lang.String, java.lang.String, java.io.Writer)
for that.
write
in class DocumentWriter
element
- Element to be saved.IOException
- if, for any reason, the element cannot be saved.protected void writeComment(Comment comment) throws IOException
writeComment
in class DocumentWriter
IOException
protected void writeProcessingInstruction(ProcessingInstruction pi) throws IOException
writeProcessingInstruction
in class DocumentWriter
IOException
protected void writeInclusion(Inclusion inclusion, Node firstIncludedNode, Node lastIncludedNode) throws IOException
writeInclusion
in class DocumentWriter
IOException
protected void writeElement(Element element) throws IOException
writeElement
in class DocumentWriter
IOException
protected void writeStartTag(String tag, Element element) throws IOException
writeStartTag
in class DocumentWriter
IOException
protected void writeAttribute(String tag, String value) throws IOException
writeAttribute
in class DocumentWriter
IOException
protected void writeEmptyEndTag(String tag, Element element) throws IOException
writeEmptyEndTag
in class DocumentWriter
IOException
protected boolean hasEmptyContentType(Element element)
hasEmptyContentType
in class DocumentWriter
protected void writeEndTag(String tag, Element element) throws IOException
writeEndTag
in class DocumentWriter
IOException
protected void writeCDATA(Text text, Element element) throws IOException
writeCDATA
in class DocumentWriter
IOException
protected void writeText(Text text) throws IOException
writeText
in class DocumentWriter
IOException
protected DocumentIndenter.State createInitialState()
protected void pushElement(Element element)
pushElement
in class DocumentWriter
protected static String getElementAttribute(Element element, ElementType elementType, Name attributeName, AttributeDefaultValue defaultValue)
protected int computeLineEnd(int lineFirst)
protected void popElement()
popElement
in class DocumentWriter
protected void pushInclusion(Inclusion inclusion, Node firstIncludedNode, Node lastIncludedNode, Node[] inclusionSpec)
protected void popInclusion()
protected void pushState(DocumentIndenter.State state)
protected void popState()
protected DocumentIndenter.State topState()