初始化项目
This commit is contained in:
301
trunk/goutil/xmlUtil/gxpath/internal/build/build.go
Normal file
301
trunk/goutil/xmlUtil/gxpath/internal/build/build.go
Normal file
@@ -0,0 +1,301 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"goutil/xmlUtil/gxpath/internal/parse"
|
||||
"goutil/xmlUtil/gxpath/internal/query"
|
||||
"goutil/xmlUtil/gxpath/xpath"
|
||||
)
|
||||
|
||||
type flag int
|
||||
|
||||
const (
|
||||
noneFlag flag = iota
|
||||
filterFlag
|
||||
)
|
||||
|
||||
// builder provides building an XPath expressions.
|
||||
type builder struct {
|
||||
depth int
|
||||
flag flag
|
||||
firstInput query.Query
|
||||
}
|
||||
|
||||
// axisPredicate creates a predicate to predicating for this axis node.
|
||||
func axisPredicate(root *parse.AxisNode) func(xpath.NodeNavigator) bool {
|
||||
// get current axix node type.
|
||||
typ := xpath.ElementNode
|
||||
if root.AxeType == "attribute" {
|
||||
typ = xpath.AttributeNode
|
||||
} else {
|
||||
switch root.Prop {
|
||||
case "comment":
|
||||
typ = xpath.CommentNode
|
||||
case "text":
|
||||
typ = xpath.TextNode
|
||||
// case "processing-instruction":
|
||||
// typ = xpath.ProcessingInstructionNode
|
||||
case "node":
|
||||
typ = xpath.ElementNode
|
||||
}
|
||||
}
|
||||
predicate := func(n xpath.NodeNavigator) bool {
|
||||
if typ == n.NodeType() {
|
||||
if root.LocalName == "" || (root.LocalName == n.LocalName() && root.Prefix == n.Prefix()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return predicate
|
||||
}
|
||||
|
||||
// processAxisNode buildes a query for the XPath axis node.
|
||||
func (b *builder) processAxisNode(root *parse.AxisNode) (query.Query, error) {
|
||||
var (
|
||||
err error
|
||||
qyInput query.Query
|
||||
qyOutput query.Query
|
||||
predicate = axisPredicate(root)
|
||||
)
|
||||
|
||||
if root.Input == nil {
|
||||
qyInput = &query.ContextQuery{}
|
||||
} else {
|
||||
if b.flag&filterFlag == 0 {
|
||||
if root.AxeType == "child" && (root.Input.Type() == parse.NodeAxis) {
|
||||
if input := root.Input.(*parse.AxisNode); input.AxeType == "descendant-or-self" {
|
||||
var qyGrandInput query.Query
|
||||
if input.Input != nil {
|
||||
qyGrandInput, _ = b.processNode(input.Input)
|
||||
} else {
|
||||
qyGrandInput = &query.ContextQuery{}
|
||||
}
|
||||
qyOutput = &query.DescendantQuery{Input: qyGrandInput, Predicate: predicate, Self: true}
|
||||
return qyOutput, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
qyInput, err = b.processNode(root.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
switch root.AxeType {
|
||||
case "ancestor":
|
||||
qyOutput = &query.AncestorQuery{Input: qyInput, Predicate: predicate}
|
||||
case "ancestor-or-self":
|
||||
qyOutput = &query.AncestorQuery{Input: qyInput, Predicate: predicate, Self: true}
|
||||
case "attribute":
|
||||
qyOutput = &query.AttributeQuery{Input: qyInput, Predicate: predicate}
|
||||
case "child":
|
||||
filter := func(n xpath.NodeNavigator) bool {
|
||||
v := predicate(n)
|
||||
switch root.Prop {
|
||||
case "text":
|
||||
v = v && n.NodeType() == xpath.TextNode
|
||||
case "node":
|
||||
v = v && (n.NodeType() == xpath.ElementNode || n.NodeType() == xpath.TextNode)
|
||||
case "comment":
|
||||
v = v && n.NodeType() == xpath.CommentNode
|
||||
}
|
||||
return v
|
||||
}
|
||||
qyOutput = &query.ChildQuery{Input: qyInput, Predicate: filter}
|
||||
case "descendant":
|
||||
qyOutput = &query.DescendantQuery{Input: qyInput, Predicate: predicate}
|
||||
case "descendant-or-self":
|
||||
qyOutput = &query.DescendantQuery{Input: qyInput, Predicate: predicate, Self: true}
|
||||
case "following":
|
||||
qyOutput = &query.FollowingQuery{Input: qyInput, Predicate: predicate}
|
||||
case "following-sibling":
|
||||
qyOutput = &query.FollowingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
|
||||
case "parent":
|
||||
qyOutput = &query.ParentQuery{Input: qyInput, Predicate: predicate}
|
||||
case "preceding":
|
||||
qyOutput = &query.PrecedingQuery{Input: qyInput, Predicate: predicate}
|
||||
case "preceding-sibling":
|
||||
qyOutput = &query.PrecedingQuery{Input: qyInput, Predicate: predicate, Sibling: true}
|
||||
case "self":
|
||||
qyOutput = &query.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
|
||||
}
|
||||
|
||||
// processFilterNode builds query.Query for the XPath filter predicate.
|
||||
func (b *builder) processFilterNode(root *parse.FilterNode) (query.Query, error) {
|
||||
b.flag |= filterFlag
|
||||
|
||||
qyInput, err := b.processNode(root.Input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qyCond, err := b.processNode(root.Condition)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qyOutput := &query.FilterQuery{Input: qyInput, Predicate: qyCond}
|
||||
return qyOutput, nil
|
||||
}
|
||||
|
||||
// processFunctionNode buildes query.Query for the XPath function node.
|
||||
func (b *builder) processFunctionNode(root *parse.FunctionNode) (query.Query, error) {
|
||||
var qyOutput query.Query
|
||||
switch root.FuncName {
|
||||
case "starts-with":
|
||||
arg1, err := b.processNode(root.Args[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
arg2, err := b.processNode(root.Args[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
startwith := &startwithFunc{arg1, arg2}
|
||||
qyOutput = &query.XPathFunction{Input: b.firstInput, Func: startwith.do}
|
||||
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.Query
|
||||
err error
|
||||
)
|
||||
if arg1, err = b.processNode(root.Args[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if arg2, err = b.processNode(root.Args[1]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(root.Args) == 3 {
|
||||
if arg3, err = b.processNode(root.Args[2]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
substring := &substringFunc{arg1, arg2, arg3}
|
||||
qyOutput = &query.XPathFunction{Input: b.firstInput, Func: substring.do}
|
||||
case "normalize-space":
|
||||
if len(root.Args) == 0 {
|
||||
return nil, errors.New("xpath: normalize-space function must have at least one parameter")
|
||||
}
|
||||
argQuery, err := b.processNode(root.Args[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qyOutput = &query.XPathFunction{Input: argQuery, Func: normalizespaceFunc}
|
||||
case "name":
|
||||
qyOutput = &query.XPathFunction{Input: b.firstInput, Func: nameFunc}
|
||||
case "last":
|
||||
qyOutput = &query.XPathFunction{Input: b.firstInput, Func: lastFunc}
|
||||
case "position":
|
||||
qyOutput = &query.XPathFunction{Input: b.firstInput, Func: positionFunc}
|
||||
case "count":
|
||||
if b.firstInput == nil {
|
||||
return nil, errors.New("xpath: expression must evaluate to node-set")
|
||||
}
|
||||
if len(root.Args) == 0 {
|
||||
return nil, fmt.Errorf("xpath: count(node-sets) function must with have parameters node-sets")
|
||||
}
|
||||
argQuery, err := b.processNode(root.Args[0])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
qyOutput = &query.XPathFunction{Input: argQuery, Func: countFunc}
|
||||
default:
|
||||
return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
|
||||
}
|
||||
return qyOutput, nil
|
||||
}
|
||||
|
||||
func (b *builder) processOperatorNode(root *parse.OperatorNode) (query.Query, error) {
|
||||
left, err := b.processNode(root.Left)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
right, err := b.processNode(root.Right)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var qyOutput query.Query
|
||||
switch root.Op {
|
||||
case "+", "-", "div", "mod": // Numeric operator
|
||||
var exprFunc func(interface{}, interface{}) interface{}
|
||||
switch root.Op {
|
||||
case "+":
|
||||
exprFunc = plusFunc
|
||||
case "-":
|
||||
exprFunc = minusFunc
|
||||
case "div":
|
||||
exprFunc = divFunc
|
||||
case "mod":
|
||||
exprFunc = modFunc
|
||||
}
|
||||
qyOutput = &query.NumericExpr{Left: left, Right: right, Do: exprFunc}
|
||||
case "=", ">", ">=", "<", "<=", "!=":
|
||||
var exprFunc func(query.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 = &query.LogicalExpr{Left: left, Right: right, Do: exprFunc}
|
||||
case "or", "and", "|":
|
||||
isOr := false
|
||||
if root.Op == "or" || root.Op == "|" {
|
||||
isOr = true
|
||||
}
|
||||
qyOutput = &query.BooleanExpr{Left: left, Right: right, IsOr: isOr}
|
||||
}
|
||||
return qyOutput, nil
|
||||
}
|
||||
|
||||
func (b *builder) processNode(root parse.Node) (q query.Query, err error) {
|
||||
if b.depth = b.depth + 1; b.depth > 1024 {
|
||||
err = errors.New("the xpath expressions is too complex")
|
||||
return
|
||||
}
|
||||
|
||||
switch root.Type() {
|
||||
case parse.NodeConstantOperand:
|
||||
n := root.(*parse.OperandNode)
|
||||
q = &query.XPathConstant{Val: n.Val}
|
||||
case parse.NodeRoot:
|
||||
q = &query.ContextQuery{Root: true}
|
||||
case parse.NodeAxis:
|
||||
q, err = b.processAxisNode(root.(*parse.AxisNode))
|
||||
b.firstInput = q
|
||||
case parse.NodeFilter:
|
||||
q, err = b.processFilterNode(root.(*parse.FilterNode))
|
||||
case parse.NodeFunction:
|
||||
q, err = b.processFunctionNode(root.(*parse.FunctionNode))
|
||||
case parse.NodeOperator:
|
||||
q, err = b.processOperatorNode(root.(*parse.OperatorNode))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Build builds a specified XPath expressions expr.
|
||||
func Build(expr string) (query.Query, error) {
|
||||
root := parse.Parse(expr)
|
||||
b := &builder{}
|
||||
return b.processNode(root)
|
||||
}
|
||||
295
trunk/goutil/xmlUtil/gxpath/internal/build/expr.go
Normal file
295
trunk/goutil/xmlUtil/gxpath/internal/build/expr.go
Normal file
@@ -0,0 +1,295 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strconv"
|
||||
|
||||
"goutil/xmlUtil/gxpath/internal/query"
|
||||
)
|
||||
|
||||
// valueType is a return value type.
|
||||
type valueType int
|
||||
|
||||
const (
|
||||
booleanType valueType = iota
|
||||
numberType
|
||||
stringType
|
||||
nodeSetType
|
||||
)
|
||||
|
||||
func getValueType(i interface{}) valueType {
|
||||
v := reflect.ValueOf(i)
|
||||
switch v.Kind() {
|
||||
case reflect.Float64:
|
||||
return numberType
|
||||
case reflect.String:
|
||||
return stringType
|
||||
case reflect.Bool:
|
||||
return booleanType
|
||||
default:
|
||||
if _, ok := i.(query.Query); ok {
|
||||
return nodeSetType
|
||||
}
|
||||
}
|
||||
panic(fmt.Errorf("xpath unknown value type: %v", v.Kind()))
|
||||
}
|
||||
|
||||
type logical func(query.Iterator, string, interface{}, interface{}) bool
|
||||
|
||||
var logicalFuncs = [][]logical{
|
||||
[]logical{cmpBoolean_Boolean, nil, nil, nil},
|
||||
[]logical{nil, cmpNumeric_Numeric, cmpNumeric_String, cmpNumeric_NodeSet},
|
||||
[]logical{nil, cmpString_Numeric, cmpString_String, cmpString_NodeSet},
|
||||
[]logical{nil, cmpNodeSet_Numeric, cmpNodeSet_String, cmpNodeSet_NodeSet},
|
||||
}
|
||||
|
||||
// number vs number
|
||||
func cmpNumberNumberF(op string, a, b float64) bool {
|
||||
switch op {
|
||||
case "=":
|
||||
return a == b
|
||||
case ">":
|
||||
return a > b
|
||||
case "<":
|
||||
return a < b
|
||||
case ">=":
|
||||
return a >= b
|
||||
case "<=":
|
||||
return a <= b
|
||||
case "!=":
|
||||
return a != b
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// string vs string
|
||||
func cmpStringStringF(op string, a, b string) bool {
|
||||
switch op {
|
||||
case "=":
|
||||
return a == b
|
||||
case ">":
|
||||
return a > b
|
||||
case "<":
|
||||
return a < b
|
||||
case ">=":
|
||||
return a >= b
|
||||
case "<=":
|
||||
return a <= b
|
||||
case "!=":
|
||||
return a != b
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpBooleanBooleanF(op string, a, b bool) bool {
|
||||
switch op {
|
||||
case "or":
|
||||
return a || b
|
||||
case "and":
|
||||
return a && b
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpNumeric_Numeric(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(float64)
|
||||
b := n.(float64)
|
||||
return cmpNumberNumberF(op, a, b)
|
||||
}
|
||||
|
||||
func cmpNumeric_String(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(float64)
|
||||
b := n.(string)
|
||||
num, err := strconv.ParseFloat(b, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return cmpNumberNumberF(op, a, num)
|
||||
}
|
||||
|
||||
func cmpNumeric_NodeSet(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(float64)
|
||||
b := n.(query.Query)
|
||||
|
||||
for {
|
||||
node := b.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
num, err := strconv.ParseFloat(node.Value(), 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if cmpNumberNumberF(op, a, num) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpNodeSet_Numeric(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(query.Query)
|
||||
b := n.(float64)
|
||||
for {
|
||||
node := a.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
num, err := strconv.ParseFloat(node.Value(), 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if cmpNumberNumberF(op, num, b) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpNodeSet_String(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(query.Query)
|
||||
b := n.(string)
|
||||
for {
|
||||
node := a.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
if cmpStringStringF(op, b, node.Value()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpNodeSet_NodeSet(t query.Iterator, op string, m, n interface{}) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpString_Numeric(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(string)
|
||||
b := n.(float64)
|
||||
num, err := strconv.ParseFloat(a, 64)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return cmpNumberNumberF(op, b, num)
|
||||
}
|
||||
|
||||
func cmpString_String(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(string)
|
||||
b := n.(string)
|
||||
return cmpStringStringF(op, a, b)
|
||||
}
|
||||
|
||||
func cmpString_NodeSet(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(string)
|
||||
b := n.(query.Query)
|
||||
for {
|
||||
node := b.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
if cmpStringStringF(op, a, node.Value()) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func cmpBoolean_Boolean(t query.Iterator, op string, m, n interface{}) bool {
|
||||
a := m.(bool)
|
||||
b := n.(bool)
|
||||
return cmpBooleanBooleanF(op, a, b)
|
||||
}
|
||||
|
||||
// eqFunc is an `=` operator.
|
||||
func eqFunc(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, "=", m, n)
|
||||
}
|
||||
|
||||
// gtFunc is an `>` operator.
|
||||
func gtFunc(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, ">", m, n)
|
||||
}
|
||||
|
||||
// geFunc is an `>=` operator.
|
||||
func geFunc(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, ">=", m, n)
|
||||
}
|
||||
|
||||
// ltFunc is an `<` operator.
|
||||
func ltFunc(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, "<", m, n)
|
||||
}
|
||||
|
||||
// leFunc is an `<=` operator.
|
||||
func leFunc(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, "<=", m, n)
|
||||
}
|
||||
|
||||
// neFunc is an `!=` operator.
|
||||
func neFunc(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, "!=", m, n)
|
||||
}
|
||||
|
||||
// orFunc is an `or` operator.
|
||||
var orFunc = func(t query.Iterator, m, n interface{}) interface{} {
|
||||
t1 := getValueType(m)
|
||||
t2 := getValueType(n)
|
||||
return logicalFuncs[t1][t2](t, "or", m, n)
|
||||
}
|
||||
|
||||
func numericExpr(m, n interface{}, cb func(float64, float64) float64) float64 {
|
||||
typ := reflect.TypeOf(float64(0))
|
||||
a := reflect.ValueOf(m).Convert(typ)
|
||||
b := reflect.ValueOf(n).Convert(typ)
|
||||
return cb(a.Float(), b.Float())
|
||||
}
|
||||
|
||||
// plusFunc is an `+` operator.
|
||||
var plusFunc = func(m, n interface{}) interface{} {
|
||||
return numericExpr(m, n, func(a, b float64) float64 {
|
||||
return a + b
|
||||
})
|
||||
}
|
||||
|
||||
// minusFunc is an `-` operator.
|
||||
var minusFunc = func(m, n interface{}) interface{} {
|
||||
return numericExpr(m, n, func(a, b float64) float64 {
|
||||
return a - b
|
||||
})
|
||||
}
|
||||
|
||||
// mulFunc is an `*` operator.
|
||||
var mulFunc = func(m, n interface{}) interface{} {
|
||||
return numericExpr(m, n, func(a, b float64) float64 {
|
||||
return a * b
|
||||
})
|
||||
}
|
||||
|
||||
// divFunc is an `DIV` operator.
|
||||
var divFunc = func(m, n interface{}) interface{} {
|
||||
return numericExpr(m, n, func(a, b float64) float64 {
|
||||
return a / b
|
||||
})
|
||||
}
|
||||
|
||||
// modFunc is an 'MOD' operator.
|
||||
var modFunc = func(m, n interface{}) interface{} {
|
||||
return numericExpr(m, n, func(a, b float64) float64 {
|
||||
return float64(int(a) % int(b))
|
||||
})
|
||||
}
|
||||
160
trunk/goutil/xmlUtil/gxpath/internal/build/func.go
Normal file
160
trunk/goutil/xmlUtil/gxpath/internal/build/func.go
Normal file
@@ -0,0 +1,160 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"goutil/xmlUtil/gxpath/internal/query"
|
||||
"goutil/xmlUtil/gxpath/xpath"
|
||||
)
|
||||
|
||||
func predicate(q query.Query) func(xpath.NodeNavigator) bool {
|
||||
type Predicater interface {
|
||||
Test(xpath.NodeNavigator) bool
|
||||
}
|
||||
if p, ok := q.(Predicater); ok {
|
||||
return p.Test
|
||||
}
|
||||
return func(xpath.NodeNavigator) bool { return true }
|
||||
}
|
||||
|
||||
// positionFunc is a XPath Node Set functions postion().
|
||||
var positionFunc = func(q query.Query, t query.Iterator) interface{} {
|
||||
var (
|
||||
count = 1
|
||||
node = t.Current()
|
||||
)
|
||||
test := predicate(q)
|
||||
for node.MoveToPrevious() {
|
||||
if test(node) {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return float64(count)
|
||||
}
|
||||
|
||||
// lastFunc is a XPath Node Set functions last().
|
||||
var lastFunc = func(q query.Query, t query.Iterator) interface{} {
|
||||
var (
|
||||
count = 0
|
||||
node = t.Current()
|
||||
)
|
||||
node.MoveToFirst()
|
||||
test := predicate(q)
|
||||
for {
|
||||
if test(node) {
|
||||
count++
|
||||
}
|
||||
if !node.MoveToNext() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return float64(count)
|
||||
}
|
||||
|
||||
// countFunc is a XPath Node Set functions count(node-set).
|
||||
var countFunc = func(q query.Query, t query.Iterator) interface{} {
|
||||
var (
|
||||
count = 0
|
||||
node = t.Current()
|
||||
)
|
||||
node.MoveToFirst()
|
||||
test := predicate(q)
|
||||
for {
|
||||
if test(node) {
|
||||
count++
|
||||
}
|
||||
if !node.MoveToNext() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return float64(count)
|
||||
}
|
||||
|
||||
// nameFunc is a XPath functions name([node-set]).
|
||||
var nameFunc = func(q query.Query, t query.Iterator) interface{} {
|
||||
return t.Current().LocalName()
|
||||
}
|
||||
|
||||
// startwithFunc is a XPath functions starts-with(string, string).
|
||||
type startwithFunc struct {
|
||||
arg1, arg2 query.Query
|
||||
}
|
||||
|
||||
func (s *startwithFunc) do(q query.Query, t query.Iterator) interface{} {
|
||||
var (
|
||||
m, n string
|
||||
ok bool
|
||||
)
|
||||
switch typ := s.arg1.Evaluate(t).(type) {
|
||||
case string:
|
||||
m = typ
|
||||
case query.Query:
|
||||
node := typ.Select(t)
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
m = node.Value()
|
||||
default:
|
||||
panic(errors.New("starts-with() function argument type must be string"))
|
||||
}
|
||||
n, ok = s.arg2.Evaluate(t).(string)
|
||||
if !ok {
|
||||
panic(errors.New("starts-with() function argument type must be string"))
|
||||
}
|
||||
return strings.HasPrefix(m, n)
|
||||
}
|
||||
|
||||
// normalizespaceFunc is XPath functions normalize-space(string?)
|
||||
var normalizespaceFunc = func(q query.Query, t query.Iterator) interface{} {
|
||||
var m string
|
||||
switch typ := q.Evaluate(t).(type) {
|
||||
case string:
|
||||
m = typ
|
||||
case query.Query:
|
||||
node := typ.Select(t)
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
m = node.Value()
|
||||
}
|
||||
return strings.TrimSpace(m)
|
||||
}
|
||||
|
||||
// substringFunc is XPath functions substring function returns a part of a given string.
|
||||
type substringFunc struct {
|
||||
arg1, arg2, arg3 query.Query
|
||||
}
|
||||
|
||||
func (f *substringFunc) do(q query.Query, t query.Iterator) interface{} {
|
||||
var m string
|
||||
switch typ := f.arg1.Evaluate(t).(type) {
|
||||
case string:
|
||||
m = typ
|
||||
case query.Query:
|
||||
node := typ.Select(t)
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
m = node.Value()
|
||||
}
|
||||
|
||||
var start, length float64
|
||||
var ok bool
|
||||
|
||||
if start, ok = f.arg2.Evaluate(t).(float64); !ok {
|
||||
panic(errors.New("substring() function first argument type must be int"))
|
||||
}
|
||||
if f.arg3 != nil {
|
||||
if length, ok = f.arg3.Evaluate(t).(float64); !ok {
|
||||
panic(errors.New("substring() function second argument type must be int"))
|
||||
}
|
||||
}
|
||||
if (len(m) - int(start)) < int(length) {
|
||||
panic(errors.New("substring() function start and length argument out of range"))
|
||||
}
|
||||
if length > 0 {
|
||||
return m[int(start):int(length+start)]
|
||||
}
|
||||
return m[int(start):]
|
||||
}
|
||||
315
trunk/goutil/xmlUtil/gxpath/internal/parse/char.go
Normal file
315
trunk/goutil/xmlUtil/gxpath/internal/parse/char.go
Normal file
@@ -0,0 +1,315 @@
|
||||
package parse
|
||||
|
||||
import "unicode"
|
||||
|
||||
var first = &unicode.RangeTable{
|
||||
R16: []unicode.Range16{
|
||||
{0x003A, 0x003A, 1},
|
||||
{0x0041, 0x005A, 1},
|
||||
{0x005F, 0x005F, 1},
|
||||
{0x0061, 0x007A, 1},
|
||||
{0x00C0, 0x00D6, 1},
|
||||
{0x00D8, 0x00F6, 1},
|
||||
{0x00F8, 0x00FF, 1},
|
||||
{0x0100, 0x0131, 1},
|
||||
{0x0134, 0x013E, 1},
|
||||
{0x0141, 0x0148, 1},
|
||||
{0x014A, 0x017E, 1},
|
||||
{0x0180, 0x01C3, 1},
|
||||
{0x01CD, 0x01F0, 1},
|
||||
{0x01F4, 0x01F5, 1},
|
||||
{0x01FA, 0x0217, 1},
|
||||
{0x0250, 0x02A8, 1},
|
||||
{0x02BB, 0x02C1, 1},
|
||||
{0x0386, 0x0386, 1},
|
||||
{0x0388, 0x038A, 1},
|
||||
{0x038C, 0x038C, 1},
|
||||
{0x038E, 0x03A1, 1},
|
||||
{0x03A3, 0x03CE, 1},
|
||||
{0x03D0, 0x03D6, 1},
|
||||
{0x03DA, 0x03E0, 2},
|
||||
{0x03E2, 0x03F3, 1},
|
||||
{0x0401, 0x040C, 1},
|
||||
{0x040E, 0x044F, 1},
|
||||
{0x0451, 0x045C, 1},
|
||||
{0x045E, 0x0481, 1},
|
||||
{0x0490, 0x04C4, 1},
|
||||
{0x04C7, 0x04C8, 1},
|
||||
{0x04CB, 0x04CC, 1},
|
||||
{0x04D0, 0x04EB, 1},
|
||||
{0x04EE, 0x04F5, 1},
|
||||
{0x04F8, 0x04F9, 1},
|
||||
{0x0531, 0x0556, 1},
|
||||
{0x0559, 0x0559, 1},
|
||||
{0x0561, 0x0586, 1},
|
||||
{0x05D0, 0x05EA, 1},
|
||||
{0x05F0, 0x05F2, 1},
|
||||
{0x0621, 0x063A, 1},
|
||||
{0x0641, 0x064A, 1},
|
||||
{0x0671, 0x06B7, 1},
|
||||
{0x06BA, 0x06BE, 1},
|
||||
{0x06C0, 0x06CE, 1},
|
||||
{0x06D0, 0x06D3, 1},
|
||||
{0x06D5, 0x06D5, 1},
|
||||
{0x06E5, 0x06E6, 1},
|
||||
{0x0905, 0x0939, 1},
|
||||
{0x093D, 0x093D, 1},
|
||||
{0x0958, 0x0961, 1},
|
||||
{0x0985, 0x098C, 1},
|
||||
{0x098F, 0x0990, 1},
|
||||
{0x0993, 0x09A8, 1},
|
||||
{0x09AA, 0x09B0, 1},
|
||||
{0x09B2, 0x09B2, 1},
|
||||
{0x09B6, 0x09B9, 1},
|
||||
{0x09DC, 0x09DD, 1},
|
||||
{0x09DF, 0x09E1, 1},
|
||||
{0x09F0, 0x09F1, 1},
|
||||
{0x0A05, 0x0A0A, 1},
|
||||
{0x0A0F, 0x0A10, 1},
|
||||
{0x0A13, 0x0A28, 1},
|
||||
{0x0A2A, 0x0A30, 1},
|
||||
{0x0A32, 0x0A33, 1},
|
||||
{0x0A35, 0x0A36, 1},
|
||||
{0x0A38, 0x0A39, 1},
|
||||
{0x0A59, 0x0A5C, 1},
|
||||
{0x0A5E, 0x0A5E, 1},
|
||||
{0x0A72, 0x0A74, 1},
|
||||
{0x0A85, 0x0A8B, 1},
|
||||
{0x0A8D, 0x0A8D, 1},
|
||||
{0x0A8F, 0x0A91, 1},
|
||||
{0x0A93, 0x0AA8, 1},
|
||||
{0x0AAA, 0x0AB0, 1},
|
||||
{0x0AB2, 0x0AB3, 1},
|
||||
{0x0AB5, 0x0AB9, 1},
|
||||
{0x0ABD, 0x0AE0, 0x23},
|
||||
{0x0B05, 0x0B0C, 1},
|
||||
{0x0B0F, 0x0B10, 1},
|
||||
{0x0B13, 0x0B28, 1},
|
||||
{0x0B2A, 0x0B30, 1},
|
||||
{0x0B32, 0x0B33, 1},
|
||||
{0x0B36, 0x0B39, 1},
|
||||
{0x0B3D, 0x0B3D, 1},
|
||||
{0x0B5C, 0x0B5D, 1},
|
||||
{0x0B5F, 0x0B61, 1},
|
||||
{0x0B85, 0x0B8A, 1},
|
||||
{0x0B8E, 0x0B90, 1},
|
||||
{0x0B92, 0x0B95, 1},
|
||||
{0x0B99, 0x0B9A, 1},
|
||||
{0x0B9C, 0x0B9C, 1},
|
||||
{0x0B9E, 0x0B9F, 1},
|
||||
{0x0BA3, 0x0BA4, 1},
|
||||
{0x0BA8, 0x0BAA, 1},
|
||||
{0x0BAE, 0x0BB5, 1},
|
||||
{0x0BB7, 0x0BB9, 1},
|
||||
{0x0C05, 0x0C0C, 1},
|
||||
{0x0C0E, 0x0C10, 1},
|
||||
{0x0C12, 0x0C28, 1},
|
||||
{0x0C2A, 0x0C33, 1},
|
||||
{0x0C35, 0x0C39, 1},
|
||||
{0x0C60, 0x0C61, 1},
|
||||
{0x0C85, 0x0C8C, 1},
|
||||
{0x0C8E, 0x0C90, 1},
|
||||
{0x0C92, 0x0CA8, 1},
|
||||
{0x0CAA, 0x0CB3, 1},
|
||||
{0x0CB5, 0x0CB9, 1},
|
||||
{0x0CDE, 0x0CDE, 1},
|
||||
{0x0CE0, 0x0CE1, 1},
|
||||
{0x0D05, 0x0D0C, 1},
|
||||
{0x0D0E, 0x0D10, 1},
|
||||
{0x0D12, 0x0D28, 1},
|
||||
{0x0D2A, 0x0D39, 1},
|
||||
{0x0D60, 0x0D61, 1},
|
||||
{0x0E01, 0x0E2E, 1},
|
||||
{0x0E30, 0x0E30, 1},
|
||||
{0x0E32, 0x0E33, 1},
|
||||
{0x0E40, 0x0E45, 1},
|
||||
{0x0E81, 0x0E82, 1},
|
||||
{0x0E84, 0x0E84, 1},
|
||||
{0x0E87, 0x0E88, 1},
|
||||
{0x0E8A, 0x0E8D, 3},
|
||||
{0x0E94, 0x0E97, 1},
|
||||
{0x0E99, 0x0E9F, 1},
|
||||
{0x0EA1, 0x0EA3, 1},
|
||||
{0x0EA5, 0x0EA7, 2},
|
||||
{0x0EAA, 0x0EAB, 1},
|
||||
{0x0EAD, 0x0EAE, 1},
|
||||
{0x0EB0, 0x0EB0, 1},
|
||||
{0x0EB2, 0x0EB3, 1},
|
||||
{0x0EBD, 0x0EBD, 1},
|
||||
{0x0EC0, 0x0EC4, 1},
|
||||
{0x0F40, 0x0F47, 1},
|
||||
{0x0F49, 0x0F69, 1},
|
||||
{0x10A0, 0x10C5, 1},
|
||||
{0x10D0, 0x10F6, 1},
|
||||
{0x1100, 0x1100, 1},
|
||||
{0x1102, 0x1103, 1},
|
||||
{0x1105, 0x1107, 1},
|
||||
{0x1109, 0x1109, 1},
|
||||
{0x110B, 0x110C, 1},
|
||||
{0x110E, 0x1112, 1},
|
||||
{0x113C, 0x1140, 2},
|
||||
{0x114C, 0x1150, 2},
|
||||
{0x1154, 0x1155, 1},
|
||||
{0x1159, 0x1159, 1},
|
||||
{0x115F, 0x1161, 1},
|
||||
{0x1163, 0x1169, 2},
|
||||
{0x116D, 0x116E, 1},
|
||||
{0x1172, 0x1173, 1},
|
||||
{0x1175, 0x119E, 0x119E - 0x1175},
|
||||
{0x11A8, 0x11AB, 0x11AB - 0x11A8},
|
||||
{0x11AE, 0x11AF, 1},
|
||||
{0x11B7, 0x11B8, 1},
|
||||
{0x11BA, 0x11BA, 1},
|
||||
{0x11BC, 0x11C2, 1},
|
||||
{0x11EB, 0x11F0, 0x11F0 - 0x11EB},
|
||||
{0x11F9, 0x11F9, 1},
|
||||
{0x1E00, 0x1E9B, 1},
|
||||
{0x1EA0, 0x1EF9, 1},
|
||||
{0x1F00, 0x1F15, 1},
|
||||
{0x1F18, 0x1F1D, 1},
|
||||
{0x1F20, 0x1F45, 1},
|
||||
{0x1F48, 0x1F4D, 1},
|
||||
{0x1F50, 0x1F57, 1},
|
||||
{0x1F59, 0x1F5B, 0x1F5B - 0x1F59},
|
||||
{0x1F5D, 0x1F5D, 1},
|
||||
{0x1F5F, 0x1F7D, 1},
|
||||
{0x1F80, 0x1FB4, 1},
|
||||
{0x1FB6, 0x1FBC, 1},
|
||||
{0x1FBE, 0x1FBE, 1},
|
||||
{0x1FC2, 0x1FC4, 1},
|
||||
{0x1FC6, 0x1FCC, 1},
|
||||
{0x1FD0, 0x1FD3, 1},
|
||||
{0x1FD6, 0x1FDB, 1},
|
||||
{0x1FE0, 0x1FEC, 1},
|
||||
{0x1FF2, 0x1FF4, 1},
|
||||
{0x1FF6, 0x1FFC, 1},
|
||||
{0x2126, 0x2126, 1},
|
||||
{0x212A, 0x212B, 1},
|
||||
{0x212E, 0x212E, 1},
|
||||
{0x2180, 0x2182, 1},
|
||||
{0x3007, 0x3007, 1},
|
||||
{0x3021, 0x3029, 1},
|
||||
{0x3041, 0x3094, 1},
|
||||
{0x30A1, 0x30FA, 1},
|
||||
{0x3105, 0x312C, 1},
|
||||
{0x4E00, 0x9FA5, 1},
|
||||
{0xAC00, 0xD7A3, 1},
|
||||
},
|
||||
}
|
||||
|
||||
var second = &unicode.RangeTable{
|
||||
R16: []unicode.Range16{
|
||||
{0x002D, 0x002E, 1},
|
||||
{0x0030, 0x0039, 1},
|
||||
{0x00B7, 0x00B7, 1},
|
||||
{0x02D0, 0x02D1, 1},
|
||||
{0x0300, 0x0345, 1},
|
||||
{0x0360, 0x0361, 1},
|
||||
{0x0387, 0x0387, 1},
|
||||
{0x0483, 0x0486, 1},
|
||||
{0x0591, 0x05A1, 1},
|
||||
{0x05A3, 0x05B9, 1},
|
||||
{0x05BB, 0x05BD, 1},
|
||||
{0x05BF, 0x05BF, 1},
|
||||
{0x05C1, 0x05C2, 1},
|
||||
{0x05C4, 0x0640, 0x0640 - 0x05C4},
|
||||
{0x064B, 0x0652, 1},
|
||||
{0x0660, 0x0669, 1},
|
||||
{0x0670, 0x0670, 1},
|
||||
{0x06D6, 0x06DC, 1},
|
||||
{0x06DD, 0x06DF, 1},
|
||||
{0x06E0, 0x06E4, 1},
|
||||
{0x06E7, 0x06E8, 1},
|
||||
{0x06EA, 0x06ED, 1},
|
||||
{0x06F0, 0x06F9, 1},
|
||||
{0x0901, 0x0903, 1},
|
||||
{0x093C, 0x093C, 1},
|
||||
{0x093E, 0x094C, 1},
|
||||
{0x094D, 0x094D, 1},
|
||||
{0x0951, 0x0954, 1},
|
||||
{0x0962, 0x0963, 1},
|
||||
{0x0966, 0x096F, 1},
|
||||
{0x0981, 0x0983, 1},
|
||||
{0x09BC, 0x09BC, 1},
|
||||
{0x09BE, 0x09BF, 1},
|
||||
{0x09C0, 0x09C4, 1},
|
||||
{0x09C7, 0x09C8, 1},
|
||||
{0x09CB, 0x09CD, 1},
|
||||
{0x09D7, 0x09D7, 1},
|
||||
{0x09E2, 0x09E3, 1},
|
||||
{0x09E6, 0x09EF, 1},
|
||||
{0x0A02, 0x0A3C, 0x3A},
|
||||
{0x0A3E, 0x0A3F, 1},
|
||||
{0x0A40, 0x0A42, 1},
|
||||
{0x0A47, 0x0A48, 1},
|
||||
{0x0A4B, 0x0A4D, 1},
|
||||
{0x0A66, 0x0A6F, 1},
|
||||
{0x0A70, 0x0A71, 1},
|
||||
{0x0A81, 0x0A83, 1},
|
||||
{0x0ABC, 0x0ABC, 1},
|
||||
{0x0ABE, 0x0AC5, 1},
|
||||
{0x0AC7, 0x0AC9, 1},
|
||||
{0x0ACB, 0x0ACD, 1},
|
||||
{0x0AE6, 0x0AEF, 1},
|
||||
{0x0B01, 0x0B03, 1},
|
||||
{0x0B3C, 0x0B3C, 1},
|
||||
{0x0B3E, 0x0B43, 1},
|
||||
{0x0B47, 0x0B48, 1},
|
||||
{0x0B4B, 0x0B4D, 1},
|
||||
{0x0B56, 0x0B57, 1},
|
||||
{0x0B66, 0x0B6F, 1},
|
||||
{0x0B82, 0x0B83, 1},
|
||||
{0x0BBE, 0x0BC2, 1},
|
||||
{0x0BC6, 0x0BC8, 1},
|
||||
{0x0BCA, 0x0BCD, 1},
|
||||
{0x0BD7, 0x0BD7, 1},
|
||||
{0x0BE7, 0x0BEF, 1},
|
||||
{0x0C01, 0x0C03, 1},
|
||||
{0x0C3E, 0x0C44, 1},
|
||||
{0x0C46, 0x0C48, 1},
|
||||
{0x0C4A, 0x0C4D, 1},
|
||||
{0x0C55, 0x0C56, 1},
|
||||
{0x0C66, 0x0C6F, 1},
|
||||
{0x0C82, 0x0C83, 1},
|
||||
{0x0CBE, 0x0CC4, 1},
|
||||
{0x0CC6, 0x0CC8, 1},
|
||||
{0x0CCA, 0x0CCD, 1},
|
||||
{0x0CD5, 0x0CD6, 1},
|
||||
{0x0CE6, 0x0CEF, 1},
|
||||
{0x0D02, 0x0D03, 1},
|
||||
{0x0D3E, 0x0D43, 1},
|
||||
{0x0D46, 0x0D48, 1},
|
||||
{0x0D4A, 0x0D4D, 1},
|
||||
{0x0D57, 0x0D57, 1},
|
||||
{0x0D66, 0x0D6F, 1},
|
||||
{0x0E31, 0x0E31, 1},
|
||||
{0x0E34, 0x0E3A, 1},
|
||||
{0x0E46, 0x0E46, 1},
|
||||
{0x0E47, 0x0E4E, 1},
|
||||
{0x0E50, 0x0E59, 1},
|
||||
{0x0EB1, 0x0EB1, 1},
|
||||
{0x0EB4, 0x0EB9, 1},
|
||||
{0x0EBB, 0x0EBC, 1},
|
||||
{0x0EC6, 0x0EC6, 1},
|
||||
{0x0EC8, 0x0ECD, 1},
|
||||
{0x0ED0, 0x0ED9, 1},
|
||||
{0x0F18, 0x0F19, 1},
|
||||
{0x0F20, 0x0F29, 1},
|
||||
{0x0F35, 0x0F39, 2},
|
||||
{0x0F3E, 0x0F3F, 1},
|
||||
{0x0F71, 0x0F84, 1},
|
||||
{0x0F86, 0x0F8B, 1},
|
||||
{0x0F90, 0x0F95, 1},
|
||||
{0x0F97, 0x0F97, 1},
|
||||
{0x0F99, 0x0FAD, 1},
|
||||
{0x0FB1, 0x0FB7, 1},
|
||||
{0x0FB9, 0x0FB9, 1},
|
||||
{0x20D0, 0x20DC, 1},
|
||||
{0x20E1, 0x3005, 0x3005 - 0x20E1},
|
||||
{0x302A, 0x302F, 1},
|
||||
{0x3031, 0x3035, 1},
|
||||
{0x3099, 0x309A, 1},
|
||||
{0x309D, 0x309E, 1},
|
||||
{0x30FC, 0x30FE, 1},
|
||||
},
|
||||
}
|
||||
132
trunk/goutil/xmlUtil/gxpath/internal/parse/node.go
Normal file
132
trunk/goutil/xmlUtil/gxpath/internal/parse/node.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// A Node is an element in the parse tree.
|
||||
type Node interface {
|
||||
Type() NodeType
|
||||
String() string
|
||||
}
|
||||
|
||||
// NodeType identifies the type of a parse tree node.
|
||||
type NodeType int
|
||||
|
||||
func (t NodeType) Type() NodeType {
|
||||
return t
|
||||
}
|
||||
|
||||
const (
|
||||
NodeRoot NodeType = iota
|
||||
NodeAxis
|
||||
NodeFilter
|
||||
NodeFunction
|
||||
NodeOperator
|
||||
NodeVariable
|
||||
NodeConstantOperand
|
||||
)
|
||||
|
||||
// RootNode holds a top-level node of tree.
|
||||
type RootNode struct {
|
||||
NodeType
|
||||
slash string
|
||||
}
|
||||
|
||||
func (r *RootNode) String() string {
|
||||
return r.slash
|
||||
}
|
||||
|
||||
// OperatorNode holds two Nodes operator.
|
||||
type OperatorNode struct {
|
||||
NodeType
|
||||
Op string
|
||||
Left Node
|
||||
Right Node
|
||||
}
|
||||
|
||||
func (o *OperatorNode) String() string {
|
||||
return fmt.Sprintf("%v%s%v", o.Left, o.Op, o.Right)
|
||||
}
|
||||
|
||||
// AxisNode holds a location step.
|
||||
type AxisNode struct {
|
||||
NodeType
|
||||
Input Node
|
||||
Prop string // node-test name.[comment|text|processing-instruction|node]
|
||||
AxeType string // name of the axes.[attribute|ancestor|child|....]
|
||||
LocalName string // local part name of node.
|
||||
Prefix string // prefix name of node.
|
||||
}
|
||||
|
||||
func (a *AxisNode) String() string {
|
||||
var b bytes.Buffer
|
||||
if a.AxeType != "" {
|
||||
b.Write([]byte(a.AxeType + "::"))
|
||||
}
|
||||
if a.Prefix != "" {
|
||||
b.Write([]byte(a.Prefix + ":"))
|
||||
}
|
||||
b.Write([]byte(a.LocalName))
|
||||
if a.Prop != "" {
|
||||
b.Write([]byte("/" + a.Prop + "()"))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// OperandNode holds a constant operand.
|
||||
type OperandNode struct {
|
||||
NodeType
|
||||
Val interface{}
|
||||
}
|
||||
|
||||
func (o *OperandNode) String() string {
|
||||
return fmt.Sprintf("%v", o.Val)
|
||||
}
|
||||
|
||||
// FilterNode holds a condition filter.
|
||||
type FilterNode struct {
|
||||
NodeType
|
||||
Input, Condition Node
|
||||
}
|
||||
|
||||
func (f *FilterNode) String() string {
|
||||
return fmt.Sprintf("%s[%s]", f.Input, f.Condition)
|
||||
}
|
||||
|
||||
// VariableNode holds a variable.
|
||||
type VariableNode struct {
|
||||
NodeType
|
||||
Name, Prefix string
|
||||
}
|
||||
|
||||
func (v *VariableNode) String() string {
|
||||
if v.Prefix == "" {
|
||||
return v.Name
|
||||
}
|
||||
return fmt.Sprintf("%s:%s", v.Prefix, v.Name)
|
||||
}
|
||||
|
||||
// FunctionNode holds a function call.
|
||||
type FunctionNode struct {
|
||||
NodeType
|
||||
Args []Node
|
||||
Prefix string
|
||||
FuncName string // function name
|
||||
}
|
||||
|
||||
func (f *FunctionNode) String() string {
|
||||
var b bytes.Buffer
|
||||
// fun(arg1, ..., argn)
|
||||
b.Write([]byte(f.FuncName))
|
||||
b.Write([]byte("("))
|
||||
for i, arg := range f.Args {
|
||||
if i > 0 {
|
||||
b.Write([]byte(","))
|
||||
}
|
||||
b.Write([]byte(fmt.Sprintf("%s", arg)))
|
||||
}
|
||||
b.Write([]byte(")"))
|
||||
return b.String()
|
||||
}
|
||||
447
trunk/goutil/xmlUtil/gxpath/internal/parse/parse.go
Normal file
447
trunk/goutil/xmlUtil/gxpath/internal/parse/parse.go
Normal file
@@ -0,0 +1,447 @@
|
||||
// https://www.w3.org/TR/xpath/
|
||||
|
||||
package parse
|
||||
|
||||
import "fmt"
|
||||
|
||||
type parser struct {
|
||||
r *scanner
|
||||
d int
|
||||
}
|
||||
|
||||
// newOperatorNode returns new operator node OperatorNode.
|
||||
func newOperatorNode(op string, left, right Node) Node {
|
||||
return &OperatorNode{NodeType: NodeOperator, Op: op, Left: left, Right: right}
|
||||
}
|
||||
|
||||
// newOperand returns new constant operand node OperandNode.
|
||||
func newOperandNode(v interface{}) Node {
|
||||
return &OperandNode{NodeType: NodeConstantOperand, Val: v}
|
||||
}
|
||||
|
||||
// newAxisNode returns new axis node AxisNode.
|
||||
func newAxisNode(axeTyp, localName, prefix, prop string, n Node) Node {
|
||||
return &AxisNode{
|
||||
NodeType: NodeAxis,
|
||||
LocalName: localName,
|
||||
Prefix: prefix,
|
||||
AxeType: axeTyp,
|
||||
Prop: prop,
|
||||
Input: n,
|
||||
}
|
||||
}
|
||||
|
||||
// newVariableNode returns new variable node VariableNode.
|
||||
func newVariableNode(prefix, name string) Node {
|
||||
return &VariableNode{NodeType: NodeVariable, Name: name, Prefix: prefix}
|
||||
}
|
||||
|
||||
// newFilterNode returns a new filter node FilterNode.
|
||||
func newFilterNode(n, m Node) Node {
|
||||
return &FilterNode{NodeType: NodeFilter, Input: n, Condition: m}
|
||||
}
|
||||
|
||||
// newRootNode returns a root node.
|
||||
func newRootNode(s string) Node {
|
||||
return &RootNode{NodeType: NodeRoot, slash: s}
|
||||
}
|
||||
|
||||
// newFunctionNode returns function call node.
|
||||
func newFunctionNode(name, prefix string, args []Node) Node {
|
||||
return &FunctionNode{NodeType: NodeFunction, Prefix: prefix, FuncName: name, Args: args}
|
||||
}
|
||||
|
||||
// testOp reports whether current item name is an operand op.
|
||||
func testOp(r *scanner, op string) bool {
|
||||
return r.typ == itemName && r.prefix == "" && r.name == op
|
||||
}
|
||||
|
||||
func isPrimaryExpr(r *scanner) bool {
|
||||
switch r.typ {
|
||||
case itemString, itemNumber, itemDollar, itemLParens:
|
||||
return true
|
||||
case itemName:
|
||||
return r.canBeFunc && !isNodeType(r)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isNodeType(r *scanner) bool {
|
||||
switch r.name {
|
||||
case "node", "text", "processing-instruction", "comment":
|
||||
return r.prefix == ""
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func isStep(item itemType) bool {
|
||||
switch item {
|
||||
case itemDot, itemDotDot, itemAt, itemAxe, itemStar, itemName:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkItem(r *scanner, typ itemType) {
|
||||
if r.typ != typ {
|
||||
panic(fmt.Sprintf("%s has an invalid token", r.text))
|
||||
}
|
||||
}
|
||||
|
||||
// parseExpression parsing the expression with input Node n.
|
||||
func (p *parser) parseExpression(n Node) Node {
|
||||
if p.d = p.d + 1; p.d > 200 {
|
||||
panic("the xpath query is too complex(depth > 200)")
|
||||
}
|
||||
n = p.parseOrExpr(n)
|
||||
p.d--
|
||||
return n
|
||||
}
|
||||
|
||||
// next scanning next item on forward.
|
||||
func (p *parser) next() bool {
|
||||
return p.r.nextItem()
|
||||
}
|
||||
|
||||
func (p *parser) skipItem(typ itemType) {
|
||||
checkItem(p.r, typ)
|
||||
p.next()
|
||||
}
|
||||
|
||||
// OrExpr ::= AndExpr | OrExpr 'or' AndExpr
|
||||
func (p *parser) parseOrExpr(n Node) Node {
|
||||
opnd := p.parseAndExpr(n)
|
||||
for {
|
||||
if !testOp(p.r, "or") {
|
||||
break
|
||||
}
|
||||
p.next()
|
||||
opnd = newOperatorNode("or", opnd, p.parseAndExpr(n))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr
|
||||
func (p *parser) parseAndExpr(n Node) Node {
|
||||
opnd := p.parseEqualityExpr(n)
|
||||
for {
|
||||
if !testOp(p.r, "and") {
|
||||
break
|
||||
}
|
||||
p.next()
|
||||
opnd = newOperatorNode("and", opnd, p.parseEqualityExpr(n))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// EqualityExpr ::= RelationalExpr | EqualityExpr '=' RelationalExpr | EqualityExpr '!=' RelationalExpr
|
||||
func (p *parser) parseEqualityExpr(n Node) Node {
|
||||
opnd := p.parseRelationalExpr(n)
|
||||
Loop:
|
||||
for {
|
||||
var op string
|
||||
switch p.r.typ {
|
||||
case itemEq:
|
||||
op = "="
|
||||
case itemNe:
|
||||
op = "!="
|
||||
default:
|
||||
break Loop
|
||||
}
|
||||
p.next()
|
||||
opnd = newOperatorNode(op, opnd, p.parseRelationalExpr(n))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// RelationalExpr ::= AdditiveExpr | RelationalExpr '<' AdditiveExpr | RelationalExpr '>' AdditiveExpr
|
||||
// | RelationalExpr '<=' AdditiveExpr
|
||||
// | RelationalExpr '>=' AdditiveExpr
|
||||
func (p *parser) parseRelationalExpr(n Node) Node {
|
||||
opnd := p.parseAdditiveExpr(n)
|
||||
Loop:
|
||||
for {
|
||||
var op string
|
||||
switch p.r.typ {
|
||||
case itemLt:
|
||||
op = "<"
|
||||
case itemGt:
|
||||
op = ">"
|
||||
case itemLe:
|
||||
op = "<="
|
||||
case itemGe:
|
||||
op = ">="
|
||||
default:
|
||||
break Loop
|
||||
}
|
||||
p.next()
|
||||
opnd = newOperatorNode(op, opnd, p.parseAdditiveExpr(n))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// AdditiveExpr ::= MultiplicativeExpr | AdditiveExpr '+' MultiplicativeExpr | AdditiveExpr '-' MultiplicativeExpr
|
||||
func (p *parser) parseAdditiveExpr(n Node) Node {
|
||||
opnd := p.parseMultiplicativeExpr(n)
|
||||
Loop:
|
||||
for {
|
||||
var op string
|
||||
switch p.r.typ {
|
||||
case itemPlus:
|
||||
op = "+"
|
||||
case itemMinus:
|
||||
op = "-"
|
||||
default:
|
||||
break Loop
|
||||
}
|
||||
p.next()
|
||||
opnd = newOperatorNode(op, opnd, p.parseMultiplicativeExpr(n))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// MultiplicativeExpr ::= UnaryExpr | MultiplicativeExpr MultiplyOperator(*) UnaryExpr
|
||||
// | MultiplicativeExpr 'div' UnaryExpr | MultiplicativeExpr 'mod' UnaryExpr
|
||||
func (p *parser) parseMultiplicativeExpr(n Node) Node {
|
||||
opnd := p.parseUnaryExpr(n)
|
||||
Loop:
|
||||
for {
|
||||
var op string
|
||||
if p.r.typ == itemStar {
|
||||
op = "*"
|
||||
} else if testOp(p.r, "div") || testOp(p.r, "mod") {
|
||||
op = p.r.name
|
||||
} else {
|
||||
break Loop
|
||||
}
|
||||
p.next()
|
||||
opnd = newOperatorNode(op, opnd, p.parseUnaryExpr(n))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// UnaryExpr ::= UnionExpr | '-' UnaryExpr
|
||||
func (p *parser) parseUnaryExpr(n Node) Node {
|
||||
minus := false
|
||||
// ignore '-' sequence
|
||||
for p.r.typ == itemMinus {
|
||||
p.next()
|
||||
minus = !minus
|
||||
}
|
||||
opnd := p.parseUnionExpr(n)
|
||||
if minus {
|
||||
opnd = newOperatorNode("*", opnd, newOperandNode(float64(-1)))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// UnionExpr ::= PathExpr | UnionExpr '|' PathExpr
|
||||
func (p *parser) parseUnionExpr(n Node) Node {
|
||||
opnd := p.parsePathExpr(n)
|
||||
Loop:
|
||||
for {
|
||||
if p.r.typ != itemUnion {
|
||||
break Loop
|
||||
}
|
||||
p.next()
|
||||
opnd2 := p.parsePathExpr(n)
|
||||
// Checking the node type that must be is node set type?
|
||||
opnd = newOperatorNode("|", opnd, opnd2)
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// PathExpr ::= LocationPath | FilterExpr | FilterExpr '/' RelativeLocationPath | FilterExpr '//' RelativeLocationPath
|
||||
func (p *parser) parsePathExpr(n Node) Node {
|
||||
var opnd Node
|
||||
if isPrimaryExpr(p.r) {
|
||||
opnd = p.parseFilterExpr(n)
|
||||
switch p.r.typ {
|
||||
case itemSlash:
|
||||
p.next()
|
||||
opnd = p.parseRelativeLocationPath(opnd)
|
||||
case itemSlashSlash:
|
||||
p.next()
|
||||
opnd = p.parseRelativeLocationPath(newAxisNode("descendant-or-self", "", "", "", opnd))
|
||||
}
|
||||
} else {
|
||||
opnd = p.parseLocationPath(nil)
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// FilterExpr ::= PrimaryExpr | FilterExpr Predicate
|
||||
func (p *parser) parseFilterExpr(n Node) Node {
|
||||
opnd := p.parsePrimaryExpr(n)
|
||||
if p.r.typ == itemLBracket {
|
||||
opnd = newFilterNode(opnd, p.parsePredicate(opnd))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// Predicate ::= '[' PredicateExpr ']'
|
||||
func (p *parser) parsePredicate(n Node) Node {
|
||||
p.skipItem(itemLBracket)
|
||||
opnd := p.parseExpression(n)
|
||||
p.skipItem(itemRBracket)
|
||||
return opnd
|
||||
}
|
||||
|
||||
// LocationPath ::= RelativeLocationPath | AbsoluteLocationPath
|
||||
func (p *parser) parseLocationPath(n Node) (opnd Node) {
|
||||
switch p.r.typ {
|
||||
case itemSlash:
|
||||
p.next()
|
||||
opnd = newRootNode("/")
|
||||
if isStep(p.r.typ) {
|
||||
opnd = p.parseRelativeLocationPath(opnd) // ?? child:: or self ??
|
||||
}
|
||||
case itemSlashSlash:
|
||||
p.next()
|
||||
opnd = newRootNode("//")
|
||||
opnd = p.parseRelativeLocationPath(newAxisNode("descendant-or-self", "", "", "", opnd))
|
||||
default:
|
||||
opnd = p.parseRelativeLocationPath(n)
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | AbbreviatedRelativeLocationPath
|
||||
func (p *parser) parseRelativeLocationPath(n Node) Node {
|
||||
opnd := n
|
||||
Loop:
|
||||
for {
|
||||
opnd = p.parseStep(opnd)
|
||||
switch p.r.typ {
|
||||
case itemSlashSlash:
|
||||
p.next()
|
||||
opnd = newAxisNode("descendant-or-self", "", "", "", opnd)
|
||||
case itemSlash:
|
||||
p.next()
|
||||
default:
|
||||
break Loop
|
||||
}
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// Step ::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep
|
||||
func (p *parser) parseStep(n Node) Node {
|
||||
axeTyp := "child" // default axes value.
|
||||
if p.r.typ == itemDot || p.r.typ == itemDotDot {
|
||||
if p.r.typ == itemDot {
|
||||
axeTyp = "self"
|
||||
} else {
|
||||
axeTyp = "parent"
|
||||
}
|
||||
p.next()
|
||||
return newAxisNode(axeTyp, "", "", "", n)
|
||||
}
|
||||
switch p.r.typ {
|
||||
case itemAt:
|
||||
p.next()
|
||||
axeTyp = "attribute"
|
||||
case itemAxe:
|
||||
axeTyp = p.r.name
|
||||
p.next()
|
||||
}
|
||||
opnd := p.parseNodeTest(n, axeTyp)
|
||||
if p.r.typ == itemLBracket {
|
||||
opnd = newFilterNode(opnd, p.parsePredicate(opnd))
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// NodeTest ::= NameTest | NodeType '(' ')' | 'processing-instruction' '(' Literal ')'
|
||||
func (p *parser) parseNodeTest(n Node, axeTyp string) (opnd Node) {
|
||||
switch p.r.typ {
|
||||
case itemName:
|
||||
if p.r.canBeFunc && isNodeType(p.r) {
|
||||
var prop string
|
||||
switch p.r.name {
|
||||
case "comment", "text", "processing-instruction", "node":
|
||||
prop = p.r.name
|
||||
}
|
||||
var name string
|
||||
p.next()
|
||||
p.skipItem(itemLParens)
|
||||
if prop == "processing-instruction" && p.r.typ != itemRParens {
|
||||
checkItem(p.r, itemString)
|
||||
name = p.r.strval
|
||||
p.next()
|
||||
}
|
||||
p.skipItem(itemRParens)
|
||||
opnd = newAxisNode(axeTyp, name, "", prop, n)
|
||||
} else {
|
||||
prefix := p.r.prefix
|
||||
name := p.r.name
|
||||
p.next()
|
||||
if p.r.name == "*" {
|
||||
name = ""
|
||||
}
|
||||
opnd = newAxisNode(axeTyp, name, prefix, "", n)
|
||||
}
|
||||
case itemStar:
|
||||
opnd = newAxisNode(axeTyp, "", "", "", n)
|
||||
p.next()
|
||||
default:
|
||||
panic("expression must evaluate to a node-set")
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall
|
||||
func (p *parser) parsePrimaryExpr(n Node) (opnd Node) {
|
||||
switch p.r.typ {
|
||||
case itemString:
|
||||
opnd = newOperandNode(p.r.strval)
|
||||
p.next()
|
||||
case itemNumber:
|
||||
opnd = newOperandNode(p.r.numval)
|
||||
p.next()
|
||||
case itemDollar:
|
||||
p.next()
|
||||
checkItem(p.r, itemName)
|
||||
opnd = newVariableNode(p.r.prefix, p.r.name)
|
||||
p.next()
|
||||
case itemLParens:
|
||||
p.next()
|
||||
opnd = p.parseExpression(n)
|
||||
p.skipItem(itemRParens)
|
||||
case itemName:
|
||||
if p.r.canBeFunc && !isNodeType(p.r) {
|
||||
opnd = p.parseMethod(nil)
|
||||
}
|
||||
}
|
||||
return opnd
|
||||
}
|
||||
|
||||
// FunctionCall ::= FunctionName '(' ( Argument ( ',' Argument )* )? ')'
|
||||
func (p *parser) parseMethod(n Node) Node {
|
||||
var args []Node
|
||||
name := p.r.name
|
||||
prefix := p.r.prefix
|
||||
|
||||
p.skipItem(itemName)
|
||||
p.skipItem(itemLParens)
|
||||
if p.r.typ != itemRParens {
|
||||
for {
|
||||
args = append(args, p.parseExpression(n))
|
||||
if p.r.typ == itemRParens {
|
||||
break
|
||||
}
|
||||
p.skipItem(itemComma)
|
||||
}
|
||||
}
|
||||
p.skipItem(itemRParens)
|
||||
return newFunctionNode(name, prefix, args)
|
||||
}
|
||||
|
||||
// Parse parsing the XPath express string expr and returns a tree Node.
|
||||
func Parse(expr string) Node {
|
||||
r := &scanner{text: expr}
|
||||
r.nextChar()
|
||||
r.nextItem()
|
||||
p := &parser{r: r}
|
||||
return p.parseExpression(nil)
|
||||
}
|
||||
284
trunk/goutil/xmlUtil/gxpath/internal/parse/scan.go
Normal file
284
trunk/goutil/xmlUtil/gxpath/internal/parse/scan.go
Normal file
@@ -0,0 +1,284 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
type itemType int
|
||||
|
||||
const (
|
||||
itemComma itemType = iota // ','
|
||||
itemSlash // '/'
|
||||
itemAt // '@'
|
||||
itemDot // '.'
|
||||
itemLParens // '('
|
||||
itemRParens // ')'
|
||||
itemLBracket // '['
|
||||
itemRBracket // ']'
|
||||
itemStar // '*'
|
||||
itemPlus // '+'
|
||||
itemMinus // '-'
|
||||
itemEq // '='
|
||||
itemLt // '<'
|
||||
itemGt // '>'
|
||||
itemBang // '!'
|
||||
itemDollar // '$'
|
||||
itemApos // '\''
|
||||
itemQuote // '"'
|
||||
itemUnion // '|'
|
||||
itemNe // '!='
|
||||
itemLe // '<='
|
||||
itemGe // '>='
|
||||
itemAnd // '&&'
|
||||
itemOr // '||'
|
||||
itemDotDot // '..'
|
||||
itemSlashSlash // '//'
|
||||
itemName // XML Name
|
||||
itemString // Quoted string constant
|
||||
itemNumber // Number constant
|
||||
itemAxe // Axe (like child::)
|
||||
itemEof // END
|
||||
)
|
||||
|
||||
type scanner struct {
|
||||
text, name, prefix string
|
||||
|
||||
pos int
|
||||
curr rune
|
||||
typ itemType
|
||||
strval string // text value at current pos
|
||||
numval float64 // number value at current pos
|
||||
canBeFunc bool
|
||||
}
|
||||
|
||||
func (s *scanner) nextChar() bool {
|
||||
if s.pos >= len(s.text) {
|
||||
s.curr = rune(0)
|
||||
return false
|
||||
}
|
||||
s.curr = rune(s.text[s.pos])
|
||||
s.pos += 1
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *scanner) nextItem() bool {
|
||||
s.skipSpace()
|
||||
switch s.curr {
|
||||
case 0:
|
||||
s.typ = itemEof
|
||||
return false
|
||||
case ',', '@', '(', ')', '|', '*', '[', ']', '+', '-', '=', '#', '$':
|
||||
s.typ = asItemType(s.curr)
|
||||
s.nextChar()
|
||||
case '<':
|
||||
s.typ = itemLt
|
||||
s.nextChar()
|
||||
if s.curr == '=' {
|
||||
s.typ = itemLe
|
||||
s.nextChar()
|
||||
}
|
||||
case '>':
|
||||
s.typ = itemGt
|
||||
s.nextChar()
|
||||
if s.curr == '=' {
|
||||
s.typ = itemGe
|
||||
s.nextChar()
|
||||
}
|
||||
case '!':
|
||||
s.typ = itemBang
|
||||
s.nextChar()
|
||||
if s.curr == '=' {
|
||||
s.typ = itemNe
|
||||
s.nextChar()
|
||||
}
|
||||
case '.':
|
||||
s.typ = itemDot
|
||||
s.nextChar()
|
||||
if s.curr == '.' {
|
||||
s.typ = itemDotDot
|
||||
s.nextChar()
|
||||
} else if isDigit(s.curr) {
|
||||
s.typ = itemNumber
|
||||
s.numval = s.scanFraction()
|
||||
}
|
||||
case '/':
|
||||
s.typ = itemSlash
|
||||
s.nextChar()
|
||||
if s.curr == '/' {
|
||||
s.typ = itemSlashSlash
|
||||
s.nextChar()
|
||||
}
|
||||
case '"', '\'':
|
||||
s.typ = itemString
|
||||
s.strval = s.scanString()
|
||||
default:
|
||||
if isDigit(s.curr) {
|
||||
s.typ = itemNumber
|
||||
s.numval = s.scanNumber()
|
||||
} else if isName(s.curr) {
|
||||
s.typ = itemName
|
||||
s.name = s.scanName()
|
||||
s.prefix = ""
|
||||
// "foo:bar" is one itemem not three because it doesn't allow spaces in between
|
||||
// We should distinct it from "foo::" and need process "foo ::" as well
|
||||
if s.curr == ':' {
|
||||
s.nextChar()
|
||||
// can be "foo:bar" or "foo::"
|
||||
if s.curr == ':' {
|
||||
// "foo::"
|
||||
s.nextChar()
|
||||
s.typ = itemAxe
|
||||
} else { // "foo:*", "foo:bar" or "foo: "
|
||||
s.prefix = s.name
|
||||
if s.curr == '*' {
|
||||
s.nextChar()
|
||||
s.name = "*"
|
||||
} else if isName(s.curr) {
|
||||
s.name = s.scanName()
|
||||
} else {
|
||||
panic(fmt.Sprintf("%s has an invalid qualified name.", s.text))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
s.skipSpace()
|
||||
if s.curr == ':' {
|
||||
s.nextChar()
|
||||
// it can be "foo ::" or just "foo :"
|
||||
if s.curr == ':' {
|
||||
s.nextChar()
|
||||
s.typ = itemAxe
|
||||
} else {
|
||||
panic(fmt.Sprintf("%s has an invalid qualified name.", s.text))
|
||||
}
|
||||
}
|
||||
}
|
||||
s.skipSpace()
|
||||
s.canBeFunc = s.curr == '('
|
||||
} else {
|
||||
panic(fmt.Sprintf("%s has an invalid token.", s.text))
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *scanner) skipSpace() {
|
||||
Loop:
|
||||
for {
|
||||
if !unicode.IsSpace(s.curr) || !s.nextChar() {
|
||||
break Loop
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *scanner) scanFraction() float64 {
|
||||
var (
|
||||
i = s.pos - 2
|
||||
c = 1 // '.'
|
||||
)
|
||||
for isDigit(s.curr) {
|
||||
s.nextChar()
|
||||
c++
|
||||
}
|
||||
v, err := strconv.ParseFloat(s.text[i:i+c], 64)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("xpath: scanFraction parse float got error: %v", err))
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (s *scanner) scanNumber() float64 {
|
||||
var (
|
||||
c int
|
||||
i = s.pos - 1
|
||||
)
|
||||
for isDigit(s.curr) {
|
||||
s.nextChar()
|
||||
c++
|
||||
}
|
||||
if s.curr == '.' {
|
||||
s.nextChar()
|
||||
c++
|
||||
for isDigit(s.curr) {
|
||||
s.nextChar()
|
||||
c++
|
||||
}
|
||||
}
|
||||
v, err := strconv.ParseFloat(s.text[i:i+c], 64)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("xpath: scanNumber parse float got error: %v", err))
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
func (s *scanner) scanString() string {
|
||||
var (
|
||||
c = 0
|
||||
end = s.curr
|
||||
)
|
||||
s.nextChar()
|
||||
i := s.pos - 1
|
||||
for s.curr != end {
|
||||
if !s.nextChar() {
|
||||
panic(errors.New("xpath: scanString got unclosed string"))
|
||||
}
|
||||
c++
|
||||
}
|
||||
s.nextChar()
|
||||
return s.text[i : i+c]
|
||||
}
|
||||
|
||||
func (s *scanner) scanName() string {
|
||||
var (
|
||||
c int
|
||||
i = s.pos - 1
|
||||
)
|
||||
for isName(s.curr) {
|
||||
c++
|
||||
if !s.nextChar() {
|
||||
break
|
||||
}
|
||||
}
|
||||
return s.text[i : i+c]
|
||||
}
|
||||
|
||||
func isName(r rune) bool {
|
||||
return string(r) != ":" && string(r) != "/" &&
|
||||
(unicode.Is(first, r) || unicode.Is(second, r) || string(r) == "*")
|
||||
}
|
||||
|
||||
func isDigit(r rune) bool {
|
||||
return unicode.IsDigit(r)
|
||||
}
|
||||
|
||||
func asItemType(r rune) itemType {
|
||||
switch r {
|
||||
case ',':
|
||||
return itemComma
|
||||
case '@':
|
||||
return itemAt
|
||||
case '(':
|
||||
return itemLParens
|
||||
case ')':
|
||||
return itemRParens
|
||||
case '|':
|
||||
return itemUnion
|
||||
case '*':
|
||||
return itemStar
|
||||
case '[':
|
||||
return itemLBracket
|
||||
case ']':
|
||||
return itemRBracket
|
||||
case '+':
|
||||
return itemPlus
|
||||
case '-':
|
||||
return itemMinus
|
||||
case '=':
|
||||
return itemEq
|
||||
case '$':
|
||||
return itemDollar
|
||||
}
|
||||
panic(fmt.Errorf("unknown item: %v", r))
|
||||
}
|
||||
7
trunk/goutil/xmlUtil/gxpath/internal/query/iter.go
Normal file
7
trunk/goutil/xmlUtil/gxpath/internal/query/iter.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package query
|
||||
|
||||
import "goutil/xmlUtil/gxpath/xpath"
|
||||
|
||||
type Iterator interface {
|
||||
Current() xpath.NodeNavigator
|
||||
}
|
||||
658
trunk/goutil/xmlUtil/gxpath/internal/query/query.go
Normal file
658
trunk/goutil/xmlUtil/gxpath/internal/query/query.go
Normal file
@@ -0,0 +1,658 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"goutil/xmlUtil/gxpath/xpath"
|
||||
)
|
||||
|
||||
// An XPath query interface.
|
||||
type Query interface {
|
||||
// Select traversing Iterator returns a query matched node xpath.NodeNavigator.
|
||||
Select(Iterator) xpath.NodeNavigator
|
||||
|
||||
// Evaluate evaluates query and returns values of the current query.
|
||||
Evaluate(Iterator) interface{}
|
||||
|
||||
// Test checks a specified xpath.NodeNavigator can passed by the current query.
|
||||
//Test(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
// ContextQuery is returns current node on the Iterator object query.
|
||||
type ContextQuery struct {
|
||||
count int
|
||||
Root bool // Moving to root-level node in the current context Iterator.
|
||||
}
|
||||
|
||||
func (c *ContextQuery) Select(t Iterator) (n xpath.NodeNavigator) {
|
||||
if c.count == 0 {
|
||||
c.count++
|
||||
n = t.Current().Copy()
|
||||
if c.Root {
|
||||
n.MoveToRoot()
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (c *ContextQuery) Evaluate(Iterator) interface{} {
|
||||
c.count = 0
|
||||
return c
|
||||
}
|
||||
|
||||
// AncestorQuery is an XPath ancestor node query.(ancestor::*|ancestor-self::*)
|
||||
type AncestorQuery struct {
|
||||
iterator func() xpath.NodeNavigator
|
||||
|
||||
Self bool
|
||||
Input Query
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (a *AncestorQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
if a.iterator == nil {
|
||||
node := a.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
first := true
|
||||
a.iterator = func() xpath.NodeNavigator {
|
||||
if first && a.Self {
|
||||
first = false
|
||||
if a.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
for node.MoveToParent() {
|
||||
if !a.Predicate(node) {
|
||||
break
|
||||
}
|
||||
return node
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if node := a.iterator(); node != nil {
|
||||
return node
|
||||
}
|
||||
a.iterator = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AncestorQuery) Evaluate(t Iterator) interface{} {
|
||||
a.Input.Evaluate(t)
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AncestorQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return a.Predicate(n)
|
||||
}
|
||||
|
||||
// AttributeQuery is an XPath attribute node query.(@*)
|
||||
type AttributeQuery struct {
|
||||
iterator func() xpath.NodeNavigator
|
||||
|
||||
Input Query
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (a *AttributeQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
if a.iterator == nil {
|
||||
node := a.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
node = node.Copy()
|
||||
a.iterator = func() xpath.NodeNavigator {
|
||||
for {
|
||||
onAttr := node.MoveToNextAttribute()
|
||||
if !onAttr {
|
||||
return nil
|
||||
}
|
||||
if a.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if node := a.iterator(); node != nil {
|
||||
return node
|
||||
}
|
||||
a.iterator = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (a *AttributeQuery) Evaluate(t Iterator) interface{} {
|
||||
a.Input.Evaluate(t)
|
||||
a.iterator = nil
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *AttributeQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return a.Predicate(n)
|
||||
}
|
||||
|
||||
// ChildQuery is an XPath child node query.(child::*)
|
||||
type ChildQuery struct {
|
||||
posit int
|
||||
iterator func() xpath.NodeNavigator
|
||||
|
||||
Input Query
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (c *ChildQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
if c.iterator == nil {
|
||||
c.posit = 0
|
||||
node := c.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
node = node.Copy()
|
||||
first := true
|
||||
c.iterator = func() xpath.NodeNavigator {
|
||||
for {
|
||||
if (first && !node.MoveToChild()) || (!first && !node.MoveToNext()) {
|
||||
return nil
|
||||
}
|
||||
first = false
|
||||
if c.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if node := c.iterator(); node != nil {
|
||||
c.posit++
|
||||
return node
|
||||
}
|
||||
c.iterator = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *ChildQuery) Evaluate(t Iterator) interface{} {
|
||||
c.Input.Evaluate(t)
|
||||
c.iterator = nil
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *ChildQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return c.Predicate(n)
|
||||
}
|
||||
|
||||
// position returns a position of current xpath.NodeNavigator.
|
||||
func (c *ChildQuery) position() int {
|
||||
return c.posit
|
||||
}
|
||||
|
||||
// DescendantQuery is an XPath descendant node query.(descendant::* | descendant-or-self::*)
|
||||
type DescendantQuery struct {
|
||||
iterator func() xpath.NodeNavigator
|
||||
|
||||
Self bool
|
||||
Input Query
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (d *DescendantQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
if d.iterator == nil {
|
||||
node := d.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
node = node.Copy()
|
||||
level := 0
|
||||
first := true
|
||||
d.iterator = func() xpath.NodeNavigator {
|
||||
if first && d.Self {
|
||||
first = false
|
||||
if d.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
|
||||
for {
|
||||
if node.MoveToChild() {
|
||||
level++
|
||||
} else {
|
||||
for {
|
||||
if level == 0 {
|
||||
return nil
|
||||
}
|
||||
if node.MoveToNext() {
|
||||
break
|
||||
}
|
||||
node.MoveToParent()
|
||||
level--
|
||||
}
|
||||
}
|
||||
if d.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if node := d.iterator(); node != nil {
|
||||
return node
|
||||
}
|
||||
d.iterator = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DescendantQuery) Evaluate(t Iterator) interface{} {
|
||||
d.Input.Evaluate(t)
|
||||
d.iterator = nil
|
||||
return d
|
||||
}
|
||||
|
||||
func (d *DescendantQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return d.Predicate(n)
|
||||
}
|
||||
|
||||
// FollowingQuery is an XPath following node query.(following::*|following-sibling::*)
|
||||
type FollowingQuery struct {
|
||||
iterator func() xpath.NodeNavigator
|
||||
|
||||
Input Query
|
||||
Sibling bool // The matching sibling node of current node.
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (f *FollowingQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
if f.iterator == nil {
|
||||
node := f.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
node = node.Copy()
|
||||
if f.Sibling {
|
||||
f.iterator = func() xpath.NodeNavigator {
|
||||
for {
|
||||
if !node.MoveToNext() {
|
||||
return nil
|
||||
}
|
||||
if f.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var q Query // descendant query
|
||||
f.iterator = func() xpath.NodeNavigator {
|
||||
for {
|
||||
if q == nil {
|
||||
for !node.MoveToNext() {
|
||||
if !node.MoveToParent() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
q = &DescendantQuery{
|
||||
Self: true,
|
||||
Input: &ContextQuery{},
|
||||
Predicate: f.Predicate,
|
||||
}
|
||||
t.Current().MoveTo(node)
|
||||
}
|
||||
if node := q.Select(t); node != nil {
|
||||
return node
|
||||
}
|
||||
q = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if node := f.iterator(); node != nil {
|
||||
return node
|
||||
}
|
||||
f.iterator = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FollowingQuery) Evaluate(t Iterator) interface{} {
|
||||
f.Input.Evaluate(t)
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *FollowingQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return f.Predicate(n)
|
||||
}
|
||||
|
||||
// PrecedingQuery is an XPath preceding node query.(preceding::*)
|
||||
type PrecedingQuery struct {
|
||||
iterator func() xpath.NodeNavigator
|
||||
Input Query
|
||||
Sibling bool // The matching sibling node of current node.
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (p *PrecedingQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
if p.iterator == nil {
|
||||
node := p.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
node = node.Copy()
|
||||
if p.Sibling {
|
||||
p.iterator = func() xpath.NodeNavigator {
|
||||
for {
|
||||
for !node.MoveToPrevious() {
|
||||
return nil
|
||||
}
|
||||
if p.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var q Query
|
||||
p.iterator = func() xpath.NodeNavigator {
|
||||
for {
|
||||
if q == nil {
|
||||
for !node.MoveToPrevious() {
|
||||
if !node.MoveToParent() {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
q = &DescendantQuery{
|
||||
Self: true,
|
||||
Input: &ContextQuery{},
|
||||
Predicate: p.Predicate,
|
||||
}
|
||||
t.Current().MoveTo(node)
|
||||
}
|
||||
if node := q.Select(t); node != nil {
|
||||
return node
|
||||
}
|
||||
q = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if node := p.iterator(); node != nil {
|
||||
return node
|
||||
}
|
||||
p.iterator = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (p *PrecedingQuery) Evaluate(t Iterator) interface{} {
|
||||
p.Input.Evaluate(t)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *PrecedingQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return p.Predicate(n)
|
||||
}
|
||||
|
||||
// ParentQuery is an XPath parent node query.(parent::*)
|
||||
type ParentQuery struct {
|
||||
Input Query
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (p *ParentQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
node := p.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
node = node.Copy()
|
||||
if node.MoveToParent() && p.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ParentQuery) Evaluate(t Iterator) interface{} {
|
||||
p.Input.Evaluate(t)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *ParentQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return p.Predicate(n)
|
||||
}
|
||||
|
||||
// SelfQuery is an Self node query.(self::*)
|
||||
type SelfQuery struct {
|
||||
Input Query
|
||||
Predicate func(xpath.NodeNavigator) bool
|
||||
}
|
||||
|
||||
func (s *SelfQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
node := s.Input.Select(t)
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if s.Predicate(node) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SelfQuery) Evaluate(t Iterator) interface{} {
|
||||
s.Input.Evaluate(t)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SelfQuery) Test(n xpath.NodeNavigator) bool {
|
||||
return s.Predicate(n)
|
||||
}
|
||||
|
||||
// FilterQuery is an XPath query for predicate filter.
|
||||
type FilterQuery struct {
|
||||
Input Query
|
||||
Predicate Query
|
||||
}
|
||||
|
||||
func (f *FilterQuery) do(t Iterator) bool {
|
||||
val := reflect.ValueOf(f.Predicate.Evaluate(t))
|
||||
switch val.Kind() {
|
||||
case reflect.Bool:
|
||||
return val.Bool()
|
||||
case reflect.String:
|
||||
return len(val.String()) > 0
|
||||
case reflect.Float64:
|
||||
pt := float64(getNodePosition(f.Input))
|
||||
return int(val.Float()) == int(pt)
|
||||
default:
|
||||
if q, ok := f.Predicate.(Query); ok {
|
||||
return q.Select(t) != nil
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (f *FilterQuery) Select(t Iterator) xpath.NodeNavigator {
|
||||
for {
|
||||
node := f.Input.Select(t)
|
||||
if node == nil {
|
||||
return node
|
||||
}
|
||||
node = node.Copy()
|
||||
//fmt.Println(node.LocalName())
|
||||
|
||||
t.Current().MoveTo(node)
|
||||
if f.do(t) {
|
||||
return node
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (f *FilterQuery) Evaluate(t Iterator) interface{} {
|
||||
f.Input.Evaluate(t)
|
||||
return f
|
||||
}
|
||||
|
||||
// FunctionQuery is an XPath function that call a function to returns
|
||||
// value of current xpath.NodeNavigator node.
|
||||
type XPathFunction struct {
|
||||
Input Query // Node Set
|
||||
Func func(Query, Iterator) interface{} // The xpath function.
|
||||
}
|
||||
|
||||
func (f *XPathFunction) Select(t Iterator) xpath.NodeNavigator {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Evaluate call a specified function that will returns the
|
||||
// following value type: number,string,boolean.
|
||||
func (f *XPathFunction) Evaluate(t Iterator) interface{} {
|
||||
return f.Func(f.Input, t)
|
||||
}
|
||||
|
||||
// XPathConstant is an XPath constant operand.
|
||||
type XPathConstant struct {
|
||||
Val interface{}
|
||||
}
|
||||
|
||||
func (c *XPathConstant) Select(t Iterator) xpath.NodeNavigator {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *XPathConstant) Evaluate(t Iterator) interface{} {
|
||||
return c.Val
|
||||
}
|
||||
|
||||
// LogicalExpr is an XPath logical expression.
|
||||
type LogicalExpr struct {
|
||||
Left, Right Query
|
||||
|
||||
Do func(Iterator, interface{}, interface{}) interface{}
|
||||
}
|
||||
|
||||
func (l *LogicalExpr) Select(t Iterator) xpath.NodeNavigator {
|
||||
// When a XPath expr is logical expression.
|
||||
node := t.Current().Copy()
|
||||
val := l.Evaluate(t)
|
||||
switch val.(type) {
|
||||
case bool:
|
||||
if val.(bool) == true {
|
||||
return node
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *LogicalExpr) Evaluate(t Iterator) interface{} {
|
||||
m := l.Left.Evaluate(t)
|
||||
n := l.Right.Evaluate(t)
|
||||
return l.Do(t, m, n)
|
||||
}
|
||||
|
||||
// NumericExpr is an XPath numeric operator expression.
|
||||
type NumericExpr struct {
|
||||
Left, Right Query
|
||||
|
||||
Do func(interface{}, interface{}) interface{}
|
||||
}
|
||||
|
||||
func (n *NumericExpr) Select(t Iterator) xpath.NodeNavigator {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NumericExpr) Evaluate(t Iterator) interface{} {
|
||||
m := n.Left.Evaluate(t)
|
||||
k := n.Right.Evaluate(t)
|
||||
return n.Do(m, k)
|
||||
}
|
||||
|
||||
type BooleanExpr struct {
|
||||
IsOr bool
|
||||
Left, Right Query
|
||||
iterator func() xpath.NodeNavigator
|
||||
}
|
||||
|
||||
func (b *BooleanExpr) Select(t Iterator) xpath.NodeNavigator {
|
||||
if b.iterator == nil {
|
||||
var list []xpath.NodeNavigator
|
||||
i := 0
|
||||
root := t.Current().Copy()
|
||||
if b.IsOr {
|
||||
for {
|
||||
node := b.Left.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
node = node.Copy()
|
||||
list = append(list, node)
|
||||
}
|
||||
t.Current().MoveTo(root)
|
||||
for {
|
||||
node := b.Right.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
node = node.Copy()
|
||||
list = append(list, node)
|
||||
}
|
||||
} else {
|
||||
var m []xpath.NodeNavigator
|
||||
var n []xpath.NodeNavigator
|
||||
for {
|
||||
node := b.Left.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
node = node.Copy()
|
||||
list = append(m, node)
|
||||
}
|
||||
t.Current().MoveTo(root)
|
||||
for {
|
||||
node := b.Right.Select(t)
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
node = node.Copy()
|
||||
list = append(n, node)
|
||||
}
|
||||
for _, k := range m {
|
||||
for _, j := range n {
|
||||
if k == j {
|
||||
list = append(list, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b.iterator = func() xpath.NodeNavigator {
|
||||
if i >= len(list) {
|
||||
return nil
|
||||
}
|
||||
node := list[i]
|
||||
i++
|
||||
return node
|
||||
}
|
||||
}
|
||||
return b.iterator()
|
||||
}
|
||||
|
||||
func (b *BooleanExpr) Evaluate(t Iterator) interface{} {
|
||||
m := b.Left.Evaluate(t)
|
||||
if m.(bool) == b.IsOr {
|
||||
return m
|
||||
}
|
||||
return b.Right.Evaluate(t)
|
||||
}
|
||||
|
||||
func getNodePosition(q Query) int {
|
||||
type Position interface {
|
||||
position() int
|
||||
}
|
||||
if count, ok := q.(Position); ok {
|
||||
return count.position()
|
||||
}
|
||||
return 1
|
||||
}
|
||||
Reference in New Issue
Block a user