-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument.go
431 lines (356 loc) · 12 KB
/
document.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
package dom
import (
"fmt"
"strings"
)
// TODO: implement name list, which is just a map of namespaces + prefixes
// known to the Document. The key should be a prefix, and the namespace URI
// is a value for the given prefix. The namespace is actually unique, but
// that means one namespace can only have one prefix, but that's not something
// XML forces. E.g. xmlns:pfx="urn" and xmlns:bleh="urn" is the same.
//
// After creating a node, find its prefix + namespace, and add it to this map.
// OR: just add namespaces in a map, use some clever thing to create a prefix
// from it? Meh.
type domDocument struct {
nodes []Node
}
// NewDocument creates a new Document which can be used to create
// custom documents using the methods supplied.
func NewDocument() Document {
return &domDocument{}
}
// NODE SPECIFIC FUNCTIONS
func (dd *domDocument) GetNodeName() string {
return "#document"
}
func (dd *domDocument) GetNodeType() NodeType {
return DocumentNode
}
// NodeValue should return null/nil for Document types like the spec says,
// but Go does not permit nil strings which are not pointers. So for now we
// just return an empty string at all times.
func (dd *domDocument) GetNodeValue() string {
return ""
}
func (dd *domDocument) GetLocalName() string {
return ""
}
func (dd *domDocument) GetChildNodes() []Node {
return dd.nodes
}
func (dd *domDocument) GetParentNode() Node {
return nil
}
// GetFirstChild returns the first child in the document. Possible nodes are
// Comment, ProcessingInstruction, or Element.
func (dd *domDocument) GetFirstChild() Node {
if dd.HasChildNodes() {
return dd.nodes[0]
}
return nil
}
func (dd *domDocument) GetLastChild() Node {
if dd.HasChildNodes() {
return dd.nodes[len(dd.nodes)-1]
}
return nil
}
func (dd *domDocument) GetAttributes() NamedNodeMap {
return nil
}
func (dd *domDocument) HasAttributes() bool {
return false
}
func (dd *domDocument) GetOwnerDocument() Document {
return nil
}
// AppendChild handles the appending of nodes to the document, and fails accordingly.
// Only 1 Element may be appended, but comments and processing instructions may appear
// in abundance.
func (dd *domDocument) AppendChild(child Node) error {
if child == nil {
return nil
}
if child == dd {
return fmt.Errorf("%v: adding a node to itself as a child", ErrorHierarchyRequest)
}
if child.GetOwnerDocument() != dd {
return ErrorWrongDocument
}
if child.GetNodeType() == ElementNode {
// Check if a Document element is already appended.
if dd.GetDocumentElement() != nil {
return fmt.Errorf("%v: a Document element already exists (<%v>)", ErrorHierarchyRequest, dd.GetDocumentElement())
}
}
if child.GetNodeType() == AttributeNode || child.GetNodeType() == TextNode {
return ErrorHierarchyRequest
}
// Child already has a parent. Remove it!
cparent := child.GetParentNode()
if cparent != nil {
cparent.RemoveChild(child)
}
child.setParentNode(dd)
dd.nodes = append(dd.nodes, child)
return nil
}
func (dd *domDocument) RemoveChild(oldChild Node) (Node, error) {
if oldChild == nil {
return nil, nil
}
for i, child := range dd.GetChildNodes() {
if child == oldChild {
// Slice trickery to remove the node at the found index:
dd.nodes = append(dd.nodes[:i], dd.nodes[i+1:]...)
return child, nil
}
}
return nil, ErrorNotFound
}
func (dd *domDocument) ReplaceChild(newChild, oldChild Node) (Node, error) {
if newChild == nil {
return nil, fmt.Errorf("%v: given new child is nil", ErrorHierarchyRequest)
}
if oldChild == nil {
return nil, fmt.Errorf("%v: given old child is nil", ErrorHierarchyRequest)
}
if newChild.GetNodeType() == AttributeNode || newChild.GetNodeType() == TextNode {
return nil, ErrorHierarchyRequest
}
// newChild must be created by the same owner document of this element.
if newChild.GetOwnerDocument() != dd {
return nil, ErrorWrongDocument
}
// Replacing a Node (which is not an element) with an element when there's already an element, should fail.
if dd.GetDocumentElement() != nil && newChild.GetNodeType() == ElementNode && oldChild.GetNodeType() != ElementNode {
return nil, ErrorHierarchyRequest
}
// Find the old child, and replace it with the new child.
for i, child := range dd.GetChildNodes() {
if child == oldChild {
// Check if newChild has a parent (i.e., it's in the tree).
ncParent := newChild.GetParentNode()
if ncParent != nil {
// Remove the newChild from its parent.
ncParent.RemoveChild(newChild)
}
// Slice trickery, again. It will make a new underlying slice with one element,
// the 'newChild', and then append the rest of the de.nodes to that.
dd.nodes = append(dd.nodes[:i], append([]Node{newChild}, dd.nodes[i+1:]...)...)
// Change the parent node:
newChild.setParentNode(dd)
return oldChild, nil
}
}
return nil, ErrorNotFound
}
func (dd *domDocument) InsertBefore(newChild, refChild Node) (Node, error) {
// If a document element is already specified, and another element is attempted
// to insert, return an error.
if newChild == nil {
// FIXME: what in this case? Is an error ok?
return nil, ErrorHierarchyRequest
}
// If refChild is nil, append to the end, and return.
if refChild == nil {
err := dd.AppendChild(newChild)
if err != nil {
return nil, err
}
return newChild, nil
}
// Cannot insert an element if there's already one element.
if newChild.GetNodeType() == ElementNode && dd.GetDocumentElement() != nil {
return nil, fmt.Errorf("%v: a Document element already exists (<%v>)", ErrorHierarchyRequest, dd.GetDocumentElement())
}
if newChild.GetNodeType() == AttributeNode || newChild.GetNodeType() == TextNode {
return nil, ErrorHierarchyRequest
}
if newChild.GetOwnerDocument() != dd {
return nil, ErrorWrongDocument
}
// Find the reference child, insert newChild before that one.
for i, child := range dd.GetChildNodes() {
if child == refChild {
// Check if newChild is in the tree already. If so, remove it.
ncParent := newChild.GetParentNode()
if ncParent != nil {
ncParent.RemoveChild(newChild)
}
newChild.setParentNode(dd)
dd.nodes = append(dd.nodes[:i], append([]Node{newChild}, dd.nodes[i:]...)...)
return newChild, nil
}
}
return nil, ErrorNotFound
}
func (dd *domDocument) HasChildNodes() bool {
return len(dd.nodes) > 0
}
func (dd *domDocument) GetPreviousSibling() Node {
return nil
}
func (dd *domDocument) GetNextSibling() Node {
return nil
}
// NamespaceURI should return nil as per the spec, but Go doesn't allow that for
// non-pointer types, so return an empty string instead.
func (dd *domDocument) GetNamespaceURI() string {
return ""
}
func (dd *domDocument) GetNamespacePrefix() string {
return ""
}
// Private functions:
func (dd *domDocument) setParentNode(parent Node) {
// no-op
}
func (dd *domDocument) setOwnerDocument(doc Document) {
// no-op
}
// DOCUMENT SPECIFIC FUNCTIONS
func (dd *domDocument) CreateElement(tagName string) (Element, error) {
name := XMLName(tagName)
if !name.IsValid() {
return nil, fmt.Errorf("%v; tagname '%v'", ErrorInvalidCharacter, tagName)
}
e := newElement(dd, tagName, "")
return e, nil
}
// CreateelementNS creates an element with the given namespace URI and tagname. If I recall correctly,
// the DOM spec mentions something about not caring about namespace URIs. As long as they are escaped,
// it's okay. Even the Xerces implementation in Java doesn't care about the namespace URI, and will be
// serialized just fine.
func (dd *domDocument) CreateElementNS(namespaceURI, tagName string) (Element, error) {
name := XMLName(tagName)
if !name.IsValid() {
return nil, fmt.Errorf("%v; tagname '%v'", ErrorInvalidCharacter, tagName)
}
e := newElement(dd, tagName, namespaceURI)
return e, nil
}
func (dd *domDocument) CreateText(text string) Text {
t := newText(dd)
t.SetText(text)
return t
}
// CreateComment creates a comment node and returns it. When the comment string contains
// a double-hyphen (--) it will return an error and the Comment will be nil. The spec
// says something differently though:
//
// No lexical check is done on the content of a comment and it is therefore possible to
// have the character sequence "--" (double-hyphen) in the content, which is illegal in
// a comment per section 2.5 of [XML 1.0]. The presence of this character sequence must
// generate a fatal error **during serialization**.
//
// E.g. this implementation doesn't fail during serialization, but way before. This may
// be subject to change to get conform the spec. The Xerces implementation in Java 8
// doesn't fail serialization, for example , but simply replaces the '--' with '- -'.
func (dd *domDocument) CreateComment(comment string) (Comment, error) {
// TODO: move validation in newComment instead of here
if strings.Contains(comment, "--") {
return nil, fmt.Errorf("%v: comments may not contain a double hyphen (--)", ErrorInvalidCharacter)
}
c := newComment(dd)
c.SetComment(comment)
return c, nil
}
func (dd *domDocument) CreateAttribute(name string) (Attr, error) {
xmlname := XMLName(name)
if !xmlname.IsValid() {
return nil, fmt.Errorf("%v: '%v'", ErrorInvalidCharacter, xmlname)
}
attr := newAttr(dd, name, "")
return attr, nil
}
func (dd *domDocument) CreateAttributeNS(namespaceURI, name string) (Attr, error) {
xmlname := XMLName(name)
if !xmlname.IsValid() {
return nil, fmt.Errorf("%v: '%v'", ErrorInvalidCharacter, xmlname)
}
attr := newAttr(dd, name, namespaceURI)
return attr, nil
}
func (dd *domDocument) CreateProcessingInstruction(target, data string) (ProcessingInstruction, error) {
pi := newProcInst(dd, target, data)
return pi, nil
}
// GetDocumentElement traverses through the child nodes and finds the first Element.
// That one will be returned as the Document element. The AppendChild function must
// take care that no two root nodes can be added to this Document.
func (dd *domDocument) GetDocumentElement() Element {
// No nodes, so return nil.
if len(dd.nodes) <= 0 {
return nil
}
for _, node := range dd.nodes {
if e, ok := node.(Element); ok {
return e
}
}
return nil
}
// GetElementsByTagName finds all descendant elements of the current element,
// with the given tag name, in document order.
func (dd *domDocument) GetElementsByTagName(tagname string) []Element {
return getElementsBy(dd, "", tagname, false)
}
// GetElementsByTagNameNS finds all descendant elements of the current element,
// with the given tag name and namespace URI, in document order.
func (dd *domDocument) GetElementsByTagNameNS(namespaceURI, tagname string) []Element {
return getElementsBy(dd, namespaceURI, tagname, true)
}
func (dd *domDocument) NormalizeDocument() {
counter := 0
for _, c := range dd.GetChildNodes() {
if e, ok := c.(Element); ok {
e.normalizeNamespaces(&counter)
}
}
}
func (dd *domDocument) LookupPrefix(namespace string) (string, bool) {
return "", false
}
func (dd *domDocument) LookupNamespaceURI(pfx string) (string, bool) {
if dd.GetDocumentElement() != nil {
return dd.GetDocumentElement().LookupNamespaceURI(pfx)
}
return "", false
}
func (dd *domDocument) IsDefaultNamespace(namespace string) bool {
// TODO ?
return false
}
// GetTextContent should return null, but Go doesn't allow null strings so this method
// will return an empty string.
func (dd *domDocument) GetTextContent() string {
return ""
}
// SetTextContent does nothing on a Document Node.
func (dd *domDocument) SetTextContent(content string) {
// no-op.
}
// CloneNode creates a copy of the Document instance. When deep is true, it will create a complete copy
// of the whole Document, recursively. When false, it's pretty useless since it will return just a plain new
// empty Document.
func (dd *domDocument) CloneNode(deep bool) Node {
cloneDoc := NewDocument()
if deep {
for _, c := range dd.GetChildNodes() {
cloneChild := cloneDoc.ImportNode(c, true)
err := cloneDoc.AppendChild(cloneChild)
if err != nil {
fmt.Println(err)
}
}
}
return cloneDoc
}
func (dd *domDocument) ImportNode(n Node, deep bool) Node {
return importNode(dd, n, deep)
}
func (dd *domDocument) String() string {
return fmt.Sprintf("%s", dd.GetNodeType())
}