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

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