File: xmlele.go

package info (click to toggle)
golang-github-christrenkamp-goxpath 1.0~alpha3%2Bgit20170922.c385f95-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: sh: 16; makefile: 3; xml: 3
file content (207 lines) | stat: -rw-r--r-- 3,613 bytes parent folder | download | duplicates (2)
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
package xmlstruct

import (
	"encoding/xml"
	"fmt"
	"reflect"
	"strings"

	"github.com/ChrisTrenkamp/goxpath/tree"
)

type XMLEle struct {
	Val     reflect.Value
	pos     int
	prnt    tree.Elem
	prntTag string
}

func (x *XMLEle) ResValue() string {
	if x.Val.Kind() != reflect.Struct {
		return fmt.Sprintf("%v", x.Val.Interface())
	}

	ret := ""
	for _, i := range x.GetChildren() {
		switch i.GetNodeType() {
		case tree.NtChd, tree.NtElem, tree.NtRoot:
			ret += i.ResValue()
		}
	}
	return ret
}

func (x *XMLEle) Pos() int {
	return x.pos
}

func (x *XMLEle) GetToken() xml.Token {
	ret := xml.StartElement{}

	if x.prntTag != "" {
		ret.Name = getTagInfo(x.prntTag).name
	} else {
		ret.Name = getXMLName(x.Val)
	}

	return ret
}

func (x *XMLEle) GetParent() tree.Elem {
	return x.prnt
}

func (x *XMLEle) GetNodeType() tree.NodeType {
	return tree.NtElem
}

func (x *XMLEle) GetChildren() []tree.Node {
	n, _ := getChildren(x, x.Val, x.pos, false)
	return n
}

func (x *XMLEle) GetAttrs() []tree.Node {
	n, _ := getChildren(x, x.Val, x.pos, true)
	return n
}

type tagInfo struct {
	name    xml.Name
	attr    bool
	cdata   bool
	comment bool
}

func getTagInfo(tag string) (ret tagInfo) {
	spl := strings.Split(tag, ",")
	name := strings.Split(spl[0], " ")
	if len(spl) >= 2 {
		for i := 1; i < len(spl); i++ {
			if spl[i] == "chardata" || spl[i] == "cdata" {
				ret.cdata = true
			} else if spl[i] == "attr" {
				ret.attr = true
			} else if spl[i] == "comment" {
				ret.comment = true
			}
		}
	}

	if len(name) == 2 {
		ret.name.Space = name[1]
	}

	ret.name.Local = name[0]
	return
}

func getXMLName(val reflect.Value) xml.Name {
	n := val.FieldByName("XMLName")
	zero := reflect.Value{}

	if zero != n {
		field, _ := val.Type().FieldByName("XMLName")
		tagInfo := getTagInfo(field.Tag.Get("xml"))
		if tagInfo.name.Local != "" {
			return tagInfo.name
		}

		if name, ok := n.Interface().(xml.Name); ok {
			return name
		}
	}

	return xml.Name{Local: val.Type().Name()}
}

func getChildren(x *XMLEle, val reflect.Value, pos int, getAttrs bool) ([]tree.Node, int) {
	if val.Kind() == reflect.Ptr {
		val = val.Elem()
	}

	if val.Kind() == reflect.Interface {
		val = reflect.ValueOf(val.Interface())
	}

	if val.Kind() != reflect.Struct {
		if getAttrs {
			return []tree.Node{}, x.pos + 1
		}

		return []tree.Node{&XMLNode{
			Val:      x.Val,
			pos:      x.pos + 1,
			prnt:     x,
			nodeType: tree.NtChd,
			prntTag:  "",
		}}, x.pos + 2
	}

	ret := make([]tree.Node, 0, val.NumField())

	for i := 0; i < val.NumField(); i++ {
		field := val.Field(i)
		name := val.Type().Field(i).Name

		if val.Type().Field(i).Anonymous {
			nodes, newPos := getChildren(x, field, pos, getAttrs)
			ret = append(ret, nodes...)
			pos = newPos
			continue
		}

		tag := val.Type().Field(i).Tag.Get("xml")

		if tag == "-" || name == "XMLName" {
			continue
		}

		tagInfo := getTagInfo(tag)

		pos++

		if tagInfo.attr || tagInfo.cdata || tagInfo.comment {
			if !getAttrs && tagInfo.attr || getAttrs && !tagInfo.attr {
				continue
			}

			child := &XMLNode{
				Val:     field,
				pos:     pos,
				prnt:    x,
				prntTag: tag,
			}

			if tagInfo.attr {
				child.nodeType = tree.NtAttr
			} else if tagInfo.cdata {
				child.nodeType = tree.NtChd
			} else {
				child.nodeType = tree.NtComm
			}

			if tagInfo.name.Local == "" {
				child.prntTag = name
			}

			ret = append(ret, child)

			continue
		}

		if getAttrs {
			continue
		}

		child := &XMLEle{Val: field, pos: pos}

		if tag == "" {
			tag = name
		}

		child.prntTag = tag
		ret = append(ret, child)
	}

	return ret, pos + 1
}