lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [5723] Prepare to validate input files with xsd schemata


From: Greg Chicares
Subject: [lmi-commits] [5723] Prepare to validate input files with xsd schemata
Date: Tue, 30 Apr 2013 00:13:13 +0000

Revision: 5723
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=5723
Author:   chicares
Date:     2013-04-30 00:13:12 +0000 (Tue, 30 Apr 2013)
Log Message:
-----------
Prepare to validate input files with xsd schemata

Modified Paths:
--------------
    lmi/trunk/input_test.cpp
    lmi/trunk/multiple_cell_document.cpp
    lmi/trunk/multiple_cell_document.hpp
    lmi/trunk/single_cell_document.cpp
    lmi/trunk/single_cell_document.hpp

Modified: lmi/trunk/input_test.cpp
===================================================================
--- lmi/trunk/input_test.cpp    2013-04-29 20:22:32 UTC (rev 5722)
+++ lmi/trunk/input_test.cpp    2013-04-30 00:13:12 UTC (rev 5723)
@@ -39,6 +39,7 @@
 #include "assert_lmi.hpp"
 #include "dbdict.hpp"
 #include "dbnames.hpp"
+#include "global_settings.hpp"
 #include "miscellany.hpp"
 #include "test_tools.hpp"
 #include "timer.hpp"
@@ -46,10 +47,12 @@
 
 #include <boost/bind.hpp>
 
+#include <xmlwrapp/document.h>
+
 #if defined BOOST_MSVC || defined __BORLANDC__
-#   include <cfloat> // floating-point hardware control
+#   include <cfloat>                    // floating-point hardware control
 #endif // defined BOOST_MSVC || defined __BORLANDC__
-#include <cstdio> // std::remove()
+#include <cstdio>                       // std::remove()
 #include <fstream>
 #include <ios>
 #include <string>
@@ -90,6 +93,8 @@
     static void mete_write();
     static void mete_cns_io();
     static void mete_ill_io();
+    static void mete_cns_xsd();
+    static void mete_ill_xsd();
 };
 
 void input_test::test_product_database()
@@ -411,6 +416,8 @@
         << "\n  Write    : " << TimeAnAliquot(mete_write               )
         << "\n  'cns' io : " << TimeAnAliquot(mete_cns_io              )
         << "\n  'ill' io : " << TimeAnAliquot(mete_ill_io              )
+        << "\n  'cns' xsd: " << TimeAnAliquot(mete_cns_xsd             )
+        << "\n  'ill' xsd: " << TimeAnAliquot(mete_ill_xsd             )
         << '\n'
         ;
 }
@@ -505,8 +512,25 @@
     test_document_io<S>("sample.ill", "replica.ill", __FILE__, __LINE__, true);
 }
 
+void input_test::mete_cns_xsd()
+{
+    static xml::document const cns = 
xml_lmi::dom_parser("sample.cns").document();
+    static multiple_cell_document const mcd;
+    mcd.validate_with_xsd_schema(cns);
+}
+
+void input_test::mete_ill_xsd()
+{
+    static xml::document const ill = 
xml_lmi::dom_parser("sample.ill").document();
+    static single_cell_document const scd;
+    scd.validate_with_xsd_schema(ill);
+}
+
 int test_main(int, char*[])
 {
+    // Location of '*.xsd' files.
+    global_settings::instance().set_data_directory("/opt/lmi/data");
+
     input_test::test();
     return EXIT_SUCCESS;
 }

Modified: lmi/trunk/multiple_cell_document.cpp
===================================================================
--- lmi/trunk/multiple_cell_document.cpp        2013-04-29 20:22:32 UTC (rev 
5722)
+++ lmi/trunk/multiple_cell_document.cpp        2013-04-30 00:13:12 UTC (rev 
5723)
@@ -30,10 +30,15 @@
 
 #include "alert.hpp"
 #include "assert_lmi.hpp"
+#include "data_directory.hpp"           // AddDataDir()
+#include "handle_exceptions.hpp"
 #include "value_cast.hpp"
 #include "xml_lmi.hpp"
 
+#include <xmlwrapp/document.h>
 #include <xmlwrapp/nodes_view.h>
+#include <xmlwrapp/schema.h>
+#include <xsltwrapp/stylesheet.h>
 
 #include <istream>
 #include <iterator>                     // std::distance()
@@ -126,6 +131,11 @@
         return;
         }
 
+    if(data_source_is_external(parser.document()))
+        {
+        validate_with_xsd_schema(parser.document());
+        }
+
     // Version 0 should have been handled above.
     LMI_ASSERT(0 < file_version);
     if(class_version() < file_version)
@@ -353,6 +363,46 @@
 }
 
 //============================================================================
