Esempio n. 1
0
func getEposNodes(doc *xml.XmlDocument) (retnodes []xml.Node, err error) {
	// grab the 'Body' element
	path := xpath.Compile("*[local-name()='Body']")
	nodes, e := doc.Root().Search(path)
	if e != nil {
		err = e
		return
	}

	// check that the data is present
	if len(nodes) < 1 || nodes[0].CountChildren() < 1 {
		err = errors.New("bad data")
		return
	}

	// get epos data
	return nodes[0].FirstChild().Search("./*")
}
Esempio n. 2
0
func (r *RssCloudRequest) Unpack(doc *xml.XmlDocument) error {
	root := doc.Root()

	requestMethods, err := root.Search("methodName/text()")
	if err != nil {
		return err
	}
	if len(requestMethods) != 1 {
		return fmt.Errorf("Could not find cloud request's request method")
	}
	r.RequestMethodName = requestMethods[0].Content()

	// Would be nice to get the child nodes but xpath will give elements only, not text nodes.
	params, err := root.Search("params/param/value")
	if err != nil {
		return err
	}
	if len(params) != 5 {
		return fmt.Errorf("Could not unpack cloud request with %d params", len(params))
	}

	node := params[0].FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with first param not text")
	}
	r.MethodName = node.Content()

	node = params[1].FirstChild()
	if node.NodeType() != xml.XML_ELEMENT_NODE {
		return fmt.Errorf("Could not unpack cloud request with second param not an element")
	}
	if !(node.Name() == "i4" || node.Name() == "int") {
		return fmt.Errorf("Could not unpack cloud request with second param a %s element, not int or i4", node.Name())
	}
	node = node.FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with second param not containing text")
	}
	port, err := strconv.Atoi(node.Content())
	if err != nil {
		return err
	}
	r.Port = uint16(port)

	node = params[2].FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with third param not text")
	}
	r.Path = node.Content()

	node = params[3].FirstChild()
	if node.NodeType() != xml.XML_TEXT_NODE {
		return fmt.Errorf("Could not unpack cloud request with fourth param not text")
	}
	r.IsXmlRpc = strings.TrimSpace(node.Content()) == "xml-rpc"

	node = params[4].FirstChild()
	if node.NodeType() != xml.XML_ELEMENT_NODE {
		return fmt.Errorf("Could not unpack cloud request with fifth param not an element")
	}
	if node.Name() != "array" {
		return fmt.Errorf("Could not unpack cloud request with fifth param a %s element, not array", node.Name())
	}
	params, err = node.Search("data/value/text()")
	if err != nil {
		return err
	}
	if len(params) < 1 {
		return fmt.Errorf("Could not unpack cloud request with fifth param containing no data values")
	}
	r.FeedURL = params[0].Content()

	logr.Debugln("Unpacked cloud request!")
	return nil
}