Blame SOURCES/0218-v2v-Add-convenience-functions-for-parsing-xpath-expr.patch

ffd6ed
From c99ff5f22228d143a0f3888a4f9bafb813f98078 Mon Sep 17 00:00:00 2001
ffd6ed
From: "Richard W.M. Jones" <rjones@redhat.com>
ffd6ed
Date: Fri, 28 Aug 2015 13:52:49 +0100
ffd6ed
Subject: [PATCH] v2v: Add convenience functions for parsing xpath expressions.
ffd6ed
ffd6ed
(cherry picked from commit 00e1260d343e1f70d6541347ba61ecb072fef799)
ffd6ed
---
ffd6ed
 v2v/Makefile.am |  2 +-
ffd6ed
 v2v/utils.ml    | 34 ++++++++++++++++++++++++++++++++++
ffd6ed
 2 files changed, 35 insertions(+), 1 deletion(-)
ffd6ed
ffd6ed
diff --git a/v2v/Makefile.am b/v2v/Makefile.am
ffd6ed
index 26955aa..98e54de 100644
ffd6ed
--- a/v2v/Makefile.am
ffd6ed
+++ b/v2v/Makefile.am
ffd6ed
@@ -66,8 +66,8 @@ SOURCES_MLI = \
ffd6ed
 SOURCES_ML = \
ffd6ed
 	stringMap.ml \
ffd6ed
 	types.ml \
ffd6ed
-	utils.ml \
ffd6ed
 	xml.ml \
ffd6ed
+	utils.ml \
ffd6ed
 	domainxml.ml \
ffd6ed
 	DOM.ml \
ffd6ed
 	kvmuid.ml \
ffd6ed
diff --git a/v2v/utils.ml b/v2v/utils.ml
ffd6ed
index 0e06913..e872ebe 100644
ffd6ed
--- a/v2v/utils.ml
ffd6ed
+++ b/v2v/utils.ml
ffd6ed
@@ -61,6 +61,40 @@ let uri_quote str =
ffd6ed
   done;
ffd6ed
   String.concat "" (List.rev !xs)
ffd6ed
 
ffd6ed
+(* Parse an xpath expression and return a string/int.  Returns
ffd6ed
+ * Some v or None if the expression doesn't match.
ffd6ed
+ *)
ffd6ed
+let xpath_string xpathctx expr =
ffd6ed
+  let obj = Xml.xpath_eval_expression xpathctx expr in
ffd6ed
+  if Xml.xpathobj_nr_nodes obj < 1 then None
ffd6ed
+  else (
ffd6ed
+    let node = Xml.xpathobj_node obj 0 in
ffd6ed
+    Some (Xml.node_as_string node)
ffd6ed
+  )
ffd6ed
+let xpath_int xpathctx expr =
ffd6ed
+  let obj = Xml.xpath_eval_expression xpathctx expr in
ffd6ed
+  if Xml.xpathobj_nr_nodes obj < 1 then None
ffd6ed
+  else (
ffd6ed
+    let node = Xml.xpathobj_node obj 0 in
ffd6ed
+    let str = Xml.node_as_string node in
ffd6ed
+    try Some (int_of_string str)
ffd6ed
+    with Failure "int_of_string" ->
ffd6ed
+      error (f_"expecting XML expression to return an integer (expression: %s, matching string: %s)")
ffd6ed
+            expr str
ffd6ed
+  )
ffd6ed
+
ffd6ed
+(* Parse an xpath expression and return a string/int; if the expression
ffd6ed
+ * doesn't match, return the default.
ffd6ed
+ *)
ffd6ed
+let xpath_string_default xpathctx expr default =
ffd6ed
+  match xpath_string xpathctx expr with
ffd6ed
+  | None -> default
ffd6ed
+  | Some s -> s
ffd6ed
+let xpath_int_default xpathctx expr default =
ffd6ed
+  match xpath_int xpathctx expr with
ffd6ed
+  | None -> default
ffd6ed
+  | Some i -> i
ffd6ed
+
ffd6ed
 external drive_name : int -> string = "v2v_utils_drive_name"
ffd6ed
 
ffd6ed
 (* Map guest architecture found by inspection to the architecture
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed