[librsvg] XmlState::acquire_xml() - Port rsvg_load_handle_xml_xinclude() to Rust into here



commit eb1ce8db650b644e9834b32c93e056646a019c21
Author: Federico Mena Quintero <federico gnome org>
Date:   Fri Dec 7 10:36:33 2018 -0600

    XmlState::acquire_xml() - Port rsvg_load_handle_xml_xinclude() to Rust into here

 librsvg/rsvg-load.c          | 28 ----------------------------
 librsvg/rsvg-load.h          |  3 ---
 rsvg_internals/src/handle.rs | 18 ++++++------------
 rsvg_internals/src/xml.rs    | 24 +++++++++++++++++-------
 4 files changed, 23 insertions(+), 50 deletions(-)
---
diff --git a/librsvg/rsvg-load.c b/librsvg/rsvg-load.c
index 0468899f..3c2a3acc 100644
--- a/librsvg/rsvg-load.c
+++ b/librsvg/rsvg-load.c
@@ -145,34 +145,6 @@ rsvg_load_finish_load (RsvgLoad *load, GError **error)
     return was_successful;
 }
 
-gboolean
-rsvg_load_handle_xml_xinclude (RsvgHandle *handle, const char *href)
-{
-    GInputStream *stream;
-
-    g_assert (handle->priv->load != NULL);
-
-    stream = rsvg_handle_acquire_stream (handle, href, NULL);
-
-    if (stream) {
-        gboolean success;
-
-        success = rsvg_xml_state_parse_from_stream (handle->priv->load->xml.rust_state,
-                                                    handle->priv->load->unlimited_size,
-                                                    stream,
-                                                    NULL, /* cancellable */
-                                                    NULL);
-
-        g_object_unref (stream);
-
-        return success;
-    } else {
-        return FALSE;
-    }
-}
-
-/* end xinclude */
-
 /* This one is defined in the C code, because the prototype has varargs
  * and we can't handle those from Rust :(
  */
diff --git a/librsvg/rsvg-load.h b/librsvg/rsvg-load.h
index 99e9fed7..1a38c9bc 100644
--- a/librsvg/rsvg-load.h
+++ b/librsvg/rsvg-load.h
@@ -34,9 +34,6 @@ void rsvg_load_free (RsvgLoad *load);
 G_GNUC_INTERNAL
 gboolean rsvg_load_finish_load(RsvgLoad *load, GError **error);
 
-G_GNUC_INTERNAL
-gboolean rsvg_load_handle_xml_xinclude (RsvgHandle *handle, const char *url);
-
 G_GNUC_INTERNAL
 gboolean rsvg_load_write (RsvgLoad *load, const guchar *buf, gsize count, GError **error) 
G_GNUC_WARN_UNUSED_RESULT;
 
diff --git a/rsvg_internals/src/handle.rs b/rsvg_internals/src/handle.rs
index 8bcf92cb..5de142bf 100644
--- a/rsvg_internals/src/handle.rs
+++ b/rsvg_internals/src/handle.rs
@@ -57,11 +57,6 @@ extern "C" {
 
     fn rsvg_handle_keep_image_data(handle: *const RsvgHandle) -> glib_sys::gboolean;
 
-    fn rsvg_load_handle_xml_xinclude(
-        handle: *mut RsvgHandle,
-        href: *const libc::c_char,
-    ) -> glib_sys::gboolean;
-
     fn rsvg_handle_get_rust(handle: *const RsvgHandle) -> *mut Handle;
 }
 
@@ -114,6 +109,12 @@ pub fn load_extern(handle: *const RsvgHandle, aurl: &AllowedUrl) -> Result<*cons
     }
 }
 
+const RSVG_HANDLE_FLAG_UNLIMITED: u32 = 1 << 0;
+
+pub fn get_unlimited_size(handle: *const RsvgHandle) -> bool {
+    unsafe { (rsvg_handle_get_flags(handle) & RSVG_HANDLE_FLAG_UNLIMITED) != 0 }
+}
+
 pub fn get_base_url<'a>(handle: *const RsvgHandle) -> Ref<'a, Option<Url>> {
     let rhandle = get_rust_handle(handle);
 
@@ -201,13 +202,6 @@ pub fn load_image_to_surface(
     Ok(surface)
 }
 
-// FIXME: distinguish between "file not found" and "invalid XML"
-pub fn load_xml_xinclude(handle: *mut RsvgHandle, aurl: &AllowedUrl) -> bool {
-    let href = aurl.url().as_str();
-
-    unsafe { from_glib(rsvg_load_handle_xml_xinclude(handle, href.to_glib_none().0)) }
-}
-
 // This function just slurps CSS data from a possibly-relative href
 // and parses it.  We'll move it to a better place in the end.
 pub fn load_css(css_styles: &mut CssStyles, handle: *mut RsvgHandle, href_str: &str) {
diff --git a/rsvg_internals/src/xml.rs b/rsvg_internals/src/xml.rs
index d4036247..1eeb8c82 100644
--- a/rsvg_internals/src/xml.rs
+++ b/rsvg_internals/src/xml.rs
@@ -13,7 +13,7 @@ use attributes::Attribute;
 use create_node::create_node_and_register_id;
 use css::{self, CssStyles};
 use defs::Defs;
-use error::set_gerror;
+use error::{set_gerror, LoadingError};
 use handle::{self, RsvgHandle};
 use node::{node_new, Node, NodeType};
 use property_bag::PropertyBag;
@@ -22,6 +22,7 @@ use style::NodeStyle;
 use svg::Svg;
 use text::NodeChars;
 use tree::Tree;
+use xml2_load::{xml_state_parse_from_stream, ParseFromStreamError};
 
 #[derive(Clone)]
 enum ContextKind {
@@ -507,13 +508,22 @@ impl XmlState {
         Ok(())
     }
 
-    fn acquire_xml(&self, aurl: &AllowedUrl) -> Result<(), AcquireError> {
+    fn acquire_xml(&mut self, aurl: &AllowedUrl) -> Result<(), AcquireError> {
         // FIXME: distinguish between "file not found" and "invalid XML"
-        if handle::load_xml_xinclude(self.handle, aurl) {
-            Ok(())
-        } else {
-            Err(AcquireError::FatalError)
-        }
+
+        let stream = handle::acquire_stream(self.handle, aurl).map_err(|e| match e {
+            LoadingError::BadDataUrl => AcquireError::FatalError,
+            _ => AcquireError::ResourceError,
+        })?;
+
+        let unlimited_size = handle::get_unlimited_size(self.handle);
+
+        // FIXME: pass a cancellable
+        xml_state_parse_from_stream(self, unlimited_size, stream, None).map_err(|e| match e {
+            ParseFromStreamError::CouldNotCreateParser => AcquireError::FatalError,
+            ParseFromStreamError::IoError(_) => AcquireError::ResourceError,
+            ParseFromStreamError::XmlParseError(_) => AcquireError::FatalError,
+        })
     }
 
     fn unsupported_xinclude_start_element(&self, name: &str) -> Context {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]