Exemple #1
0
func NewXPath(docPtr unsafe.Pointer) (xpath *XPath) {
	if docPtr == nil {
		return
	}
	xpath = &XPath{ContextPtr: C.xmlXPathNewContext((*C.xmlDoc)(docPtr)), ResultPtr: nil}
	runtime.SetFinalizer(xpath, (*XPath).Free)
	return
}
Exemple #2
0
func (d *XmlDoc) XPath(xpathExpr string) *XPathResult {
	context := C.xmlXPathNewContext(d.Ptr)
	defer C.xmlXPathFreeContext(context)

	result := C.xmlXPathEvalExpression(stringToXmlChar(xpathExpr), context)

	fmt.Println("XPath: ", xpathExpr, " Type: ", result._type, " Result: ", result)

	return &XPathResult{ptr: result}
}
Exemple #3
0
// Note that although we are specifying `n... Node` for the argument,
// only the first, node is considered for the context node
func NewXPathContext(n ...Node) (*XPathContext, error) {
	ctx := C.xmlXPathNewContext(nil)
	ctx.namespaces = nil

	obj := &XPathContext{ptr: ctx}
	if len(n) > 0 {
		obj.SetContextNode(n[0])
	}
	return obj, nil
}
Exemple #4
0
func (n *XmlNode) XPath(xpathExpr string) *XPathResult {
	context := C.xmlXPathNewContext((C.xmlDocPtr)(unsafe.Pointer(n.Ptr.doc)))
	context.node = n.Ptr
	defer C.xmlXPathFreeContext(context)

	result := C.xmlXPathEvalExpression(stringToXmlChar(xpathExpr), context)

	fmt.Println("XPath: ", xpathExpr, " Type: ", result._type, " Result: ", result)

	return &XPathResult{ptr: result}
}
Exemple #5
0
// Parse html object with doc - used in testing for "forwarding" samlresponses from html to http
// Disables error reporting - libxml2 complains about html5 elements
func NewHtmlXp(html []byte) *Xp {
	x := new(Xp)
	//x.doc = C.htmlParseDoc((*C.xmlChar)(unsafe.Pointer(&html[0])), nil)
	ctxt := C.htmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&html[0])), C.int(len(html)))
	C.htmlCtxtUseOptions(ctxt, C.HTML_PARSE_NOERROR)
	C.htmlParseDocument(ctxt)
	x.doc = ctxt.myDoc
	C.htmlFreeParserCtxt(ctxt)
	x.xpathCtx = C.xmlXPathNewContext(x.doc)
	runtime.SetFinalizer(x, (*Xp).free)
	return x
}
Exemple #6
0
// Make a copy of the Xp object - shares the document with the source, but allocates a new xmlXPathContext because
// they are not thread/gorutine safe as the context is set for each query call
// Only the document "owning" Xp releases the C level document and it needs be around as long as any copies - ie. do
// not let the original document be garbage collected or havoc will be wreaked
func (src *Xp) CpXp() (xp *Xp) {
	xp = new(Xp)
	xp.doc = src.doc
	xp.master = src
	//xp.doc = C.xmlCopyDoc(src.doc, 1)
	xp.xpathCtx = C.xmlXPathNewContext(xp.doc)
	runtime.SetFinalizer(xp, (*Xp).freexpathCtx)

	for _, ns := range namespaces {
		C.xmlXPathRegisterNs(xp.xpathCtx, ns.prefix, ns.ns_uri)
	}
	return
}
Exemple #7
0
// Parse SAML xml to Xp object with doc and xpath with relevant namespaces registered
func NewXp(xml []byte) *Xp {
	x := new(Xp)
	if len(xml) == 0 {
		x.doc = C.xmlNewDoc((*C.xmlChar)(unsafe.Pointer(C.CString("1.0"))))
	} else {
		x.doc = C.xmlParseMemory((*C.char)(unsafe.Pointer(&xml[0])), C.int(len(xml)))
	}
	x.xpathCtx = C.xmlXPathNewContext(x.doc)
	runtime.SetFinalizer(x, (*Xp).free)

	for _, ns := range namespaces {
		C.xmlXPathRegisterNs(x.xpathCtx, ns.prefix, ns.ns_uri)
	}
	return x
}
Exemple #8
0
// xmlXPathNewContext
func NewXPathContext(doc *Document) *XPathContext {
	return &XPathContext{C.xmlXPathNewContext(doc.Ptr)}
}