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

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

	tok := node.GetToken()

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

	return tree.String(ret), nil
}
Beispiel #2
0
func _string(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	if len(args) == 1 {
		return tree.String(args[0].String()), nil
	}

	return tree.String(c.NodeSet.String()), nil
}
Beispiel #3
0
func bothNodeOperator(left tree.NodeSet, right tree.NodeSet, f *xpFilt, op string) error {
	var err error
	for _, l := range left {
		for _, r := range right {
			lStr := l.ResValue()
			rStr := r.ResValue()

			if eqOps[op] {
				err = equalsOperator(tree.String(lStr), tree.String(rStr), f, op)
				if err == nil && f.ctx.String() == tree.True {
					return nil
				}
			} else {
				err = numberOperator(tree.String(lStr), tree.String(rStr), f, op)
				if err == nil && f.ctx.String() == tree.True {
					return nil
				}
			}
		}
	}

	f.ctx = tree.Bool(false)

	return nil
}
Beispiel #4
0
func substringAfter(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ind := strings.Index(args[0].String(), args[1].String())
	if ind == -1 {
		return tree.String(""), nil
	}

	return tree.String(args[0].String()[ind+len(args[1].String()):]), nil
}
Beispiel #5
0
func substringBefore(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ind := strings.Index(args[0].String(), args[1].String())
	if ind == -1 {
		return tree.String(""), nil
	}

	return tree.String(args[0].String()[:ind]), nil
}
Beispiel #6
0
func concat(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ret := ""

	for _, i := range args {
		ret += i.String()
	}

	return tree.String(ret), nil
}
Beispiel #7
0
func substring(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	str := args[0].String()

	bNum, bErr := round(c, args[1])
	if bErr != nil {
		return nil, bErr
	}

	b := bNum.(tree.Num).Num()

	if float64(b-1) >= float64(len(str)) || math.IsNaN(float64(b)) {
		return tree.String(""), nil
	}

	if len(args) == 2 {
		if b <= 1 {
			b = 1
		}

		return tree.String(str[int(b)-1:]), nil
	}

	eNum, eErr := round(c, args[2])
	if eErr != nil {
		return nil, eErr
	}

	e := eNum.(tree.Num).Num()

	if e <= 0 || math.IsNaN(float64(e)) || (math.IsInf(float64(b), 0) && math.IsInf(float64(e), 0)) {
		return tree.String(""), nil
	}

	if b <= 1 {
		e = b + e - 1
		b = 1
	}

	if float64(b+e-1) >= float64(len(str)) {
		e = tree.Num(len(str)) - b + 1
	}

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

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

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

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

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

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

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

	return tree.String(ret), nil
}
Beispiel #9
0
func normalizeSpace(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	var str string
	if len(args) == 1 {
		str = args[0].String()
	} else {
		str = c.NodeSet.String()
	}

	str = strings.TrimSpace(str)

	return tree.String(spaceTrim.ReplaceAllString(str, " ")), nil
}
Beispiel #10
0
func rightNodeOperator(left tree.Result, right tree.NodeSet, f *xpFilt, op string) error {
	var err error
	for _, r := range right {
		rStr := r.ResValue()

		if eqOps[op] {
			err = equalsOperator(left, tree.String(rStr), f, op)
			if err == nil && f.ctx.String() == "true" {
				return nil
			}
		} else {
			err = numberOperator(left, tree.String(rStr), f, op)
			if err == nil && f.ctx.String() == "true" {
				return nil
			}
		}
	}

	f.ctx = tree.Bool(false)

	return nil
}
Beispiel #11
0
func translate(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	ret := args[0].String()
	src := args[1].String()
	repl := args[2].String()

	for i := range src {
		r := ""
		if i < len(repl) {
			r = string(repl[i])
		}

		ret = strings.Replace(ret, string(src[i]), r, -1)
	}

	return tree.String(ret), nil
}
Beispiel #12
0
func xfStrLit(f *xpFilt, val string) {
	f.ctx = tree.String(val)
}
Beispiel #13
0
func basenameFunc(c tree.Ctx, args ...tree.Result) (tree.Result, error) {
	parsedURL, err := url.Parse(args[0].String())
	return tree.String(path.Base(parsedURL.Path)), err
}