| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
) |
| 7 |
|
| 8 |
type flag int |
| 9 |
|
| 10 |
var flagsEnum = struct { |
| 11 |
None flag |
| 12 |
SmartDesc flag |
| 13 |
PosFilter flag |
| 14 |
Filter flag |
| 15 |
Condition flag |
| 16 |
}{ |
| 17 |
None: 0, |
| 18 |
SmartDesc: 1, |
| 19 |
PosFilter: 2, |
| 20 |
Filter: 4, |
| 21 |
Condition: 8, |
| 22 |
} |
| 23 |
|
| 24 |
type builderProp int |
| 25 |
|
| 26 |
var builderProps = struct { |
| 27 |
None builderProp |
| 28 |
PosFilter builderProp |
| 29 |
HasPosition builderProp |
| 30 |
HasLast builderProp |
| 31 |
NonFlat builderProp |
| 32 |
}{ |
| 33 |
None: 0, |
| 34 |
PosFilter: 1, |
| 35 |
HasPosition: 2, |
| 36 |
HasLast: 4, |
| 37 |
NonFlat: 8, |
| 38 |
} |
| 39 |
|
| 40 |
// builder provides building an XPath expressions. |
| 41 |
type builder struct { |
| 42 |
parseDepth int |
| 43 |
firstInput query |
| 44 |
} |
| 45 |
|
| 46 |
// axisPredicate creates a predicate to predicating for this axis node. |
| 47 |
func axisPredicate(root *axisNode) func(NodeNavigator) bool { |
| 48 |
nametest := root.LocalName != "" || root.Prefix != "" |
| 49 |
predicate := func(n NodeNavigator) bool { |
| 50 |
if root.typeTest == n.NodeType() || root.typeTest == allNode { |
| 51 |
if nametest { |
| 52 |
type namespaceURL interface { |
| 53 |
NamespaceURL() string |
| 54 |
} |
| 55 |
if ns, ok := n.(namespaceURL); ok && root.hasNamespaceURI { |
| 56 |
return root.LocalName == n.LocalName() && root.namespaceURI == ns.NamespaceURL() |
| 57 |
} |
| 58 |
if root.LocalName == n.LocalName() && root.Prefix == n.Prefix() { |
| 59 |
return true |
| 60 |
} |
| 61 |
} else { |
| 62 |
return true |
| 63 |
} |
| 64 |
} |
| 65 |
return false |
| 66 |
} |
| 67 |
|
| 68 |
return predicate |
| 69 |
} |
| 70 |
|
| 71 |
// processAxis processes a query for the XPath axis node. |
| 72 |
func (b *builder) processAxis(root *axisNode, flags flag, props *builderProp) (query, error) { |
| 73 |
var ( |
| 74 |
err error |
| 75 |
qyInput query |
| 76 |
qyOutput query |
| 77 |
) |
| 78 |
b.firstInput = nil |
| 79 |
predicate := axisPredicate(root) |
| 80 |
|
| 81 |
if root.Input == nil { |
| 82 |
qyInput = &contextQuery{} |
| 83 |
*props = builderProps.None |
| 84 |
} else { |
| 85 |
inputFlags := flagsEnum.None |
| 86 |
if (flags & flagsEnum.Filter) == 0 { |
| 87 |
if root.AxisType == "child" && (root.Input.Type() == nodeAxis) { |
| 88 |
if input := root.Input.(*axisNode); input.AxisType == "descendant-or-self" { |
| 89 |
var qyGrandInput query |
| 90 |
if input.Input != nil { |
| 91 |
qyGrandInput, err = b.processNode(input.Input, flagsEnum.SmartDesc, props) |
| 92 |
if err != nil { |
| 93 |
return nil, err |
| 94 |
} |
| 95 |
} else { |
| 96 |
qyGrandInput = &contextQuery{} |
| 97 |
} |
| 98 |
qyOutput = &descendantQuery{name: root.LocalName, Input: qyGrandInput, Predicate: predicate, Self: false} |
| 99 |
*props |= builderProps.NonFlat |
| 100 |
return qyOutput, nil |
| 101 |
} |
| 102 |
} |
| 103 |
if root.AxisType == "descendant" || root.AxisType == "descendant-or-self" { |
| 104 |
inputFlags |= flagsEnum.SmartDesc |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
qyInput, err = b.processNode(root.Input, inputFlags, props) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
switch root.AxisType { |
| 115 |
case "ancestor": |
| 116 |
qyOutput = &ancestorQuery{name: root.LocalName, Input: qyInput, Predicate: predicate} |
| 117 |
*props |= builderProps.NonFlat |
| 118 |
case "ancestor-or-self": |
| 119 |
qyOutput = &ancestorQuery{name: root.LocalName, Input: qyInput, Predicate: predicate, Self: true} |
| 120 |
*props |= builderProps.NonFlat |
| 121 |
case "attribute": |
| 122 |
qyOutput = &attributeQuery{name: root.LocalName, Input: qyInput, Predicate: predicate} |
| 123 |
case "child": |
| 124 |
if (*props & builderProps.NonFlat) == 0 { |
| 125 |
qyOutput = &childQuery{name: root.LocalName, Input: qyInput, Predicate: predicate} |
| 126 |
} else { |
| 127 |
qyOutput = &cachedChildQuery{name: root.LocalName, Input: qyInput, Predicate: predicate} |
| 128 |
} |
| 129 |
case "descendant": |
| 130 |
if (flags & flagsEnum.SmartDesc) != flagsEnum.None { |
| 131 |
qyOutput = &descendantOverDescendantQuery{name: root.LocalName, Input: qyInput, MatchSelf: false, Predicate: predicate} |
| 132 |
} else { |
| 133 |
qyOutput = &descendantQuery{name: root.LocalName, Input: qyInput, Predicate: predicate} |
| 134 |
} |
| 135 |
*props |= builderProps.NonFlat |
| 136 |
case "descendant-or-self": |
| 137 |
if (flags & flagsEnum.SmartDesc) != flagsEnum.None { |
| 138 |
qyOutput = &descendantOverDescendantQuery{name: root.LocalName, Input: qyInput, MatchSelf: true, Predicate: predicate} |
| 139 |
} else { |
| 140 |
qyOutput = &descendantQuery{name: root.LocalName, Input: qyInput, Predicate: predicate, Self: true} |
| 141 |
} |
| 142 |
*props |= builderProps.NonFlat |
| 143 |
case "following": |
| 144 |
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate} |
| 145 |
*props |= builderProps.NonFlat |
| 146 |
case "following-sibling": |
| 147 |
qyOutput = &followingQuery{Input: qyInput, Predicate: predicate, Sibling: true} |
| 148 |
case "parent": |
| 149 |
qyOutput = &parentQuery{Input: qyInput, Predicate: predicate} |
| 150 |
case "preceding": |
| 151 |
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate} |
| 152 |
*props |= builderProps.NonFlat |
| 153 |
case "preceding-sibling": |
| 154 |
qyOutput = &precedingQuery{Input: qyInput, Predicate: predicate, Sibling: true} |
| 155 |
case "self": |
| 156 |
qyOutput = &selfQuery{Input: qyInput, Predicate: predicate} |
| 157 |
case "namespace": |
| 158 |
// haha,what will you do someting?? |
| 159 |
default: |
| 160 |
err = fmt.Errorf("unknown axe type: %s", root.AxisType) |
| 161 |
return nil, err |
| 162 |
} |
| 163 |
return qyOutput, nil |
| 164 |
} |
| 165 |
|
| 166 |
func canBeNumber(q query) bool { |
| 167 |
if q.ValueType() != xpathResultType.Any { |
| 168 |
return q.ValueType() == xpathResultType.Number |
| 169 |
} |
| 170 |
return true |
| 171 |
} |
| 172 |
|
| 173 |
// processFilterNode builds query for the XPath filter predicate. |
| 174 |
func (b *builder) processFilter(root *filterNode, flags flag, props *builderProp) (query, error) { |
| 175 |
first := (flags & flagsEnum.Filter) == 0 |
| 176 |
|
| 177 |
qyInput, err := b.processNode(root.Input, (flags | flagsEnum.Filter), props) |
| 178 |
if err != nil { |
| 179 |
return nil, err |
| 180 |
} |
| 181 |
firstInput := b.firstInput |
| 182 |
|
| 183 |
var propsCond builderProp |
| 184 |
cond, err := b.processNode(root.Condition, flags, &propsCond) |
| 185 |
if err != nil { |
| 186 |
return nil, err |
| 187 |
} |
| 188 |
|
| 189 |
// Checking whether is number |
| 190 |
if canBeNumber(cond) || ((propsCond & (builderProps.HasPosition | builderProps.HasLast)) != 0) { |
| 191 |
propsCond |= builderProps.HasPosition |
| 192 |
flags |= flagsEnum.PosFilter |
| 193 |
} |
| 194 |
|
| 195 |
if root.Input.Type() != nodeFilter { |
| 196 |
*props &= ^builderProps.PosFilter |
| 197 |
} |
| 198 |
|
| 199 |
if (propsCond & builderProps.HasPosition) != 0 { |
| 200 |
*props |= builderProps.PosFilter |
| 201 |
} |
| 202 |
|
| 203 |
if (propsCond & builderProps.HasPosition) != builderProps.None { |
| 204 |
if (propsCond & builderProps.HasLast) != 0 { |
| 205 |
// https://github.com/antchfx/xpath/issues/76 |
| 206 |
// https://github.com/antchfx/xpath/issues/78 |
| 207 |
if qyFunc, ok := cond.(*functionQuery); ok { |
| 208 |
switch qyFunc.Input.(type) { |
| 209 |
case *filterQuery: |
| 210 |
cond = &lastFuncQuery{Input: qyFunc.Input} |
| 211 |
case *groupQuery: |
| 212 |
cond = &lastFuncQuery{Input: qyFunc.Input} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
merge := (qyInput.Properties() & queryProps.Merge) != 0 |
| 219 |
if first && firstInput != nil { |
| 220 |
if merge && ((*props & builderProps.PosFilter) != 0) { |
| 221 |
var ( |
| 222 |
rootQuery = &contextQuery{} |
| 223 |
parent query |
| 224 |
) |
| 225 |
switch axisQuery := firstInput.(type) { |
| 226 |
case *ancestorQuery: |
| 227 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 228 |
parent = axisQuery.Input |
| 229 |
axisQuery.Input = rootQuery |
| 230 |
} |
| 231 |
case *attributeQuery: |
| 232 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 233 |
parent = axisQuery.Input |
| 234 |
axisQuery.Input = rootQuery |
| 235 |
} |
| 236 |
case *childQuery: |
| 237 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 238 |
parent = axisQuery.Input |
| 239 |
axisQuery.Input = rootQuery |
| 240 |
} |
| 241 |
case *cachedChildQuery: |
| 242 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 243 |
parent = axisQuery.Input |
| 244 |
axisQuery.Input = rootQuery |
| 245 |
} |
| 246 |
case *descendantQuery: |
| 247 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 248 |
parent = axisQuery.Input |
| 249 |
axisQuery.Input = rootQuery |
| 250 |
} |
| 251 |
case *followingQuery: |
| 252 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 253 |
parent = axisQuery.Input |
| 254 |
axisQuery.Input = rootQuery |
| 255 |
} |
| 256 |
case *precedingQuery: |
| 257 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 258 |
parent = axisQuery.Input |
| 259 |
axisQuery.Input = rootQuery |
| 260 |
} |
| 261 |
case *parentQuery: |
| 262 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 263 |
parent = axisQuery.Input |
| 264 |
axisQuery.Input = rootQuery |
| 265 |
} |
| 266 |
case *selfQuery: |
| 267 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 268 |
parent = axisQuery.Input |
| 269 |
axisQuery.Input = rootQuery |
| 270 |
} |
| 271 |
case *groupQuery: |
| 272 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 273 |
parent = axisQuery.Input |
| 274 |
axisQuery.Input = rootQuery |
| 275 |
} |
| 276 |
case *descendantOverDescendantQuery: |
| 277 |
if _, ok := axisQuery.Input.(*contextQuery); !ok { |
| 278 |
parent = axisQuery.Input |
| 279 |
axisQuery.Input = rootQuery |
| 280 |
} |
| 281 |
} |
| 282 |
b.firstInput = nil |
| 283 |
child := &filterQuery{Input: qyInput, Predicate: cond, NoPosition: false} |
| 284 |
if parent != nil { |
| 285 |
return &mergeQuery{Input: parent, Child: child}, nil |
| 286 |
} |
| 287 |
return child, nil |
| 288 |
} |
| 289 |
b.firstInput = nil |
| 290 |
} |
| 291 |
|
| 292 |
resultQuery := &filterQuery{ |
| 293 |
Input: qyInput, |
| 294 |
Predicate: cond, |
| 295 |
NoPosition: (propsCond & builderProps.HasPosition) == 0, |
| 296 |
} |
| 297 |
return resultQuery, nil |
| 298 |
} |
| 299 |
|
| 300 |
// processFunctionNode processes query for the XPath function node. |
| 301 |
func (b *builder) processFunction(root *functionNode, props *builderProp) (query, error) { |
| 302 |
// Reset builder props |
| 303 |
*props = builderProps.None |
| 304 |
|
| 305 |
var qyOutput query |
| 306 |
switch root.FuncName { |
| 307 |
case "lower-case": |
| 308 |
arg, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 309 |
if err != nil { |
| 310 |
return nil, err |
| 311 |
} |
| 312 |
qyOutput = &functionQuery{Func: lowerCaseFunc(arg)} |
| 313 |
case "starts-with": |
| 314 |
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 315 |
if err != nil { |
| 316 |
return nil, err |
| 317 |
} |
| 318 |
arg2, err := b.processNode(root.Args[1], flagsEnum.None, props) |
| 319 |
if err != nil { |
| 320 |
return nil, err |
| 321 |
} |
| 322 |
qyOutput = &functionQuery{Func: startwithFunc(arg1, arg2)} |
| 323 |
case "ends-with": |
| 324 |
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 325 |
if err != nil { |
| 326 |
return nil, err |
| 327 |
} |
| 328 |
arg2, err := b.processNode(root.Args[1], flagsEnum.None, props) |
| 329 |
if err != nil { |
| 330 |
return nil, err |
| 331 |
} |
| 332 |
qyOutput = &functionQuery{Func: endwithFunc(arg1, arg2)} |
| 333 |
case "contains": |
| 334 |
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 335 |
if err != nil { |
| 336 |
return nil, err |
| 337 |
} |
| 338 |
arg2, err := b.processNode(root.Args[1], flagsEnum.None, props) |
| 339 |
if err != nil { |
| 340 |
return nil, err |
| 341 |
} |
| 342 |
qyOutput = &functionQuery{Func: containsFunc(arg1, arg2)} |
| 343 |
case "matches": |
| 344 |
//matches(string , pattern) |
| 345 |
if len(root.Args) != 2 { |
| 346 |
return nil, errors.New("xpath: matches function must have two parameters") |
| 347 |
} |
| 348 |
var ( |
| 349 |
arg1, arg2 query |
| 350 |
err error |
| 351 |
) |
| 352 |
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil { |
| 353 |
return nil, err |
| 354 |
} |
| 355 |
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil { |
| 356 |
return nil, err |
| 357 |
} |
| 358 |
// Issue #92, testing the regular expression before. |
| 359 |
if q, ok := arg2.(*constantQuery); ok { |
| 360 |
if _, err = getRegexp(q.Val.(string)); err != nil { |
| 361 |
return nil, fmt.Errorf("matches() got error. %v", err) |
| 362 |
} |
| 363 |
} |
| 364 |
qyOutput = &functionQuery{Func: matchesFunc(arg1, arg2)} |
| 365 |
case "substring": |
| 366 |
//substring( string , start [, length] ) |
| 367 |
if len(root.Args) < 2 { |
| 368 |
return nil, errors.New("xpath: substring function must have at least two parameter") |
| 369 |
} |
| 370 |
var ( |
| 371 |
arg1, arg2, arg3 query |
| 372 |
err error |
| 373 |
) |
| 374 |
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil { |
| 375 |
return nil, err |
| 376 |
} |
| 377 |
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil { |
| 378 |
return nil, err |
| 379 |
} |
| 380 |
if len(root.Args) == 3 { |
| 381 |
if arg3, err = b.processNode(root.Args[2], flagsEnum.None, props); err != nil { |
| 382 |
return nil, err |
| 383 |
} |
| 384 |
} |
| 385 |
qyOutput = &functionQuery{Func: substringFunc(arg1, arg2, arg3)} |
| 386 |
case "substring-before", "substring-after": |
| 387 |
//substring-xxxx( haystack, needle ) |
| 388 |
if len(root.Args) != 2 { |
| 389 |
return nil, errors.New("xpath: substring-before function must have two parameters") |
| 390 |
} |
| 391 |
var ( |
| 392 |
arg1, arg2 query |
| 393 |
err error |
| 394 |
) |
| 395 |
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil { |
| 396 |
return nil, err |
| 397 |
} |
| 398 |
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil { |
| 399 |
return nil, err |
| 400 |
} |
| 401 |
qyOutput = &functionQuery{ |
| 402 |
Func: substringIndFunc(arg1, arg2, root.FuncName == "substring-after"), |
| 403 |
} |
| 404 |
case "string-length": |
| 405 |
// string-length( [string] ) |
| 406 |
if len(root.Args) < 1 { |
| 407 |
return nil, errors.New("xpath: string-length function must have at least one parameter") |
| 408 |
} |
| 409 |
arg1, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 410 |
if err != nil { |
| 411 |
return nil, err |
| 412 |
} |
| 413 |
qyOutput = &functionQuery{Func: stringLengthFunc(arg1)} |
| 414 |
case "normalize-space": |
| 415 |
var arg node |
| 416 |
if len(root.Args) > 0 { |
| 417 |
arg = root.Args[0] |
| 418 |
} else { |
| 419 |
arg = newAxisNode("self", allNode, "", "", "", nil) |
| 420 |
} |
| 421 |
arg1, err := b.processNode(arg, flagsEnum.None, props) |
| 422 |
if err != nil { |
| 423 |
return nil, err |
| 424 |
} |
| 425 |
qyOutput = &functionQuery{Func: normalizespaceFunc(arg1)} |
| 426 |
case "replace": |
| 427 |
//replace( string , string, string ) |
| 428 |
if len(root.Args) != 3 { |
| 429 |
return nil, errors.New("xpath: replace function must have three parameters") |
| 430 |
} |
| 431 |
var ( |
| 432 |
arg1, arg2, arg3 query |
| 433 |
err error |
| 434 |
) |
| 435 |
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil { |
| 436 |
return nil, err |
| 437 |
} |
| 438 |
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil { |
| 439 |
return nil, err |
| 440 |
} |
| 441 |
if arg3, err = b.processNode(root.Args[2], flagsEnum.None, props); err != nil { |
| 442 |
return nil, err |
| 443 |
} |
| 444 |
qyOutput = &functionQuery{Func: replaceFunc(arg1, arg2, arg3)} |
| 445 |
case "translate": |
| 446 |
//translate( string , string, string ) |
| 447 |
if len(root.Args) != 3 { |
| 448 |
return nil, errors.New("xpath: translate function must have three parameters") |
| 449 |
} |
| 450 |
var ( |
| 451 |
arg1, arg2, arg3 query |
| 452 |
err error |
| 453 |
) |
| 454 |
if arg1, err = b.processNode(root.Args[0], flagsEnum.None, props); err != nil { |
| 455 |
return nil, err |
| 456 |
} |
| 457 |
if arg2, err = b.processNode(root.Args[1], flagsEnum.None, props); err != nil { |
| 458 |
return nil, err |
| 459 |
} |
| 460 |
if arg3, err = b.processNode(root.Args[2], flagsEnum.None, props); err != nil { |
| 461 |
return nil, err |
| 462 |
} |
| 463 |
qyOutput = &functionQuery{Func: translateFunc(arg1, arg2, arg3)} |
| 464 |
case "not": |
| 465 |
if len(root.Args) == 0 { |
| 466 |
return nil, errors.New("xpath: not function must have at least one parameter") |
| 467 |
} |
| 468 |
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 469 |
if err != nil { |
| 470 |
return nil, err |
| 471 |
} |
| 472 |
qyOutput = &functionQuery{Func: notFunc(argQuery)} |
| 473 |
case "name", "local-name", "namespace-uri": |
| 474 |
if len(root.Args) > 1 { |
| 475 |
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName) |
| 476 |
} |
| 477 |
var ( |
| 478 |
arg query |
| 479 |
err error |
| 480 |
) |
| 481 |
if len(root.Args) == 1 { |
| 482 |
arg, err = b.processNode(root.Args[0], flagsEnum.None, props) |
| 483 |
if err != nil { |
| 484 |
return nil, err |
| 485 |
} |
| 486 |
} |
| 487 |
switch root.FuncName { |
| 488 |
case "name": |
| 489 |
qyOutput = &functionQuery{Func: nameFunc(arg)} |
| 490 |
case "local-name": |
| 491 |
qyOutput = &functionQuery{Func: localNameFunc(arg)} |
| 492 |
case "namespace-uri": |
| 493 |
qyOutput = &functionQuery{Func: namespaceFunc(arg)} |
| 494 |
} |
| 495 |
case "true", "false": |
| 496 |
val := root.FuncName == "true" |
| 497 |
qyOutput = &functionQuery{ |
| 498 |
Func: func(_ query, _ iterator) interface{} { |
| 499 |
return val |
| 500 |
}, |
| 501 |
} |
| 502 |
case "last": |
| 503 |
qyOutput = &functionQuery{Input: b.firstInput, Func: lastFunc()} |
| 504 |
*props |= builderProps.HasLast |
| 505 |
case "position": |
| 506 |
qyOutput = &functionQuery{Input: b.firstInput, Func: positionFunc()} |
| 507 |
*props |= builderProps.HasPosition |
| 508 |
case "boolean", "number", "string": |
| 509 |
var inp query |
| 510 |
if len(root.Args) > 1 { |
| 511 |
return nil, fmt.Errorf("xpath: %s function must have at most one parameter", root.FuncName) |
| 512 |
} |
| 513 |
if len(root.Args) == 1 { |
| 514 |
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 515 |
if err != nil { |
| 516 |
return nil, err |
| 517 |
} |
| 518 |
inp = argQuery |
| 519 |
} |
| 520 |
switch root.FuncName { |
| 521 |
case "boolean": |
| 522 |
qyOutput = &functionQuery{Func: booleanFunc(inp)} |
| 523 |
case "string": |
| 524 |
qyOutput = &functionQuery{Func: stringFunc(inp)} |
| 525 |
case "number": |
| 526 |
qyOutput = &functionQuery{Func: numberFunc(inp)} |
| 527 |
} |
| 528 |
case "count": |
| 529 |
if len(root.Args) == 0 { |
| 530 |
return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets") |
| 531 |
} |
| 532 |
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 533 |
if err != nil { |
| 534 |
return nil, err |
| 535 |
} |
| 536 |
qyOutput = &functionQuery{Func: countFunc(argQuery)} |
| 537 |
case "sum": |
| 538 |
if len(root.Args) == 0 { |
| 539 |
return nil, fmt.Errorf("xpath: sum(node-sets) function must with have parameters node-sets") |
| 540 |
} |
| 541 |
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 542 |
if err != nil { |
| 543 |
return nil, err |
| 544 |
} |
| 545 |
qyOutput = &functionQuery{Func: sumFunc(argQuery)} |
| 546 |
case "ceiling", "floor", "round": |
| 547 |
if len(root.Args) == 0 { |
| 548 |
return nil, fmt.Errorf("xpath: ceiling(node-sets) function must with have parameters node-sets") |
| 549 |
} |
| 550 |
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 551 |
if err != nil { |
| 552 |
return nil, err |
| 553 |
} |
| 554 |
switch root.FuncName { |
| 555 |
case "ceiling": |
| 556 |
qyOutput = &functionQuery{Func: ceilingFunc(argQuery)} |
| 557 |
case "floor": |
| 558 |
qyOutput = &functionQuery{Func: floorFunc(argQuery)} |
| 559 |
case "round": |
| 560 |
qyOutput = &functionQuery{Func: roundFunc(argQuery)} |
| 561 |
} |
| 562 |
case "concat": |
| 563 |
if len(root.Args) < 2 { |
| 564 |
return nil, fmt.Errorf("xpath: concat() must have at least two arguments") |
| 565 |
} |
| 566 |
var args []query |
| 567 |
for _, v := range root.Args { |
| 568 |
q, err := b.processNode(v, flagsEnum.None, props) |
| 569 |
if err != nil { |
| 570 |
return nil, err |
| 571 |
} |
| 572 |
args = append(args, q) |
| 573 |
} |
| 574 |
qyOutput = &functionQuery{Func: concatFunc(args...)} |
| 575 |
case "reverse": |
| 576 |
if len(root.Args) == 0 { |
| 577 |
return nil, fmt.Errorf("xpath: reverse(node-sets) function must with have parameters node-sets") |
| 578 |
} |
| 579 |
argQuery, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 580 |
if err != nil { |
| 581 |
return nil, err |
| 582 |
} |
| 583 |
qyOutput = &transformFunctionQuery{Input: argQuery, Func: reverseFunc} |
| 584 |
case "string-join": |
| 585 |
if len(root.Args) != 2 { |
| 586 |
return nil, fmt.Errorf("xpath: string-join(node-sets, separator) function requires node-set and argument") |
| 587 |
} |
| 588 |
input, err := b.processNode(root.Args[0], flagsEnum.None, props) |
| 589 |
if err != nil { |
| 590 |
return nil, err |
| 591 |
} |
| 592 |
arg1, err := b.processNode(root.Args[1], flagsEnum.None, props) |
| 593 |
if err != nil { |
| 594 |
return nil, err |
| 595 |
} |
| 596 |
qyOutput = &functionQuery{Func: stringJoinFunc(input, arg1)} |
| 597 |
default: |
| 598 |
return nil, fmt.Errorf("not yet support this function %s()", root.FuncName) |
| 599 |
} |
| 600 |
return qyOutput, nil |
| 601 |
} |
| 602 |
|
| 603 |
func (b *builder) processOperator(root *operatorNode, props *builderProp) (query, error) { |
| 604 |
var ( |
| 605 |
leftProp builderProp |
| 606 |
rightProp builderProp |
| 607 |
) |
| 608 |
|
| 609 |
left, err := b.processNode(root.Left, flagsEnum.None, &leftProp) |
| 610 |
if err != nil { |
| 611 |
return nil, err |
| 612 |
} |
| 613 |
right, err := b.processNode(root.Right, flagsEnum.None, &rightProp) |
| 614 |
if err != nil { |
| 615 |
return nil, err |
| 616 |
} |
| 617 |
*props = leftProp | rightProp |
| 618 |
|
| 619 |
var qyOutput query |
| 620 |
switch root.Op { |
| 621 |
case "+", "-", "*", "div", "mod": // Numeric operator |
| 622 |
var exprFunc func(iterator, interface{}, interface{}) interface{} |
| 623 |
switch root.Op { |
| 624 |
case "+": |
| 625 |
exprFunc = plusFunc |
| 626 |
case "-": |
| 627 |
exprFunc = minusFunc |
| 628 |
case "*": |
| 629 |
exprFunc = mulFunc |
| 630 |
case "div": |
| 631 |
exprFunc = divFunc |
| 632 |
case "mod": |
| 633 |
exprFunc = modFunc |
| 634 |
} |
| 635 |
qyOutput = &numericQuery{Left: left, Right: right, Do: exprFunc} |
| 636 |
case "=", ">", ">=", "<", "<=", "!=": |
| 637 |
var exprFunc func(iterator, interface{}, interface{}) interface{} |
| 638 |
switch root.Op { |
| 639 |
case "=": |
| 640 |
exprFunc = eqFunc |
| 641 |
case ">": |
| 642 |
exprFunc = gtFunc |
| 643 |
case ">=": |
| 644 |
exprFunc = geFunc |
| 645 |
case "<": |
| 646 |
exprFunc = ltFunc |
| 647 |
case "<=": |
| 648 |
exprFunc = leFunc |
| 649 |
case "!=": |
| 650 |
exprFunc = neFunc |
| 651 |
} |
| 652 |
qyOutput = &logicalQuery{Left: left, Right: right, Do: exprFunc} |
| 653 |
case "or", "and": |
| 654 |
isOr := false |
| 655 |
if root.Op == "or" { |
| 656 |
isOr = true |
| 657 |
} |
| 658 |
qyOutput = &booleanQuery{Left: left, Right: right, IsOr: isOr} |
| 659 |
case "|": |
| 660 |
*props |= builderProps.NonFlat |
| 661 |
qyOutput = &unionQuery{Left: left, Right: right} |
| 662 |
} |
| 663 |
return qyOutput, nil |
| 664 |
} |
| 665 |
|
| 666 |
func (b *builder) processNode(root node, flags flag, props *builderProp) (q query, err error) { |
| 667 |
if b.parseDepth = b.parseDepth + 1; b.parseDepth > 1024 { |
| 668 |
err = errors.New("the xpath expressions is too complex") |
| 669 |
return |
| 670 |
} |
| 671 |
*props = builderProps.None |
| 672 |
switch root.Type() { |
| 673 |
case nodeConstantOperand: |
| 674 |
n := root.(*operandNode) |
| 675 |
q = &constantQuery{Val: n.Val} |
| 676 |
case nodeRoot: |
| 677 |
q = &absoluteQuery{} |
| 678 |
case nodeAxis: |
| 679 |
q, err = b.processAxis(root.(*axisNode), flags, props) |
| 680 |
b.firstInput = q |
| 681 |
case nodeFilter: |
| 682 |
q, err = b.processFilter(root.(*filterNode), flags, props) |
| 683 |
b.firstInput = q |
| 684 |
case nodeFunction: |
| 685 |
q, err = b.processFunction(root.(*functionNode), props) |
| 686 |
case nodeOperator: |
| 687 |
q, err = b.processOperator(root.(*operatorNode), props) |
| 688 |
case nodeGroup: |
| 689 |
q, err = b.processNode(root.(*groupNode).Input, flagsEnum.None, props) |
| 690 |
if err != nil { |
| 691 |
return |
| 692 |
} |
| 693 |
q = &groupQuery{Input: q} |
| 694 |
b.firstInput = q |
| 695 |
case nodeVariable: |
| 696 |
// Variables have no binding in a compiled expression, so they cannot |
| 697 |
// resolve to a query. A bare "$x" already surfaces as an undeclared |
| 698 |
// variable error because the nil result reaches the caller, but a |
| 699 |
// variable nested in a larger expression (e.g. "$x/@attr") would be |
| 700 |
// swallowed here and left as a nil sub-query that panics at select |
| 701 |
// time. Report it as undeclared instead. |
| 702 |
n := root.(*variableNode) |
| 703 |
name := n.Name |
| 704 |
if n.Prefix != "" { |
| 705 |
name = n.Prefix + ":" + name |
| 706 |
} |
| 707 |
err = fmt.Errorf("undeclared variable in XPath expression: $%s", name) |
| 708 |
} |
| 709 |
b.parseDepth-- |
| 710 |
return |
| 711 |
} |
| 712 |
|
| 713 |
// build builds a specified XPath expressions expr. |
| 714 |
func build(expr string, namespaces map[string]string) (q query, err error) { |
| 715 |
defer func() { |
| 716 |
if e := recover(); e != nil { |
| 717 |
switch x := e.(type) { |
| 718 |
case string: |
| 719 |
err = errors.New(x) |
| 720 |
case error: |
| 721 |
err = x |
| 722 |
default: |
| 723 |
err = errors.New("unknown panic") |
| 724 |
} |
| 725 |
} |
| 726 |
}() |
| 727 |
root := parse(expr, namespaces) |
| 728 |
b := &builder{} |
| 729 |
props := builderProps.None |
| 730 |
return b.processNode(root, flagsEnum.None, &props) |
| 731 |
} |