-
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathXMLCBWriter.ts
More file actions
146 lines (137 loc) · 3.93 KB
/
Copy pathXMLCBWriter.ts
File metadata and controls
146 lines (137 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { XMLBuilderCBOptions, XMLCBWriterOptions } from "../interfaces"
import { BaseCBWriter } from "./BaseCBWriter"
/**
* Serializes XML nodes.
*/
export class XMLCBWriter extends BaseCBWriter<XMLCBWriterOptions> {
private _lineLength = 0
/**
* Initializes a new instance of `XMLCBWriter`.
*
* @param builderOptions - XML builder options
*/
constructor(builderOptions: XMLBuilderCBOptions) {
super(builderOptions)
}
/** @inheritdoc */
frontMatter(): string {
return ""
}
/** @inheritdoc */
declaration(version: "1.0", encoding?: string, standalone?: boolean): string {
let markup = this._beginLine() + "<?xml"
markup += " version=\"" + version + "\""
if (encoding !== undefined) {
markup += " encoding=\"" + encoding + "\""
}
if (standalone !== undefined) {
markup += " standalone=\"" + (standalone ? "yes" : "no") + "\""
}
markup += "?>"
return markup
}
/** @inheritdoc */
docType(name: string, publicId: string, systemId: string): string {
let markup = this._beginLine()
if (publicId && systemId) {
markup += "<!DOCTYPE " + name + " PUBLIC \"" + publicId + "\" \"" + systemId + "\">"
} else if (publicId) {
markup += "<!DOCTYPE " + name + " PUBLIC \"" + publicId + "\">"
} else if (systemId) {
markup += "<!DOCTYPE " + name + " SYSTEM \"" + systemId + "\">"
} else {
markup += "<!DOCTYPE " + name + ">"
}
return markup
}
/** @inheritdoc */
comment(data: string): string {
return this._beginLine() + "<!--" + data + "-->"
}
/** @inheritdoc */
text(data: string): string {
return data
}
/** @inheritdoc */
instruction(target: string, data: string): string {
if (data) {
return this._beginLine() + "<?" + target + " " + data + "?>"
} else {
return this._beginLine() + "<?" + target + "?>"
}
}
/** @inheritdoc */
cdata(data: string): string {
return this._beginLine() + "<![CDATA[" +
data.replace(/]]>/g, "]]]]><![CDATA[>") + "]]>"
}
/** @inheritdoc */
openTagBegin(name: string): string {
this._lineLength += 1 + name.length
return this._beginLine() + "<" + name
}
/** @inheritdoc */
openTagEnd(name: string, selfClosing: boolean, voidElement: boolean): string {
if (voidElement) {
return " />"
} else if (selfClosing) {
if (this._writerOptions.allowEmptyTags) {
return "></" + name + ">"
} else if (this._writerOptions.spaceBeforeSlash) {
return " />"
} else {
return "/>"
}
} else {
return ">"
}
}
/** @inheritdoc */
closeTag(name: string, hasTextPayload?: boolean): string {
const ending = hasTextPayload ? '' : this._beginLine();
return ending + "</" + name + ">";
}
/** @inheritdoc */
attribute(name: string, value: string): string {
let str = name + "=\"" + value + "\""
if (this._writerOptions.prettyPrint && this._writerOptions.width > 0 &&
this._lineLength + 1 + str.length > this._writerOptions.width) {
str = this._beginLine() + this._indent(1) + str
this._lineLength = str.length
return str
} else {
this._lineLength += 1 + str.length
return " " + str
}
}
/** @inheritdoc */
beginElement(name: string): void { }
/** @inheritdoc */
endElement(name: string): void { }
/**
* Produces characters to be prepended to a line of string in pretty-print
* mode.
*/
private _beginLine(): string {
if (this._writerOptions.prettyPrint) {
const str = (this.hasData ? this._writerOptions.newline : "") +
this._indent(this._writerOptions.offset + this.level)
this._lineLength = str.length
return str
} else {
return ""
}
}
/**
* Produces an indentation string.
*
* @param level - depth of the tree
*/
private _indent(level: number): string {
if (level <= 0) {
return ""
} else {
return this._writerOptions.indent.repeat(level)
}
}
}