[glom] Move the XSLT file into the GResource too.



commit dcd559a238b784fb87225f4f4fbbddf17e26336b
Author: Murray Cumming <murrayc murrayc com>
Date:   Tue Oct 22 12:57:37 2013 +0200

    Move the XSLT file into the GResource too.

 Makefile.am                                  |    3 +-
 Makefile_libglom.am                          |    4 +--
 data/glom.gresource.xml                      |    3 ++
 {xslt => data/xslt}/print_report_to_html.xsl |    0
 glom/libglom/xsl_utils.cc                    |   47 ++++++++++++-------------
 5 files changed, 28 insertions(+), 29 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 1b7571f..1a74019 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -157,8 +157,7 @@ EXTRA_DIST += \
        data/ui/developer/window_translations.glade \
        data/ui/developer/window_users.glade
 
-xsltdir = $(pkgdatadir)/xslt
-dist_xslt_DATA = xslt/print_report_to_html.xsl
+EXTRA_DIST += data/xslt/print_report_to_html.xsl
 
 # Get the base filenames only, without the extension: 
 example_files_names ?= $(patsubst %.glom,%,$(notdir $(example_files)))
diff --git a/Makefile_libglom.am b/Makefile_libglom.am
index 9876805..39ccf40 100644
--- a/Makefile_libglom.am
+++ b/Makefile_libglom.am
@@ -16,9 +16,7 @@
 ## along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 
-libglom_defines = \
--DGLOM_PKGDATADIR_XSLT=\""$(pkgdatadir)/xslt"\" \
--DGLOM_PKGDATADIR_XSLT_NOTINSTALLED=\""$(top_srcdir)/xslt"\"
+libglom_defines =
 
 lib_LTLIBRARIES = glom/libglom/libglom- GLOM_ABI_VERSION@.la
 
diff --git a/data/glom.gresource.xml b/data/glom.gresource.xml
index 8fb7427..b201239 100644
--- a/data/glom.gresource.xml
+++ b/data/glom.gresource.xml
@@ -53,6 +53,7 @@
     <file preprocess="xml-stripblanks">data/ui/operator/dialog_import_csv_progress.glade</file>
     <file preprocess="xml-stripblanks">data/ui/operator/dialog_new_record.glade</file>
     <file preprocess="xml-stripblanks">data/ui/operator/window_main.glade</file>
+
     <file>data/icons/glom-button.png</file>
     <file>data/icons/glom-field.png</file>
     <file>data/icons/glom-group.png</file>
@@ -63,6 +64,8 @@
     <file>data/icons/glom-related-records.png</file>
     <file>data/icons/glom-text.png</file>
     <file>data/icons/48x48/glom.png</file>
+
+    <file preprocess="xml-stripblanks">data/xslt/print_report_to_html.xsl</file> 
   </gresource>
 </gresources>
 
diff --git a/xslt/print_report_to_html.xsl b/data/xslt/print_report_to_html.xsl
similarity index 100%
rename from xslt/print_report_to_html.xsl
rename to data/xslt/print_report_to_html.xsl
diff --git a/glom/libglom/xsl_utils.cc b/glom/libglom/xsl_utils.cc
index b37c84d..34e3bbd 100644
--- a/glom/libglom/xsl_utils.cc
+++ b/glom/libglom/xsl_utils.cc
@@ -44,29 +44,15 @@
 
 namespace
 {
-  static std::string get_xsl_file_dir()
+  static std::string get_xslt_filepath(const std::string& xsl_file)
   {
-#ifdef G_OS_WIN32
-    gchar* directory = g_win32_get_package_installation_directory_of_module(0);
-    const std::string xsltdir = Glib::build_filename(directory,
-        "share" G_DIR_SEPARATOR_S "glom" G_DIR_SEPARATOR_S "xslt");
-    g_free(directory);
-    return xsltdir;
-#else
-    return GLOM_PKGDATADIR_XSLT;
-#endif
-  }
-
-  static std::string get_xslt_file(const std::string& xsl_file)
-  {
-    const std::string result = Glib::build_filename(get_xsl_file_dir(), xsl_file);
-
-    // Check that it exists:
-    const Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(result);
-    if(file && file->query_exists())
-      return result;
+    const std::string resource_path = "/org/gnome/glom/data/xslt/" + xsl_file;
+    if(!g_resources_get_info(resource_path.c_str(), G_RESOURCE_LOOKUP_FLAGS_NONE, 0, 0, 0))
+    {
+      std::cerr << G_STRFUNC << ": xslt resource not found: " << resource_path << std::endl;
+    }
 
-    return Glib::build_filename(GLOM_PKGDATADIR_XSLT_NOTINSTALLED, xsl_file);
+    return "resource://" + resource_path;
   }
 }
 
@@ -74,7 +60,7 @@ namespace Glom
 {
 
 
-static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std::string& filepath_xslt)
+static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std::string& resource_path_xslt)
 {
   //Debug output:
   //std::cout << "XML before XSLT processing: " << std::endl;
@@ -83,10 +69,23 @@ static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std
   //nonconst.write_to_stream_formatted(std::cout);
   //std::cout << std::endl;
 
+  Glib::RefPtr<Gio::File> file = Gio::File::create_for_uri(resource_path_xslt); //This must use a 
resource:// URI.
+  char* xslt_data = 0;
+  gsize xslt_length = 0;
+  try
+  {
+    file->load_contents(xslt_data, xslt_length);
+  }
+  catch(const xmlpp::exception& ex)
+  {
+    std::cerr << G_STRFUNC << ": Could not load XSLT from resource path: " << resource_path_xslt << 
std::endl;
+    return Glib::ustring();
+  }
+
   Glib::ustring result;
 
   //Use libxslt to transform the XML:
-  xmlDocPtr style = xmlReadFile(filepath_xslt.c_str(), 0, 0);
+  xmlDocPtr style = xmlReadDoc((xmlChar*)xslt_data, 0, 0, 0);
   if(style)
   {
     //We need this to be able to use the exsl: functions, even if we declare the namespace at the start of 
the xsl.
@@ -130,7 +129,7 @@ static Glib::ustring xslt_process(const xmlpp::Document& xml_document, const std
 Glib::ustring GlomXslUtils::transform(const xmlpp::Document& xml_document, const std::string& xsl_file_path)
 {
   //Use libxslt to convert the XML to HTML:
-  return xslt_process(xml_document, get_xslt_file(xsl_file_path));
+  return xslt_process(xml_document, get_xslt_filepath(xsl_file_path));
   //std::cout << "After xslt: " << result << std::endl;
 }
 


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