qrz/vendor/github.com/antchfx/xpath/build.go

767 lines
21 KiB
Go
Raw Normal View History

2019-09-07 11:58:52 +02:00
package xpath
import (
"errors"
"fmt"
)
type flag int
2024-04-20 09:40:17 +02:00
var flagsEnum = struct {
None flag
SmartDesc flag
PosFilter flag
Filter flag
Condition flag
}{
None: 0,
SmartDesc: 1,
PosFilter: 2,
Filter: 4,
Condition: 8,
}
type builderProp int
var builderProps = struct {
None builderProp
PosFilter builderProp
HasPosition builderProp
HasLast builderProp
NonFlat builderProp
}{
None: 0,
PosFilter: 1,
HasPosition: 2,
HasLast: 4,
NonFlat: 8,
}
2019-09-07 11:58:52 +02:00
// builder provides building an XPath expressions.
type builder struct {
2024-04-20 09:40:17 +02:00
parseDepth int
2019-09-07 11:58:52 +02:00
firstInput query
}
// axisPredicate creates a predicate to predicating for this axis node.
func axisPredicate(root *axisNode) func(NodeNavigator) bool {
// get current axix node type.
typ := ElementNode
switch root.AxeType {
case "attribute":
typ = AttributeNode
case "self", "parent":
typ = allNode
default:
switch root.Prop {
case "comment":
typ = CommentNode
case "text":
typ = TextNode
// case "processing-instruction":
// typ = ProcessingInstructionNode
case "node":
typ = allNode
}
}
nametest := root.LocalName != "" || root.Prefix != ""
predicate := func(n NodeNavigator) bool {
2022-07-16 11:43:41 +02:00
if typ == n.NodeType() || typ == allNode {
2019-09-07 11:58:52 +02:00
if nametest {
2023-03-17 13:25:01 +01:00
type namespaceURL interface {
NamespaceURL() string
}
if ns, ok := n.(namespaceURL); ok && root.hasNamespaceURI {
return root.LocalName == n.LocalName() && root.namespaceURI == ns.NamespaceURL()
}
2019-09-07 11:58:52 +02:00
if root.LocalName == n.LocalName() && root.Prefix == n.Prefix() {
return true
}
} else {
return true
}
}
return false
}
return predicate
}
2024-04-20 09:40:17 +02:00
// processAxis processes a query for the XPath axis node.
func (b *builder) processAxis(root *axisNode, flags flag, props *builderProp) (query, error) {
2019-09-07 11:58:52 +02:00
var (
2024-04-20 09:40:17 +02:00
err error
qyInput query
qyOutput query
2019-09-07 11:58:52 +02:00
)
2024-04-20 09:40:17 +02:00
b.firstInput = nil
predicate := axisPredicate(root)
2019-09-07 11:58:52 +02:00
if root.Input == nil {
qyInput = &contextQuery{}
2024-04-20 09:40:17 +02:00
*props = builderProps.None
2019-09-07 11:58:52 +02:00
} else {
2024-04-20 09:40:17 +02:00
inputFlags := flagsEnum.None
2019-09-07 11:58:52 +02:00
if root.AxeType == "child" && (root.Input.Type() == nodeAxis) {
if input := root.Input.(*axisNode); input.AxeType == "descendant-or-self" {
var qyGrandInput query
if input.Input != nil {
2024-04-20 09:40:17 +02:00
qyGrandInput, _ = b.processNode(input.Input, flagsEnum.SmartDesc, props)
2019-09-07 11:58:52 +02:00
} else {
qyGrandInput = &contextQuery{}
}
2020-12-05 17:36:50 +01:00
// fix #20: https://github.com/antchfx/htmlquery/issues/20
filter := func(n NodeNavigator) bool {
v := predicate(n)
switch root.Prop {
case "text":
v = v && n.NodeType() == TextNode
case "comment":
v = v && n.NodeType() == CommentNode
}
return v
}
2024-04-20 09:40:17 +02:00
qyOutput = &descendantQuery{name: root.LocalName, Input: qyGrandInput, Predicate: filter, Self: false}
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
return qyOutput, nil
}
2024-04-20 09:40:17 +02:00
} else if ((flags & flagsEnum.Filter) == 0) && (root.AxeType == "descendant" || root.AxeType == "descendant-or-self") {
inputFlags |= flagsEnum.SmartDesc
2019-09-07 11:58:52 +02:00
}
2024-04-20 09:40:17 +02:00
qyInput, err = b.processNode(root.Input, inputFlags, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
}
switch root.AxeType {
case "ancestor":
2024-04-20 09:40:17 +02:00
qyOutput = &ancestorQuery{name: root.LocalName, Input: qyInput, Predicate: predicate}
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
case "ancestor-or-self":
2024-04-20 09:40:17 +02:00
qyOutput = &ancestorQuery{name: root.LocalName, Input: qyInput, Predicate: predicate, Self: true}
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
case "attribute":
2024-04-20 09:40:17 +02:00
qyOutput = &attributeQuery{name: root.LocalName, Input: qyInput, Predicate: predicate}
2019-09-07 11:58:52 +02:00
case "child":
filter := func(n NodeNavigator) bool {
v := predicate(n)
switch root.Prop {
case "text":
v = v && n.NodeType() == TextNode
case "node":
v = v && (n.NodeType() == ElementNode || n.NodeType() == TextNode)
case "comment":
v = v && n.NodeType() == CommentNode
}
return v
}
2024-04-20 09:40:17 +02:00
if (*props & builderProps.NonFlat) == 0 {
qyOutput = &childQuery{name: root.LocalName, Input: qyInput, Predicate: filter}
} else {
qyOutput = &cachedChildQuery{name: root.LocalName, Input: qyInput, Predicate: filter}
}
2019-09-07 11:58:52 +02:00
case "descendant":
2024-04-20 09:40:17 +02:00
if (flags & flagsEnum.SmartDesc) != flagsEnum.None {
qyOutput = &descendantOverDescendantQuery{name: root.LocalName, Input: qyInput, MatchSelf: false, Predicate: predicate}
} else {
qyOutput = &descendantQuery{name: root.LocalName, Input: qyInput, Predicate: predicate}
}
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
case "descendant-or-self":
2024-04-20 09:40:17 +02:00
if (flags & flagsEnum.SmartDesc) != flagsEnum.None {
qyOutput = &descendantOverDescendantQuery{name: root.LocalName, Input: qyInput, MatchSelf: true, Predicate: predicate}
} else {
qyOutput = &descendantQuery{name: root.LocalName, Input: qyInput, Predicate: predicate, Self: true}
}
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
case "following":
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate}
2024-04-20 09:40:17 +02:00
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
case "following-sibling":
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
case "parent":
qyOutput = &parentQuery{Input: qyInput, Predicate: predicate}
case "preceding":
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate}
2024-04-20 09:40:17 +02:00
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
case "preceding-sibling":
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
case "self":
qyOutput = &selfQuery{Input: qyInput, Predicate: predicate}
case "namespace":
// haha,what will you do someting??
default:
err = fmt.Errorf("unknown axe type: %s", root.AxeType)
return nil, err
}
return qyOutput, nil
}
2024-04-20 09:40:17 +02:00
func canBeNumber(q query) bool {
if q.ValueType() != xpathResultType.Any {
return q.ValueType() == xpathResultType.Number
}
return true
}
2019-09-07 11:58:52 +02:00
// processFilterNode builds query for the XPath filter predicate.
2024-04-20 09:40:17 +02:00
func (b *builder) processFilter(root *filterNode, flags flag, props *builderProp) (query, error) {
first := (flags & flagsEnum.Filter) == 0
2019-09-07 11:58:52 +02:00
2024-04-20 09:40:17 +02:00
qyInput, err := b.processNode(root.Input, (flags | flagsEnum.Filter), props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
firstInput := b.firstInput
var propsCond builderProp
cond, err := b.processNode(root.Condition, flags, &propsCond)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
// Checking whether is number
if canBeNumber(cond) || ((propsCond & (builderProps.HasPosition | builderProps.HasLast)) != 0) {
propsCond |= builderProps.HasPosition
flags |= flagsEnum.PosFilter
}
if root.Input.Type() != nodeFilter {
*props &= ^builderProps.PosFilter
}
if (propsCond & builderProps.HasPosition) != 0 {
*props |= builderProps.PosFilter
}
merge := (qyInput.Properties() & queryProps.Merge) != 0
if (propsCond & builderProps.HasPosition) != builderProps.None {
if (propsCond & builderProps.HasLast) != 0 {
// https://github.com/antchfx/xpath/issues/76
// https://github.com/antchfx/xpath/issues/78
if qyFunc, ok := cond.(*functionQuery); ok {
switch qyFunc.Input.(type) {
case *filterQuery:
cond = &lastQuery{Input: qyFunc.Input}
}
}
}
}
if first && firstInput != nil {
if merge && ((*props & builderProps.PosFilter) != 0) {
qyInput = &filterQuery{Input: qyInput, Predicate: cond, NoPosition: false}
var (
rootQuery = &contextQuery{}
parent query
)
switch axisQuery := firstInput.(type) {
case *ancestorQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *attributeQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *childQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *cachedChildQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *descendantQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *followingQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *precedingQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *parentQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *selfQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *groupQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
case *descendantOverDescendantQuery:
if _, ok := axisQuery.Input.(*contextQuery); !ok {
parent = axisQuery.Input
axisQuery.Input = rootQuery
}
}
b.firstInput = nil
if parent != nil {
return &mergeQuery{Input: parent, Child: qyInput}, nil
}
return qyInput, nil
}
b.firstInput = nil
}
resultQuery := &filterQuery{
Input: qyInput,
Predicate: cond,
NoPosition: (propsCond & builderProps.HasPosition) == 0,
}
return resultQuery, nil
2019-09-07 11:58:52 +02:00
}
// processFunctionNode processes query for the XPath function node.
2024-04-20 09:40:17 +02:00
func (b *builder) processFunction(root *functionNode, props *builderProp) (query, error) {
// Reset builder props
*props = builderProps.None
2019-09-07 11:58:52 +02:00
var qyOutput query
switch root.FuncName {
2024-04-20 09:40:17 +02:00
case "lower-case":
arg, err := b.processNode(root.Args[0], flagsEnum.None, props)
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: arg, Func: lowerCaseFunc}
2019-09-07 11:58:52 +02:00
case "starts-with":
2024-04-20 09:40:17 +02:00
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
arg2, err := b.processNode(root.Args[1], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: startwithFunc(arg1, arg2)}
2019-09-07 11:58:52 +02:00
case "ends-with":
2024-04-20 09:40:17 +02:00
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
arg2, err := b.processNode(root.Args[1], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: endwithFunc(arg1, arg2)}
2019-09-07 11:58:52 +02:00
case "contains":
2024-04-20 09:40:17 +02:00
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
arg2, err := b.processNode(root.Args[1], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: containsFunc(arg1, arg2)}
2020-12-05 17:36:50 +01:00
case "matches":
//matches(string , pattern)
if len(root.Args) != 2 {
return nil, errors.New("xpath: matches function must have two parameters")
}
var (
arg1, arg2 query
err error
)
2024-04-20 09:40:17 +02:00
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil {
2020-12-05 17:36:50 +01:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil {
2020-12-05 17:36:50 +01:00
return nil, err
}
2023-11-02 12:38:06 +01:00
// Issue #92, testing the regular expression before.
if q, ok := arg2.(*constantQuery); ok {
if _, err = getRegexp(q.Val.(string)); err != nil {
return nil, fmt.Errorf("matches() got error. %v", err)
}
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: matchesFunc(arg1, arg2)}
2019-09-07 11:58:52 +02:00
case "substring":
//substring( string , start [, length] )
if len(root.Args) < 2 {
return nil, errors.New("xpath: substring function must have at least two parameter")
}
var (
arg1, arg2, arg3 query
err error
)
2024-04-20 09:40:17 +02:00
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
if len(root.Args) == 3 {
2024-04-20 09:40:17 +02:00
if arg3, err = b.processNode(root.Args[2], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: substringFunc(arg1, arg2, arg3)}
2019-09-07 11:58:52 +02:00
case "substring-before", "substring-after":
//substring-xxxx( haystack, needle )
if len(root.Args) != 2 {
return nil, errors.New("xpath: substring-before function must have two parameters")
}
var (
arg1, arg2 query
err error
)
2024-04-20 09:40:17 +02:00
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
qyOutput = &functionQuery{
2024-04-20 09:40:17 +02:00
Func: substringIndFunc(arg1, arg2, root.FuncName == "substring-after"),
2019-09-07 11:58:52 +02:00
}
case "string-length":
// string-length( [string] )
if len(root.Args) < 1 {
return nil, errors.New("xpath: string-length function must have at least one parameter")
}
2024-04-20 09:40:17 +02:00
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: stringLengthFunc(arg1)}
2019-09-07 11:58:52 +02:00
case "normalize-space":
if len(root.Args) == 0 {
return nil, errors.New("xpath: normalize-space function must have at least one parameter")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: normalizespaceFunc}
2020-12-05 17:36:50 +01:00
case "replace":
//replace( string , string, string )
if len(root.Args) != 3 {
return nil, errors.New("xpath: replace function must have three parameters")
}
var (
arg1, arg2, arg3 query
err error
)
2024-04-20 09:40:17 +02:00
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil {
2020-12-05 17:36:50 +01:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil {
2020-12-05 17:36:50 +01:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg3, err = b.processNode(root.Args[2], flagsEnum.None, props); err != nil {
2020-12-05 17:36:50 +01:00
return nil, err
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: replaceFunc(arg1, arg2, arg3)}
2019-09-07 11:58:52 +02:00
case "translate":
//translate( string , string, string )
if len(root.Args) != 3 {
return nil, errors.New("xpath: translate function must have three parameters")
}
var (
arg1, arg2, arg3 query
err error
)
2024-04-20 09:40:17 +02:00
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
2024-04-20 09:40:17 +02:00
if arg3, err = b.processNode(root.Args[2], flagsEnum.None, props); err != nil {
2019-09-07 11:58:52 +02:00
return nil, err
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: translateFunc(arg1, arg2, arg3)}
2019-09-07 11:58:52 +02:00
case "not":
if len(root.Args) == 0 {
return nil, errors.New("xpath: not function must have at least one parameter")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: notFunc}
case "name", "local-name", "namespace-uri":
if len(root.Args) > 1 {
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName)
}
2020-12-05 17:36:50 +01:00
var (
arg query
err error
)
2019-09-07 11:58:52 +02:00
if len(root.Args) == 1 {
2024-04-20 09:40:17 +02:00
arg, err = b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
}
switch root.FuncName {
case "name":
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: nameFunc(arg)}
2019-09-07 11:58:52 +02:00
case "local-name":
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: localNameFunc(arg)}
2019-09-07 11:58:52 +02:00
case "namespace-uri":
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: namespaceFunc(arg)}
2019-09-07 11:58:52 +02:00
}
case "true", "false":
val := root.FuncName == "true"
qyOutput = &functionQuery{
Func: func(_ query, _ iterator) interface{} {
return val
},
}
case "last":
2024-04-20 09:40:17 +02:00
//switch typ := b.firstInput.(type) {
//case *groupQuery, *filterQuery:
// https://github.com/antchfx/xpath/issues/76
// https://github.com/antchfx/xpath/issues/78
//qyOutput = &lastQuery{Input: typ}
//default:
qyOutput = &functionQuery{Func: lastFunc}
//}
*props |= builderProps.HasLast
2019-09-07 11:58:52 +02:00
case "position":
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: positionFunc}
*props |= builderProps.HasPosition
2019-09-07 11:58:52 +02:00
case "boolean", "number", "string":
2024-04-20 09:40:17 +02:00
var inp query
2019-09-07 11:58:52 +02:00
if len(root.Args) > 1 {
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName)
}
if len(root.Args) == 1 {
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
inp = argQuery
}
f := &functionQuery{Input: inp}
switch root.FuncName {
case "boolean":
f.Func = booleanFunc
case "string":
f.Func = stringFunc
case "number":
f.Func = numberFunc
}
qyOutput = f
case "count":
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: countFunc}
case "sum":
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: sum(node-sets) function must with have parameters node-sets")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: sumFunc}
case "ceiling", "floor", "round":
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: ceiling(node-sets) function must with have parameters node-sets")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
f := &functionQuery{Input: argQuery}
switch root.FuncName {
case "ceiling":
f.Func = ceilingFunc
case "floor":
f.Func = floorFunc
case "round":
f.Func = roundFunc
}
qyOutput = f
case "concat":
if len(root.Args) < 2 {
return nil, fmt.Errorf("xpath: concat() must have at least two arguments")
}
var args []query
for _, v := range root.Args {
2024-04-20 09:40:17 +02:00
q, err := b.processNode(v, flagsEnum.None, props)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
args = append(args, q)
}
2024-04-20 09:40:17 +02:00
qyOutput = &functionQuery{Func: concatFunc(args...)}
2020-12-05 17:36:50 +01:00
case "reverse":
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: reverse(node-sets) function must with have parameters node-sets")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2020-12-05 17:36:50 +01:00
if err != nil {
return nil, err
}
qyOutput = &transformFunctionQuery{Input: argQuery, Func: reverseFunc}
2023-11-02 12:38:06 +01:00
case "string-join":
if len(root.Args) != 2 {
return nil, fmt.Errorf("xpath: string-join(node-sets, separator) function requires node-set and argument")
}
2024-04-20 09:40:17 +02:00
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props)
2023-11-02 12:38:06 +01:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
arg1, err := b.processNode(root.Args[1], flagsEnum.None, props)
2023-11-02 12:38:06 +01:00
if err != nil {
return nil, err
}
qyOutput = &functionQuery{Input: argQuery, Func: stringJoinFunc(arg1)}
2019-09-07 11:58:52 +02:00
default:
return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
}
2024-04-20 09:40:17 +02:00
if funcQuery, ok := qyOutput.(*functionQuery); ok && funcQuery.Input == nil {
funcQuery.Input = b.firstInput
}
2019-09-07 11:58:52 +02:00
return qyOutput, nil
}
2024-04-20 09:40:17 +02:00
func (b *builder) processOperator(root *operatorNode, props *builderProp) (query, error) {
var (
leftProp builderProp
rightProp builderProp
)
left, err := b.processNode(root.Left, flagsEnum.None, &leftProp)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
right, err := b.processNode(root.Right, flagsEnum.None, &rightProp)
2019-09-07 11:58:52 +02:00
if err != nil {
return nil, err
}
2024-04-20 09:40:17 +02:00
*props = leftProp | rightProp
2019-09-07 11:58:52 +02:00
var qyOutput query
switch root.Op {
2020-12-05 17:36:50 +01:00
case "+", "-", "*", "div", "mod": // Numeric operator
2024-08-19 03:34:22 +02:00
var exprFunc func(iterator, interface{}, interface{}) interface{}
2019-09-07 11:58:52 +02:00
switch root.Op {
case "+":
exprFunc = plusFunc
case "-":
exprFunc = minusFunc
2020-12-05 17:36:50 +01:00
case "*":
exprFunc = mulFunc
2019-09-07 11:58:52 +02:00
case "div":
exprFunc = divFunc
case "mod":
exprFunc = modFunc
}
qyOutput = &numericQuery{Left: left, Right: right, Do: exprFunc}
case "=", ">", ">=", "<", "<=", "!=":
var exprFunc func(iterator, interface{}, interface{}) interface{}
switch root.Op {
case "=":
exprFunc = eqFunc
case ">":
exprFunc = gtFunc
case ">=":
exprFunc = geFunc
case "<":
exprFunc = ltFunc
case "<=":
exprFunc = leFunc
case "!=":
exprFunc = neFunc
}
qyOutput = &logicalQuery{Left: left, Right: right, Do: exprFunc}
case "or", "and":
isOr := false
if root.Op == "or" {
isOr = true
}
qyOutput = &booleanQuery{Left: left, Right: right, IsOr: isOr}
case "|":
2024-04-20 09:40:17 +02:00
*props |= builderProps.NonFlat
2019-09-07 11:58:52 +02:00
qyOutput = &unionQuery{Left: left, Right: right}
}
return qyOutput, nil
}
2024-04-20 09:40:17 +02:00
func (b *builder) processNode(root node, flags flag, props *builderProp) (q query, err error) {
if b.parseDepth = b.parseDepth + 1; b.parseDepth > 1024 {
2019-09-07 11:58:52 +02:00
err = errors.New("the xpath expressions is too complex")
return
}
2024-04-20 09:40:17 +02:00
*props = builderProps.None
2019-09-07 11:58:52 +02:00
switch root.Type() {
case nodeConstantOperand:
n := root.(*operandNode)
q = &constantQuery{Val: n.Val}
case nodeRoot:
2024-04-20 09:40:17 +02:00
q = &absoluteQuery{}
2019-09-07 11:58:52 +02:00
case nodeAxis:
2024-04-20 09:40:17 +02:00
q, err = b.processAxis(root.(*axisNode), flags, props)
2019-09-07 11:58:52 +02:00
b.firstInput = q
case nodeFilter:
2024-04-20 09:40:17 +02:00
q, err = b.processFilter(root.(*filterNode), flags, props)
2023-03-17 13:25:01 +01:00
b.firstInput = q
2019-09-07 11:58:52 +02:00
case nodeFunction:
2024-04-20 09:40:17 +02:00
q, err = b.processFunction(root.(*functionNode), props)
2019-09-07 11:58:52 +02:00
case nodeOperator:
2024-04-20 09:40:17 +02:00
q, err = b.processOperator(root.(*operatorNode), props)
2021-08-30 19:45:06 +02:00
case nodeGroup:
2024-04-20 09:40:17 +02:00
q, err = b.processNode(root.(*groupNode).Input, flagsEnum.None, props)
2021-08-30 19:45:06 +02:00
if err != nil {
return
}
q = &groupQuery{Input: q}
2024-04-20 09:40:17 +02:00
if b.firstInput == nil {
b.firstInput = q
}
2019-09-07 11:58:52 +02:00
}
2024-04-20 09:40:17 +02:00
b.parseDepth--
2019-09-07 11:58:52 +02:00
return
}
// build builds a specified XPath expressions expr.
2023-03-17 13:25:01 +01:00
func build(expr string, namespaces map[string]string) (q query, err error) {
2019-09-07 11:58:52 +02:00
defer func() {
if e := recover(); e != nil {
switch x := e.(type) {
case string:
err = errors.New(x)
case error:
err = x
default:
err = errors.New("unknown panic")
}
}
}()
2023-03-17 13:25:01 +01:00
root := parse(expr, namespaces)
2019-09-07 11:58:52 +02:00
b := &builder{}
2024-04-20 09:40:17 +02:00
props := builderProps.None
return b.processNode(root, flagsEnum.None, &props)
2019-09-07 11:58:52 +02:00
}