[Liblas-commits] hg-main-tree: We can now serialize a libpc::Schema -> XML

liblas-commits at liblas.org liblas-commits at liblas.org
Tue May 24 13:47:29 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/a828f88d3f17
changeset: 722:a828f88d3f17
user:      Howard Butler <hobu.inc at gmail.com>
date:      Tue May 24 12:47:21 2011 -0500
description:
We can now serialize a libpc::Schema -> XML

diffstat:

 include/libpc/XMLSchema.hpp |  105 ++++++++++---
 src/XMLSchema.cpp           |  322 ++++++++++++++++++++++++++++++++++++++-----
 test/data/schemas/LAS.xsd   |    4 +-
 test/unit/XMLSchemaTest.cpp |   29 +++-
 4 files changed, 385 insertions(+), 75 deletions(-)

diffs (truncated from 735 to 300 lines):

diff -r 47f1757b7ba8 -r a828f88d3f17 include/libpc/XMLSchema.hpp
--- a/include/libpc/XMLSchema.hpp	Thu May 12 14:01:47 2011 -0500
+++ b/include/libpc/XMLSchema.hpp	Tue May 24 12:47:21 2011 -0500
@@ -37,12 +37,11 @@
 
 #include <libpc/libpc.hpp>
 #include <libpc/Schema.hpp>
+#include <libpc/Utils.hpp>
 #include <libpc/SchemaLayout.hpp>
 #include <libpc/Dimension.hpp>
 #include <libpc/DimensionLayout.hpp>
-
-#include <libpc/drivers/oci/Common.hpp>
-#include <libpc/drivers/oci/Reader.hpp>
+#include <libpc/exceptions.hpp>
 
 #include <string>
 #include <stdarg.h>
@@ -62,7 +61,7 @@
 #include <boost/concept_check.hpp>
 #include <boost/function.hpp>
 
-namespace libpc { 
+namespace libpc { namespace schema {
 
 
 void OCISchemaGenericErrorHandler (void * ctx, const char* message, ...);
@@ -76,64 +75,81 @@
     {}
 };
 
+class schema_loading_error : public schema_error
+{
+public:
+    schema_loading_error(std::string const& msg)
+        : schema_error(msg)
+    {}
+};
 
+class schema_writing_error : public schema_error
+{
+public:
+    schema_writing_error(std::string const& msg)
+        : schema_error(msg)
+    {}
+};
 
-class schema_validation_error : public libpc_error
+
+class schema_validation_error : public schema_error
 {
 public:
     schema_validation_error(std::string const& msg)
-        : libpc_error(msg)
+        : schema_error(msg)
     {}
 };
 
-class schema_parsing_error : public libpc_error
+class schema_parsing_error : public schema_error
 {
 public:
     schema_parsing_error(std::string const& msg)
-        : libpc_error(msg)
+        : schema_error(msg)
     {}
 };
 
-class schema_generic_error : public libpc_error
+class schema_generic_error : public schema_error
 {
 public:
     schema_generic_error(std::string const& msg)
-        : libpc_error(msg)
+        : schema_error(msg)
     {}
 };
 
 
-class LIBPC_DLL XMLSchema
+// We're going to put all of our libxml2 primatives into shared_ptrs 
+// that have custom deleters that clean up after themselves so we 
+// have a good chance at having clean exception-safe code    
+typedef boost::shared_ptr<void> DocPtr;
+typedef boost::shared_ptr<void> SchemaParserCtxtPtr;    
+typedef boost::shared_ptr<void> SchemaPtr;
+typedef boost::shared_ptr<void> SchemaValidCtxtPtr;
+typedef boost::shared_ptr<void> TextWriterPtr;
+typedef boost::shared_ptr<void> BufferPtr;
+typedef boost::shared_ptr<void> CharPtr;
+
+class LIBPC_DLL Reader
 {
 public:
-    XMLSchema(std::string const& xml, std::string const& xmlschema);
-    ~XMLSchema();
+    Reader(std::string const& xml, std::string const& xmlschema);
+    Reader(std::istream* xml, std::istream* schema);
+    ~Reader();
 
 
 
 protected:
 
-    void LoadSchema();
+    void Initialize();
+    void Load();
     Dimension::DataType GetDimensionType(std::string const& interpretation);
     Dimension::Field GetDimensionField(std::string const& name, boost::uint32_t position);
     
 private:
     
-    XMLSchema& operator=(const XMLSchema&); // not implemented
-    XMLSchema(const XMLSchema&); // not implemented;
+    Reader& operator=(const Reader&); // not implemented
+    Reader(const Reader&); // not implemented;
 
 
-    
-    // We're going to put all of our libxml2 primatives into shared_ptrs 
-    // that have custom deleters that clean up after themselves so we 
-    // have a good chance at having clean exception-safe code    
-    typedef boost::shared_ptr<void> DocPtr;
-    typedef boost::shared_ptr<void> SchemaParserCtxtPtr;    
-    typedef boost::shared_ptr<void> SchemaPtr;
-    typedef boost::shared_ptr<void> SchemaValidCtxtPtr;
-    typedef boost::shared_ptr<void> TextWriterPtr;
-
-    TextWriterPtr writeHeader(DocPtr doc);
 
 
     DocPtr m_doc;
@@ -149,13 +165,46 @@
     void* m_global_context;
     libpc::Schema m_schema;
     
+    std::string m_xml;
+    std::string m_xsd;
     
     
 
 };
 
 
+class LIBPC_DLL Writer
+{
+public:
+    Writer(libpc::Schema const& schema);
+    ~Writer() {};
 
-} // namespaces
+    std::string write();
+
+
+protected:
+
+
+    
+private:
+    
+    Writer& operator=(const Writer&); // not implemented
+    Writer(const Writer&); // not implemented;
+
+
+    
+
+    void write(TextWriterPtr w);
+    void writeSchema(TextWriterPtr w);
+    void* m_global_context;
+    libpc::Schema const& m_schema;
+    
+    
+
+};
+
+
+
+}} // namespaces
 
 #endif
