Files
libguestfs/mllib/xpath_helpers.ml
Richard W.M. Jones 61d4891ef4 mllib: Split ‘Common_utils’ into ‘Std_utils’ + ‘Common_utils’.
The new module ‘Std_utils’ contains only functions which are pure
OCaml and depend only on the OCaml stdlib.  Therefore these functions
may be used by the generator.

The new module is moved to ‘common/mlstdutils’.

This also removes the "<stdlib>" hack, and the code which copied the
library around.

Also ‘Guestfs_config’, ‘Libdir’ and ‘StringMap’ modules are moved
since these are essentially the same.

The bulk of this change is just updating files which use
‘open Common_utils’ to add ‘open Std_utils’ where necessary.
2017-07-10 17:01:59 +01:00

55 lines
1.9 KiB
OCaml

(* virt-v2v
* Copyright (C) 2009-2017 Red Hat Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*)
open Printf
open Std_utils
open Common_utils
open Common_gettext.Gettext
(* Parse an xpath expression and return a string/int. Returns
* [Some v], or [None] if the expression doesn't match.
*)
let xpath_eval parsefn xpathctx expr =
let obj = Xml.xpath_eval_expression xpathctx expr in
if Xml.xpathobj_nr_nodes obj < 1 then None
else (
let node = Xml.xpathobj_node obj 0 in
let str = Xml.node_as_string node in
try Some (parsefn str)
with Failure _ ->
error (f_"expecting XML expression to return an integer (expression: %s, matching string: %s)")
expr str
)
let xpath_string = xpath_eval identity
let xpath_int = xpath_eval int_of_string
let xpath_int64 = xpath_eval Int64.of_string
(* Parse an xpath expression and return a string/int; if the expression
* doesn't match, return the default.
*)
let xpath_eval_default parsefn xpath expr default =
match xpath_eval parsefn xpath expr with
| None -> default
| Some s -> s
let xpath_string_default = xpath_eval_default identity
let xpath_int_default = xpath_eval_default int_of_string
let xpath_int64_default = xpath_eval_default Int64.of_string