summaryrefslogtreecommitdiff
path: root/vendor/github.com/mitchellh/packer/vendor/github.com/ChrisTrenkamp/goxpath/internal/parser/intfns/nodesetfns.go
blob: 7286a304259c1cdc98b9489069aec646b4126853 (plain)
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
package intfns

import (
	"encoding/xml"
	"fmt"

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

func last(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	return tree.Num(c.Size), nil
}

func position(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	return tree.Num(c.Pos), nil
}

func count(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	n, ok := args[0].(tree.NodeSet)
	if !ok {
		return nil, fmt.Errorf("Cannot convert object to a node-set")
	}

	return tree.Num(len(n)), nil
}

func localName(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	var n tree.NodeSet
	ok := true
	if len(args) == 1 {
		n, ok = args[0].(tree.NodeSet)
	} else {
		n = c.NodeSet
	}
	if !ok {
		return nil, fmt.Errorf("Cannot convert object to a node-set")
	}

	ret := ""
	if len(n) == 0 {
		return tree.String(ret), nil
	}
	node := n[0]

	tok := node.GetToken()

	switch node.GetNodeType() {
	case tree.NtElem:
		ret = tok.(xml.StartElement).Name.Local
	case tree.NtAttr:
		ret = tok.(xml.Attr).Name.Local
	case tree.NtPi:
		ret = tok.(xml.ProcInst).Target
	}

	return tree.String(ret), nil
}

func namespaceURI(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	var n tree.NodeSet
	ok := true
	if len(args) == 1 {
		n, ok = args[0].(tree.NodeSet)
	} else {
		n = c.NodeSet
	}
	if !ok {
		return nil, fmt.Errorf("Cannot convert object to a node-set")
	}

	ret := ""
	if len(n) == 0 {
		return tree.String(ret), nil
	}
	node := n[0]

	tok := node.GetToken()

	switch node.GetNodeType() {
	case tree.NtElem:
		ret = tok.(xml.StartElement).Name.Space
	case tree.NtAttr:
		ret = tok.(xml.Attr).Name.Space
	}

	return tree.String(ret), nil
}

func name(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	var n tree.NodeSet
	ok := true
	if len(args) == 1 {
		n, ok = args[0].(tree.NodeSet)
	} else {
		n = c.NodeSet
	}
	if !ok {
		return nil, fmt.Errorf("Cannot convert object to a node-set")
	}

	ret := ""
	if len(n) == 0 {
		return tree.String(ret), nil
	}
	node := n[0]

	switch node.GetNodeType() {
	case tree.NtElem:
		t := node.GetToken().(xml.StartElement)
		space := ""

		if t.Name.Space != "" {
			space = fmt.Sprintf("{%s}", t.Name.Space)
		}

		ret = fmt.Sprintf("%s%s", space, t.Name.Local)
	case tree.NtAttr:
		t := node.GetToken().(xml.Attr)
		space := ""

		if t.Name.Space != "" {
			space = fmt.Sprintf("{%s}", t.Name.Space)
		}

		ret = fmt.Sprintf("%s%s", space, t.Name.Local)
	case tree.NtPi:
		ret = fmt.Sprintf("%s", node.GetToken().(xml.ProcInst).Target)
	}

	return tree.String(ret), nil
}