初始化项目
This commit is contained in:
12
trunk/goutil/xmlUtil/doc.go
Normal file
12
trunk/goutil/xmlUtil/doc.go
Normal file
@@ -0,0 +1,12 @@
|
||||
/*
|
||||
xml操作工具类:
|
||||
此操作工具类来源于:https://github.com/antchfx/xquery
|
||||
根据实际情况。我去掉了对golang.org/x/net/html/charset的依赖,并添加了各种xml加载函数
|
||||
|
||||
由于go默认编码的原因,如果文本编码不是utf-8 将会出现乱码
|
||||
|
||||
使用方式
|
||||
root := xmlUtil.LoadFromString(xml)
|
||||
nodes:= root.SelectElements(xpath)
|
||||
*/
|
||||
package xmlUtil
|
||||
110
trunk/goutil/xmlUtil/gxpath/README.md
Normal file
110
trunk/goutil/xmlUtil/gxpath/README.md
Normal file
@@ -0,0 +1,110 @@
|
||||
gxpath
|
||||
====
|
||||
gxpath is XPath packages for the Go, that lets you extract data from the custom documents using XPath expression.
|
||||
|
||||
**[XQuery](https://github.com/antchfx/xquery)** : gxpath implemented, lets you extract data from HTML/XML documents using XPath.
|
||||
|
||||
### Features
|
||||
|
||||
#### The basic XPath patterns.
|
||||
|
||||
> The basic XPath patterns cover 90% of the cases that most stylesheets will need.
|
||||
|
||||
- `node` : Selects all child elements with nodeName of node.
|
||||
|
||||
- `*` : Selects all child elements.
|
||||
|
||||
- `@attr` : Selects the attribute attr.
|
||||
|
||||
- `@*` : Selects all attributes.
|
||||
|
||||
- `node()` : Matches an org.w3c.dom.Node.
|
||||
|
||||
- `text()` : Matches a org.w3c.dom.Text node.
|
||||
|
||||
- `comment()` : Matches a comment.
|
||||
|
||||
- `.` : Selects the current node.
|
||||
|
||||
- `..` : Selects the parent of current node.
|
||||
|
||||
- `/` : Selects the document node.
|
||||
|
||||
- `a[expr]` : Select only those nodes matching a which also satisfy the expression expr.
|
||||
|
||||
- `a[n]` : Selects the nth matching node matching a When a filter's expression is a number, XPath selects based on position.
|
||||
|
||||
- `a/b` : For each node matching a, add the nodes matching b to the result.
|
||||
|
||||
- `a//b` : For each node matching a, add the descendant nodes matching b to the result.
|
||||
|
||||
- `//b` : Returns elements in the entire document matching b.
|
||||
|
||||
- `a|b` : All nodes matching a or b.
|
||||
|
||||
#### Node Axes
|
||||
|
||||
- `child::*` : The child axis selects children of the current node.
|
||||
|
||||
- `descendant::*` : The descendant axis selects descendants of the current node. It is equivalent to '//'.
|
||||
|
||||
- `descendant-or-self::*` : Selects descendants including the current node.
|
||||
|
||||
- `attribute::*` : Selects attributes of the current element. It is equivalent to @*
|
||||
|
||||
- `following-sibling::*` : Selects nodes after the current node.
|
||||
|
||||
- `preceding-sibling::*` : Selects nodes before the current node.
|
||||
|
||||
- `following::*` : Selects the first matching node following in document order, excluding descendants.
|
||||
|
||||
- `preceding::*` : Selects the first matching node preceding in document order, excluding ancestors.
|
||||
|
||||
- `parent::*` : Selects the parent if it matches. The '..' pattern from the core is equivalent to 'parent::node()'.
|
||||
|
||||
- `ancestor::*` : Selects matching ancestors.
|
||||
|
||||
- `ancestor-or-self::*` : Selects ancestors including the current node.
|
||||
|
||||
- `self::*` : Selects the current node. '.' is equivalent to 'self::node()'.
|
||||
|
||||
#### Expressions
|
||||
|
||||
The gxpath supported three types: number, boolean, string.
|
||||
|
||||
- `path` : Selects nodes based on the path.
|
||||
|
||||
- `a = b` : Standard comparisons.
|
||||
|
||||
* a = b True if a equals b.
|
||||
* a != b True if a is not equal to b.
|
||||
* a < b True if a is less than b.
|
||||
* a <= b True if a is less than or equal to b.
|
||||
* a > b True if a is greater than b.
|
||||
* a >= b True if a is greater than or equal to b.
|
||||
|
||||
- `a + b` : Arithmetic expressions.
|
||||
|
||||
* `- a` Unary minus
|
||||
* a + b Add
|
||||
* a - b Substract
|
||||
* a * b Multiply
|
||||
* a div b Divide
|
||||
* a mod b Floating point mod, like Java.
|
||||
|
||||
- `(expr)` : Parenthesized expressions.
|
||||
|
||||
- `fun(arg1, ..., argn)` : Function calls.
|
||||
|
||||
* position()
|
||||
* last()
|
||||
* count(node-set)
|
||||
* name()
|
||||
* starts-with(string,string)
|
||||
* normalize-space(string)
|
||||
* substring(string,start[,length])
|
||||
* come more
|
||||
|
||||
- `a or b` : Boolean or.
|
||||
|
||||
- `a and b` : Boolean and.
|
||||
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
|
||||
}
|
||||
40
trunk/goutil/xmlUtil/gxpath/select.go
Normal file
40
trunk/goutil/xmlUtil/gxpath/select.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package gxpath
|
||||
|
||||
import (
|
||||
"goutil/xmlUtil/gxpath/internal/build"
|
||||
"goutil/xmlUtil/gxpath/internal/query"
|
||||
"goutil/xmlUtil/gxpath/xpath"
|
||||
)
|
||||
|
||||
// NodeIterator holds all matched Node object.
|
||||
type NodeIterator struct {
|
||||
node xpath.NodeNavigator
|
||||
query query.Query
|
||||
}
|
||||
|
||||
// Current returns current node which matched.
|
||||
func (t *NodeIterator) Current() xpath.NodeNavigator {
|
||||
return t.node
|
||||
}
|
||||
|
||||
// MoveNext moves Navigator to the next match node.
|
||||
func (t *NodeIterator) MoveNext() bool {
|
||||
n := t.query.Select(t)
|
||||
if n != nil {
|
||||
if !t.node.MoveTo(n) {
|
||||
t.node = n.Copy()
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Select selects a node set using the specified XPath expression.
|
||||
func Select(root xpath.NodeNavigator, expr string) *NodeIterator {
|
||||
qy, err := build.Build(expr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
t := &NodeIterator{query: qy, node: root}
|
||||
return t
|
||||
}
|
||||
496
trunk/goutil/xmlUtil/gxpath/select_test.go
Normal file
496
trunk/goutil/xmlUtil/gxpath/select_test.go
Normal file
@@ -0,0 +1,496 @@
|
||||
package gxpath
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"goutil/xmlUtil/gxpath/xpath"
|
||||
)
|
||||
|
||||
var html *TNode = example()
|
||||
|
||||
/*
|
||||
testXPath2(t, html, "//title/node()", 1) still have some error,will fix in future.
|
||||
testXPath(t, html, "//*[count(*)=3]", "ul")
|
||||
*/
|
||||
|
||||
func TestSelf(t *testing.T) {
|
||||
testXPath(t, html, ".", "html")
|
||||
testXPath(t, html.FirstChild, ".", "head")
|
||||
testXPath(t, html, "self::*", "html")
|
||||
testXPath(t, html.LastChild, "self::body", "body")
|
||||
testXPath2(t, html, "//body/./ul/li/a", 3)
|
||||
}
|
||||
|
||||
func TestParent(t *testing.T) {
|
||||
testXPath(t, html.LastChild, "..", "html")
|
||||
testXPath(t, html.LastChild, "parent::*", "html")
|
||||
a := selectNode(html, "//li/a")
|
||||
testXPath(t, a, "parent::*", "li")
|
||||
testXPath(t, html, "//title/parent::head", "head")
|
||||
}
|
||||
|
||||
func TestAttribute(t *testing.T) {
|
||||
testXPath(t, html, "@lang='en'", "html")
|
||||
testXPath2(t, html, "@lang='zh'", 0)
|
||||
testXPath2(t, html, "//@href", 3)
|
||||
testXPath2(t, html, "//a[@*]", 3)
|
||||
}
|
||||
|
||||
func TestRelativePath(t *testing.T) {
|
||||
testXPath(t, html, "head", "head")
|
||||
testXPath(t, html, "/head", "head")
|
||||
testXPath(t, html, "body//li", "li")
|
||||
testXPath(t, html, "/head/title", "title")
|
||||
|
||||
testXPath2(t, html, "/body/ul/li/a", 3)
|
||||
testXPath(t, html, "//title", "title")
|
||||
testXPath(t, html, "//title/..", "head")
|
||||
testXPath(t, html, "//title/../..", "html")
|
||||
testXPath2(t, html, "//a[@href]", 3)
|
||||
testXPath(t, html, "//ul/../footer", "footer")
|
||||
}
|
||||
|
||||
func TestChild(t *testing.T) {
|
||||
testXPath(t, html, "/child::head", "head")
|
||||
testXPath(t, html, "/child::head/child::title", "title")
|
||||
testXPath(t, html, "//title/../child::title", "title")
|
||||
testXPath(t, html.Parent, "//child::*", "html")
|
||||
}
|
||||
|
||||
func TestDescendant(t *testing.T) {
|
||||
testXPath2(t, html, "descendant::*", 15)
|
||||
testXPath2(t, html, "/head/descendant::*", 2)
|
||||
testXPath2(t, html, "//ul/descendant::*", 7) // <li> + <a>
|
||||
testXPath2(t, html, "//ul/descendant::li", 4) // <li>
|
||||
}
|
||||
|
||||
func TestAncestor(t *testing.T) {
|
||||
testXPath2(t, html, "/body/footer/ancestor::*", 2) // body>html
|
||||
testXPath2(t, html, "/body/ul/li/a/ancestor::li", 3)
|
||||
}
|
||||
|
||||
func TestFollowingSibling(t *testing.T) {
|
||||
var list []*TNode
|
||||
list = selectNodes(html, "//li/following-sibling::*")
|
||||
for _, n := range list {
|
||||
if n.Data != "li" {
|
||||
t.Fatalf("expected node is li,but got:%s", n.Data)
|
||||
}
|
||||
}
|
||||
|
||||
list = selectNodes(html, "//ul/following-sibling::*") // p,footer
|
||||
for _, n := range list {
|
||||
if n.Data != "p" && n.Data != "footer" {
|
||||
t.Fatal("expected node is not one of the following nodes: [p,footer]")
|
||||
}
|
||||
}
|
||||
testXPath(t, html, "//ul/following-sibling::footer", "footer")
|
||||
list = selectNodes(html, "//h1/following::*") // ul>li>a,p,footer
|
||||
if list[0].Data != "ul" {
|
||||
t.Fatal("expected node is not ul")
|
||||
}
|
||||
if list[1].Data != "li" {
|
||||
t.Fatal("expected node is not li")
|
||||
}
|
||||
if list[len(list)-1].Data != "footer" {
|
||||
t.Fatal("expected node is not footer")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrecedingSibling(t *testing.T) {
|
||||
testXPath(t, html, "/body/footer/preceding-sibling::*", "p")
|
||||
testXPath2(t, html, "/body/footer/preceding-sibling::*", 3) // p,ul,h1
|
||||
list := selectNodes(html, "//h1/preceding::*") // head>title>meta
|
||||
if list[0].Data != "head" {
|
||||
t.Fatal("expected is not head")
|
||||
}
|
||||
if list[1].Data != "title" {
|
||||
t.Fatal("expected is not title")
|
||||
}
|
||||
if list[2].Data != "meta" {
|
||||
t.Fatal("expected is not meta")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStarWide(t *testing.T) {
|
||||
testXPath(t, html, "/head/*", "title")
|
||||
testXPath2(t, html, "//ul/*", 4)
|
||||
testXPath(t, html, "@*", "html")
|
||||
testXPath2(t, html, "/body/h1/*", 0)
|
||||
testXPath2(t, html, `//ul/*/a`, 3)
|
||||
}
|
||||
|
||||
func TestNodeTestType(t *testing.T) {
|
||||
testXPath(t, html, "//title/text()", "Hello")
|
||||
testXPath(t, html, "//a[@href='/']/text()", "Home")
|
||||
testXPath2(t, html, "//head/node()", 2)
|
||||
testXPath2(t, html, "//ul/node()", 4)
|
||||
}
|
||||
|
||||
func TestPosition(t *testing.T) {
|
||||
testXPath3(t, html, "/head[1]", html.FirstChild) // compare to 'head' element
|
||||
ul := selectNode(html, "//ul")
|
||||
testXPath3(t, html, "/head[last()]", html.FirstChild)
|
||||
testXPath3(t, html, "//li[1]", ul.FirstChild)
|
||||
testXPath3(t, html, "//li[4]", ul.LastChild)
|
||||
testXPath3(t, html, "//li[last()]", ul.LastChild)
|
||||
}
|
||||
|
||||
func TestPredicate(t *testing.T) {
|
||||
testXPath(t, html.Parent, "html[@lang='en']", "html")
|
||||
testXPath(t, html, "//a[@href='/']", "a")
|
||||
testXPath(t, html, "//meta[@name]", "meta")
|
||||
ul := selectNode(html, "//ul")
|
||||
testXPath3(t, html, "//li[position()=4]", ul.LastChild)
|
||||
testXPath3(t, html, "//li[position()=1]", ul.FirstChild)
|
||||
testXPath2(t, html, "//li[position()>0]", 4)
|
||||
testXPath3(t, html, "//a[text()='Home']", selectNode(html, "//a[1]"))
|
||||
}
|
||||
|
||||
func TestOr_And(t *testing.T) {
|
||||
list := selectNodes(html, "//h1|//footer")
|
||||
if len(list) == 0 {
|
||||
t.Fatal("//h1|//footer no any node found")
|
||||
}
|
||||
if list[0].Data != "h1" {
|
||||
t.Fatalf("expected first node of node-set is h1,but got %s", list[0].Data)
|
||||
}
|
||||
if list[1].Data != "footer" {
|
||||
t.Fatalf("expected first node of node-set is footer,but got %s", list[1].Data)
|
||||
}
|
||||
|
||||
list = selectNodes(html, "//a[@id=1 or @id=2]")
|
||||
if list[0] != selectNode(html, "//a[@id=1]") {
|
||||
t.Fatal("node is not equal")
|
||||
}
|
||||
if list[1] != selectNode(html, "//a[@id=2]") {
|
||||
t.Fatal("node is not equal")
|
||||
}
|
||||
testXPath3(t, html, "//a[@id=1 and @href='/']", selectNode(html, "//a[1]"))
|
||||
testXPath3(t, html, "//a[text()='Home' and @id='1']", selectNode(html, "//a[1]"))
|
||||
}
|
||||
|
||||
func TestFunction(t *testing.T) {
|
||||
testXPath2(t, html, "//*[name()='a']", 3)
|
||||
testXPath(t, html, "//*[starts-with(name(),'h1')]", "h1")
|
||||
testXPath2(t, html, "//*[starts-with(@href,'/a')]", 2) // a links: `/account`,`/about`
|
||||
testXPath3(t, html, "//h1[normalize-space(text())='This is a H1']", selectNode(html, "//h1"))
|
||||
testXPath3(t, html, "//title[substring(.,0)='Hello']", selectNode(html, "//title"))
|
||||
testXPath3(t, html, "//title[substring(text(),0,4)='Hell']", selectNode(html, "//title"))
|
||||
}
|
||||
|
||||
func TestOperationOrLogical(t *testing.T) {
|
||||
testXPath3(t, html, "//li[1+1]", selectNode(html, "//li[2]"))
|
||||
testXPath3(t, html, "//li[5 div 2]", selectNode(html, "//li[2]"))
|
||||
testXPath3(t, html, "//li[3 mod 2]", selectNode(html, "//li[1]"))
|
||||
testXPath3(t, html, "//li[3 - 2]", selectNode(html, "//li[1]"))
|
||||
testXPath2(t, html, "//li[position() mod 2 = 0 ]", 2) // //li[2],li[4]
|
||||
testXPath2(t, html, "//a[@id>=1]", 3) // //a[@id>=1] == a[1],a[2],a[3]
|
||||
testXPath2(t, html, "//a[@id<2]", 1) // //a[@id>=1] == a[1]
|
||||
testXPath2(t, html, "//a[@id!=2]", 2) // //a[@id>=1] == a[1],a[3]
|
||||
testXPath2(t, html, "//a[@id=1 or @id=3]", 2) // //a[@id>=1] == a[1],a[3]
|
||||
testXPath3(t, html, "//a[@id=1 and @href='/']", selectNode(html, "//a[1]"))
|
||||
}
|
||||
|
||||
func testXPath(t *testing.T, root *TNode, expr string, expected string) {
|
||||
node := selectNode(root, expr)
|
||||
if node == nil {
|
||||
t.Fatalf("`%s` returns node is nil", expr)
|
||||
}
|
||||
if node.Data != expected {
|
||||
t.Fatalf("`%s` expected node is %s,but got %s", expr, expected, node.Data)
|
||||
}
|
||||
}
|
||||
|
||||
func testXPath2(t *testing.T, root *TNode, expr string, expected int) {
|
||||
list := selectNodes(root, expr)
|
||||
if len(list) != expected {
|
||||
t.Fatalf("`%s` expected node numbers is %d,but got %d", expr, expected, len(list))
|
||||
}
|
||||
}
|
||||
|
||||
func testXPath3(t *testing.T, root *TNode, expr string, expected *TNode) {
|
||||
node := selectNode(root, expr)
|
||||
if node == nil {
|
||||
t.Fatalf("`%s` returns node is nil", expr)
|
||||
}
|
||||
if node != expected {
|
||||
t.Fatalf("`%s` %s != %s", expr, node.Value(), expected.Value())
|
||||
}
|
||||
}
|
||||
|
||||
func selectNode(root *TNode, expr string) (n *TNode) {
|
||||
t := Select(createNavigator(root), expr)
|
||||
if t.MoveNext() {
|
||||
n = (t.Current().(*TNodeNavigator)).curr
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func selectNodes(root *TNode, expr string) []*TNode {
|
||||
t := Select(createNavigator(root), expr)
|
||||
var nodes []*TNode
|
||||
for t.MoveNext() {
|
||||
node := (t.Current().(*TNodeNavigator)).curr
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
return nodes
|
||||
}
|
||||
|
||||
func createNavigator(n *TNode) *TNodeNavigator {
|
||||
return &TNodeNavigator{curr: n, root: n, attr: -1}
|
||||
}
|
||||
|
||||
type Attribute struct {
|
||||
Key, Value string
|
||||
}
|
||||
|
||||
type TNode struct {
|
||||
Parent, FirstChild, LastChild, PrevSibling, NextSibling *TNode
|
||||
|
||||
Type xpath.NodeType
|
||||
Data string
|
||||
Attr []Attribute
|
||||
}
|
||||
|
||||
func (n *TNode) Value() string {
|
||||
if n.Type == xpath.TextNode {
|
||||
return n.Data
|
||||
}
|
||||
|
||||
var buff bytes.Buffer
|
||||
var output func(*TNode)
|
||||
output = func(node *TNode) {
|
||||
if node.Type == xpath.TextNode {
|
||||
buff.WriteString(node.Data)
|
||||
}
|
||||
for child := node.FirstChild; child != nil; child = child.NextSibling {
|
||||
output(child)
|
||||
}
|
||||
}
|
||||
output(n)
|
||||
return buff.String()
|
||||
}
|
||||
|
||||
// TNodeNavigator is for navigating TNode.
|
||||
type TNodeNavigator struct {
|
||||
curr, root *TNode
|
||||
attr int
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) NodeType() xpath.NodeType {
|
||||
if n.curr.Type == xpath.ElementNode && n.attr != -1 {
|
||||
return xpath.AttributeNode
|
||||
}
|
||||
return n.curr.Type
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) LocalName() string {
|
||||
if n.attr != -1 {
|
||||
return n.curr.Attr[n.attr].Key
|
||||
}
|
||||
return n.curr.Data
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) Prefix() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) Value() string {
|
||||
switch n.curr.Type {
|
||||
case xpath.CommentNode:
|
||||
return n.curr.Data
|
||||
case xpath.ElementNode:
|
||||
if n.attr != -1 {
|
||||
return n.curr.Attr[n.attr].Value
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
node := n.curr.FirstChild
|
||||
for node != nil {
|
||||
if node.Type == xpath.TextNode {
|
||||
buf.WriteString(strings.TrimSpace(node.Data))
|
||||
}
|
||||
node = node.NextSibling
|
||||
}
|
||||
return buf.String()
|
||||
case xpath.TextNode:
|
||||
return n.curr.Data
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) Copy() xpath.NodeNavigator {
|
||||
n2 := *n
|
||||
return &n2
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToRoot() {
|
||||
n.curr = n.root
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToParent() bool {
|
||||
if node := n.curr.Parent; node != nil {
|
||||
n.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToNextAttribute() bool {
|
||||
if n.attr >= len(n.curr.Attr)-1 {
|
||||
return false
|
||||
}
|
||||
n.attr++
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToChild() bool {
|
||||
if node := n.curr.FirstChild; node != nil {
|
||||
n.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToFirst() bool {
|
||||
if n.curr.PrevSibling == nil {
|
||||
return false
|
||||
}
|
||||
for {
|
||||
node := n.curr.PrevSibling
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
n.curr = node
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) String() string {
|
||||
return n.Value()
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToNext() bool {
|
||||
if node := n.curr.NextSibling; node != nil {
|
||||
n.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveToPrevious() bool {
|
||||
if node := n.curr.PrevSibling; node != nil {
|
||||
n.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (n *TNodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
|
||||
node, ok := other.(*TNodeNavigator)
|
||||
if !ok || node.root != n.root {
|
||||
return false
|
||||
}
|
||||
|
||||
n.curr = node.curr
|
||||
n.attr = node.attr
|
||||
return true
|
||||
}
|
||||
|
||||
func createNode(data string, typ xpath.NodeType) *TNode {
|
||||
return &TNode{Data: data, Type: typ, Attr: make([]Attribute, 0)}
|
||||
}
|
||||
|
||||
func (t *TNode) createChildNode(data string, typ xpath.NodeType) *TNode {
|
||||
n := createNode(data, typ)
|
||||
n.Parent = t
|
||||
if t.FirstChild == nil {
|
||||
t.FirstChild = n
|
||||
} else {
|
||||
t.LastChild.NextSibling = n
|
||||
n.PrevSibling = t.LastChild
|
||||
}
|
||||
t.LastChild = n
|
||||
return n
|
||||
}
|
||||
|
||||
func (t *TNode) appendNode(data string, typ xpath.NodeType) *TNode {
|
||||
n := createNode(data, typ)
|
||||
n.Parent = t.Parent
|
||||
t.NextSibling = n
|
||||
n.PrevSibling = t
|
||||
if t.Parent != nil {
|
||||
t.Parent.LastChild = n
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (t *TNode) addAttribute(k, v string) {
|
||||
t.Attr = append(t.Attr, Attribute{k, v})
|
||||
}
|
||||
|
||||
func example() *TNode {
|
||||
/*
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Hello</title>
|
||||
<meta name="language" content="en"/>
|
||||
</head>
|
||||
<body>
|
||||
<h1> This is a H1 </h1>
|
||||
<ul>
|
||||
<li><a id="1" href="/">Home</a></li>
|
||||
<li><a id="2" href="/about">about</a></li>
|
||||
<li><a id="3" href="/account">login</a></li>
|
||||
<li></li>
|
||||
</ul>
|
||||
<p>
|
||||
Hello,This is an example for gxpath.
|
||||
</p>
|
||||
<footer>footer script</footer>
|
||||
</body>
|
||||
</html>
|
||||
*/
|
||||
doc := createNode("", xpath.RootNode)
|
||||
xhtml := doc.createChildNode("html", xpath.ElementNode)
|
||||
xhtml.addAttribute("lang", "en")
|
||||
|
||||
// The HTML head section.
|
||||
head := xhtml.createChildNode("head", xpath.ElementNode)
|
||||
n := head.createChildNode("title", xpath.ElementNode)
|
||||
n = n.createChildNode("Hello", xpath.TextNode)
|
||||
n = head.createChildNode("meta", xpath.ElementNode)
|
||||
n.addAttribute("name", "language")
|
||||
n.addAttribute("content", "en")
|
||||
// The HTML body section.
|
||||
body := xhtml.createChildNode("body", xpath.ElementNode)
|
||||
n = body.createChildNode("h1", xpath.ElementNode)
|
||||
n = n.createChildNode(" This is a H1 ", xpath.TextNode)
|
||||
ul := body.createChildNode("ul", xpath.ElementNode)
|
||||
n = ul.createChildNode("li", xpath.ElementNode)
|
||||
n = n.createChildNode("a", xpath.ElementNode)
|
||||
n.addAttribute("id", "1")
|
||||
n.addAttribute("href", "/")
|
||||
n = n.createChildNode("Home", xpath.TextNode)
|
||||
n = ul.createChildNode("li", xpath.ElementNode)
|
||||
n = n.createChildNode("a", xpath.ElementNode)
|
||||
n.addAttribute("id", "2")
|
||||
n.addAttribute("href", "/about")
|
||||
n = n.createChildNode("about", xpath.TextNode)
|
||||
n = ul.createChildNode("li", xpath.ElementNode)
|
||||
n = n.createChildNode("a", xpath.ElementNode)
|
||||
n.addAttribute("id", "3")
|
||||
n.addAttribute("href", "/account")
|
||||
n = n.createChildNode("login", xpath.TextNode)
|
||||
n = ul.createChildNode("li", xpath.ElementNode)
|
||||
|
||||
n = body.createChildNode("p", xpath.ElementNode)
|
||||
n = n.createChildNode("Hello,This is an example for gxpath.", xpath.TextNode)
|
||||
|
||||
n = body.createChildNode("footer", xpath.ElementNode)
|
||||
n = n.createChildNode("footer script", xpath.TextNode)
|
||||
|
||||
return xhtml
|
||||
}
|
||||
25
trunk/goutil/xmlUtil/gxpath/xpath/node.go
Normal file
25
trunk/goutil/xmlUtil/gxpath/xpath/node.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package xpath
|
||||
|
||||
// A Node is an element node that can navigating to
|
||||
// an node attribute and another node.
|
||||
//type Node interface{}
|
||||
|
||||
// A type of XPath node.
|
||||
type NodeType int
|
||||
|
||||
const (
|
||||
// A root node of the XML document or node tree.
|
||||
RootNode NodeType = iota
|
||||
|
||||
// An element, such as <element>.
|
||||
ElementNode
|
||||
|
||||
// An attribute, such as id='123'.
|
||||
AttributeNode
|
||||
|
||||
// The text content of a node.
|
||||
TextNode
|
||||
|
||||
// A comment node, such as <!-- my comment -->
|
||||
CommentNode
|
||||
)
|
||||
43
trunk/goutil/xmlUtil/gxpath/xpath/xpath.go
Normal file
43
trunk/goutil/xmlUtil/gxpath/xpath/xpath.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package xpath
|
||||
|
||||
// NodeNavigator provides cursor model for navigating XML data.
|
||||
type NodeNavigator interface {
|
||||
// NodeType returns the XPathNodeType of the current node.
|
||||
NodeType() NodeType
|
||||
|
||||
// LocalName gets the Name of the current node.
|
||||
LocalName() string
|
||||
|
||||
// Prefix returns namespace prefix associated with the current node.
|
||||
Prefix() string
|
||||
|
||||
// Value gets the value of current node.
|
||||
Value() string
|
||||
|
||||
// Copy does a deep copy of the NodeNavigator and all its components.
|
||||
Copy() NodeNavigator
|
||||
|
||||
// MoveToRoot moves the NodeNavigator to the root node of the current node.
|
||||
MoveToRoot()
|
||||
|
||||
// MoveToParent moves the NodeNavigator to the parent node of the current node.
|
||||
MoveToParent() bool
|
||||
|
||||
// MoveToNextAttribute moves the NodeNavigator to the next attribute on current node.
|
||||
MoveToNextAttribute() bool
|
||||
|
||||
// MoveToChild moves the NodeNavigator to the first child node of the current node.
|
||||
MoveToChild() bool
|
||||
|
||||
// MoveToFirst moves the NodeNavigator to the first sibling node of the current node.
|
||||
MoveToFirst() bool
|
||||
|
||||
// MoveToNext moves the NodeNavigator to the next sibling node of the current node.
|
||||
MoveToNext() bool
|
||||
|
||||
// MoveToPrevious moves the NodeNavigator to the previous sibling node of the current node.
|
||||
MoveToPrevious() bool
|
||||
|
||||
// MoveTo moves the NodeNavigator to the same position as the specified NodeNavigator.
|
||||
MoveTo(NodeNavigator) bool
|
||||
}
|
||||
46
trunk/goutil/xmlUtil/load.go
Normal file
46
trunk/goutil/xmlUtil/load.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package xmlUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// 从文件加载
|
||||
// filePath:文件路径
|
||||
// 返回值:
|
||||
// *Node:根节点对象
|
||||
// error:错误信息
|
||||
func LoadFromFile(filePath string) (*Node, error) {
|
||||
data, errMsg := ioutil.ReadFile(filePath)
|
||||
if errMsg != nil {
|
||||
return nil, errMsg
|
||||
}
|
||||
|
||||
return LoadFromByte(data)
|
||||
}
|
||||
|
||||
// 从字节数组加载
|
||||
// data:文档数据
|
||||
// 返回值:
|
||||
// *Node:根节点对象
|
||||
// error:错误信息
|
||||
func LoadFromByte(data []byte) (*Node, error) {
|
||||
return LoadFromString(string(data))
|
||||
}
|
||||
|
||||
// 文档字符串
|
||||
// doc:文档字符串
|
||||
// 返回值:
|
||||
// *Node:根节点对象
|
||||
// error:错误信息
|
||||
func LoadFromString(doc string) (*Node, error) {
|
||||
// xml.Decoder doesn't properly handle whitespace in some doc
|
||||
// see songTextString.xml test case ...
|
||||
reg, _ := regexp.Compile("[ \t\n\r]*<")
|
||||
doc = reg.ReplaceAllString(doc, "<")
|
||||
|
||||
b := bytes.NewBufferString(doc)
|
||||
|
||||
return LoadFromReader(b)
|
||||
}
|
||||
62
trunk/goutil/xmlUtil/load_test.go
Normal file
62
trunk/goutil/xmlUtil/load_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package xmlUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// 测试从文件加载
|
||||
func TestLoadFromFile(t *testing.T) {
|
||||
root, errMsg := LoadFromFile("sample.xml")
|
||||
if errMsg != nil {
|
||||
t.Error("文件加载失败:", errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
node := root.SelectElement("html/head/title")
|
||||
if node == nil {
|
||||
t.Error("读取节点失败:", "html/head/title")
|
||||
}
|
||||
|
||||
fmt.Println("节点值:", strings.TrimSpace(node.InnerText()))
|
||||
}
|
||||
|
||||
// 测试从字符串加载
|
||||
func TestLoadFromString(t *testing.T) {
|
||||
var xml string = `
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Hello</title>
|
||||
<meta name="language" content="en"/>
|
||||
</head>
|
||||
<body>
|
||||
<h1> This is a H1 </h1>
|
||||
<ul>
|
||||
<li><a id="1" href="/">Home</a></li>
|
||||
<li><a id="2" href="/about">about</a></li>
|
||||
<li><a id="3" href="/account">login</a></li>
|
||||
<li></li>
|
||||
</ul>
|
||||
<p>
|
||||
Hello,This is an example for gxpath.
|
||||
</p>
|
||||
<footer>footer script</footer>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
root, errMsg := LoadFromString(xml)
|
||||
if errMsg != nil {
|
||||
t.Error("文件加载失败:", errMsg)
|
||||
t.Fail()
|
||||
return
|
||||
}
|
||||
|
||||
node := root.SelectElement("html/head/title")
|
||||
if node == nil {
|
||||
t.Error("读取节点失败:", "html/head/title")
|
||||
}
|
||||
|
||||
fmt.Println("节点值:", strings.TrimSpace(node.InnerText()))
|
||||
}
|
||||
345
trunk/goutil/xmlUtil/node.go
Normal file
345
trunk/goutil/xmlUtil/node.go
Normal file
@@ -0,0 +1,345 @@
|
||||
package xmlUtil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"container/list"
|
||||
"encoding/xml"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// A NodeType is the type of a Node.
|
||||
type NodeType uint
|
||||
|
||||
const (
|
||||
// 文档对象节点(根节点)
|
||||
DocumentNode NodeType = iota
|
||||
|
||||
// 头不声明节点
|
||||
DeclarationNode
|
||||
|
||||
// 元素节点
|
||||
ElementNode
|
||||
|
||||
// 节点文本
|
||||
TextNode
|
||||
|
||||
// 注释
|
||||
CommentNode
|
||||
)
|
||||
|
||||
// xml节点对象
|
||||
type Node struct {
|
||||
Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
|
||||
|
||||
Type NodeType
|
||||
NodeName string
|
||||
Namespace string
|
||||
Attr []xml.Attr
|
||||
|
||||
level int // node level in the tree
|
||||
}
|
||||
|
||||
// InnerText returns the text between the start and end tags of the object.
|
||||
func (n *Node) InnerText() string {
|
||||
if n.Type == TextNode || n.Type == CommentNode {
|
||||
return n.NodeName
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
for child := n.FirstChild; child != nil; child = child.NextSibling {
|
||||
// filt commentnode
|
||||
if child.Type == CommentNode {
|
||||
continue
|
||||
}
|
||||
|
||||
buf.WriteString(child.InnerText())
|
||||
}
|
||||
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func outputXML(buf *bytes.Buffer, n *Node) {
|
||||
if n.Type == TextNode || n.Type == CommentNode {
|
||||
buf.WriteString(strings.TrimSpace(n.NodeName))
|
||||
return
|
||||
}
|
||||
buf.WriteString("<" + n.NodeName)
|
||||
for _, attr := range n.Attr {
|
||||
if attr.Name.Space != "" {
|
||||
buf.WriteString(fmt.Sprintf(` %s:%s="%s"`, attr.Name.Space, attr.Name.Local, attr.Value))
|
||||
} else {
|
||||
buf.WriteString(fmt.Sprintf(` %s="%s"`, attr.Name.Local, attr.Value))
|
||||
}
|
||||
}
|
||||
buf.WriteString(">")
|
||||
for child := n.FirstChild; child != nil; child = child.NextSibling {
|
||||
outputXML(buf, child)
|
||||
}
|
||||
buf.WriteString(fmt.Sprintf("</%s>", n.NodeName))
|
||||
}
|
||||
|
||||
// OutputXML returns the text that including tags name.
|
||||
func (n *Node) OutputXML() string {
|
||||
var buf bytes.Buffer
|
||||
outputXML(&buf, n)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
// get all children
|
||||
func (n *Node) Children() []*Node {
|
||||
childrenList := make([]*Node, 0)
|
||||
nowChild := n.FirstChild
|
||||
for {
|
||||
if nowChild == nil {
|
||||
break
|
||||
}
|
||||
|
||||
childrenList = append(childrenList, nowChild)
|
||||
if nowChild == n.LastChild {
|
||||
break
|
||||
}
|
||||
|
||||
nowChild = nowChild.NextSibling
|
||||
}
|
||||
|
||||
return childrenList
|
||||
}
|
||||
|
||||
// get children len
|
||||
func (n *Node) ChildrenLen() int {
|
||||
var count int = 0
|
||||
nowChild := n.FirstChild
|
||||
for {
|
||||
if nowChild == nil {
|
||||
break
|
||||
}
|
||||
|
||||
count += 1
|
||||
if nowChild == n.LastChild {
|
||||
break
|
||||
}
|
||||
|
||||
nowChild = nowChild.NextSibling
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
// get all attribute
|
||||
func (n *Node) ALLAttribute() []xml.Attr {
|
||||
if n.Attr == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return n.Attr[:]
|
||||
}
|
||||
|
||||
// 获取属性个数
|
||||
func (n *Node) AttributeLen() int {
|
||||
if n.Attr == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return len(n.Attr)
|
||||
}
|
||||
|
||||
// 输出所有(主要用于测试)
|
||||
func (this *Node) OutALL() {
|
||||
stack := list.New()
|
||||
tmpItem := this
|
||||
for {
|
||||
if tmpItem != nil {
|
||||
stack.PushBack(tmpItem)
|
||||
tmpItem = tmpItem.NextSibling
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
|
||||
for {
|
||||
if stack.Len() <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
nowNode := stack.Front().Value.(*Node)
|
||||
stack.Remove(stack.Front())
|
||||
for _, item := range nowNode.Children() {
|
||||
stack.PushFront(item)
|
||||
}
|
||||
|
||||
fmt.Println("name:", nowNode.NodeName, " level: ", nowNode.level, " attr:", nowNode.Attr)
|
||||
}
|
||||
}
|
||||
|
||||
// SelectElements finds child elements with the specified name.
|
||||
func (n *Node) SelectElements(name string) []*Node {
|
||||
return Find(n, name)
|
||||
}
|
||||
|
||||
// SelectElements finds child elements with the specified name.
|
||||
func (n *Node) SelectElement(name string) *Node {
|
||||
return FindOne(n, name)
|
||||
}
|
||||
|
||||
// SelectAttr returns the attribute value with the specified name.
|
||||
func (n *Node) SelectAttr(name string) (string, bool) {
|
||||
var local, space string
|
||||
local = name
|
||||
if i := strings.Index(name, ":"); i > 0 {
|
||||
space = name[:i]
|
||||
local = name[i+1:]
|
||||
}
|
||||
for _, attr := range n.Attr {
|
||||
if attr.Name.Local == local && attr.Name.Space == space {
|
||||
return attr.Value, true
|
||||
}
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
// 给节点添加属性值
|
||||
func addAttr(n *Node, key, val string) {
|
||||
var attr xml.Attr
|
||||
if i := strings.Index(key, ":"); i > 0 {
|
||||
attr = xml.Attr{
|
||||
Name: xml.Name{Space: key[:i], Local: key[i+1:]},
|
||||
Value: val,
|
||||
}
|
||||
} else {
|
||||
attr = xml.Attr{
|
||||
Name: xml.Name{Local: key},
|
||||
Value: val,
|
||||
}
|
||||
}
|
||||
|
||||
n.Attr = append(n.Attr, attr)
|
||||
}
|
||||
|
||||
// 给节点添加子节点
|
||||
func addChild(parent, n *Node) {
|
||||
n.Parent = parent
|
||||
if parent.FirstChild == nil {
|
||||
parent.FirstChild = n
|
||||
} else {
|
||||
parent.LastChild.NextSibling = n
|
||||
n.PrevSibling = parent.LastChild
|
||||
}
|
||||
|
||||
parent.LastChild = n
|
||||
}
|
||||
|
||||
// 给节点添加兄弟节点
|
||||
func addSibling(sibling, n *Node) {
|
||||
n.Parent = sibling.Parent
|
||||
sibling.NextSibling = n
|
||||
n.PrevSibling = sibling
|
||||
if sibling.Parent != nil {
|
||||
sibling.Parent.LastChild = n
|
||||
}
|
||||
}
|
||||
|
||||
// 从reader里面加载xml文档
|
||||
func LoadFromReader(r io.Reader) (*Node, error) {
|
||||
var (
|
||||
decoder = xml.NewDecoder(r) //// xml解码对象
|
||||
doc = &Node{Type: DocumentNode}
|
||||
level = 0
|
||||
declared = false
|
||||
)
|
||||
var prev *Node = doc
|
||||
for {
|
||||
tok, err := decoder.Token()
|
||||
switch {
|
||||
case err == io.EOF:
|
||||
goto quit
|
||||
case err != nil:
|
||||
return nil, err
|
||||
}
|
||||
|
||||
switch tok := tok.(type) {
|
||||
case xml.StartElement:
|
||||
//if !declared { // if have no xml node,we also need add children
|
||||
// return nil, errors.New("xml: document is invalid")
|
||||
//}
|
||||
// if there is no xml node.then create it
|
||||
if declared == false {
|
||||
level++
|
||||
|
||||
tmpNode := &Node{Type: DeclarationNode, level: level}
|
||||
addAttr(tmpNode, "version", "1.0")
|
||||
addAttr(tmpNode, "encoding", "UTF-8")
|
||||
declared = true
|
||||
if level == prev.level {
|
||||
addSibling(prev, tmpNode)
|
||||
} else if level > prev.level {
|
||||
addChild(prev, tmpNode)
|
||||
}
|
||||
|
||||
prev = tmpNode
|
||||
}
|
||||
node := &Node{
|
||||
Type: ElementNode,
|
||||
NodeName: tok.Name.Local,
|
||||
Namespace: tok.Name.Space,
|
||||
Attr: tok.Attr,
|
||||
level: level,
|
||||
}
|
||||
//fmt.Println(fmt.Sprintf("start > %s : %d", node.Data, level))
|
||||
if level == prev.level {
|
||||
addSibling(prev, node)
|
||||
} else if level > prev.level {
|
||||
addChild(prev, node)
|
||||
} else if level < prev.level {
|
||||
for i := prev.level - level; i > 1; i-- {
|
||||
prev = prev.Parent
|
||||
}
|
||||
addSibling(prev.Parent, node)
|
||||
}
|
||||
prev = node
|
||||
level++
|
||||
case xml.EndElement:
|
||||
level--
|
||||
case xml.CharData:
|
||||
node := &Node{Type: TextNode, NodeName: string(tok), level: level}
|
||||
if level == prev.level {
|
||||
addSibling(prev, node)
|
||||
} else if level > prev.level {
|
||||
addChild(prev, node)
|
||||
}
|
||||
case xml.Comment:
|
||||
node := &Node{Type: CommentNode, NodeName: string(tok), level: level}
|
||||
if level == prev.level {
|
||||
addSibling(prev, node)
|
||||
} else if level > prev.level {
|
||||
addChild(prev, node)
|
||||
}
|
||||
case xml.ProcInst: // Processing Instruction
|
||||
if declared || (!declared && tok.Target != "xml") {
|
||||
return nil, errors.New("xml: document is invalid")
|
||||
}
|
||||
level++
|
||||
node := &Node{Type: DeclarationNode, level: level}
|
||||
pairs := strings.Split(string(tok.Inst), " ")
|
||||
for _, pair := range pairs {
|
||||
pair = strings.TrimSpace(pair)
|
||||
if i := strings.Index(pair, "="); i > 0 {
|
||||
addAttr(node, pair[:i], strings.Trim(pair[i+1:], `"`))
|
||||
}
|
||||
}
|
||||
declared = true
|
||||
if level == prev.level {
|
||||
addSibling(prev, node)
|
||||
} else if level > prev.level {
|
||||
addChild(prev, node)
|
||||
}
|
||||
prev = node
|
||||
case xml.Directive:
|
||||
}
|
||||
|
||||
}
|
||||
quit:
|
||||
return doc, nil
|
||||
}
|
||||
186
trunk/goutil/xmlUtil/node_test.go
Normal file
186
trunk/goutil/xmlUtil/node_test.go
Normal file
@@ -0,0 +1,186 @@
|
||||
package xmlUtil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func findNode(root *Node, name string) *Node {
|
||||
node := root.FirstChild
|
||||
for {
|
||||
if node == nil || node.NodeName == name {
|
||||
break
|
||||
}
|
||||
node = node.NextSibling
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func childNodes(root *Node, name string) []*Node {
|
||||
var list []*Node
|
||||
node := root.FirstChild
|
||||
for {
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
if node.NodeName == name {
|
||||
list = append(list, node)
|
||||
}
|
||||
node = node.NextSibling
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func testNode(t *testing.T, n *Node, expected string) {
|
||||
if n.NodeName != expected {
|
||||
t.Fatalf("expected node name is %s,but got %s", expected, n.NodeName)
|
||||
}
|
||||
}
|
||||
|
||||
func testAttr(t *testing.T, n *Node, name, expected string) {
|
||||
for _, attr := range n.Attr {
|
||||
if attr.Name.Local == name && attr.Value == expected {
|
||||
return
|
||||
}
|
||||
}
|
||||
t.Fatalf("not found attribute %s in the node %s", name, n.NodeName)
|
||||
}
|
||||
|
||||
func testValue(t *testing.T, val, expected string) {
|
||||
if val != expected {
|
||||
t.Fatalf("expected value is %s,but got %s", expected, val)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
s := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<bookstore>
|
||||
<book>
|
||||
<title lang="en">Harry Potter</title>
|
||||
<price>29.99</price>
|
||||
</book>
|
||||
<book>
|
||||
<title lang="en">Learning XML</title>
|
||||
<price>39.95</price>
|
||||
</book>
|
||||
</bookstore>`
|
||||
root, err := LoadFromReader(strings.NewReader(s))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if root.Type != DocumentNode {
|
||||
t.Fatal("top node of tree is not DocumentNode")
|
||||
}
|
||||
|
||||
declarNode := root.FirstChild
|
||||
if declarNode.Type != DeclarationNode {
|
||||
t.Fatal("first child node of tree is not DeclarationNode")
|
||||
}
|
||||
|
||||
if declarNode.Attr[0].Name.Local != "version" && declarNode.Attr[0].Value != "1.0" {
|
||||
t.Fatal("version attribute not expected")
|
||||
}
|
||||
|
||||
bookstore := root.LastChild
|
||||
if bookstore.NodeName != "bookstore" {
|
||||
t.Fatal("bookstore elem not found")
|
||||
}
|
||||
if bookstore.FirstChild.NodeName != "\n" {
|
||||
t.Fatal("first child node of bookstore is not empty node(\n)")
|
||||
}
|
||||
books := childNodes(bookstore, "book")
|
||||
if len(books) != 2 {
|
||||
t.Fatalf("expected book element count is 2, but got %d", len(books))
|
||||
}
|
||||
// first book element
|
||||
testNode(t, findNode(books[0], "title"), "title")
|
||||
testAttr(t, findNode(books[0], "title"), "lang", "en")
|
||||
testValue(t, findNode(books[0], "price").InnerText(), "29.99")
|
||||
testValue(t, findNode(books[0], "title").InnerText(), "Harry Potter")
|
||||
|
||||
// second book element
|
||||
testNode(t, findNode(books[1], "title"), "title")
|
||||
testAttr(t, findNode(books[1], "title"), "lang", "en")
|
||||
testValue(t, findNode(books[1], "price").InnerText(), "39.95")
|
||||
|
||||
testValue(t, books[0].OutputXML(), `<book><title lang="en">Harry Potter</title><price>29.99</price></book>`)
|
||||
}
|
||||
|
||||
func TestTooNested(t *testing.T) {
|
||||
s := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<AAA>
|
||||
<BBB>
|
||||
<DDD>
|
||||
<CCC>
|
||||
<DDD/>
|
||||
<EEE/>
|
||||
</CCC>
|
||||
</DDD>
|
||||
</BBB>
|
||||
<CCC>
|
||||
<DDD>
|
||||
<EEE>
|
||||
<DDD>
|
||||
<FFF/>
|
||||
</DDD>
|
||||
</EEE>
|
||||
</DDD>
|
||||
</CCC>
|
||||
</AAA>`
|
||||
root, err := LoadFromReader(strings.NewReader(s))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
aaa := findNode(root, "AAA")
|
||||
if aaa == nil {
|
||||
t.Fatal("AAA node not exists")
|
||||
}
|
||||
ccc := aaa.LastChild
|
||||
if ccc.NodeName != "CCC" {
|
||||
t.Fatalf("expected node is CCC,but got %s", ccc.NodeName)
|
||||
}
|
||||
bbb := ccc.PrevSibling
|
||||
if bbb.NodeName != "BBB" {
|
||||
t.Fatalf("expected node is bbb,but got %s", bbb.NodeName)
|
||||
}
|
||||
ddd := findNode(bbb, "DDD")
|
||||
testNode(t, ddd, "DDD")
|
||||
testNode(t, ddd.LastChild, "CCC")
|
||||
}
|
||||
|
||||
func TestSelectElement(t *testing.T) {
|
||||
s := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<AAA>
|
||||
<BBB id="1"/>
|
||||
<CCC id="2">
|
||||
<DDD/>
|
||||
</CCC>
|
||||
<CCC id="3">
|
||||
<DDD/>
|
||||
</CCC>
|
||||
</AAA>`
|
||||
root, err := LoadFromReader(strings.NewReader(s))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
version, _ := root.FirstChild.SelectAttr("version")
|
||||
if version != "1.0" {
|
||||
t.Fatal("version!=1.0")
|
||||
}
|
||||
aaa := findNode(root, "AAA")
|
||||
var n *Node
|
||||
n = aaa.SelectElement("BBB")
|
||||
if n == nil {
|
||||
t.Fatalf("n is nil")
|
||||
}
|
||||
n = aaa.SelectElement("CCC")
|
||||
if n == nil {
|
||||
t.Fatalf("n is nil")
|
||||
}
|
||||
|
||||
var ns []*Node
|
||||
ns = aaa.SelectElements("CCC")
|
||||
if len(ns) != 2 {
|
||||
t.Fatalf("len(ns)!=2")
|
||||
}
|
||||
}
|
||||
193
trunk/goutil/xmlUtil/query.go
Normal file
193
trunk/goutil/xmlUtil/query.go
Normal file
@@ -0,0 +1,193 @@
|
||||
package xmlUtil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"goutil/xmlUtil/gxpath"
|
||||
"goutil/xmlUtil/gxpath/xpath"
|
||||
)
|
||||
|
||||
// xml节点查找结构(用于遍历xml节点)
|
||||
type xmlNodeNavigator struct {
|
||||
root, curr *Node
|
||||
attr int
|
||||
}
|
||||
|
||||
// 节点类型
|
||||
func (x *xmlNodeNavigator) NodeType() xpath.NodeType {
|
||||
switch x.curr.Type {
|
||||
case CommentNode:
|
||||
return xpath.CommentNode
|
||||
case TextNode:
|
||||
return xpath.TextNode
|
||||
case DeclarationNode, DocumentNode:
|
||||
return xpath.RootNode
|
||||
case ElementNode:
|
||||
if x.attr != -1 {
|
||||
return xpath.AttributeNode
|
||||
}
|
||||
return xpath.ElementNode
|
||||
}
|
||||
panic(fmt.Sprintf("unknown XML node type: %v", x.curr.Type))
|
||||
}
|
||||
|
||||
// 当前查找的节点名或属性名
|
||||
func (x *xmlNodeNavigator) LocalName() string {
|
||||
if x.attr != -1 {
|
||||
return x.curr.Attr[x.attr].Name.Local
|
||||
}
|
||||
return x.curr.NodeName
|
||||
|
||||
}
|
||||
|
||||
// 名节点前缀
|
||||
func (x *xmlNodeNavigator) Prefix() string {
|
||||
return x.curr.Namespace
|
||||
}
|
||||
|
||||
// 节点值或属性值
|
||||
func (x *xmlNodeNavigator) Value() string {
|
||||
switch x.curr.Type {
|
||||
case CommentNode:
|
||||
return x.curr.NodeName
|
||||
case ElementNode:
|
||||
if x.attr != -1 {
|
||||
return x.curr.Attr[x.attr].Value
|
||||
}
|
||||
return x.curr.InnerText()
|
||||
case TextNode:
|
||||
return x.curr.NodeName
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// 创建一个拷贝对象
|
||||
func (x *xmlNodeNavigator) Copy() xpath.NodeNavigator {
|
||||
n := *x
|
||||
return &n
|
||||
}
|
||||
|
||||
// 移动到根节点
|
||||
func (x *xmlNodeNavigator) MoveToRoot() {
|
||||
x.curr = x.root
|
||||
}
|
||||
|
||||
// 移动到父节点
|
||||
func (x *xmlNodeNavigator) MoveToParent() bool {
|
||||
if node := x.curr.Parent; node != nil {
|
||||
x.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 移动到下一个属性
|
||||
func (x *xmlNodeNavigator) MoveToNextAttribute() bool {
|
||||
if x.attr >= len(x.curr.Attr)-1 {
|
||||
return false
|
||||
}
|
||||
x.attr++
|
||||
return true
|
||||
}
|
||||
|
||||
// 移动到子节点
|
||||
func (x *xmlNodeNavigator) MoveToChild() bool {
|
||||
if node := x.curr.FirstChild; node != nil {
|
||||
x.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 移动到第一个节点
|
||||
func (x *xmlNodeNavigator) MoveToFirst() bool {
|
||||
if x.curr.PrevSibling == nil {
|
||||
return false
|
||||
}
|
||||
for {
|
||||
node := x.curr.PrevSibling
|
||||
if node == nil {
|
||||
break
|
||||
}
|
||||
x.curr = node
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// 节点的值
|
||||
func (x *xmlNodeNavigator) String() string {
|
||||
return x.Value()
|
||||
}
|
||||
|
||||
// 移动到下一个兄弟节点
|
||||
func (x *xmlNodeNavigator) MoveToNext() bool {
|
||||
if node := x.curr.NextSibling; node != nil {
|
||||
x.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 移动到上一个兄弟节点
|
||||
func (x *xmlNodeNavigator) MoveToPrevious() bool {
|
||||
if node := x.curr.PrevSibling; node != nil {
|
||||
x.curr = node
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// 移动到指定节点
|
||||
func (x *xmlNodeNavigator) MoveTo(other xpath.NodeNavigator) bool {
|
||||
node, ok := other.(*xmlNodeNavigator)
|
||||
if !ok || node.root != x.root {
|
||||
return false
|
||||
}
|
||||
|
||||
x.curr = node.curr
|
||||
x.attr = node.attr
|
||||
return true
|
||||
}
|
||||
|
||||
// CreateXPathNavigator creates a new xpath.NodeNavigator for the specified html.Node.
|
||||
func CreateXPathNavigator(top *Node) xpath.NodeNavigator {
|
||||
return &xmlNodeNavigator{curr: top, root: top, attr: -1}
|
||||
}
|
||||
|
||||
// 按照xpath查找所有匹配的节点
|
||||
// top:根节点
|
||||
// expr:xpath表达式
|
||||
// 返回值:
|
||||
// []*Node:结果
|
||||
func Find(top *Node, expr string) []*Node {
|
||||
t := gxpath.Select(CreateXPathNavigator(top), expr)
|
||||
var elems []*Node
|
||||
for t.MoveNext() {
|
||||
elems = append(elems, (t.Current().(*xmlNodeNavigator)).curr)
|
||||
}
|
||||
return elems
|
||||
}
|
||||
|
||||
// 按照xpath查找第一个匹配的节点
|
||||
// top:根节点
|
||||
// expr:xpath表达式
|
||||
// 返回值:
|
||||
// *Node:查找到的第一个节点
|
||||
func FindOne(top *Node, expr string) *Node {
|
||||
t := gxpath.Select(CreateXPathNavigator(top), expr)
|
||||
var elem *Node
|
||||
if t.MoveNext() {
|
||||
elem = (t.Current().(*xmlNodeNavigator)).curr
|
||||
}
|
||||
return elem
|
||||
}
|
||||
|
||||
// FindEach searches the html.Node and calls functions cb.
|
||||
func FindEach(top *Node, expr string, cb func(int, *Node)) {
|
||||
t := gxpath.Select(CreateXPathNavigator(top), expr)
|
||||
var i int
|
||||
for t.MoveNext() {
|
||||
cb(i, (t.Current().(*xmlNodeNavigator)).curr)
|
||||
i++
|
||||
}
|
||||
}
|
||||
153
trunk/goutil/xmlUtil/query_test.go
Normal file
153
trunk/goutil/xmlUtil/query_test.go
Normal file
@@ -0,0 +1,153 @@
|
||||
package xmlUtil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var doc = loadXml()
|
||||
|
||||
func TestXPath(t *testing.T) {
|
||||
if list := Find(doc, "//book"); len(list) != 12 {
|
||||
t.Fatal("count(//book) != 12")
|
||||
}
|
||||
if node := FindOne(doc, "//book[@id='bk101']"); node == nil {
|
||||
t.Fatal("//book[@id='bk101] is not found")
|
||||
}
|
||||
if node := FindOne(doc, "//book[price>=44.95]"); node == nil {
|
||||
t.Fatal("//book/price>=44.95 is not found")
|
||||
}
|
||||
if list := Find(doc, "//book[genre='Fantasy']"); len(list) != 4 {
|
||||
t.Fatal("//book[genre='Fantasy'] items count is not equal 4")
|
||||
}
|
||||
}
|
||||
|
||||
func loadXml() *Node {
|
||||
// https://msdn.microsoft.com/en-us/library/ms762271(v=vs.85).aspx
|
||||
s := `
|
||||
<?xml version="1.0"?>
|
||||
<catalog>
|
||||
<book id="bk101">
|
||||
<author>Gambardella, Matthew</author>
|
||||
<title>XML Developer's Guide</title>
|
||||
<genre>Computer</genre>
|
||||
<price>44.95</price>
|
||||
<publish_date>2000-10-01</publish_date>
|
||||
<description>An in-depth look at creating applications
|
||||
with XML.</description>
|
||||
</book>
|
||||
<book id="bk102">
|
||||
<author>Ralls, Kim</author>
|
||||
<title>Midnight Rain</title>
|
||||
<genre>Fantasy</genre>
|
||||
<price>5.95</price>
|
||||
<publish_date>2000-12-16</publish_date>
|
||||
<description>A former architect battles corporate zombies,
|
||||
an evil sorceress, and her own childhood to become queen
|
||||
of the world.</description>
|
||||
</book>
|
||||
<book id="bk103">
|
||||
<author>Corets, Eva</author>
|
||||
<title>Maeve Ascendant</title>
|
||||
<genre>Fantasy</genre>
|
||||
<price>5.95</price>
|
||||
<publish_date>2000-11-17</publish_date>
|
||||
<description>After the collapse of a nanotechnology
|
||||
society in England, the young survivors lay the
|
||||
foundation for a new society.</description>
|
||||
</book>
|
||||
<book id="bk104">
|
||||
<author>Corets, Eva</author>
|
||||
<title>Oberon's Legacy</title>
|
||||
<genre>Fantasy</genre>
|
||||
<price>5.95</price>
|
||||
<publish_date>2001-03-10</publish_date>
|
||||
<description>In post-apocalypse England, the mysterious
|
||||
agent known only as Oberon helps to create a new life
|
||||
for the inhabitants of London. Sequel to Maeve
|
||||
Ascendant.</description>
|
||||
</book>
|
||||
<book id="bk105">
|
||||
<author>Corets, Eva</author>
|
||||
<title>The Sundered Grail</title>
|
||||
<genre>Fantasy</genre>
|
||||
<price>5.95</price>
|
||||
<publish_date>2001-09-10</publish_date>
|
||||
<description>The two daughters of Maeve, half-sisters,
|
||||
battle one another for control of England. Sequel to
|
||||
Oberon's Legacy.</description>
|
||||
</book>
|
||||
<book id="bk106">
|
||||
<author>Randall, Cynthia</author>
|
||||
<title>Lover Birds</title>
|
||||
<genre>Romance</genre>
|
||||
<price>4.95</price>
|
||||
<publish_date>2000-09-02</publish_date>
|
||||
<description>When Carla meets Paul at an ornithology
|
||||
conference, tempers fly as feathers get ruffled.</description>
|
||||
</book>
|
||||
<book id="bk107">
|
||||
<author>Thurman, Paula</author>
|
||||
<title>Splish Splash</title>
|
||||
<genre>Romance</genre>
|
||||
<price>4.95</price>
|
||||
<publish_date>2000-11-02</publish_date>
|
||||
<description>A deep sea diver finds true love twenty
|
||||
thousand leagues beneath the sea.</description>
|
||||
</book>
|
||||
<book id="bk108">
|
||||
<author>Knorr, Stefan</author>
|
||||
<title>Creepy Crawlies</title>
|
||||
<genre>Horror</genre>
|
||||
<price>4.95</price>
|
||||
<publish_date>2000-12-06</publish_date>
|
||||
<description>An anthology of horror stories about roaches,
|
||||
centipedes, scorpions and other insects.</description>
|
||||
</book>
|
||||
<book id="bk109">
|
||||
<author>Kress, Peter</author>
|
||||
<title>Paradox Lost</title>
|
||||
<genre>Science Fiction</genre>
|
||||
<price>6.95</price>
|
||||
<publish_date>2000-11-02</publish_date>
|
||||
<description>After an inadvertant trip through a Heisenberg
|
||||
Uncertainty Device, James Salway discovers the problems
|
||||
of being quantum.</description>
|
||||
</book>
|
||||
<book id="bk110">
|
||||
<author>O'Brien, Tim</author>
|
||||
<title>Microsoft .NET: The Programming Bible</title>
|
||||
<genre>Computer</genre>
|
||||
<price>36.95</price>
|
||||
<publish_date>2000-12-09</publish_date>
|
||||
<description>Microsoft's .NET initiative is explored in
|
||||
detail in this deep programmer's reference.</description>
|
||||
</book>
|
||||
<book id="bk111">
|
||||
<author>O'Brien, Tim</author>
|
||||
<title>MSXML3: A Comprehensive Guide</title>
|
||||
<genre>Computer</genre>
|
||||
<price>36.95</price>
|
||||
<publish_date>2000-12-01</publish_date>
|
||||
<description>The Microsoft MSXML3 parser is covered in
|
||||
detail, with attention to XML DOM interfaces, XSLT processing,
|
||||
SAX and more.</description>
|
||||
</book>
|
||||
<book id="bk112">
|
||||
<author>Galos, Mike</author>
|
||||
<title>Visual Studio 7: A Comprehensive Guide</title>
|
||||
<genre>Computer</genre>
|
||||
<price>49.95</price>
|
||||
<publish_date>2001-04-16</publish_date>
|
||||
<description>Microsoft Visual Studio 7 is explored in depth,
|
||||
looking at how Visual Basic, Visual C++, C#, and ASP+ are
|
||||
integrated into a comprehensive development
|
||||
environment.</description>
|
||||
</book>
|
||||
</catalog>`
|
||||
node, err := LoadFromReader(strings.NewReader(s))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return node
|
||||
}
|
||||
20
trunk/goutil/xmlUtil/sample.xml
Normal file
20
trunk/goutil/xmlUtil/sample.xml
Normal file
@@ -0,0 +1,20 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title> <!-- 阿斯大三大四的 -->
|
||||
这里有中文哦</title>
|
||||
<meta name="language" content="en"/>
|
||||
</head>
|
||||
<body>
|
||||
<h1> This is a H1 </h1>
|
||||
<ul>
|
||||
<li><a id="1" href="/">阿萨德</a></li>
|
||||
<li><a id="2" href="/about">哈哈哈</a></li>
|
||||
<li><a id="3" href="/account">他写的</a></li>
|
||||
<li></li>
|
||||
</ul>
|
||||
<p>
|
||||
Hello,This is an example for gxpath.
|
||||
</p>
|
||||
<footer>footer script</footer>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user