Пример #1
0
//need to add an error as a return value b/c xpath evaluation can return error
func (xpath *XPath) Evaluate(nodePtr unsafe.Pointer, xpathExpr *Expression) (nodes []unsafe.Pointer, err error) {
	if nodePtr == nil {
		//evaluating xpath on a  nil node returns no result.
		return
	}
	xpath.ContextPtr.node = (*C.xmlNode)(nodePtr)
	if xpath.ResultPtr != nil {
		C.xmlXPathFreeObject(xpath.ResultPtr)
	}

	xpath.ResultPtr = C.xmlXPathCompiledEval(xpathExpr.Ptr, xpath.ContextPtr)
	if xpath.ResultPtr == nil {
		err = errors.New("err in evaluating xpath: " + xpathExpr.String())
		return
	}

	if nodesetPtr := xpath.ResultPtr.nodesetval; nodesetPtr != nil {
		if nodesetSize := int(nodesetPtr.nodeNr); nodesetSize > 0 {
			nodes = make([]unsafe.Pointer, nodesetSize)
			for i := 0; i < nodesetSize; i++ {
				nodes[i] = unsafe.Pointer(C.fetchNode(nodesetPtr, C.int(i)))
			}
		}
	}
	return
}
Пример #2
0
// Evaluate an XPath. The returned result is stored in the struct. Call ReturnType to
// discover the type of result, and call one of the ResultAs* functions to return a
// copy of the result as a particular type.
func (xpath *XPath) Evaluate(nodePtr unsafe.Pointer, xpathExpr *Expression) (err error) {
	if nodePtr == nil {
		//evaluating xpath on a nil node returns no result.
		return
	}

	oldXPContextDoc := xpath.ContextPtr.doc
	oldXPContextNode := xpath.ContextPtr.node
	oldXPProximityPosition := xpath.ContextPtr.proximityPosition
	oldXPContextSize := xpath.ContextPtr.contextSize
	oldXPNsNr := xpath.ContextPtr.nsNr
	oldXPNamespaces := xpath.ContextPtr.namespaces

	xpath.ContextPtr.node = (*C.xmlNode)(nodePtr)
	if xpath.ResultPtr != nil {
		C.xmlXPathFreeObject(xpath.ResultPtr)
	}
	xpath.ResultPtr = C.xmlXPathCompiledEval(xpathExpr.Ptr, xpath.ContextPtr)

	xpath.ContextPtr.doc = oldXPContextDoc
	xpath.ContextPtr.node = oldXPContextNode
	xpath.ContextPtr.proximityPosition = oldXPProximityPosition
	xpath.ContextPtr.contextSize = oldXPContextSize
	xpath.ContextPtr.nsNr = oldXPNsNr
	xpath.ContextPtr.namespaces = oldXPNamespaces

	if xpath.ResultPtr == nil {
		err = errors.New("err in evaluating xpath: " + xpathExpr.String())
		return
	}
	return
}
Пример #3
0
func (xpath *XPath) Free() {
	if xpath.ContextPtr != nil {
		C.xmlXPathFreeContext(xpath.ContextPtr)
		xpath.ContextPtr = nil
	}
	if xpath.ResultPtr != nil {
		C.xmlXPathFreeObject(xpath.ResultPtr)
		xpath.ResultPtr = nil
	}
}
Пример #4
0
func (xpath *XPath) Free() {
	if xpath.ContextPtr != nil {
		ClearScope(unsafe.Pointer(xpath.ContextPtr))
		C.xmlXPathFreeContext(xpath.ContextPtr)
		xpath.ContextPtr = nil
	}
	if xpath.ResultPtr != nil {
		C.xmlXPathFreeObject(xpath.ResultPtr)
		xpath.ResultPtr = nil
	}
}
Пример #5
0
// C14n Canonicalise the node using the SAML specified exclusive method
// Very slow on large documents with node != nil
func (xp *Xp) C14n(node *C.xmlNode) string {
	var buffer *C.xmlChar
	var nodeset *C.xmlNodeSet

	if node != nil {
		C.xmlXPathSetContextNode(node, xp.xpathCtx)
		xpathObj := C.xmlXPathEvalExpression(exclc14nxpath, xp.xpathCtx)
		defer C.xmlXPathFreeObject(xpathObj)
		nodeset = xpathObj.nodesetval
		//fmt.Printf("%+v\n", nodeset)
	}

	C.xmlC14NDocDumpMemory(xp.doc, nodeset, C.XML_C14N_EXCLUSIVE_1_0, nil, 0, &buffer)
	defer C.free(unsafe.Pointer(buffer))
	p := (*C.char)(unsafe.Pointer(buffer))
	return C.GoString(p)
}
Пример #6
0
func (xpath *XPath) Evaluate(nodePtr unsafe.Pointer, xpathExpr *Expression) (nodes []unsafe.Pointer) {
	if nodePtr == nil {
		return
	}
	xpath.ContextPtr.node = (*C.xmlNode)(nodePtr)
	if xpath.ResultPtr != nil {
		C.xmlXPathFreeObject(xpath.ResultPtr)
	}
	xpath.ResultPtr = C.xmlXPathCompiledEval(xpathExpr.Ptr, xpath.ContextPtr)
	if nodesetPtr := xpath.ResultPtr.nodesetval; nodesetPtr != nil {
		if nodesetSize := int(nodesetPtr.nodeNr); nodesetSize > 0 {
			nodes = make([]unsafe.Pointer, nodesetSize)
			for i := 0; i < nodesetSize; i++ {
				nodes[i] = unsafe.Pointer(C.fetchNode(nodesetPtr, C.int(i)))
			}
		}
	}
	return
}
Пример #7
0
// xmlXPathFreeObject
func (obj *XPathObject) Free() {
	C.xmlXPathFreeObject(obj.Ptr)
	obj.Ptr = nil
}
Пример #8
0
func (x *XPathObject) Free() {
	//	if x.ptr.nodesetval != nil {
	//		C.xmlXPathFreeNodeSet(x.ptr.nodesetval)
	//	}
	C.xmlXPathFreeObject(x.ptr)
}