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

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