Blame SOURCES/0033-v2v-DOM-Add-doc_to_string-function.patch

97ae69
From 4b5304704a5fa4cad1e5869ce97f27ff64ac492f Mon Sep 17 00:00:00 2001
97ae69
From: "Richard W.M. Jones" <rjones@redhat.com>
97ae69
Date: Tue, 20 Feb 2018 15:21:48 +0000
97ae69
Subject: [PATCH] v2v: DOM: Add doc_to_string function.
97ae69
97ae69
Convert a document to an in-memory string.
97ae69
97ae69
(cherry picked from commit 802c8635cc2537a7d4b7af8981c670e2fdbb2675)
97ae69
---
97ae69
 v2v/DOM.ml  | 61 +++++++++++++++++++++++++++++++++--------------------
97ae69
 v2v/DOM.mli |  3 +++
97ae69
 2 files changed, 41 insertions(+), 23 deletions(-)
97ae69
97ae69
diff --git a/v2v/DOM.ml b/v2v/DOM.ml
97ae69
index 7f66e0920..8b106224b 100644
97ae69
--- a/v2v/DOM.ml
97ae69
+++ b/v2v/DOM.ml
97ae69
@@ -46,43 +46,48 @@ let e name attrs children =
97ae69
  * we will be writing, ie. libvirt XML and OVF metadata, where
97ae69
  * whitespace is generally not significant, but readability is useful.
97ae69
  *)
97ae69
-let rec node_to_chan ?(indent = 0) chan = function
97ae69
-  | PCData str -> output_string chan (xml_quote_pcdata str)
97ae69
+let rec node_to_buf ?(indent = 0) buf = function
97ae69
+  | PCData str ->
97ae69
+     Buffer.add_string buf (xml_quote_pcdata str)
97ae69
   | Comment str ->
97ae69
-    output_spaces chan indent;
97ae69
-    fprintf chan "" (xml_quote_pcdata str)
97ae69
-  | Element e -> element_to_chan ~indent chan e
97ae69
-and element_to_chan ?(indent = 0) chan
97ae69
+     buffer_add_spaces buf indent;
97ae69
+     bprintf buf "" (xml_quote_pcdata str)
97ae69
+  | Element e ->
97ae69
+     element_to_buf ~indent buf e
97ae69
+and element_to_buf ?(indent = 0) buf
97ae69
     { e_name = name; e_attrs = attrs; e_children = children } =
97ae69
-  output_spaces chan indent;
97ae69
-  fprintf chan "<%s" name;
97ae69
-  List.iter (fun (n, v) -> fprintf chan " %s='%s'" n (xml_quote_attr v)) attrs;
97ae69
+  buffer_add_spaces buf indent;
97ae69
+  bprintf buf "<%s" name;
97ae69
+  List.iter (fun (n, v) -> bprintf buf " %s='%s'" n (xml_quote_attr v)) attrs;
97ae69
   if children <> [] then (
97ae69
-    output_string chan ">";
97ae69
+    Buffer.add_string buf ">";
97ae69
     let last_child_was_element = ref false in
97ae69
     List.iter (
97ae69
       function
97ae69
       | Element _ as child ->
97ae69
         last_child_was_element := true;
97ae69
-        output_char chan '\n';
97ae69
-        node_to_chan ~indent:(indent+2) chan child;
97ae69
+        Buffer.add_char buf '\n';
97ae69
+        node_to_buf ~indent:(indent+2) buf child;
97ae69
       | PCData _ as child ->
97ae69
         last_child_was_element := false;
97ae69
-        node_to_chan ~indent:(indent+2) chan child;
97ae69
+        node_to_buf ~indent:(indent+2) buf child;
97ae69
       | Comment _ as child ->
97ae69
         last_child_was_element := true;
97ae69
-        output_char chan '\n';
97ae69
-        node_to_chan ~indent:(indent+2) chan child;
97ae69
+        Buffer.add_char buf '\n';
97ae69
+        node_to_buf ~indent:(indent+2) buf child;
97ae69
     ) children;
97ae69
     if !last_child_was_element then (
97ae69
-      output_char chan '\n';
97ae69
-      output_spaces chan indent
97ae69
+      Buffer.add_char buf '\n';
97ae69
+      buffer_add_spaces buf indent
97ae69
     );
97ae69
-    fprintf chan "</%s>" name
97ae69
+    bprintf buf "</%s>" name
97ae69
   ) else (
97ae69
-    output_string chan "/>"
97ae69
+    Buffer.add_string buf "/>"
97ae69
   )
97ae69
 
97ae69
+and buffer_add_spaces buf n =
97ae69
+  Buffer.add_string buf (String.spaces n)
97ae69
+
97ae69
 (* Quote XML <element attr='...'> content.  Note you must use single
97ae69
  * quotes around the attribute.
97ae69
  *)
97ae69
@@ -99,10 +104,20 @@ and xml_quote_pcdata str =
97ae69
   let str = String.replace str ">" ">" in
97ae69
   str
97ae69
 
97ae69
-let doc_to_chan chan (Doc doc) =
97ae69
-  fprintf chan "\n";
97ae69
-  element_to_chan chan doc;
97ae69
-  fprintf chan "\n"
97ae69
+let doc_to_buf buf (Doc doc) =
97ae69
+  bprintf buf "\n";
97ae69
+  element_to_buf buf doc;
97ae69
+  bprintf buf "\n"
97ae69
+
97ae69
+let doc_to_string doc =
97ae69
+  let buf = Buffer.create 4096 in
97ae69
+  doc_to_buf buf doc;
97ae69
+  Buffer.contents buf
97ae69
+
97ae69
+let doc_to_chan chan doc =
97ae69
+  let buf = Buffer.create 4096 in
97ae69
+  doc_to_buf buf doc;
97ae69
+  Buffer.output_buffer chan buf
97ae69
 
97ae69
 let path_to_nodes (Doc doc) path =
97ae69
   match path with
97ae69
diff --git a/v2v/DOM.mli b/v2v/DOM.mli
97ae69
index 1aa89b7e7..99223c389 100644
97ae69
--- a/v2v/DOM.mli
97ae69
+++ b/v2v/DOM.mli
97ae69
@@ -60,6 +60,9 @@ v}
97ae69
 v}
97ae69
 *)
97ae69
 
97ae69
+val doc_to_string : doc -> string
97ae69
+(** Convert a document to a string representation. *)
97ae69
+
97ae69
 val doc_to_chan : out_channel -> doc -> unit
97ae69
 (** Write the XML document to an output channel. *)
97ae69
 
97ae69
-- 
97ae69
2.17.1
97ae69