public class XmlSequWriter
extends java.lang.Object
#testWritetree2(File, StringBuilder)
etc with following template:
open(file, "US-ASCII", out); writeElement("root"); writeText("contentRoot", true); writeElement("child1"); writeAttribute("attr", "value"); writeText("contentChild <???> special chars", false); writeElementEnd(); writeElement("child2"); writeAttribute("attr2", "text ? special chars"); close();The output for that is:
<?xml version="1.0" encoding="US-ASCII"?> <root> contentRoot <child1 attr="value">contentChild <ÄÜÖ> special chars</child1> <child2 attr2="text € special chars"/> </root>Note: The source contains the transcription for Javadoc. See the original. Javadoc presents correctly.
CodeCharset
Modifier and Type | Class and Description |
---|---|
private static class |
XmlSequWriter.ElementInfo |
Modifier and Type | Field and Description |
---|---|
(package private) boolean |
bElementStart |
(package private) boolean |
bFullEncoding
true then no reason to output transcription characters.
|
boolean |
bTreeComment |
(package private) XmlSequWriter.ElementInfo |
elementCurr
Info of the current element.
|
(package private) java.util.Stack<XmlSequWriter.ElementInfo> |
elementsParent
Store all parent elements, necessary for close etc.
|
private java.nio.charset.Charset |
encodingFile
The used encoding for File output and also the encoding to convert Java internal UTF-16 to the file's enconding.
|
private CodeCharset |
encodingJava2File
The encoding to convert Java internal UTF-16 to the file's encoding.
|
private java.nio.file.FileSystem |
fileSystem
Used for zip files.
|
private java.io.OutputStream |
fwrFile |
(package private) int |
indent |
(package private) int |
nColumn |
(package private) int |
nColumnMax |
private java.lang.String |
replaceBasics |
(package private) java.lang.String |
sEncoding |
private static java.lang.String |
sIndent |
private static java.lang.String[] |
stdReplacement |
private java.lang.Appendable |
twr |
static java.lang.String |
version
Version, License and History:
2024-07-05 uses a temporary
wsb to see what was written in debug. |
private java.io.Writer |
wrFile |
private java.lang.StringBuilder |
wsb
This is for debugging, see what was written at last.
|
private static int |
zWsb |
Constructor and Description |
---|
XmlSequWriter() |
Modifier and Type | Method and Description |
---|---|
void |
close()
Writes closing elementsParent if necessary, then close the files.
|
java.lang.String |
elementCurrTag() |
void |
fileclose() |
java.lang.String |
open(java.io.File file,
java.lang.String encodingArg,
java.lang.Appendable buffer)
Opens for writing in a file, closes an opened file if any is open.
|
void |
replaceNewline(boolean bReplace)
Switch the mode for replacing a \n in texts or attribute values.
|
void |
setDebugTextOut(java.lang.Appendable out)
Sets an output for text output of the created XML.
|
void |
setEncoding(java.lang.String sEncoding) |
java.lang.String |
toString() |
void |
writeAttribute(java.lang.String name,
java.lang.String value) |
void |
writeComment() |
void |
writeElement(java.lang.String sTag)
Writes a new Element with its tag in a new line with incremented indentation.
|
void |
writeElementEnd()
Writes a simple end "" or "/>" depending on invocation of
#writeElementHeadEnd() before |
private void |
writeElementEnd(boolean bNewline)
Writes a simple end "" or "/>" depending on invocation of
#writeElementHeadEnd() before |
void |
writeElementEnd(java.lang.String sTag)
Writes the or .../> in the same line as the opened tag
or in a new line, depending on written child elements.
|
private void |
writeElementEnd(java.lang.String sTag,
boolean bNewline)
Writes the or .../> but checks whether this is the correct tag.
|
void |
writeElementEndInline(java.lang.String sTag)
Writes the or .../> anyway without newline, independent of the tree situation.
|
void |
writeElementHeadEnd(boolean bNewline)
Writes a simple end ">" because child nodes may be added to the element.
|
void |
writeElementInline(java.lang.String sTag)
Writes a new Element with its tag in the line without line break and change or indentation.
|
private void |
writeHead() |
void |
writeText(java.lang.CharSequence txt,
boolean bNewline) |
private void |
wrNewline() |
private void |
wrTxtBuffered(java.lang.CharSequence txt)
Writes the text immediately as given to the debug buffer file,
but {@link #wrTxtToFile(CharSequence) changes non writeable UTF-16 characters to the file encoding.
|
private void |
wrTxtByteEncoding(java.lang.String txt)
Simple writes the ASCII 8 bit code- Text to
fwrFile |
private void |
wrTxtToFile(java.lang.CharSequence txt)
Write as given, without replacement of "<" etc, it is for the immediately output.
|
private void |
wrTxtWithReplacements(java.lang.CharSequence txt)
Writes text with all replacements.
|
void |
XXXflush() |
java.lang.String |
XXXopenZip(java.io.File file,
java.lang.String sPathInZip,
java.lang.String encoding,
java.lang.Appendable buffer)
Deprecated.
|
public static final java.lang.String version
wsb
to see what was written in debug.
The wrTxtToFile(CharSequence)
does yet the conversion to special character for non UTF-8 encoding.
writeElementEndInline(String)
necessary for LibreOffice
because this tool interprets a newline before </text:p> as a space in the paragraph.
It was obvious if a paragraph does only contain an image, because then no writeElementInline(String)
was called which also forces <:text:p> on end of line.
writeElementInline(String)
to write contigous content in one line.
writeElementEnd(String)
important first to check correctness,
second also for documentation which kind of element is closed.
#openZip(File, String, String, Appendable)
Now also to write zips.
private java.nio.file.FileSystem fileSystem
private java.io.Writer wrFile
private java.io.OutputStream fwrFile
private java.lang.Appendable twr
private java.lang.StringBuilder wsb
private static int zWsb
java.lang.String sEncoding
private java.nio.charset.Charset encodingFile
private CodeCharset encodingJava2File
boolean bFullEncoding
boolean bElementStart
int nColumn
int nColumnMax
XmlSequWriter.ElementInfo elementCurr
java.util.Stack<XmlSequWriter.ElementInfo> elementsParent
int indent
private java.lang.String replaceBasics
private static java.lang.String[] stdReplacement
public boolean bTreeComment
private static java.lang.String sIndent
public void setEncoding(java.lang.String sEncoding) throws java.nio.charset.IllegalCharsetNameException
java.nio.charset.IllegalCharsetNameException
public java.lang.String open(java.io.File file, java.lang.String encodingArg, java.lang.Appendable buffer) throws java.io.IOException
file
- can be null, then only the buffer is written.encodingArg
- can be null, then the last used or given setEncoding(String)
is valid.
Default is "UTF-8". If given the setEncoding(String)
is called here.
buffer
- can be null, if given, the output is written there, the same as in setDebugTextOut(Appendable)
The buffer is written with replaced characters by 샞 as in file.java.io.IOException
- only on closing an opened file. Not expected.@Deprecated public java.lang.String XXXopenZip(java.io.File file, java.lang.String sPathInZip, java.lang.String encoding, java.lang.Appendable buffer) throws java.io.IOException
file
- can be null, then only the buffer is written.encoding
- can be null, then the last used or given setEncoding(String)
is valid.
Default is "UTF-8". If given the setEncoding(String)
is called here.
buffer
- can be null, if given, the output is written there, the same as in setDebugTextOut(Appendable)
The buffer is written with replaced characters by 샞 as in file.java.io.IOException
- only on closing an opened file. Not expected.public void close() throws java.io.IOException
java.io.IOException
public void XXXflush() throws java.io.IOException
java.io.IOException
public void fileclose()
public void setDebugTextOut(java.lang.Appendable out)
#open(File, String)
is not used respectively close()
is called before,
the XML output is written only to this Appendable.
If #open(File, String)
is used too, the XML output is written twice, to this Appendable too.
The Appendable instance can used outside of this XML write operations in any kind, it can be delete etc.
Hence it may be used as debug check.out
- if null then this output is disabled.public void replaceNewline(boolean bReplace)
bReplace
- true, then do not output a \n (line break), instead output
false then break the line with \n if it occurs in texts and attribute values.private void writeHead() throws java.io.IOException
java.io.IOException
public void writeComment()
public void writeElement(java.lang.String sTag) throws java.io.IOException
writeElementInline(String)
.
It is a child node of a non closed writeElement(String)
before.
After this call firstly writeAttribute(String, String)
can be written,
then writeElement(String)
for child nodes or #writeText()
sTag
- java.io.IOException
public void writeElementInline(java.lang.String sTag) throws java.io.IOException
writeElement(String)
before.
After this call firstly writeAttribute(String, String)
can be written,
then writeElement(String)
for child nodes or #writeText()
sTag
- java.io.IOException
private void wrNewline() throws java.io.IOException
java.io.IOException
public void writeElementHeadEnd(boolean bNewline) throws java.io.IOException
java.io.IOException
public void writeElementEnd(java.lang.String sTag) throws java.io.IOException
sTag
- should correspond with the open tag.java.io.IOException
java.lang.IllegalStateException
- on tag mismatch.private void writeElementEnd(java.lang.String sTag, boolean bNewline) throws java.io.IOException
sTag
- should correspond with the open tag.bNewline
- true then writes the in a newline,
but not if the element was created with writeElementInline(String)
.java.io.IOException
java.lang.IllegalStateException
- on tag mismatch.public void writeElementEndInline(java.lang.String sTag) throws java.io.IOException
sTag
- should correspond with the open tag.java.io.IOException
java.lang.IllegalStateException
- on tag mismatch.public void writeElementEnd() throws java.io.IOException
#writeElementHeadEnd()
beforejava.io.IOException
private void writeElementEnd(boolean bNewline) throws java.io.IOException
#writeElementHeadEnd()
beforejava.io.IOException
public void writeAttribute(java.lang.String name, java.lang.String value) throws java.io.IOException
java.io.IOException
public void writeText(java.lang.CharSequence txt, boolean bNewline) throws java.io.IOException
java.io.IOException
private void wrTxtWithReplacements(java.lang.CharSequence txt) throws java.io.IOException
txt
- java.io.IOException
private void wrTxtBuffered(java.lang.CharSequence txt) throws java.io.IOException
private void wrTxtToFile(java.lang.CharSequence txt) throws java.io.IOException
wrTxtBuffered(CharSequence)
.
But due to the file encoding non writable character are transformed to "ģ" in its UTF-16 value.txt
- java.io.IOException
private void wrTxtByteEncoding(java.lang.String txt) throws java.io.IOException
fwrFile
txt
- java.io.IOException
public java.lang.String elementCurrTag()
public java.lang.String toString()
toString
in class java.lang.Object