+bool multiple_cell_document::data_source_is_external(xml::document const&) 
const
+{
+    return false; // Actual implementation coming soon.
+}
+
+//============================================================================
+void multiple_cell_document::validate_with_xsd_schema(xml::document const& d) 
const
+{
+    try
+        {
+        xsd_schema().validate(cell_sorter().apply(d));
+        }
+    catch(...)
+        {
+        warning() << "Schema validation failed--diagnostics follow." << 
std::flush;
+        report_exception();
+        }
+}
+
+/// Stylesheet to sort <cell> elements.
+///
+/// This is needed for an external system that cannot economically
+/// provide xml with alphabetically-sorted elements.
+
+xslt::stylesheet& multiple_cell_document::cell_sorter() const
+{
+    static std::string const f("sort_cell_subelements.xsl");
+    static xslt::stylesheet z(xml_lmi::dom_parser(AddDataDir(f)).document());
+    return z;
+}
+
+//============================================================================
+xml::schema const& multiple_cell_document::xsd_schema() const
+{
+    static std::string const f("multiple_cell_document.xsd");
+    static xml::schema const z(xml_lmi::dom_parser(AddDataDir(f)).document());
+    return z;
+}
+
+//============================================================================
 void multiple_cell_document::read(std::istream const& is)
 {
     xml_lmi::dom_parser parser(is);

Modified: lmi/trunk/multiple_cell_document.hpp
===================================================================
--- lmi/trunk/multiple_cell_document.hpp        2013-04-29 20:22:32 UTC (rev 
5722)
+++ lmi/trunk/multiple_cell_document.hpp        2013-04-30 00:13:12 UTC (rev 
5723)
@@ -44,6 +44,7 @@
     friend class CensusDocument;
     friend class CensusView;
     friend class CensusViewOld; // Obsolescent listview version.
+    friend class input_test;    // For mete_cns_xsd().
 
   public:
     multiple_cell_document();
@@ -66,6 +67,11 @@
     int                class_version() const;
     std::string const& xml_root_name() const;
 
+    bool data_source_is_external(xml::document const&) const;
+    void validate_with_xsd_schema(xml::document const&) const;
+    xslt::stylesheet& cell_sorter() const;
+    xml::schema const& xsd_schema() const;
+
     // Default parameters for the whole case, stored as a vector for
     // parallelism with class_parms_ and cell_parms_. Naturally, this
     // vector must have exactly one element.

Modified: lmi/trunk/single_cell_document.cpp
===================================================================
--- lmi/trunk/single_cell_document.cpp  2013-04-29 20:22:32 UTC (rev 5722)
+++ lmi/trunk/single_cell_document.cpp  2013-04-30 00:13:12 UTC (rev 5723)
@@ -28,10 +28,16 @@
 
 #include "single_cell_document.hpp"
 
+#include "alert.hpp"
 #include "assert_lmi.hpp"
+#include "data_directory.hpp"           // AddDataDir()
+#include "handle_exceptions.hpp"
 #include "xml_lmi.hpp"
 
+#include <xmlwrapp/document.h>
 #include <xmlwrapp/nodes_view.h>
+#include <xmlwrapp/schema.h>
+#include <xsltwrapp/stylesheet.h>
 
 #include <istream>
 #include <ostream>
@@ -84,6 +90,11 @@
 //============================================================================
 void single_cell_document::parse(xml_lmi::dom_parser const& parser)
 {
+    if(data_source_is_external(parser.document()))
+        {
+        validate_with_xsd_schema(parser.document());
+        }
+
     xml::element const& root(parser.root_node(xml_root_name()));
 
     xml::const_nodes_view const elements(root.elements());
@@ -97,6 +108,46 @@
 }
 
 //============================================================================
+bool single_cell_document::data_source_is_external(xml::document const&) const
+{
+    return false; // Actual implementation coming soon.
+}
+
+//============================================================================
+void single_cell_document::validate_with_xsd_schema(xml::document const& d) 
const
+{
+    try
+        {
+        xsd_schema().validate(cell_sorter().apply(d));
+        }
+    catch(...)
+        {
+        warning() << "Schema validation failed--diagnostics follow." << 
std::flush;
+        report_exception();
+        }
+}
+
+/// Stylesheet to sort <cell> elements.
+///
+/// This is needed for an external system that cannot economically
+/// provide xml with alphabetically-sorted elements.
+
+xslt::stylesheet& single_cell_document::cell_sorter() const
+{
+    static std::string const f("sort_cell_subelements.xsl");
+    static xslt::stylesheet z(xml_lmi::dom_parser(AddDataDir(f)).document());
+    return z;
+}
+
+//============================================================================
+xml::schema const& single_cell_document::xsd_schema() const
+{
+    static std::string const f("single_cell_document.xsd");
+    static xml::schema const z(xml_lmi::dom_parser(AddDataDir(f)).document());
+    return z;
+}
+
+//============================================================================
 void single_cell_document::read(std::istream const& is)
 {
     xml_lmi::dom_parser parser(is);

Modified: lmi/trunk/single_cell_document.hpp
===================================================================
--- lmi/trunk/single_cell_document.hpp  2013-04-29 20:22:32 UTC (rev 5722)
+++ lmi/trunk/single_cell_document.hpp  2013-04-30 00:13:12 UTC (rev 5723)
@@ -41,6 +41,7 @@
 {
     friend class IllustrationDocument;
     friend class IllustrationView;
+    friend class input_test; // For mete_ill_xsd().
 
   public:
     single_cell_document();
@@ -59,6 +60,11 @@
     int                class_version() const;
     std::string const& xml_root_name() const;
 
+    bool data_source_is_external(xml::document const&) const;
+    void validate_with_xsd_schema(xml::document const&) const;
+    xslt::stylesheet& cell_sorter() const;
+    xml::schema const& xsd_schema() const;
+
     Input input_data_;
 };
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]