-
Notifications
You must be signed in to change notification settings - Fork 7
/
dtd.go
453 lines (395 loc) · 10.9 KB
/
dtd.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package xmlwriter
import (
"fmt"
)
// DTD represents a Document Type Definition to be written by the Writer.
type DTD struct {
Name string
PublicID string
SystemID string
}
func (d DTD) kind() NodeKind { return DTDNode }
func (d DTD) start(w *Writer) error {
if err := w.pushBegin(DTDNode, noNodeFlag|docNodeFlag); err != nil {
return err
}
np := &w.nodes[w.current+1]
np.clear()
np.kind = DTDNode
np.flag = dtdNodeFlag
np.dtd = d
return w.pushEnd()
}
func (d DTD) open(n *node, w *Writer) error {
if w.Enforce {
if len(d.Name) == 0 {
return fmt.Errorf("xmlwriter: DTD name must not be empty")
}
if err := CheckName(d.Name); err != nil {
return err
}
}
w.printer.WriteString("<!DOCTYPE ")
w.printer.WriteString(d.Name)
if d.PublicID != "" || d.SystemID != "" {
w.printer.WriteByte(' ')
return w.printer.writeExternalID(d.PublicID, d.SystemID, w.Enforce)
}
return w.printer.cachedWriteError()
}
func (d DTD) opened(n *node, w *Writer, prev NodeState) error {
if n.children > 0 {
w.printer.WriteString(" [")
}
return w.printer.cachedWriteError()
}
func (d DTD) end(n *node, w *Writer, prev NodeState) error {
if n.children > 0 {
w.printer.WriteString("]>")
} else {
w.printer.WriteString(">")
}
return w.printer.cachedWriteError()
}
const (
// DTDElemEmpty is a DTDElem Decl used when the element is empty.
DTDElemEmpty = "EMPTY"
// DTDElemAny is a DTDElem Decl used when the element can contain any element.
DTDElemAny = "ANY"
// DTDElemPCData is a DTDElem Decl used when the element can contain parsed
// character data.
DTDElemPCData = "#PCDATA"
)
// DTDElem represents a DTD element definition to be written by the Writer.
//
// Examples:
//
// DTDElem{Name: "elem", Decl: DTDElemEmpty} -> <!ELEMENT elem EMPTY>
// DTDElem{Name: "elem", Decl: "(a|b)*"} -> <!ELEMENT elem (a|b)*>
//
type DTDElem struct {
Name string
Decl string
}
func (d DTDElem) kind() NodeKind { return DTDElemNode }
func (d DTDElem) writable() {}
func (d DTDElem) write(w *Writer) error {
if w.Enforce {
if err := w.checkParent(noNodeFlag | dtdNodeFlag); err != nil {
return err
}
if len(d.Name) == 0 {
return fmt.Errorf("xmlwriter: ELEMENT name must not be empty")
}
if len(d.Decl) == 0 {
return fmt.Errorf("xmlwriter: ELEMENT decl must not be empty")
}
if err := CheckName(d.Name); err != nil {
return err
}
}
if err := w.writeBeginNext(DTDElemNode); err != nil {
return err
}
w.printer.WriteString("<!ELEMENT ")
w.printer.WriteString(d.Name)
w.printer.WriteByte(' ')
w.printer.WriteString(d.Decl)
w.printer.WriteString(">")
if w.Indenter != nil {
w.last = Event{StateEnded, DTDElemNode, 0}
}
return w.printer.cachedWriteError()
}
// DTDEntity represents a DTD entity definition to be written by the Writer.
//
// Examples:
//
// DTDEntity{Name: "pants", Content: ">"}
// <!ENTITY pants ">">
//
// DTDEntity{Name: "pants", SystemID: "sys"}
// <!ENTITY pants SYSTEM "sys">
//
// DTDEntity{Name: "pants", SystemID: "sys", IsPE: true}
// <!ENTITY % pants SYSTEM "sys">
//
// DTDEntity{Name: "pants", SystemID: "sys", PublicID: "pub", NDataID: "nd"}
// <!ENTITY pants PUBLIC "pub" "sys" NDATA nd>
//
type DTDEntity struct {
Name string
Content string
IsPE bool
PublicID string
SystemID string
NDataID string
}
func (d DTDEntity) kind() NodeKind { return DTDEntityNode }
func (d DTDEntity) writable() {}
func (d DTDEntity) write(w *Writer) error {
if w.Enforce {
if len(d.Name) == 0 {
return fmt.Errorf("xmlwriter: ENTITY name must not be empty")
}
if err := CheckName(d.Name); err != nil {
return err
}
if err := w.checkParent(noNodeFlag | dtdNodeFlag); err != nil {
return err
}
}
if err := w.writeBeginNext(DTDEntityNode); err != nil {
return err
}
w.printer.WriteString("<!ENTITY ")
if d.IsPE {
w.printer.WriteString("% ")
}
w.printer.WriteString(d.Name)
if d.SystemID != "" || d.PublicID != "" {
w.printer.WriteByte(' ')
// external ref
if w.Enforce && d.Content != "" {
return fmt.Errorf("xmlwriter: external ID and content cannot both be provided")
}
if err := w.printer.writeExternalID(d.PublicID, d.SystemID, w.Enforce); err != nil {
return err
}
if d.NDataID != "" {
if !d.IsPE {
w.printer.WriteString(" NDATA ")
if w.Enforce {
if err := CheckName(d.NDataID); err != nil {
return err
}
}
w.printer.WriteString(d.NDataID)
} else {
return fmt.Errorf("xmlwriter: IsPE and NDataID both provided")
}
}
} else {
// explicit content (parental advisory)
if w.Enforce && d.NDataID != "" {
return fmt.Errorf("xmlwriter: external ID required for NDataID")
}
w.printer.WriteByte(' ')
if err := w.printer.writeEntityValue(d.Content, w.Enforce); err != nil {
return err
}
}
w.printer.WriteString(">")
if w.Indenter != nil {
w.last = Event{StateEnded, DTDEntityNode, 0}
}
return w.printer.cachedWriteError()
}
// DTDAttList represents a DTD attribute list to be written by the Writer.
//
// Examples:
//
// DTDAttList{Name: "yep", Attrs: []DTDAttr{
// {Name: "a1", Type: DTDAttrString, Required: true},
// {Name: "a2", Type: DTDAttrString, Required: true},
// }}))
// <!ATTLIST yep a1 CDATA #REQUIRED a2 CDATA #REQUIRED>
//
type DTDAttList struct {
Name string
Attrs []DTDAttr
}
func (d DTDAttList) start(w *Writer) error {
if err := w.pushBegin(DTDAttListNode, noNodeFlag|dtdNodeFlag); err != nil {
return err
}
np := &w.nodes[w.current+1]
np.clear()
np.kind = DTDAttListNode
np.flag = dtdAttListNodeFlag
np.dtdAttList = d
return w.pushEnd()
}
func (d DTDAttList) kind() NodeKind { return DTDAttListNode }
func (d DTDAttList) write(w *Writer) error {
if err := w.StartDTDAttList(d); err != nil {
return err
}
if err := w.EndDTDAttList(); err != nil {
return err
}
return nil
}
func (d DTDAttList) open(n *node, w *Writer) error {
if w.Enforce {
if len(d.Name) == 0 {
return fmt.Errorf("xmlwriter: DTD attlist name must not be empty")
}
if err := CheckName(d.Name); err != nil {
return err
}
}
w.printer.WriteString("<!ATTLIST ")
w.printer.WriteString(d.Name)
return w.printer.cachedWriteError()
}
func (d DTDAttList) opened(n *node, w *Writer, prev NodeState) error {
for _, attr := range d.Attrs {
if err := attr.write(w); err != nil {
return err
}
}
n.dtdAttList.Attrs = nil
return nil
}
func (d DTDAttList) end(n *node, w *Writer, prev NodeState) error {
return w.printer.WriteByte('>')
}
// DTDAttrType constrains the valid values for the Type property of the DTDAttr
// struct.
type DTDAttrType string
const (
DTDAttrString DTDAttrType = "CDATA"
DTDAttrID DTDAttrType = "ID"
DTDAttrIDRef DTDAttrType = "IDREF"
DTDAttrIDRefs DTDAttrType = "IDREFS"
DTDAttrEntity DTDAttrType = "ENTITY"
DTDAttrEntities DTDAttrType = "ENTITIES"
DTDAttrNmtoken DTDAttrType = "NMTOKEN"
DTDAttrNmtokens DTDAttrType = "NMTOKENS"
)
// DTDAttrDefaultType represents the possible values from the DefaultDecl production in
// the DTD spec:
// https://www.w3.org/TR/REC-xml/#NT-DefaultDecl
type DTDAttrDefaultType int
const (
// If Value is empty, this attribute is DTDAttrImplied, otherwise it will be a
// standard DTDAttrDefault value (without #FIXED). If you need to represent the
// empty string as your default value, you will need to explicitly use DTDAttrDefault
// or DTDAttrFixed.
DTDAttrInfer DTDAttrDefaultType = iota
// DTDAttr.Value represents the default for this attribute. Value may be an
// empty string.
DTDAttrDefault
// The attribute MUST always be provided. DTDAttr.Value must be empty.
DTDAttrRequired
// No default value is provided. DTDAttr.Value must be empty.
DTDAttrImplied
// The #FIXED keyword states that the attribute MUST always have the default value.
// If the declaration is neither #REQUIRED nor #IMPLIED, then the AttValue value
// contains the declared default value.
DTDAttrFixed
)
// DTDAttr represents a DTD attribute to be written by the Writer.
type DTDAttr struct {
Name string
Type DTDAttrType
Default DTDAttrDefaultType
Value string
}
func (d DTDAttr) kind() NodeKind { return DTDAttrNode }
func (d DTDAttr) write(w *Writer) error {
if w.Enforce {
if err := w.checkParent(noNodeFlag | dtdAttListNodeFlag); err != nil {
return err
}
if len(d.Name) == 0 {
return fmt.Errorf("xmlwriter: DTD attr name must not be empty")
}
if len(d.Type) == 0 {
return fmt.Errorf("xmlwriter: DTD attr type must not be empty")
}
if err := CheckName(d.Name); err != nil {
return err
}
}
if err := w.writeBeginNext(DTDAttrNode); err != nil {
return err
}
// HACK: if there are no parents and we are writing these outside an
// attlist, this leading space will always be present.
w.printer.WriteByte(' ')
w.printer.WriteString(d.Name)
w.printer.WriteByte(' ')
w.printer.WriteString(string(d.Type))
w.printer.WriteByte(' ')
dflt := d.Default
if dflt == DTDAttrInfer {
if d.Value != "" {
dflt = DTDAttrDefault
} else {
dflt = DTDAttrImplied
}
}
switch dflt {
case DTDAttrDefault:
w.printer.WriteString(`"`)
w.printer.EscapeAttrString(d.Value)
w.printer.WriteByte('"')
case DTDAttrFixed:
w.printer.WriteString(`#FIXED `)
w.printer.WriteString(`"`)
w.printer.EscapeAttrString(d.Value)
w.printer.WriteByte('"')
case DTDAttrRequired:
if d.Value != "" {
return fmt.Errorf("xmlwriter: #REQUIRED DTD attr must not declare Value")
}
w.printer.WriteString("#REQUIRED")
case DTDAttrImplied:
if d.Value != "" {
return fmt.Errorf("xmlwriter: #IMPLIED DTD attr must not declare Value")
}
w.printer.WriteString("#IMPLIED")
default:
return fmt.Errorf("xmlwriter: unknown DTDAttr default type")
}
if w.Indenter != nil {
w.last = Event{StateEnded, DTDAttrNode, 0}
}
return w.printer.cachedWriteError()
}
// Notation represents an XML notation declaration to be written by the Writer.
// https://www.w3.org/TR/xml/#dt-notation
type Notation struct {
Name string
SystemID string
PublicID string
}
func (n Notation) kind() NodeKind { return NotationNode }
func (n Notation) write(w *Writer) error {
if w.Enforce {
if err := w.checkParent(noNodeFlag | dtdNodeFlag); err != nil {
return err
}
if len(n.Name) == 0 {
return fmt.Errorf("xmlwriter: NOTATION name must not be empty")
}
if err := CheckName(n.Name); err != nil {
return err
}
if len(n.PublicID) == 0 && len(n.SystemID) == 0 {
return fmt.Errorf("xmlwriter: NOTATION requires external ID: '<!NOTATION' S Name S (ExternalID | PublicID) S? '>'")
}
}
if err := w.writeBeginNext(NotationNode); err != nil {
return err
}
w.printer.WriteString("<!NOTATION ")
w.printer.WriteString(n.Name)
w.printer.WriteByte(' ')
if n.SystemID != "" {
if err := w.printer.writeExternalID(n.PublicID, n.SystemID, w.Enforce); err != nil {
return err
}
} else if n.PublicID != "" {
if err := w.printer.writePublicID(n.PublicID, n.SystemID, w.Enforce); err != nil {
return err
}
}
w.printer.WriteString(">")
if w.Indenter != nil {
w.last = Event{StateEnded, NotationNode, 0}
}
return w.printer.cachedWriteError()
}