-
Notifications
You must be signed in to change notification settings - Fork 0
/
element.go
195 lines (173 loc) · 3.89 KB
/
element.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
package xml
import (
"bytes"
_xml "encoding/xml"
"fmt"
"strings"
"github.com/cruffinoni/xml-generator/xml/attributes"
"github.com/cruffinoni/xml-generator/xml/saver"
)
type Element struct {
StartElement _xml.StartElement
EndElement _xml.EndElement
Attr attributes.Attributes
Data *Data
index int
Next *Element
Prev *Element
Child *Element
Parent *Element
}
const DefaultSpacing = 4
func (e *Element) ToXML(spacing int) string {
var sb strings.Builder
n := e
spaces := strings.Repeat(" ", spacing)
for n != nil {
sb.WriteString("\n" + spaces)
if n.IsEmpty() {
sb.WriteString("<" + n.GetName())
if !n.Attr.Empty() {
sb.WriteString(" " + n.Attr.Join(" "))
}
sb.WriteString("/>")
n = n.Next
continue
}
sb.WriteString("<" + n.GetName())
if !n.Attr.Empty() {
sb.WriteString(" " + n.Attr.Join(" "))
}
sb.WriteString(">")
if n.Child != nil {
sb.WriteString(n.Child.ToXML(spacing + DefaultSpacing))
}
if n.Data != nil {
sb.WriteString(n.Data.GetString())
sb.WriteString("</" + n.GetName() + ">")
} else {
sb.WriteString("\n" + spaces + "</" + n.GetName() + ">")
}
n = n.Next
}
return sb.String()
}
func (e *Element) TransformToXML(buffer *saver.Buffer) error {
buffer.WriteString(e.ToXML((buffer.GetDepth() + 1) * DefaultSpacing))
return nil
}
func (e *Element) GetXMLTag() []byte {
return []byte(e.GetName())
}
func (e *Element) IsEmpty() bool {
return e.Child == nil && e.Data == nil
}
func (e *Element) DisplayDebug() string {
var sb strings.Builder
n := e
for n != nil {
sb.WriteString(fmt.Sprintf("Node %p (%v) [parent: %p] ", n, n.StartElement.Name.Local, n.Parent))
if n.Child != nil {
sb.WriteString(fmt.Sprintf("[child: %p] ", n.Child))
}
n = n.Next
}
return sb.String()
}
// GetIndex returns the index of the element in the list of elements or 0 if the element is not in a list
func (e *Element) GetIndex() int {
return e.index
}
func (e *Element) Pretty(spacing int) string {
var sb strings.Builder
n := e
for n != nil {
sb.WriteString(strings.Repeat(" ", spacing) + "> " + n.xmlPath().String())
if !n.Attr.Empty() {
sb.WriteString(" [" + n.Attr.Join(", ") + "]")
}
if n.Child != nil {
//sb.WriteString("\n")
sb.WriteString(n.Child.Pretty(spacing + 2))
}
n = n.Next
}
return sb.String()
}
func (e *Element) GetName() string {
return e.StartElement.Name.Local
}
func (e *Element) DisplayAllXMLPaths() string {
var (
sb strings.Builder
n = e
)
for n != nil {
sb.WriteString(">" + n.xmlPath().String())
if n.Child != nil {
sb.WriteString(n.Child.DisplayAllXMLPaths())
}
sb.WriteString("\n")
n = n.Next
}
return sb.String()
}
func (e *Element) xmlPath() *bytes.Buffer {
b := &bytes.Buffer{}
b.WriteString(e.StartElement.Name.Local)
if e.index > 0 {
b.WriteString(fmt.Sprintf("[%d]", e.index))
}
return b
}
func (e *Element) XMLPath() string {
var (
n = e
sb []byte
)
for n != nil {
buffer := []byte{'>'}
buffer = append(buffer, n.xmlPath().Bytes()...)
sb = append(buffer, sb...)
n = n.Parent
}
return string(sb[1:])
}
func (e *Element) FindTagFromData(data string) []*Element {
var (
result = make([]*Element, 0)
n = e
)
for n != nil {
if n.Data != nil {
if n.Data.GetString() == data {
return []*Element{n}
}
}
if n.Child != nil {
if r := n.Child.FindTagFromData(data); r != nil {
result = append(result, r...)
}
}
n = n.Next
}
return result
}
func (e *Element) SetAttributes(_ attributes.Attributes) {
// We ignore the attribution because the structure has already set the attributes
}
func (e *Element) GetAttributes() attributes.Attributes {
return e.Attr
}
// interface.Assigner interface implementation
func (e *Element) GetPath() string {
// We avoid to use GetPath in generics type
return ""
}
func (e *Element) Assign(elem *Element) error {
if elem == nil {
return nil
}
*e = *elem
return nil
}