Esempio n. 1
0
func main() {

	parseTree := xmltree.MustParseXML(os.Stdin, func(s *xmltree.ParseOptions) {
		s.Strict = false
	})
	goxpath.Marshal(parseTree, os.Stdout)
}
Esempio n. 2
0
func main() {
	// no first command line arguments
	if len(os.Args) <= 1 {
		println("Usage:\r\n input.xml")
	}

	urlBasename := goxpath.MustParse(`basename(../@Url)`)
	transTimer := goxpath.MustParse(`ancestor::ms:TransactionTimer/@Name`)

	f, err := os.Open(os.Args[1])

	if err != nil {
		panic(err)
	}

	parseTree := xmltree.MustParseXML(f)
	reportingNames, err := goxpath.MustParse(`//ms:Request/@ReportingName`).ExecNode(parseTree, register)

	if err != nil {
		panic(err)
	}

	for _, i := range reportingNames {
		attr := i.(xmlnode.XMLNode)
		val := attr.Token.(*xml.Attr)

		val.Value = transTimer.MustExec(attr, register).String() + " - " +
			urlBasename.MustExec(attr, register).String()
	}

	goxpath.Marshal(parseTree, os.Stdout)
}
Esempio n. 3
0
func TestNodePos(t *testing.T) {
	ns := map[string]string{"test": "http://test", "test2": "http://test2", "test3": "http://test3"}
	x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test" attr1="foo"><p2 xmlns="http://test2" xmlns:test="http://test3" attr2="bar">text</p2></p1>`
	testPos := func(path string, pos int) {
		res := MustParse(path).MustExec(xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns }).(tree.NodeSet)
		if len(res) != 1 {
			t.Errorf("Result length not 1: %s", path)
			return
		}
		exPos := res[0].(tree.Node).Pos()
		if exPos != pos {
			t.Errorf("Node position not correct.  Recieved %d, expected %d", exPos, pos)
		}
	}
	testPos("/", 0)
	testPos("/*", 1)
	testPos("/*/namespace::*[1]", 2)
	testPos("/*/namespace::*[2]", 3)
	testPos("/*/attribute::*[1]", 4)
	testPos("//*:p2", 5)
	testPos("//*:p2/namespace::*[1]", 6)
	testPos("//*:p2/namespace::*[2]", 7)
	testPos("//*:p2/namespace::*[3]", 8)
	testPos("//*:p2/attribute::*[1]", 9)
	testPos("//text()", 10)
}
Esempio n. 4
0
func TestMarshalErr(t *testing.T) {
	x := `<?xml version="1.0" encoding="UTF-8"?><p1><p2/></p1>`
	n := xmltree.MustParseXML(bytes.NewBufferString(x))
	f := tree.FindNodeByPos(n, 3).(*xmlele.XMLEle)
	f.Name.Local = ""
	buf := &bytes.Buffer{}
	err := Marshal(n, buf)
	if err == nil {
		t.Error("No error")
	}
}
Esempio n. 5
0
func TestXMLHandlerSubstractUsage(t *testing.T) {
	xp := goxpath.MustParse(path.Join("/broadWorksCDR/cdrData/"))
	xmlTree := xmltree.MustParseXML(bytes.NewBufferString(cdrXmlBroadsoft), optsNotStrict)
	cdrs := goxpath.MustExec(xp, xmlTree, nil)
	cdrWithUsage := cdrs[1]
	if usage, err := handlerSubstractUsage(cdrWithUsage, utils.ParseRSRFieldsMustCompile("broadWorksCDR>cdrData>basicModule>releaseTime;^|;broadWorksCDR>cdrData>basicModule>answerTime", utils.INFIELD_SEP),
		utils.HierarchyPath([]string{"broadWorksCDR", "cdrData"}), "UTC"); err != nil {
		t.Error(err)
	} else if usage != time.Duration(13483000000) {
		t.Errorf("Expected: 13.483s, received: %v", usage)
	}
}
Esempio n. 6
0
func TestRoot2(t *testing.T) {
	x := xmltree.MustParseXML(bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?><test><path/></test>`))
	x = MustParse("/test/path").MustExec(x).(tree.NodeSet)[0]
	for _, i := range []string{"/", "//test"} {
		res, err := MarshalStr(MustParse(i).MustExec(x).(tree.NodeSet)[0])
		if err != nil {
			t.Error(err)
		}
		if res != "<test><path></path></test>" {
			t.Error("Incorrect result", res)
		}
	}
}
Esempio n. 7
0
func TestParseXMLPanic(t *testing.T) {
	errs := 0
	defer func() {
		if errs != 1 {
			t.Error("Err not 1")
		}
	}()
	defer func() {
		if r := recover(); r != nil {
			errs++
		}
	}()
	xmltree.MustParseXML(bytes.NewBufferString("<root/>"))
}
Esempio n. 8
0
func TestExecPanic(t *testing.T) {
	errs := 0
	defer func() {
		if errs != 1 {
			t.Error("Err not 1")
		}
	}()
	defer func() {
		if r := recover(); r != nil {
			errs++
		}
	}()
	MustParse("foo()").MustExec(xmltree.MustParseXML(bytes.NewBufferString(xml.Header + "<root/>")))
}
Esempio n. 9
0
func TestNSSort(t *testing.T) {
	testNS := func(n tree.Node, url string) {
		if n.(tree.NS).Value != url {
			t.Errorf("Unexpected namespace %s.  Expecting %s", n.(tree.NS).Value, url)
		}
	}
	ns := map[string]string{"test": "http://test", "test2": "http://test2", "test3": "http://test3"}
	x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test" xmlns:test2="http://test2" xmlns:test3="http://test3" attr2="bar"/>`
	res := MustParse("/*:p1/namespace::*").MustExec(xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns }).(tree.NodeSet)
	testNS(res[0], ns["test"])
	testNS(res[1], ns["test2"])
	testNS(res[2], ns["test3"])
	testNS(res[3], "http://www.w3.org/XML/1998/namespace")
}
Esempio n. 10
0
func execErr(xp, x string, errStr string, ns map[string]string, t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			t.Error("Panicked: from XPath expr: '" + xp)
			t.Error(r)
			t.Error(string(debug.Stack()))
		}
	}()
	_, err := ParseExec(xp, xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns; o.Funcs = custFns })

	if err.Error() != errStr {
		t.Error("Incorrect result:'" + err.Error() + "' from XPath expr: '" + xp + "'.  Expecting: '" + errStr + "'")
		return
	}
}
Esempio n. 11
0
func execVal(xp, x string, exp string, ns map[string]string, t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			t.Error("Panicked: from XPath expr: '" + xp)
			t.Error(r)
			t.Error(string(debug.Stack()))
		}
	}()
	res := MustParse(xp).MustExec(xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns })

	if res.String() != exp {
		t.Error("Incorrect result:'" + res.String() + "' from XPath expr: '" + xp + "'.  Expecting: '" + exp + "'")
		return
	}
}
Esempio n. 12
0
func TestGoxpathNode(t *testing.T) {
	opts := func(o *Opts) { o.Funcs = custFns }
	x := xmltree.MustParseXML(bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8"?><p1><p2/></p1>`))
	_, err := MustParse(`dummy() = 1`).ExecNode(x, opts)
	if err == nil {
		t.Error("Error not nil")
	}
	_, err = MustParse(`dummy()`).ExecNode(x, opts)
	if err == nil {
		t.Error("Error not nil")
	}
	n, err := MustParse(`/p1`).ExecNode(x, opts)
	if len(n) != 1 || err != nil {
		t.Error("Incorrect result")
	}
}
Esempio n. 13
0
func TestVariable(t *testing.T) {
	x := xmltree.MustParseXML(bytes.NewBufferString(xml.Header + "<p1><p2>foo</p2><p3>bar</p3></p1>"))
	xp := MustParse(`/p1/p2`)
	res := xp.MustExec(x)
	opt := func(o *Opts) {
		o.Vars["prev"] = res
	}
	xp = MustParse(`$prev = 'foo'`)
	if res, err := xp.ExecBool(x, opt); err != nil || !res {
		t.Error("Incorrect result", res, err)
	}
	if _, err := xp.ExecBool(x); err == nil {
		t.Error("Error not nil")
	}
	if _, err := Parse(`$ = 'foo'`); err == nil {
		t.Error("Parse error not nil")
	}
}
Esempio n. 14
0
func TestFindNodeByPos(t *testing.T) {
	x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns="http://test" attr1="foo"><p2 xmlns="http://test2" xmlns:test="http://test3" attr2="bar"><p3/>text<p4/></p2></p1>`
	nt := xmltree.MustParseXML(bytes.NewBufferString(x))
	if tree.FindNodeByPos(nt, 5).GetNodeType() != tree.NtElem {
		t.Error("Node 5 not element")
	}
	if tree.FindNodeByPos(nt, 14).GetNodeType() != tree.NtChd {
		t.Error("Node 14 not char data")
	}
	if tree.FindNodeByPos(nt, 4).GetNodeType() != tree.NtAttr {
		t.Error("Node 4 not attribute")
	}
	if tree.FindNodeByPos(nt, 3).GetNodeType() != tree.NtNs {
		t.Error("Node 3 not namespace")
	}
	if tree.FindNodeByPos(nt, 19) != nil {
		t.Error("Invalid node returned")
	}
}
Esempio n. 15
0
func TestXMLElementText(t *testing.T) {
	xp := goxpath.MustParse(path.Join("/broadWorksCDR/cdrData/"))
	xmlTree := xmltree.MustParseXML(bytes.NewBufferString(cdrXmlBroadsoft), optsNotStrict)
	cdrs := goxpath.MustExec(xp, xmlTree, nil)
	cdrWithoutUserNr := cdrs[0]
	if _, err := elementText(cdrWithoutUserNr, "cdrData/basicModule/userNumber"); err != utils.ErrNotFound {
		t.Error(err)
	}
	cdrWithUser := cdrs[1]
	if val, err := elementText(cdrWithUser, "cdrData/basicModule/userNumber"); err != nil {
		t.Error(err)
	} else if val != "1001" {
		t.Errorf("Expecting: 1001, received: %s", val)
	}
	if val, err := elementText(cdrWithUser, "/cdrData/centrexModule/locationList/locationInformation/locationType"); err != nil {
		t.Error(err)
	} else if val != "Primary Device" {
		t.Errorf("Expecting: <Primary Device>, received: <%s>", val)
	}
}
Esempio n. 16
0
func TestFindAttr(t *testing.T) {
	x := `<?xml version="1.0" encoding="UTF-8"?><p1 xmlns:test="http://test" attr1="foo" test:attr2="bar" />`
	nt := xmltree.MustParseXML(bytes.NewBufferString(x))
	res, _ := ParseExec("/p1", nt)
	node := res.(tree.NodeSet)[0].(tree.Elem)
	if val, ok := tree.GetAttributeVal(node, "attr1", ""); !ok || val != "foo" {
		t.Error("attr1 not foo")
	}
	if val, ok := tree.GetAttributeVal(node, "attr2", "http://test"); !ok || val != "bar" {
		t.Error("attr2 not bar")
	}
	if val, ok := tree.GetAttributeVal(node, "attr3", ""); ok || val != "" {
		t.Error("attr3 is set")
	}
	if val := tree.GetAttrValOrEmpty(node, "attr3", ""); val != "" {
		t.Error("attr3 is set")
	}
	if val := tree.GetAttrValOrEmpty(node, "attr1", ""); val != "foo" {
		t.Error("attr1 not foo")
	}
}
Esempio n. 17
0
func execPath(xp, x string, exp []string, ns map[string]string, t *testing.T) {
	defer func() {
		if r := recover(); r != nil {
			t.Error("Panicked: from XPath expr: '" + xp)
			t.Error(r)
			t.Error(string(debug.Stack()))
		}
	}()
	res := MustParse(xp).MustExec(xmltree.MustParseXML(bytes.NewBufferString(x)), func(o *Opts) { o.NS = ns }).(tree.NodeSet)

	if len(res) != len(exp) {
		t.Error("Result length not valid in XPath expression '"+xp+"':", len(res), ", expecting", len(exp))
		for i := range res {
			t.Error(MarshalStr(res[i].(tree.Node)))
		}
		return
	}

	for i := range res {
		r, err := MarshalStr(res[i].(tree.Node))
		if err != nil {
			t.Error(err.Error())
			return
		}
		valid := false
		for j := range exp {
			if r == exp[j] {
				valid = true
			}
		}
		if !valid {
			t.Error("Incorrect result in XPath expression '" + xp + "':" + r)
			t.Error("Expecting one of:")
			for j := range exp {
				t.Error(exp[j])
			}
			return
		}
	}
}