diff -r 47f1757b7ba8 -r a828f88d3f17 src/XMLSchema.cpp
--- a/src/XMLSchema.cpp	Thu May 12 14:01:47 2011 -0500
+++ b/src/XMLSchema.cpp	Tue May 24 12:47:21 2011 -0500
@@ -37,12 +37,14 @@
 
 #include <sstream>
 #include <iostream>
+#include <istream>
 #include <list>
 #include <cstdlib>
 #include <map>
 #include <algorithm>
 
 #include <string.h>
+#include <stdlib.h>
 
 
 struct XMLDocDeleter
@@ -90,13 +92,31 @@
    }
 };
 
+struct BufferDeleter
+{
+   template <typename T>
+   void operator()(T* ptr)
+   {
+       ::xmlBufferFree(ptr);
+   }
+};
+
+struct xmlCharDeleter
+{
+   template <typename T>
+   void operator()(T* ptr)
+   {
+       ::xmlFree(ptr);
+   }
+};
+
 
 static bool sort_dimensions(libpc::DimensionLayout const& a, libpc::DimensionLayout const& b)
 {
    return a < b;
 }
 
-namespace libpc { 
+namespace libpc { namespace schema {
 
 
 void OCISchemaStructuredErrorHandler
@@ -210,11 +230,11 @@
 // XML_PARSE_NONET No network access
 // http://xmlsoft.org/html/libxml-parser.html#xmlParserOption
 
-XMLSchema::XMLSchema(std::string const& xml, std::string const &xsd)
-: m_doc_options(XML_PARSE_NONET)
+
+void Reader::Initialize() 
 {
-    if (xml.size() == 0) throw schema_generic_error("Inputted XML has no size, is there data there?");
-    if (xsd.size() == 0) throw schema_generic_error("Inputted XSD has no size, is there data there?");
+    if (m_xml.size() == 0) throw schema_generic_error("Inputted XML has no size, is there data there?");
+    if (m_xsd.size() == 0) throw schema_generic_error("Inputted XSD has no size, is there data there?");
 
 
     LIBXML_TEST_VERSION
@@ -224,11 +244,11 @@
 
 
     m_doc = DocPtr(
-                   xmlReadMemory(xml.c_str(), xml.size(), NULL, NULL, m_doc_options),
+                   xmlReadMemory(m_xml.c_str(), m_xml.size(), NULL, NULL, m_doc_options),
                    XMLDocDeleter());
 
     m_schema_doc = DocPtr(
-                        xmlReadMemory(xsd.c_str(), xsd.size(), NULL, NULL, m_doc_options),
+                        xmlReadMemory(m_xsd.c_str(), m_xsd.size(), NULL, NULL, m_doc_options),
                          XMLDocDeleter());
 
     m_schema_parser_ctx = SchemaParserCtxtPtr(
@@ -258,13 +278,59 @@
 
     if (valid_schema != 0)
         throw schema_error("Document did not validate against schema!");
+    
+}
 
-    LoadSchema();
+Reader::Reader(std::string const& xml, std::string const &xsd)
+: m_doc_options(XML_PARSE_NONET)
+{
+    
+    m_xml = xml;
+    m_xsd = xsd;
+    Initialize();
+    Load();
     return;
 }
 
+Reader::Reader(std::istream* xml, std::istream *xsd) : m_doc_options(XML_PARSE_NONET)
+{
+    
+    if (!xml)
+        throw schema_generic_error("libpc::schema::Reader: xml istream pointer was null!");
+    
+    std::istream::pos_type size;
 
-XMLSchema::~XMLSchema()
+    std::vector<char> data;
+    xml->seekg(0, std::ios::end);
+    size = xml->tellg();
+    data.resize(static_cast<std::vector<char>::size_type>(size));
+    xml->seekg (0, std::ios::beg);
+    xml->read (&data.front(), size);
+    xml->seekg (0, std::ios::beg);
+
+    m_xml = std::string(&data[0], data.size());
+
+    if (xsd)


More information about the Liblas-commits mailing list