public class DocumentWriter extends Object
DocumentIndenter
,
SaveDocument
Modifier and Type | Field and Description |
---|---|
protected ElementMatcher |
cdataSections |
protected DocumentType |
documentType |
protected DiagnosticUtil.DuplicateIDFilter |
duplicateIDFilter |
protected boolean |
favorInteroperability |
protected boolean |
filterDuplicateIDs |
protected Writer |
out |
protected Boolean |
predeclarePrefixPrefs |
protected boolean |
preserveInclusions |
protected QNameHelper |
qNameHelper |
protected com.xmlmind.xml.save.TextHelper |
textHelper |
Constructor and Description |
---|
DocumentWriter(Writer out)
Constructs a writer writing XML to specified output.
|
Modifier and Type | Method and Description |
---|---|
static void |
addXMLBase(Document doc,
boolean addAttribute)
Adds xml:base properties or attributes to all included elements
found in specified document.
|
protected void |
beginWrite(Document document) |
protected void |
beginWrite(Element element) |
protected static void |
checkComment(Comment comment) |
protected static void |
checkProcessingInstruction(ProcessingInstruction pi) |
protected void |
endWrite(Document document) |
protected void |
endWrite(Element element) |
void |
flush()
Flush the underlying
Writer . |
XPath[] |
getCdataSectionElements()
Returns the XPath of elements which are to be saved as CDATA sections.
|
CharEntities |
getCharEntities()
Returns the map specified using
setCharEntities(com.xmlmind.xml.load.CharEntities) . |
CharRange[] |
getCharsSavedAsEntityRefs()
Returns which characters, even if they are supported by the encoding,
are always saved as entity references.
|
DocumentType |
getDocumentType()
Returns the document type of the document to be saved.
|
String |
getEncoding()
Returns the encoding being used for the save file.
|
boolean |
getFavorInteroperability()
Returns
true if interoperability with HTML/SGML should be
favored as recommended in the W3C XML specification. |
boolean |
getFilterDuplicateIDs()
Returns
true if this writer must mangle false duplicate
IDs resulting from the use of included elements. |
Boolean |
getPredeclarePrefixPreferences()
Returns a boolean indicating whether all the namespace prefixes
specified using
setPrefixPreferences(com.xmlmind.xml.name.PrefixEntry[]) are pre-declared
on the root element of the saved document. |
PrefixEntry[] |
getPrefixPreferences()
Returns the prefix/namespace associations to be preferably used by this
writer for saved files.
|
boolean |
getPreserveInclusions()
Returns
false if inclusions (references to external
entities, XIncludes, etc) are to be ```expanded'' in the XML files
created by this writer. |
Writer |
getWriter()
Returns the underlying
Writer . |
protected boolean |
hasEmptyContentType(Element element) |
protected boolean |
isEmptyElement(Element element) |
protected void |
popElement() |
protected boolean |
predeclarePrefixPrefs() |
protected void |
pushElement(Element element) |
void |
setCdataSectionElements(XPath[] cdataSectionElements)
Specifies the XPath of elements which are to be saved as CDATA
sections.
|
void |
setCharEntities(CharEntities map)
Specifies a map defining character entities.
|
void |
setCharsSavedAsEntityRefs(CharRange[] ranges)
Specifies which characters, even if they are supported by the encoding,
are always saved as entity references.
|
void |
setDocumentType(DocumentType documentType)
Specifies the document type of the document to be saved.
|
void |
setEncoding(String name)
Specifies the encoding being used for the save file.
|
void |
setFavorInteroperability(boolean favor)
Specifies whether interoperability with HTML/SGML should be favored as
recommended in the W3C XML specification.
|
void |
setFilterDuplicateIDs(boolean filter)
Allows to mangle false duplicate IDs resulting from the use of included
elements.
|
void |
setPredeclarePrefixPreferences(Boolean predeclare)
Specifies whether all the namespace prefixes
specified using
setPrefixPreferences(com.xmlmind.xml.name.PrefixEntry[]) are pre-declared
on the root element of the saved document. |
void |
setPrefixPreferences(PrefixEntry[] prefixPreferences)
Specifies the prefix/namespace associations to be preferably used by
this writer.
|
void |
setPreserveInclusions(boolean preserve)
Specify
false if inclusions (references to external
entities, XIncludes, etc) are to be ``expanded'' in the XML files
created by this writer. |
protected static String |
shorten(String text) |
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 |
writeAttributes(Element element) |
protected void |
writeCDATA(Text text,
Element element) |
protected void |
writeComment(Comment comment) |
protected void |
writeDocumentTypeDeclaration(DocumentTypeDeclaration dtd) |
protected void |
writeElement(Element element) |
protected void |
writeElementProperties(Element element)
Write certain element properties in the form of processing-instructions.
|
protected void |
writeEmptyEndTag(String tag,
Element element) |
protected void |
writeEndTag(String tag,
Element element) |
protected void |
writeEntityRef(EntityRef entityRef) |
protected void |
writeFrame() |
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) |
protected Writer out
protected QNameHelper qNameHelper
protected com.xmlmind.xml.save.TextHelper textHelper
protected boolean preserveInclusions
protected boolean filterDuplicateIDs
protected DiagnosticUtil.DuplicateIDFilter duplicateIDFilter
protected ElementMatcher cdataSections
protected DocumentType documentType
protected boolean favorInteroperability
protected Boolean predeclarePrefixPrefs
public DocumentWriter(Writer out)
public void setEncoding(String name) throws UnsupportedEncodingException
Default value is SaveOptions.FALLBACK_ENCODING
.
UnsupportedEncodingException
public String getEncoding()
public void setCharEntities(CharEntities map)
Without such map, characters not supported by the encoding cannot be saved as references to character entities.
If, for example, a map has been specified and this map defines the Euro sign as "€" and the encoding does not support the Euro sign (e.g. US-ASCII), then the Euro sign will be saved as "€". In all other cases, the Euro sign will be saved as "€".
Initally, this map is not specified (that is, is null
).
getCharEntities()
public CharEntities getCharEntities()
setCharEntities(com.xmlmind.xml.load.CharEntities)
.public void setCharsSavedAsEntityRefs(CharRange[] ranges)
For example, the Copyright sign is supported by the ISO-8859-1 encoding but you may prefer to see it saved as "©" in the XML file created by this DocumentWriter. In such case, specify a CharRange containing character 169.
Default: null
public CharRange[] getCharsSavedAsEntityRefs()
null
if
such characters have not been specified.public void setPrefixPreferences(PrefixEntry[] prefixPreferences)
If these preferences are not specified, this writer will automatically generate prefixes as needed: ns, ns2, ns3, ...
Default: null
.
public PrefixEntry[] getPrefixPreferences()
null
if such preferences
have not been specified.public void setPreserveInclusions(boolean preserve)
false
if inclusions (references to external
entities, XIncludes, etc) are to be ``expanded'' in the XML files
created by this writer.
Default: true
.
getPreserveInclusions()
public boolean getPreserveInclusions()
false
if inclusions (references to external
entities, XIncludes, etc) are to be ```expanded'' in the XML files
created by this writer.public void setFilterDuplicateIDs(boolean filter)
If the document to be saved contains two elements having a "foo" ID and one of them is just an included replica of the other one, then turning this option on will replace ID "foo" by ID "foo.NNN" for the replica.
Default: true
.
No effect unless inclusions are not preserved.
filter
- if true
, mangle false duplicate IDs
resulting from the use of included elements.setPreserveInclusions(boolean)
public boolean getFilterDuplicateIDs()
true
if this writer must mangle false duplicate
IDs resulting from the use of included elements. Returns
false
otherwise.setFilterDuplicateIDs(boolean)
public void setCdataSectionElements(XPath[] cdataSectionElements)
In order to be saved as CDATA sections (<![CDATA[ ]]>), these elements must only contain text and nothing else (no comments or processing instructions).
Default: null
.
cdataSectionElements
- XPath of elements which are to be saved as
CDATA sections
For efficiency reasons, an XPath whose last step does not test an element name is ignored. For example, "foo//*" is ignored.
public XPath[] getCdataSectionElements()
null
if such elements have not been specified.public void setDocumentType(DocumentType documentType)
null
.public DocumentType getDocumentType()
null
.public void setFavorInteroperability(boolean favor)
Default: false
.
If true
, this means that:
public boolean getFavorInteroperability()
true
if interoperability with HTML/SGML should be
favored as recommended in the W3C XML specification. Returns
false
otherwise.public void setPredeclarePrefixPreferences(Boolean predeclare)
setPrefixPreferences(com.xmlmind.xml.name.PrefixEntry[])
are pre-declared
on the root element of the saved document.
May be null
which means "automatically determined".
Default: null
.
getPredeclarePrefixPreferences()
public Boolean getPredeclarePrefixPreferences()
setPrefixPreferences(com.xmlmind.xml.name.PrefixEntry[])
are pre-declared
on the root element of the saved document.
May return null
which means "automatically determined",
that is, true
unless option favorInteroperability
is turned on or the document is conforming to a DTD.
Value true
is the most conservative choice because
it avoids errors when, for example, some attribute values reference
qualified XML names as it is often the case with schemas,
stylesheets, configuration files, etc.
protected boolean predeclarePrefixPrefs()
public void write(Document document) throws IOException
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.
document
- Document to be saved.IOException
- if, for any reason, the document cannot be
saved.protected void beginWrite(Document document) throws IOException
IOException
protected void endWrite(Document document) throws IOException
IOException
public void write(Element element) throws IOException
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.
element
- Element to be saved.IOException
- if, for any reason, the element cannot be saved.protected void beginWrite(Element element) throws IOException
IOException
protected void endWrite(Element element) throws IOException
IOException
public void flush() throws IOException
Writer
.IOException
getWriter()
protected void writeDocumentTypeDeclaration(DocumentTypeDeclaration dtd) throws IOException
IOException
protected void writeComment(Comment comment) throws IOException
IOException
protected static void checkComment(Comment comment) throws IOException
IOException
protected void writeProcessingInstruction(ProcessingInstruction pi) throws IOException
IOException
protected static void checkProcessingInstruction(ProcessingInstruction pi) throws IOException
IOException
protected void writeInclusion(Inclusion inclusion, Node firstIncludedNode, Node lastIncludedNode) throws IOException
IOException
protected void writeEntityRef(EntityRef entityRef) throws IOException
IOException
protected void writeElement(Element element) throws IOException
IOException
protected void writeStartTag(String tag, Element element) throws IOException
IOException
protected void writeAttributes(Element element) throws IOException
IOException
protected void writeFrame() throws IOException
IOException
protected void writeAttribute(String tag, String value) throws IOException
IOException
protected void writeElementProperties(Element element) throws IOException
Base implementation does nothing at all as properties are not supposed to be serialized.
IOException
protected void writeEmptyEndTag(String tag, Element element) throws IOException
IOException
protected boolean isEmptyElement(Element element)
protected boolean hasEmptyContentType(Element element)
protected void writeEndTag(String tag, Element element) throws IOException
IOException
protected void writeCDATA(Text text, Element element) throws IOException
IOException
protected void writeText(Text text) throws IOException
IOException
protected void pushElement(Element element)
protected void popElement()
public static void addXMLBase(Document doc, boolean addAttribute)
This method is needed to implement xmltool indent -flat.
doc
- the document to be annotated or modifiedaddAttribute
- if true
, add an xml:base
attribute rather than an xml:base property.
Adding an xml:base attribute may cause validation
errors to be reported. This is not the case with
the xml:base property which is anyway also saved to disk
as a xml:base attribute by a DocumentWriter
or
DocumentIndenter
.